#!/bin/bash
#180129 rewritten.
#180207 fix for quirky xerus64 8.3.90
#180224 /etc/init.d/z-cups-network-fix name now cups-net-fix
#180730 major rewrite, work without samba, only 'smbclient', plus restructuring.
#180803 move /etc/cups/client.conf to /root/.cups/ (see also /etc/init.d/cups-net-fix)
#180804 call auto_firewall_func when any radiobutton/checkbox selected.
#180807 fakeidentd new alternative to smbd or dnsmasq for identity export.
#180811 sshfs: rox gave error when copied a file. no longer using rover, nor ssh0 container.
#180812 probe for android phone connected via usb.
#180813 redesign to re-enter main gui.
#180816 only guaranteed path on remote pc is /root
#180822 /mnt/phone did not exist.
#181221 strange fix for smbclient.
#190117 new url for easyshare-super-easy-network-file-and-printer-sharing.html
#190328 add warning about many usb cables not support data transfer.
#190731 remove dependence on 'nmap' utility.
#200710 detect unauthorized device when do adb probe of phone.

export TEXTDOMAIN=easyshare
export OUTPUT_CHARSET=UTF-8

PARAM1="$1"
M_close="`gettext 'Close'`"
CR='
'
TICKICON='/usr/share/pixmaps/easyshare/tick_green.svg'
CROSSICON='/usr/share/pixmaps/easyshare/cross_red.svg'
HELPICON="gtk-index" #gtk-info

. /etc/rc.d/PUPSTATE #180811 want WKG_DEV, WKG_DIR
if [ "$WKG_DEV" ];then #180811
 MYPATH="/root" #"/mnt/${WKG_DEV}/${WKG_DIR}home" #easy linux  180816
 MYPATHremote="/root" #"/mnt/wkg/home" #180813 180816
else
 MYPATH="/root" #/file  180816
 MYPATHremote="/root" #"/file" #180813 180816
fi

mkdir -p /mnt/phone #180822
mkdir -p /root/.cups
mkdir -p /tmp/easyshare
mkdir -p /var/local/easyshare
ln -snf ../../usr/share/pixmaps/easyshare/samba-setup100x25.png /tmp/easyshare/share-setup.png
ln -snf ../../usr/local/EasyShare/samba-setup /tmp/easyshare/setup
touch /var/local/easyshare/remote-ip-hn
echo -n 'false' > /tmp/easyshare/scan_change_flg

M_t1="$(gettext 'Scanned for remote shares, no change')"
echo "popup 'level=top background=#a0a0ff terminate=ok|<b><big>${M_t1}</big></b>'" > /tmp/easyshare/msg-no-change
M_t1="$(gettext 'Scanned for remote printers, no change')"
echo "popup 'level=top background=#a0a0ff terminate=ok|<b><big>${M_t1}</big></b>'" > /tmp/easyshare/msg-cups-no-change

#180730 check if samba installed...
HAVESAMBA='yes'
if ! which smbd >/dev/null ;then HAVESAMBA='no'; fi
if ! which nmbd >/dev/null ;then HAVESAMBA='no'; fi

#to probe for remote shares, use smbd or dnsmasq/nmap. 180807 fakeidentd
#this actually means, how remote pcs can query this pc...
if [ "$HAVESAMBA" == "yes" ];then
 REMOTE_PROBE_METHOD='smbd'
else
 #REMOTE_PROBE_METHOD='dnsmasq'
 REMOTE_PROBE_METHOD='fakeidentd'
fi
echo -n "$REMOTE_PROBE_METHOD" > /tmp/easyshare/remote_probe_method
case "$REMOTE_PROBE_METHOD" in
 dnsmasq)    INITSCRIPT=dnsmasq ;;
 smbd)       INITSCRIPT=rc.samba ;;
 fakeidentd) INITSCRIPT=es-ident ;;
esac

#180125 check utilities
BADmsg=''
#180730 use getlocalip or hostname to get local ip...
LOCALIPexe=''
if which hostname >/dev/null ;then
 GOODflg="$(hostname --help 2>&1 | grep '\-I')"
 [ "$GOODflg" ] && LOCALIPexe='hostname'
 if [ ! "$LOCALIPexe" ];then
  if which getlocalip >/dev/null ;then
   LOCALIPexe='getlocalip'
  else
   BADmsg="${CR}$(gettext "The full 'hostname' utility is required, that supports the '-I' option.")"
  fi
 fi
else
 BADmsg="${CR}$(gettext "The 'hostname' utility is missing. Install the 'hostname' package.")"
fi
#if ! which smbd >/dev/null ;then BADmsg="${BADmsg}${CR}The 'smbd' utility is missing. Install the 'Samba' package."; fi
#if ! which nmbd >/dev/null ;then BADmsg="${BADmsg}${CR}The 'nmbd' utility is missing. Install the 'Samba' package."; fi
if ! which sshfs >/dev/null ;then BADmsg="${BADmsg}${CR}The 'sshfs' utility is missing. Install the 'sshfs' package."; fi
if ! which ssh >/dev/null ;then BADmsg="${BADmsg}${CR}The 'ssh' utility is missing. Install the 'openssh' package."; fi
if ! which nbtscan >/dev/null ;then BADmsg="${BADmsg}${CR}The 'nbtscan' utility is missing. Install the 'nbtscan' package."; fi
if ! which mount.cifs >/dev/null ;then BADmsg="${BADmsg}${CR}The 'mount.cifs' utility is missing. Install the 'cifs-utils' package."; fi
#if ! which nmap >/dev/null ;then BADmsg="${BADmsg}${CR}The 'nmap' utility is missing. Install the 'nmap' package."; fi
if ! which arp-scan >/dev/null ;then BADmsg="${BADmsg}${CR}The 'arp-scan' utility is missing. Install the 'arp-scan' package."; fi
if ! which mpscan >/dev/null ;then BADmsg="${BADmsg}${CR}The 'mpscan' utility is missing. Install the 'mpscan' package."; fi
if ! which lpstat >/dev/null ;then BADmsg="${BADmsg}${CR}The 'lpstat' utility is missing. Install the 'cups' package."; fi
if ! which ident-user-enum.pl >/dev/null ;then BADmsg="${BADmsg}${CR}The 'ident-user-enum.pl' perl script is missing."; fi
if ! which firewall_ng >/dev/null ;then BADmsg="${BADmsg}${CR}The 'firewall_ng' utility is missing. Install 01mickos 'firewall_ng' pet package."; fi
if which firewall_ng >/dev/null;then
 FWNG="$(which firewall_ng)"
 [ "$(grep '180807 BK' ${FWNG})" == "" ] && BADmsg="${BADmsg}${CR}The 'firewall_ng' utility exists, but it needs to be the special version created by 01micko, with mods by BK."
fi
[ "$(whoami)" != "root" ] && BADmsg="${BADmsg}${CR}You must run EasyShare as the 'root' user (administrator)."
[ ! -f /etc/init.d/cups ] && BADmsg="${BADmsg}${CR}Script '/etc/init.d/cups' does not exist. Does it have a different name?"
[ ! -f /etc/init.d/${INITSCRIPT} ] && BADmsg="${BADmsg}${CR}Script '/etc/init.d/${INITSCRIPT}' does not exist. Does it have a different name?"
[ ! -f /etc/init.d/sshd ] && BADmsg="${BADmsg}${CR}Script '/etc/init.d/sshd' does not exist. Does it have a different name?"
BADflg="$(echo -n "$BADmsg" | wc -l)"
if [ "$BADflg" != "0" ];then
 M_e1="$(gettext 'Aborting EasyShare')"
 pupmessage -bg '#FFA0A0' -title "${M_e1}" "${BADmsg}"
 exit
fi
echo -n "$LOCALIPexe" > /var/local/easyshare/local-ip-exe #180730 read by /etc/init.d/dnsmasq

rox_delayed_func() { #180813
 sleep 1
 rox -d ${1}
}

##########################################
###network connection###
LOCALIPADDRESS=''
IFUPflg="$(ifconfig | grep '^[a-z].* Link encap:Ethernet')"
if [ ! "$IFUPflg" ];then
 M0_1="$(gettext 'Share files and printers over a local network')"
 M0_a='<b><span fgcolor="DarkRed">'"$(gettext 'This computer is not connected to a local network.')"'</span></b>'
 echo -n "$M0_a" > /tmp/easyshare/temp0
 M0_b="$(gettext 'You must click the <b>Network Connect</b> button, or <b>connect</b> icon on the desktop, to setup a connection to a local network.')"
 export ES_NONET_DLG="
 <window title=\"EasyShare\" icon-name=\"gtk-network\" resizable=\"false\">
 <vbox>
  <text use-markup=\"true\"><label>\"<b>${M0_1}</b>\"</label></text>
  <hseparator></hseparator>
  <hbox>
   <vbox>
    <text use-markup=\"true\"><label>\"  \"</label><input file>/tmp/easyshare/temp0</input></text>
    <text use-markup=\"true\"><label>\"${M0_b}\"</label></text>
   </vbox>
   <vbox>
    <pixmap>
     <width>56</width>
     <input file>/usr/share/pixmaps/puppy/network_connect.svg</input>
    </pixmap>
    <button>
     <label>$(gettext 'Network Connect')</label>
     <action function=\"exit\">network</action>
    </button>
   </vbox>
  </hbox>
  <hseparator></hseparator>
  <hbox>
   <button><label>$(gettext 'Exit')</label><action function=\"exit\">quit</action></button>
  </hbox>
 </vbox>
 </window>"
 RETVALS="$(gtkdialog --program=ES_NONET_DLG --center)"
 eval "$RETVALS"
 [ "$EXIT" != "network" ] && exit
 defaultconnect
 exec /usr/local/EasyShare/easyshare
fi
##########################################
#continue only if have network connection#

if [ "$LOCALIPexe" == "getlocalip" ];then #180730
 LOCALIPADDRESS="$(getlocalip | grep -v '^lo' | tail -n 1 | cut -f 2 -d ' ')"
else
 LOCALIPADDRESS="$(hostname -I | cut -f 1 -d ' ')" #180129
fi
LOCALHOSTNAME="$(hostname)"

###preliminary settings###
[ ! -s /var/local/easyshare/cups-mode ] && echo -n '0' > /var/local/easyshare/cups-mode #disable printing
DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
[ ! -s /var/local/easyshare/file-mode ] && echo -n "0" > /var/local/easyshare/file-mode #disable file sharing
DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
[ ! -f /var/local/easyshare/network-shares-flag ] && echo -n 'true' > /var/local/easyshare/network-shares-flag
TICK_NS="$(cat /var/local/easyshare/network-shares-flag)" #whether to probe for shares on others pcs.
[ ! -f /var/local/easyshare/android-usb-flag ] && echo -n 'false' > /var/local/easyshare/android-usb-flag
TICK_AU="$(cat /var/local/easyshare/android-usb-flag)" #180812 whether probe for android phone connected via usb.

#setup the dnsmasq server. ref: /usr/local/EasyShare/dnsmasq-notes
LIP24="${LOCALIPADDRESS%.*}" #ex: 192.168.0.3 becomes 192.168.0
export LIP24 #so will be visible in funcs called from gtkdialog gui.
case "$DEF_FILE_MODE" in
 0)
  #this pc not sharing any files. 180807
  if pidof sshd >/dev/null;then
   /bin/sh /etc/init.d/sshd stop >/dev/null 2>&1
  fi
  chmod 644 /etc/init.d/sshd
  if pidof ${REMOTE_PROBE_METHOD} >/dev/null ; then #fakeidentd, smbd, or dnsmasq
   /etc/init.d/${INITSCRIPT} stop #es-ident, rc.samba, or dnsmasq
   chmod 644 /etc/init.d/${INITSCRIPT}
  fi
 ;;
 *) #1=samba or 2=sshfs
  if [ "$REMOTE_PROBE_METHOD" == "dnsmasq" ];then
   #remote pcs can query this pc and obtain hostname.
   echo "${LIP24}.254 ${LOCALHOSTNAME}" > /etc/easyshare-hosts
   echo -n '' > /etc/easyshare-dnsmasq.conf
   #180730 we have /etc/init.d/dnsmasq, it needs to start at bootup, after network is up...
   #ref: /etc/eventmanager
  fi
  if [ "$DEF_FILE_MODE" == "2" ];then #180807
   if ! pidof sshd >/dev/null;then
    chmod 755 /etc/init.d/sshd
    /bin/sh /etc/init.d/sshd start >/dev/null 2>&1
   fi
  fi
  chmod 755 /etc/init.d/${INITSCRIPT} #es-ident, rc.samba, or dnsmasq
  if ! pidof ${REMOTE_PROBE_METHOD} >/dev/null ; then #fakeidentd, smbd, or dnsmasq
   /etc/init.d/${INITSCRIPT} start
  fi
 ;;
esac

#preliminary samba
touch /var/local/easyshare/samba-shares-auth
[ "$(grep "^${LOCALHOSTNAME} " /var/local/easyshare/samba-shares-auth)" == "" ] && echo "${LOCALHOSTNAME} root woofwoof" >> /var/local/easyshare/samba-shares-auth
SAMBA_LOCAL_USER="$(grep "^${LOCALHOSTNAME} " /var/local/easyshare/samba-shares-auth | cut -f 2 -d ' ')"
SAMBA_LOCAL_PASSWORD="$(grep "^${LOCALHOSTNAME} " /var/local/easyshare/samba-shares-auth | cut -f 3 -d ' ')"

###cups funcs###
scan_cups_servers_func() {
 #called from the printers-scan button in the remote section.
 #similar to cupsmode3_func
 M_scs1="$(gettext 'Please wait, probing for network printers...')"
 popup "background=#FFE080|<b><big>${M_scs1} </big></b>"
 cp -f /tmp/easyshare/remote-printers /tmp/easyshare/remote-printersBAK
 cp -f /root/.cups/client.conf /tmp/easyshare/cups-client.conf-bak
 #have to find ip-address of remote printer...
 REMIPS="$(arp-scan --localnet | grep '^[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9]' | cut -f 1)"
 if [ "$REMIPS" ];then
  for remIP in $REMIPS
  do
   [ ! "$remIP" ] && continue
   REMfnd="$(timeout -t 1 mpscan -p 631 $remIP | grep -o 'OK')" #port 631 is for the cups server.
   if [ "$REMfnd" == "OK" ];then
    echo "ServerName ${remIP}" > /root/.cups/client.conf
    #180803 my router has a samba server in it, need to exclude it...
    lpstat -v >/dev/null 2>&1 #probes for printers at ip-address specified in client.conf
    if [ $? -ne 0 ];then
     cp -f /tmp/easyshare/cups-client.conf-bak /root/.cups/client.conf
     continue
    fi
    break
   fi
  done
 fi
 diff -q /root/.cups/client.conf /tmp/easyshare/cups-client.conf-bak >/dev/null 2>&1
 [ $? -ne 0 ] && /etc/init.d/cups restart
 sleep 0.1
 if [ -s /root/.cups/client.conf ];then
  NETWORKPRINTERS="$(lpstat -v | grep -v 'CUPS\-PDF' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 1 -d ':')"
 else
  NETWORKPRINTERS=""
 fi
 [ ! "$NETWORKPRINTERS" ] && NETWORKPRINTERS="-$(gettext 'nothing')-"
 NPnum=$(echo "$NETWORKPRINTERS" | wc -l)
 echo "<b>${NETWORKPRINTERS}</b>" > /tmp/easyshare/remote-printers
 if [ $NPnum -gt 1 ];then
  M_snp1="$(gettext 'Network printers:')"
 else
  M_snp1="$(gettext 'Network printer:')"
 fi
 echo -n "$M_snp1" > /tmp/easyshare/title-remote-printers
 diff -q /tmp/easyshare/remote-printers /tmp/easyshare/remote-printersBAK >/dev/null 2>&1
 if [ $? -eq 0 ];then
  echo -n 'true' > /tmp/easyshare/scan_change_flg #no change
 else
  echo -n 'false' > /tmp/easyshare/scan_change_flg
 fi
 killall popup
}

cupsmode0_func() { #disable printing.
 CMnum='0'
 DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
 #if [ "$DEF_CUPS_MODE" != "$CMnum" ];then
  pidof cupsd >/dev/null
  [ $? -eq 0 ] && /bin/sh /etc/init.d/cups stop >/dev/null 2>&1
  rm -f /etc/cups/cupsd.conf.O 2>/dev/null
  rm -f /etc/cups/printers.conf.O 2>/dev/null
  rm -f /root/.cups/client.conf 2>/dev/null
  echo -n "$CMnum" > /var/local/easyshare/cups-mode
  chmod 644 /etc/init.d/cups-net-fix
  chmod 644 /etc/init.d/cups
  cp -f /usr/local/EasyShare/cupsd.conf-local-only /etc/cups/cupsd.conf
  sed -i -e 's%^Shared Yes$%Shared No%' /etc/cups/printers.conf
 #fi
 VIS_LOCAL_CUPS='false'; VIS_REMOTE_CUPS='false'
 TICK_CM0="true"; TICK_CM1="false"; TICK_CM2="false"; TICK_CM3="false"; 
}
cupsmode1_func() { #private local printer.
 CMnum='1'
 DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
 #if [ "$DEF_CUPS_MODE" != "$CMnum" ];then
  pidof cupsd >/dev/null
  [ $? -eq 0 ] && /bin/sh /etc/init.d/cups stop >/dev/null 2>&1
  rm -f /etc/cups/cupsd.conf.O 2>/dev/null
  rm -f /etc/cups/printers.conf.O 2>/dev/null
  rm -f /root/.cups/client.conf 2>/dev/null
  echo -n "$CMnum" > /var/local/easyshare/cups-mode
  chmod 644 /etc/init.d/cups-net-fix
  chmod 755 /etc/init.d/cups
  cp -f /usr/local/EasyShare/cupsd.conf-local-only /etc/cups/cupsd.conf
  sed -i -e 's%^Shared Yes$%Shared No%' /etc/cups/printers.conf
  /etc/init.d/cups start >/dev/null 2>&1
 #fi
 VIS_LOCAL_CUPS='false'; VIS_REMOTE_CUPS='false'
 TICK_CM0="false"; TICK_CM1="true"; TICK_CM2="false"; TICK_CM3="false"; 
}
cupsmode2_func() { #shared local printer.
 CMnum='2'
 DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
 #if [ "$DEF_CUPS_MODE" != "$CMnum" ];then
  pidof cupsd >/dev/null
  [ $? -eq 0 ] && /bin/sh /etc/init.d/cups stop >/dev/null 2>&1
  rm -f /etc/cups/cupsd.conf.O 2>/dev/null
  rm -f /etc/cups/printers.conf.O 2>/dev/null
  rm -f /root/.cups/client.conf 2>/dev/null
  echo -n "$CMnum" > /var/local/easyshare/cups-mode
  chmod 644 /etc/init.d/cups-net-fix
  chmod 755 /etc/init.d/cups
  cp -f /usr/local/EasyShare/cupsd.conf-share-local-printers-AND-allow-remote-administration /etc/cups/cupsd.conf
  sed -i -e 's%^Shared No$%Shared Yes%' /etc/cups/printers.conf
  /etc/init.d/cups start >/dev/null 2>&1
  sleep 0.1
  LOCALPRINTERS="$(lpstat -v | grep -v 'CUPS\-PDF' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 1 -d ':')"
  [ ! "$LOCALPRINTERS" ] && LOCALPRINTERS="-$(gettext 'nothing')-"
  LPnum=$(echo "$LOCALPRINTERS" | wc -l)
  echo "<b>${LOCALPRINTERS}</b>" > /tmp/easyshare/local-printers
  if [ $LPnum -gt 1 ];then
   M_slp1="$(gettext 'Shared local printers:')"
  else
   M_slp1="$(gettext 'Shared local printer:')"
  fi
  echo -n "$M_slp1" > /tmp/easyshare/title-local-printers
 #fi
 if ! pidof smbd > /dev/null ;then
  . /root/.config/firewall/fw.conf
  if [ "$CONFIG_SAMBA" == "true" -a "$CONFIG_SAMBA_CLIENT" == "true" ];then
   M_slp2="$(gettext 'The CUPS daemon is running, which is sufficient for Linux-to-Linux printing. However, to print to or from Windows PCs, the Samba daemon must also be running -- <b>Samba Setup</b> will now be run.')"
   popup "process=wait backgrount=#ffa0a0 terminate=ok level=top|<big>${M_slp2}</big>"
   /usr/local/EasyShare/samba-setup
  fi
 fi
 VIS_LOCAL_CUPS='true'; VIS_REMOTE_CUPS='false'
 TICK_CM0="false"; TICK_CM1="false"; TICK_CM2="true"; TICK_CM3="false"; 
}
cupsmode3_func() { #network printer.
 CMnum='3'
 DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
 #if [ "$DEF_CUPS_MODE" != "$CMnum" ];then
  M_cm3_1="$(gettext 'Please wait, probing for network printers...')"
  popup "background=#FFE080|<big>${M_cm3_1} </big>"
  pidof cupsd >/dev/null
  [ $? -eq 0 ] && /bin/sh /etc/init.d/cups stop >/dev/null 2>&1
  rm -f /etc/cups/cupsd.conf.O 2>/dev/null
  rm -f /etc/cups/printers.conf.O 2>/dev/null
  rm -f /root/.cups/client.conf 2>/dev/null
  echo -n "$CMnum" > /var/local/easyshare/cups-mode
  chmod 755 /etc/init.d/cups
  cp -f /usr/local/EasyShare/cupsd.conf-local-only /etc/cups/cupsd.conf
  sed -i -e 's%^Shared Yes$%Shared No%' /etc/cups/printers.conf
  #have to find ip-address of remote printer...
  REMIPS="$(arp-scan --localnet | grep '^[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9]' | cut -f 1)"
  if [ "$REMIPS" ];then
   for remIP in $REMIPS
   do
    [ ! "$remIP" ] && continue
    REMfnd="$(timeout -t 1 mpscan -p 631 $remIP | grep -o 'OK')"
    if [ "$REMfnd" == "OK" ];then
     echo "ServerName ${remIP}" > /root/.cups/client.conf
     #180803 my router has a samba server in it, need to exclude it...
     lpstat -v >/dev/null 2>&1 #probes for printers at ip-address specified in client.conf
     if [ $? -ne 0 ];then
      rm -f /root/.cups/client.conf
      continue
     fi
     break
    fi
   done
  fi
  #this checks ip-address in client.conf is valid...
  chmod 755 /etc/init.d/cups-net-fix
  /etc/init.d/cups start >/dev/null 2>&1
  sleep 0.1
  if [ -s /root/.cups/client.conf ];then
   NETWORKPRINTERS="$(lpstat -v | grep -v 'CUPS\-PDF' | tr -s ' ' | cut -f 3 -d ' ' | cut -f 1 -d ':')"
  else
   NETWORKPRINTERS=""
  fi
  [ ! "$NETWORKPRINTERS" ] && NETWORKPRINTERS="-$(gettext 'nothing')-"
  NPnum=$(echo "$NETWORKPRINTERS" | wc -l)
  echo "<b>${NETWORKPRINTERS}</b>" > /tmp/easyshare/remote-printers
  if [ $NPnum -gt 1 ];then
   M_snp1="$(gettext 'Network printers:')"
  else
   M_snp1="$(gettext 'Network printer:')"
  fi
  echo -n "$M_snp1" > /tmp/easyshare/title-remote-printers
  killall popup
 #fi
 if ! pidof smbd > /dev/null ;then
  . /root/.config/firewall/fw.conf
  if [ "$CONFIG_SAMBA" == "true" -a "$CONFIG_SAMBA_CLIENT" == "true" ];then
   M_slp2="$(gettext 'The CUPS daemon is running, which is sufficient for Linux-to-Linux printing. However, to print to or from Windows PCs, the Samba daemon must also be running -- <b>Samba Setup</b> will now be run.')"
   popup "process=wait backgrount=#ffa0a0 terminate=ok level=top|<big>${M_slp2}</big>"
   /usr/local/EasyShare/samba-setup
  fi
 fi
 VIS_LOCAL_CUPS='false'; VIS_REMOTE_CUPS='true'
 TICK_CM0="false"; TICK_CM1="false"; TICK_CM2="false"; TICK_CM3="true"; 
}
export -f cupsmode0_func
export -f cupsmode1_func
export -f cupsmode2_func
export -f cupsmode3_func
export -f scan_cups_servers_func

###scan remote servers###
#note, $REMOTE_PROBE_METHOD is for this pc, how other pcs can query and obtain hostname.
#to probe other pcs, need to check for dnsmasq (unique to easy/quirky), or smb/samba servers running.
scan_file_servers_func() {
 M_fs1="$(gettext 'Please wait, probing for network shares...')"
 popup "background=#FFE080|<b><big>${M_fs1} </big></b>"
 FIRSTflg=0
 [ ! -f /tmp/easyshare/remote-ip-hnBAK ] && FIRSTflg=1
 cp -f /var/local/easyshare/remote-ip-hn /tmp/easyshare/remote-ip-hnBAK
 NAMESERVERIP="$(grep '^nameserver ' /etc/resolv.conf | tr -s ' ' | cut -f 2 -d ' ')"
 echo -n '' > /var/local/easyshare/remote-ip-hn
 REMIPS="$(arp-scan --localnet | grep '^[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9]' | cut -f 1)"
 if [ "$REMIPS" ];then
  #find computers with samba/dns servers...
  for remIP in $REMIPS
  do
   [ ! "$remIP" ] && continue
   for aNSIP in $NAMESERVERIP #there may be more than one. actually, this check is not needed.
   do
    [ "$remIP" == "$aNSIP" ] && continue 2
   done
   REMfnd="$(timeout -t 1 mpscan -p 445 ${remIP} | grep -o 'OK')"
   if [ "$REMfnd" == "OK" ];then
    #query remote samba/smb server...
    #note, remote smb/samba server uses port 445.
    #note, quirky xerus64 ubuntu nbtscan has different output from pyro.
    SFO="$(nbtscan --help 2>&1 | tr '\t' ' ' | tr -s ' ' | grep -o '^ \-s ')" #test for script friendly output
    if [ "$SFO" ];then
     IP_WG_HN="$(nbtscan -s '|' ${remIP} 2>/dev/null | tr -d ' ' | cut -f 1,2 -d '|')" 
     #...ex: 192.168.0.4|DESKTOP-U4IHIEA|<server>|<unknown>|00:ce:39:cd:3e:cc
     #       becomes 192.168.0.4|DESKTOP-U4IHIEA
    else #pyro64
     IP_WG_HN0="$(nbtscan ${remIP} 2>/dev/null | tr '\' ' ' | tr -s ' ' | tr ' ' '|')"
     #...ex: 192.168.0.3|WORKGROUP|EASYPC25889|SHARING
     IP_WG_HN="$(echo -n "$IP_WG_HN0" | cut -f 1,3 -d '|')"
     #...ex: 192.168.0.3|EASYPC25889
    fi
    if [ "$IP_WG_HN" ];then
     if [ "$(grep "^SMB:${IP_WG_HN}$" /var/local/easyshare/remote-ip-hn)" == "" ];then
      echo "SMB:${IP_WG_HN}" >> /var/local/easyshare/remote-ip-hn
     fi
    fi
   else
    #180807 try for fakeidentd server next...
    REMfnd="$(timeout -t 1 mpscan -p 113 ${remIP} | grep -o 'OK')"
    if [ "$REMfnd" == "OK" ];then
     #query remote fakeidentd server... ref: /usr/sbin/ident-user-enum.pl
     REMident="$(ident-user-enum.pl ${remIP} 113 | grep "^${remIP}:" | tr '\t' ' ' | tr -s ' ' | cut -f 2 -d ' ')"
     if [ "$REMident" ];then
      remHN="${REMident/ES/}" #fakeidentd prefixed the hostname with "ES"
      echo "IDENT:${remIP}|${remHN}" >> /var/local/easyshare/remote-ip-hn
     fi
    else
     #180807 deprecated...
     true
     #REMfnd="$(timeout -t 1 mpscan -p 53 ${remIP} | grep -o 'OK')"
     #if [ "$REMfnd" == "OK" ];then
     # #query remote easy/quirky dnsmasq server...
     # #note, remote dnsmasq server uses port 53.
     # #use nmap... ref: /usr/local/EasyShare/dnsmasq-notes
     # revDNS="$(nmap --dns-servers ${remIP} -R -sL ${LIP24}.254 2>/dev/null)"
     # if [ "$revDNS" ];then
     #  remHN="$(echo "$revDNS" | grep "^Nmap scan report .*(${LIP24}\.254)" | tr -s ' ' | cut -f 5 -d ' ')"
     #  [ "$remHN" ] && echo "DNSMASQ:${remIP}|${remHN}" >> /var/local/easyshare/remote-ip-hn
     # fi
     #fi
    fi
   fi
  done
 fi
 [ $FIRSTflg -eq 1 ] && cp -f /var/local/easyshare/remote-ip-hn /tmp/easyshare/remote-ip-hnBAK
 diff -q /var/local/easyshare/remote-ip-hn /tmp/easyshare/remote-ip-hnBAK >/dev/null 2>&1
 if [ $? -eq 0 ];then
  echo -n 'true' > /tmp/easyshare/scan_change_flg #no change.
 else
  echo -n 'false' > /tmp/easyshare/scan_change_flg
 fi
 killall popup
}

#180125
remote_folders_func() {
 HNxml=""
 if [ -s /var/local/easyshare/remote-ip-hn ];then #180114
  SHAREnames="$(cat /var/local/easyshare/remote-ip-hn | cut -f 2 -d '|')" #180207
  REMnum=0
  for aREMPC in `cat /var/local/easyshare/remote-ip-hn | tr '\n' ' '`
  do
   #180730 take off prefix, SMB: or DNSMASQ: 180807 or IDENT:
   aREMOTE_SERVER="${aREMPC/:*/}"
   aREMPC="${aREMPC/*:/}"
   #ex: 192.168.0.3|WORKGROUP|EASYPC25889|SHARING
   xIP="$(echo -n "$aREMPC" | cut -f 1 -d '|' | tr '.' 'x')" #192.168.0.3 becomes 192x168x0x3
   aHN="$(echo -n "$aREMPC" | cut -f 2 -d '|')" #180207
   #MNTD="$(find /mnt/ -mindepth 1 -maxdepth 1 -type d -name "${aHN}_[0-9]")"
   MNTD="$(grep -o "/mnt/${aHN}_[0-9]" /proc/mounts)"
   [ ! "$MNTD" ] && MNTD="$(grep -o "/mnt/${aHN} " /proc/mounts)"
   if [ ! "$MNTD" ];then
    HNxml="${HNxml}
    <vbox>
     <text use-markup=\"true\"><label>\"<b>${aHN}</b>\"</label></text>
     <hbox><button><label>$(gettext 'Mount')</label><action>EXIT:mount_${aREMOTE_SERVER}_${xIP}</action></button></hbox>
    </vbox>"
    REMnum=$(($REMnum+1))
   else
    for aMNTD in $MNTD
    do
     MNTfolder="${aMNTD/*\//}"
     HNxml="${HNxml}
     <vbox>
      <text use-markup=\"true\"><label>\"<b>${MNTfolder}</b>\"</label></text>
      <hbox><button><input file>/usr/local/lib/X11/pixmaps/folder48.png</input><action>rox -x ${aMNTD}</action><action>rox -d ${aMNTD} & </action></button></hbox>
      <hbox><button><label>$(gettext 'Unmount')</label><action>EXIT:unmount_${aREMOTE_SERVER}_${MNTfolder}</action></button></hbox>
     </vbox>"
     REMnum=$(($REMnum+1))
    done
   fi
  done
  if [ $REMnum -gt 1 ];then
   M4_ns="$(gettext 'Network shares:')"
  else
   M4_ns="$(gettext 'Network share:')"
  fi
  REMOTEFILESxml="
   <text><label>${M4_ns}</label></text>
   <hbox>
    ${HNxml}
   </hbox>"
 else
  REMOTEFILESxml="
   <text><label>$(gettext 'Network shares:')</label></text>
   <text use-markup=\"true\"><label>\"<b>-$(gettext 'nothing')-</b>\"</label></text>
  "
 fi
}

###compose a info msg in setup section###
setup_info_func() {
 ln -snf ../../usr/share/pixmaps/puppy/execute.svg /tmp/easyshare/setup-icon.svg
 DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
 REMOTE_PROBE_METHOD="$(cat /tmp/easyshare/remote_probe_method)"
 case $DEF_FILE_MODE in
  1) #samba
   if pidof smbd >/dev/null; then
    M_si1='<span fgcolor="DarkGreen">'"$(gettext 'The Samba daemon is running, so OK. Click <b>Samba Setup</b> button if you want to review the settings, or to disable the daemon.')"'</span>'
   else
    M_si1='<span fgcolor="DarkRed">'"$(gettext 'The Samba daemon is not running. To share folders, you must click on <b>Samba Setup</b> button to activate it.')"'</span>'
    ln -snf ../../usr/share/pixmaps/easyshare/cross_red.svg /tmp/easyshare/setup-icon.svg
   fi
  ;;
  2) #sshfs
   if pidof sshd >/dev/null; then
    if [ "$REMOTE_PROBE_METHOD" == "smbd" ];then
     case "${CONFIG_SAMBA}${CONFIG_SAMBA_CLIENT}" in
      truetrue) M_si1='<span fgcolor="DarkGreen">'"$(gettext 'The SSH daemon is running, so OK. Click <b>SSHFS Setup</b> button if you want to review the settings, or to disable the daemon.')"'</span>' ;;
      *)        M_si1='<span fgcolor="DarkGreen">'"$(gettext 'The SSH daemon is running, so OK to share a local folder.')"'</span><span fgcolor="DarkRed">'" $(gettext 'However, the Samba daemon is required to discover remote shares.')"'</span>' ;;
     esac
    else
     M_si1='<span fgcolor="DarkGreen">'"$(gettext 'The SSH daemon is running, so OK. Click <b>SSHFS Setup</b> button if you want to review the settings, or to disable the daemon.')"'</span>'
    fi
   else
    M_si1='<span fgcolor="DarkRed">'"$(gettext 'The SSH daemon is not running. To share folders, you must click on <b>SSHFS Setup</b> button to activate it.')"'</span>'
    ln -snf ../../usr/share/pixmaps/easyshare/cross_red.svg /tmp/easyshare/setup-icon.svg
   fi
  ;;
  *)
   M_si1='<span fgcolor="DarkBlue">'"$(gettext 'You have chosen not to share local files over the network.')"'</span>'
  ;;
 esac
 echo "$M_si1" > /tmp/easyshare/setup-info
}

#setup for local folders xml...
local_folders_func() {
 VIS_LOCAL_FILES='true'
 VIS_LOCAL_VIEW1='false'; VIS_LOCAL_VIEW2='false'; VIS_LOCAL_VIEW3='false'
 echo -n 'false' > /tmp/easyshare/local-view1-flg
 echo -n 'false' > /tmp/easyshare/local-view2-flg
 echo -n 'false' > /tmp/easyshare/local-view3-flg

 DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
 case "$DEF_FILE_MODE" in
  1) #samba
   VIEW1xml=''; VIEW2xml=''; VIEW3xml=''
   SLF1flg=''; SLF2flg=''; SLF3flg=''
   localPATH1="$(grep -iE 'path' /etc/samba/smb.conf| grep -v '^#' |grep -v 'var'|grep -v 'tmp'|awk 'NR==1{print $3}')" #ex: /mnt/wkg/home
   aSHARENAME="`grep -iE '^\[' /etc/samba/smb.conf|grep -v 'global'|grep -v 'printers'|sed -e 's%\[%%' -e 's%\]%%'| awk 'NR==1'`" #extracts string "puppyshare"
   if [ "$localPATH1" -a "$aSHARENAME" ];then
    SLF1flg='1'
    VIS_LOCAL_VIEW1='true'
    basePATH1="$(basename "${localPATH1}")"
    echo -n "<b>${basePATH1}</b>" > /tmp/easyshare/local-folder1
    echo -e "/bin/sh\nrox -d '${localPATH1}'" > /tmp/easyshare/rox-local-folder1
    chmod 755 /tmp/easyshare/rox-local-folder1
    echo -n 'true' > /tmp/easyshare/local-view1-flg
   fi
   localPATH2="$(grep -iE 'path' /etc/samba/smb.conf| grep -v '^#' |grep -v 'var'|grep -v 'tmp'|awk 'NR==2{print $3}')"
   aSHARENAME="`grep -iE '^\[' /etc/samba/smb.conf|grep -v 'global'|grep -v 'printers'|sed -e 's%\[%%' -e 's%\]%%'| awk 'NR==2'`"
   if [ "$localPATH2" -a "$aSHARENAME" ];then
    SLF2flg='1'
    VIS_LOCAL_VIEW2='true'
    basePATH2="$(basename "${localPATH2}")"
    echo -n "<b>${basePATH2}</b>" > /tmp/easyshare/local-folder2
    echo -e "/bin/sh\nrox -d '${localPATH2}'" > /tmp/easyshare/rox-local-folder2
    chmod 755 /tmp/easyshare/rox-local-folder2
    echo -n 'true' > /tmp/easyshare/local-view2-flg
   fi
   localPATH3="$(grep -iE 'path' /etc/samba/smb.conf| grep -v '^#' |grep -v 'var'|grep -v 'tmp'|awk 'NR==3{print $3}')"
   aSHARENAME="`grep -iE '^\[' /etc/samba/smb.conf|grep -v 'global'|grep -v 'printers'|sed -e 's%\[%%' -e 's%\]%%'| awk 'NR==3'`"
   if [ "$localPATH3" -a "$aSHARENAME" ];then
    SLF3flg='1'
    VIS_LOCAL_VIEW3='true'
    basePATH3="$(basename "${localPATH3}")"
    echo -n "<b>${basePATH3}</b>" > /tmp/easyshare/local-folder3
    echo -e "/bin/sh\nrox -d '${localPATH3}'" > /tmp/easyshare/rox-local-folder3
    chmod 755 /tmp/easyshare/rox-local-folder3
    echo -n 'true' > /tmp/easyshare/local-view3-flg
   fi
   M_slf="$(gettext 'Shared local folders:')"
   case "${SLF1flg}${SLF2flg}${SLF3flg}" in
    "1")   M_slf="$(gettext 'Shared local folder:')" ;;
   esac
  ;;
  2) #sshfs
   VIEW1xml=''; VIEW2xml=''; VIEW3xml=''
   SLF1flg=''; SLF2flg=''; SLF3flg=''
   localPATH1="${MYPATH}" #180811
   SLF1flg='1'
   VIS_LOCAL_VIEW1='true'
   basePATH1="$(basename "${localPATH1}")"
   echo -n "<b>${basePATH1}</b>" > /tmp/easyshare/local-folder1
   echo -e "/bin/sh\nrox -d '${localPATH1}'" > /tmp/easyshare/rox-local-folder1
   chmod 755 /tmp/easyshare/rox-local-folder1
   echo -n 'true' > /tmp/easyshare/local-view1-flg
   M_slf="$(gettext 'Shared local folders:')"
   case "${SLF1flg}${SLF2flg}${SLF3flg}" in
    "1")   M_slf="$(gettext 'Shared local folder:')" ;;
   esac
  ;;
  *) #disabled file sharing
   true
  ;;
 esac
 echo "$M_slf" > /tmp/easyshare/M_slf
}

###file sharing funcs###
filemode0_func() { #disable file sharing from this pc
 FMnum='0'
 DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
 #if [ "$DEF_FILE_MODE" != "$FMnum" ];then
  DEF_FILE_MODE='0'
  echo -n "0" > /var/local/easyshare/file-mode
  if pidof sshd >/dev/null;then
   /bin/sh /etc/init.d/sshd stop >/dev/null 2>&1
  fi
  chmod 644 /etc/init.d/sshd
  if pidof dnsmasq >/dev/null ;then /etc/init.d/dnsmasq stop >/dev/null 2>&1 ; fi
  chmod 644 /etc/init.d/dnsmasq
  if pidof smbd >/dev/null ;then
   /etc/init.d/rc.samba stop >/dev/null 2>&1
   chmod 644 /etc/init.d/rc.samba
  fi
  if pidof fakeidentd >/dev/null ;then #180807
   /etc/init.d/es-ident stop >/dev/null 2>&1
   chmod 644 /etc/init.d/es-ident
  fi
  setup_info_func
 #fi
 VIS_LOCAL_FILES='false'
 TICK_FM0='true'; TICK_FM1='false'; TICK_FM2='false'
}
filemode1_func() { #other pcs access share served by samba on this pc.
 FMnum='1'
 DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
 REMOTE_PROBE_METHOD="$(cat /tmp/easyshare/remote_probe_method)"
 #if [ "$DEF_FILE_MODE" != "$FMnum" ];then
  DEF_FILE_MODE='1'
  echo -n "1" > /var/local/easyshare/file-mode
  if pidof sshd >/dev/null;then
   /bin/sh /etc/init.d/sshd stop >/dev/null 2>&1
  fi
  chmod 644 /etc/init.d/sshd
  ln -snf ../../usr/share/pixmaps/easyshare/samba-setup100x25.png /tmp/easyshare/share-setup.png
  ln -snf ../../usr/local/EasyShare/samba-setup /tmp/easyshare/setup
  
  #$INITSCRIPT will be either "dnsmasq" or "rc.samba", services by which other pcs probe this pc.
  #$REMOTE_PROBE_METHOD will be "dnsmasq" or "smbd", the actual daemons, ditto.
  case "$REMOTE_PROBE_METHOD" in
   dnsmasq)
    if ! pidof dnsmasq >/dev/null ; then
     chmod 755 /etc/init.d/dnsmasq
     /etc/init.d/dnsmasq start
    fi
   ;;
   fakeidentd) #180807
    if ! pidof fakeidentd >/dev/null ; then
     chmod 755 /etc/init.d/es-ident
     /etc/init.d/es-ident start
    fi
   ;;
   *)
    true
   ;;
  esac
  
  setup_info_func
 #fi
 VIS_LOCAL_FILES='true'
 TICK_FM0='false'; TICK_FM1='true'; TICK_FM2='false'
}
filemode2_func() { #sshfs
 #clicked the "SSHFS" radiobutton.
 FMnum='2'
 DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
 REMOTE_PROBE_METHOD="$(cat /tmp/easyshare/remote_probe_method)"
 #if [ "$DEF_FILE_MODE" != "$FMnum" ];then
  DEF_FILE_MODE='2'
  echo -n "2" > /var/local/easyshare/file-mode
  ln -snf ../../usr/share/pixmaps/easyshare/sshfs-setup100x25.png /tmp/easyshare/share-setup.png
  ln -snf ../../usr/local/EasyShare/ssh-setup /tmp/easyshare/setup
  
  #$INITSCRIPT will be either "dnsmasq" or "rc.samba", services by which other pcs probe this pc.
  #$REMOTE_PROBE_METHOD will be "dnsmasq" or "smbd", the actual daemons, ditto.
  case "$REMOTE_PROBE_METHOD" in
   dnsmasq)
    if ! pidof dnsmasq >/dev/null ; then
     chmod 755 /etc/init.d/dnsmasq
     /etc/init.d/dnsmasq start
    fi
   ;;
   fakeidentd) #180807
    if ! pidof fakeidentd >/dev/null ; then
     chmod 755 /etc/init.d/es-ident
     /etc/init.d/es-ident start
    fi
   ;;
   *)
    true
   ;;
  esac
  
  setup_info_func
 #fi
 VIS_LOCAL_FILES='true'
 TICK_FM0='false'; TICK_FM1='false'; TICK_FM2='true'
}
export -f filemode0_func
export -f filemode1_func
export -f filemode2_func
export -f setup_info_func
export -f scan_file_servers_func
export -f local_folders_func

#180131 unmount when a file-mode radiobutton toggles to unselected
unmount1_func() { #samba
 MNTPTS="$(grep '^//192.* cifs ' /proc/mounts | cut -f 2 -d ' ' | tr '\n' ' ')"
 for aMNTPT in $MNTPTS
 do
  sync
  rox -D $aMNTPT >/dev/null 2>&1
  umount $aMNTPT >/dev/null 2>&1
 done
}
unmount2_func() { #sshfs
 MNTPTS="$(grep ' fuse\.sshfs ' /proc/mounts | cut -f 2 -d ' ' | tr '\n' ' ')"
 for aMNTPT in $MNTPTS
 do
  sync
  rox -D $aMNTPT >/dev/null 2>&1
  fusermount -u $aMNTPT >/dev/null 2>&1
 done
}
export -f unmount1_func
export -f unmount2_func


###firewall section###
#180731 rewrite, now have automated firewall_ng
#default firewall settings, block all ports...
[ ! -d /root/.config/firewall ] && mkdir /root/.config/firewall
if [ ! -f /root/.config/firewall/fw.conf ];then
echo 'GENERIC=true
LOGGING=false
MAIN=false
EX=false
CONFIG_SSH=false
CONFIG_CUPS=false
CONFIG_SAMBA=false
CONFIG_SAMBA_CLIENT=false
CONFIG_DLNA=false
CONFIG_NTP=false
CONFIG_FTP=false
CONFIG_HTTP=false
CONFIG_DNS=false
CONFIG_IDENT=false
CONFIG_DHCP=false
CONFIG_NFS=false' > /root/.config/firewall/fw.conf
else
 . /root/.config/firewall/fw.conf
 [ ! "$CONFIG_IDENT" ] && CONFIG_IDENT=false #180807
fi
#variables to consider:
#HAVESAMBA=yes|no                  yes if have smbd and nmbd daemons.
#REMOTE_PROBE_METHOD=smbd|dnsmasq|fakeidentd  local server used by other pcs to probe this pc. (to acquire hostname)
#DEF_FILE_MODE=0|1|2               0=disabled, 1=samba, 2=sshfs how files are to be shared from this pc
#DEF_CUPS_MODE=0|1|2|3             0=disabled, 1=private local printer, 2=shared local printer, 3=network printer
#TICK_NS=true|false                whether to probe for shares on others pcs.
#TICK_AU=true|false                whether probe for android phone connected via usb.

auto_firewall_func() {
 #optional param=CM[0-3] or FM[0-2] or NS[true|false]
 PFF="${1}"
 FWflg="$(cat /var/local/easyshare/auto-firewall-flag)"
 REMOTE_PROBE_METHOD="$(cat /tmp/easyshare/remote_probe_method)"
 if [ "${FWflg}" == "false" ];then
  [ -x /etc/init.d/rc.firewall ] && /etc/init.d/rc.firewall stop
  chmod 644 /etc/init.d/rc.firewall
  return
 fi
 case "${PFF}" in
  CM*)
   DEF_CUPS_MODE="${PFF/CM/}"
   DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
   TICK_NS="$(cat /var/local/easyshare/network-shares-flag)"
  ;;
  FM*)
   DEF_FILE_MODE="${PFF/FM/}"
   DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
   TICK_NS="$(cat /var/local/easyshare/network-shares-flag)"
  ;;
  NS*) #network shares checkbox
   TICK_NS="${PFF/NS/}"
   DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
   DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
  ;;
  *)
   DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"
   DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
   TICK_NS="$(cat /var/local/easyshare/network-shares-flag)"
  ;;
 esac
 NETEXPORTflg=yes #yes if this pc will be serving files or printer.
 . /root/.config/firewall/fw.conf
 CONFIG_CUPS=false; CONFIG_SAMBA=false; CONFIG_SAMBA_CLIENT=false
 CONFIG_SSH=false; CONFIG_DNS=false; CONFIG_IDENT=false
 case "${DEF_FILE_MODE}${DEF_CUPS_MODE}" in
  00) NETEXPORTflg=no ;;
  01) NETEXPORTflg=no ;;
  02) CONFIG_CUPS=true ;;
  03) NETEXPORTflg=no ;;
  10) CONFIG_SAMBA=true; CONFIG_SAMBA_CLIENT=true ;;
  11) CONFIG_SAMBA=true; CONFIG_SAMBA_CLIENT=true ;;
  12) CONFIG_SAMBA=true; CONFIG_SAMBA_CLIENT=true; CONFIG_CUPS=true ;;
  13) CONFIG_SAMBA=true; CONFIG_SAMBA_CLIENT=true ;;
  20) CONFIG_SSH=true ;;
  21) CONFIG_SSH=true ;;
  22) CONFIG_SSH=true; CONFIG_CUPS=true ;;
  23) CONFIG_SSH=true ;;
 esac
 if [ "$NETEXPORTflg" == "yes" ];then
  case "$REMOTE_PROBE_METHOD" in
   dnsmasq)    CONFIG_DNS=true ;;
   smbd)       CONFIG_SAMBA=true; CONFIG_SAMBA_CLIENT=true ;;
   fakeidentd) CONFIG_IDENT=true ;;
  esac
 fi
 if [ "$TICK_NS" == "true" ];then #checkbox enable/disable remote file shares
  CONFIG_SAMBA_CLIENT=true
  CONFIG_SSH=true
 fi
 #now, autogenerate firewall...
 echo "GENERIC=$GENERIC
LOGGING=$LOGGING
MAIN=$MAIN
EX=$EX
CONFIG_SSH=$CONFIG_SSH
CONFIG_CUPS=$CONFIG_CUPS
CONFIG_SAMBA=$CONFIG_SAMBA
CONFIG_SAMBA_CLIENT=$CONFIG_SAMBA_CLIENT
CONFIG_DLNA=$CONFIG_DLNA
CONFIG_NTP=$CONFIG_NTP
CONFIG_FTP=$CONFIG_FTP
CONFIG_HTTP=$CONFIG_HTTP
CONFIG_DNS=$CONFIG_DNS
CONFIG_IDENT=$CONFIG_IDENT
CONFIG_DHCP=$CONFIG_DHCP
CONFIG_NFS=$CONFIG_NFS" > /root/.config/firewall/fw.conf2
 if [ "$(iptables -L -n | grep -E '^ACCEPT |^DROP |^RETURN ')" == "" ];then #check if already running.
  true #no, so run the installer.
 else
  if [ -e /etc/init.d/rc.firewall ];then
   chmod 755 /etc/init.d/rc.firewall
   diff -q /root/.config/firewall/fw.conf /root/.config/firewall/fw.conf2 >/dev/null
   if [ $? -eq 0 ];then
    rm -f /root/.config/firewall/fw.conf2
    return #no change to firewall.
   fi
  fi
 fi
 mv -f /root/.config/firewall/fw.conf2 /root/.config/firewall/fw.conf
 #this is it, creates /etc/init.d/rc.firewall and runs it...
 /usr/sbin/firewall_ng nogui
} #auto_firewall_func
export -f auto_firewall_func

[ ! -f /var/local/easyshare/auto-firewall-flag ] && echo -n "true" > /var/local/easyshare/auto-firewall-flag
FWflg="$(cat /var/local/easyshare/auto-firewall-flag)"
auto_firewall_func

###gui sections visibility###
VIS_SETUP="true"; VIS_LOCAL="true"; VIS_REMOTE="true"; VIS_SETUP_BTN="true"
VIS_LOCAL_CUPS="true"; VIS_LOCAL_FILES="true"
VIS_REMOTE_CUPS="true"; VIS_REMOTE_FILES="true"
VIS_REMOTE_SCAN_FILES="false"; VIS_REMOTE_SCAN_CUPS="false"
VIS_AU='false'; VIS_AU_FILES='false'; VIS_AU_ASK='false' #180812 android usb section.
#DEF_FILE_MODE=0|1|2               0=disabled, 1=samba, 2=sshfs how files are to be shared from this pc
#DEF_CUPS_MODE=0|1|2|3             0=disabled, 1=private local printer, 2=shared local printer, 3=network
case "${DEF_CUPS_MODE}" in
 0) VIS_LOCAL_CUPS=false; VIS_REMOTE_CUPS=false ;;
 1) VIS_REMOTE_CUPS=false ;;
 2) VIS_REMOTE_CUPS=false ;;
 3) true ;;
esac
case "${DEF_FILE_MODE}" in
 0) VIS_LOCAL_FILES=false; VIS_SETUP_BTN=false ;;
 1) true ;;
 2) true ;;
esac
if [ "$NETEXPORTflg" == "no" ];then
 VIS_LOCAL=false
fi
#visibility of remote section...
#TICK_NS true/false checkbox, enable/disable access remote folders.
case "${TICK_NS}${DEF_CUPS_MODE}" in
 0[012]) VIS_REMOTE=false; VIS_REMOTE_CUPS=false; VIS_REMOTE_FILES=false ;;
 03)     VIS_REMOTE=true; VIS_REMOTE_CUPS=true; VIS_REMOTE_FILES=false ;;
 13)     VIS_REMOTE=true; VIS_REMOTE_CUPS=true; VIS_REMOTE_FILES=true ;;
 *)      VIS_REMOTE=true; VIS_REMOTE_CUPS=false; VIS_REMOTE_FILES=true ;;
esac
[ "$TICK_AU" == "true" ] && VIS_AU=true #180812

###setup section: cups###
#generate xml code with radiobuttons to select options
#DEF_CUPS_MODE=0|1|2|3             0=disabled, 1=private local printer, 2=shared local printer, 3=network
#cupmode[0123]_func() will change these text strings...
M_slp1="$(gettext 'Shared local printers:')"
echo -n "$M_slp1" > /tmp/easyshare/title-local-printers
LOCALPRINTERS="-$(gettext 'nothing')-"
echo "<b>${LOCALPRINTERS}</b>" > /tmp/easyshare/local-printers
M_snp1="$(gettext 'Network printers:')"
echo -n "$M_snp1" > /tmp/easyshare/title-remote-printers
NETWORKPRINTERS="-$(gettext 'nothing')-"
echo "<b>${NETWORKPRINTERS}</b>" > /tmp/easyshare/remote-printers
echo 'no setup info' > /tmp/easyshare/setup-info
echo -n "$DEF_CUPS_MODE" > /var/local/easyshare/cups-mode
SENSITIVE_CM0="true"; SENSITIVE_CM1="true"; SENSITIVE_CM2="true"; SENSITIVE_CM3="true"
TICK_CM0="false"; TICK_CM1="false"; TICK_CM2="false"; TICK_CM3="false"; 
case "$DEF_CUPS_MODE" in
 0) cupsmode0_func ;;
 1) cupsmode1_func ;;
 2) cupsmode2_func ;;
 3) cupsmode3_func ;;
esac
if [ "$VIS_SETUP" == "true" ];then
  CUPSMODExml="<vbox>
   <text><label>$(gettext 'Printer sharing:')</label></text>
   <radiobutton>
    <label>$(gettext 'Disable printing')</label>
    <default>${TICK_CM0}</default>
    <sensitive>${SENSITIVE_CM0}</sensitive>
    <variable>RADIO_CM0</variable>
    <action signal=\"button-release-event\">auto_firewall_func CM0</action>
    <action signal=\"button-release-event\">cupsmode0_func</action>
    <action signal=\"button-release-event\" function=\"hide\">BOX_LOCAL_CUPS</action>
    <action signal=\"button-release-event\" function=\"hide\">BOX_REMOTE_CUPS</action>
    <action signal=\"button-release-event\" function=\"hide\">REMOTE_SCAN_CUPS</action>
   </radiobutton>
   <radiobutton>
    <label>$(gettext 'Private local printer')</label>
    <default>${TICK_CM1}</default>
    <sensitive>${SENSITIVE_CM1}</sensitive>
    <variable>RADIO_CM1</variable>
    <action condition=\"active_is_true(RADIO_CM1)\">auto_firewall_func CM1</action>
    <action condition=\"active_is_true(RADIO_CM1)\">cupsmode1_func</action>
    <action condition=\"active_is_true(RADIO_CM1)\" function=\"hide\">BOX_LOCAL_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM1)\" function=\"hide\">BOX_REMOTE_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM1)\" function=\"hide\">REMOTE_SCAN_CUPS</action>
   </radiobutton>
   <radiobutton>
    <label>$(gettext 'Share local printer')</label>
    <default>${TICK_CM2}</default>
    <sensitive>${SENSITIVE_CM2}</sensitive>
    <variable>RADIO_CM2</variable>
    <action condition=\"active_is_true(RADIO_CM2)\">auto_firewall_func CM2</action>
    <action condition=\"active_is_true(RADIO_CM2)\">cupsmode2_func</action>
    <action condition=\"active_is_true(RADIO_CM2)\" function=\"show\">BOX_LOCAL</action>
    <action condition=\"active_is_true(RADIO_CM2)\" function=\"show\">BOX_LOCAL_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM2)\" function=\"hide\">BOX_REMOTE_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM2)\" type=\"refresh\">TITLE_LOCAL_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM2)\" type=\"refresh\">PRINTERS_LOCAL_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM2)\" function=\"hide\">REMOTE_SCAN_CUPS</action>
   </radiobutton>
   <radiobutton>
    <label>$(gettext 'Network printer')</label>
    <default>${TICK_CM3}</default>
    <sensitive>${SENSITIVE_CM3}</sensitive>
    <variable>RADIO_CM3</variable>
    <action condition=\"active_is_true(RADIO_CM3)\">auto_firewall_func CM3</action>
    <action condition=\"active_is_true(RADIO_CM3)\">cupsmode3_func</action>
    <action condition=\"active_is_true(RADIO_CM3)\" function=\"show\">BOX_REMOTE</action>
    <action condition=\"active_is_true(RADIO_CM3)\" function=\"hide\">BOX_LOCAL_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM3)\" function=\"show\">BOX_REMOTE_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM3)\" type=\"refresh\">TITLE_REMOTE_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM3)\" type=\"refresh\">PRINTERS_REMOTE_CUPS</action>
    <action condition=\"active_is_true(RADIO_CM3)\" function=\"show\">REMOTE_SCAN_CUPS</action>
   </radiobutton>
  </vbox>"
fi

#handlers for network-shares checkbox...
ns_true_func() {
 echo -n 'true' > /var/local/easyshare/network-shares-flag
}
ns_false_func() {
 echo -n 'false' > /var/local/easyshare/network-shares-flag
}
export -f ns_true_func
export -f ns_false_func

###setup section: file###
#TICK_NS true|false  whether to probe for shares on others pcs.
SENSITIVE_FM0='true'; SENSITIVE_FM1='true'; SENSITIVE_FM2='true'
[ "$HAVESAMBA" == "no" ] && SENSITIVE_FM1='false'
TICK_FM0='true'; TICK_FM1='false'; TICK_FM2='false'
#DEF_FILE_MODE=0|1|2               0=disabled, 1=samba, 2=sshfs how files are to be shared from this pc
case "${DEF_FILE_MODE}" in
 0) filemode0_func ;;
 1) filemode1_func ;;
 2) filemode2_func ;;
esac
if [ "$VIS_SETUP" == "true" ];then
 FILEMODExml="<vbox>
  <text><label>$(gettext 'File sharing:')</label></text>
  <radiobutton>
   <label>$(gettext 'none')</label>
   <default>${TICK_FM0}</default>
   <sensitive>${SENSITIVE_FM0}</sensitive>
   <variable>RADIO_FM0</variable>
   <action condition=\"active_is_true(RADIO_FM0)\">auto_firewall_func FM0</action>
   <action condition=\"active_is_true(RADIO_FM0)\">unmount1_func</action>
   <action condition=\"active_is_true(RADIO_FM0)\">unmount2_func</action>
   <action condition=\"active_is_true(RADIO_FM0)\">filemode0_func</action>
   <action condition=\"active_is_true(RADIO_FM0)\" type=\"refresh\">TXT_SETUP</action>
   <action condition=\"active_is_true(RADIO_FM0)\" type=\"refresh\">PIX_SETUP</action>
   <action condition=\"active_is_true(RADIO_FM0)\" function=\"hide\">BTN_SETUP_RUN</action>
   <action condition=\"active_is_true(RADIO_FM0)\" function=\"hide\">BOX_LOCAL_FILES</action>
  </radiobutton>
  <radiobutton>
   <label>$(gettext 'Samba')</label>
   <default>${TICK_FM1}</default>
   <sensitive>${SENSITIVE_FM1}</sensitive>
   <variable>RADIO_FM1</variable>
   <action signal=\"button-release-event\">auto_firewall_func FM1</action>
   <action condition=\"active_is_false(RADIO_FM1)\">unmount1_func</action>
   <action signal=\"button-release-event\">unmount2_func</action>
   <action signal=\"button-release-event\" function=\"show\">BOX_LOCAL</action>
   <action signal=\"button-release-event\" function=\"show\">BOX_LOCAL_FILES</action>
   <action signal=\"button-release-event\">filemode1_func</action>
   <action signal=\"button-release-event\" type=\"refresh\">TXT_SETUP</action>
   <action signal=\"button-release-event\" type=\"refresh\">PIX_SETUP</action>
   <action signal=\"button-release-event\" function=\"show\">BTN_SETUP_RUN</action>
   <action signal=\"button-release-event\" type=\"refresh\">BTN_SETUP_RUN</action>
   <action signal=\"button-release-event\">local_folders_func</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view1-flg )\">show:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view1-flg )\">refresh:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view1-flg )\">hide:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view2-flg )\">show:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view2-flg )\">refresh:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view2-flg )\">hide:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view3-flg )\">show:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view3-flg )\">refresh:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view3-flg )\">hide:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_false(pidof smbd > /dev/null || echo false)\">hide:BOX_LOCAL_FILES</action>
  </radiobutton>
  <radiobutton>
   <label>$(gettext 'SSHFS')</label>
   <default>${TICK_FM2}</default>
   <sensitive>${SENSITIVE_FM2}</sensitive>
   <variable>RADIO_FM2</variable>
   <action signal=\"button-release-event\">auto_firewall_func FM2</action>
   <action condition=\"active_is_false(RADIO_FM2)\">unmount2_func</action>
   <action signal=\"button-release-event\">unmount1_func</action>
   <action signal=\"button-release-event\" function=\"show\">BOX_LOCAL</action>
   <action signal=\"button-release-event\" function=\"show\">BOX_LOCAL_FILES</action>
   <action signal=\"button-release-event\">filemode2_func</action>
   <action signal=\"button-release-event\" type=\"refresh\">TXT_SETUP</action>
   <action signal=\"button-release-event\" type=\"refresh\">PIX_SETUP</action>
   <action signal=\"button-release-event\" function=\"show\">BTN_SETUP_RUN</action>
   <action signal=\"button-release-event\" type=\"refresh\">BTN_SETUP_RUN</action>
   <action signal=\"button-release-event\">local_folders_func</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view1-flg )\">show:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view1-flg )\">refresh:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view1-flg )\">hide:LOCAL_VIEW1</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view2-flg )\">show:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view2-flg )\">refresh:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view2-flg )\">hide:LOCAL_VIEW2</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view3-flg )\">show:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_true( cat /tmp/easyshare/local-view3-flg )\">refresh:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_false( cat /tmp/easyshare/local-view3-flg )\">hide:LOCAL_VIEW3</action>
   <action signal=\"button-release-event\" condition=\"command_is_false(pidof sshd > /dev/null || echo false)\">hide:BOX_LOCAL_FILES</action>
  </radiobutton>
  <checkbox>
   <label>$(gettext 'Network shares')</label>
   <default>${TICK_NS}</default>
   <variable>CHK_NS</variable>
   <action condition=\"active_is_true(CHK_NS)\">auto_firewall_func NStrue</action>
   <action condition=\"active_is_false(CHK_NS)\">auto_firewall_func NSfalse</action>
   <action condition=\"active_is_true(CHK_NS)\">ns_true_func</action>
   <action condition=\"active_is_false(CHK_NS)\">ns_false_func</action>
   <action condition=\"active_is_false(CHK_NS)\" function=\"hide\">BOX_REMOTE_FILES</action>
   <action condition=\"active_is_false(CHK_NS)\" function=\"hide\">REMOTE_SCAN_FILES</action>
   <action condition=\"active_is_false(CHK_NS)\" function=\"hide\">BOX_REMOTE_ASK</action>
   <action condition=\"active_is_false(CHK_NS)\">echo -n '' > /var/local/easyshare/remote-ip-hn</action>
   <action condition=\"active_is_true(CHK_NS)\" function=\"show\">BOX_REMOTE</action>
   <action condition=\"active_is_true(CHK_NS)\" function=\"hide\">BOX_REMOTE_FILES</action>
   <action condition=\"active_is_true(CHK_NS)\" function=\"show\">REMOTE_SCAN_FILES</action>
   <action condition=\"active_is_true(CHK_NS)\" function=\"show\">BOX_REMOTE_ASK</action>
   <action condition=\"active_is_true(CHK_NS)\">echo -n '' > /var/local/easyshare/remote-ip-hn</action>
  </checkbox>
  <checkbox>
   <label>$(gettext 'Android USB')</label>
   <default>${TICK_AU}</default>
   <variable>CHK_AU</variable>
   <action condition=\"active_is_true(CHK_AU)\">echo -n true > /var/local/easyshare/android-usb-flag</action>
   <action condition=\"active_is_false(CHK_AU)\">echo -n false > /var/local/easyshare/android-usb-flag</action>
   <action condition=\"active_is_false(CHK_AU)\">android_usb_stop_func</action>
   <action condition=\"active_is_true(CHK_AU)\" function=\"show\">BOX_AU</action>
   <action condition=\"active_is_false(CHK_AU)\" function=\"hide\">BOX_AU</action>
   <action condition=\"active_is_false(CHK_AU)\" function=\"hide\">BOX_AU_FILES</action>
   <action condition=\"active_is_true(CHK_AU)\" function=\"show\">BOX_AU_ASK</action>
   <action condition=\"active_is_false(CHK_AU)\" function=\"hide\">BOX_AU_ASK</action>
  </checkbox>
 </vbox>"
fi

SETUPxml="<text><label>\"  \"</label></text>"
FWxml="<text><label>\"  \"</label></text>"
if [ "$VIS_SETUP" == "true" ];then
 
 H_fw1="$(gettext 'Help: Firewall')"
 H_fw2="$(gettext 'Servers, background processes, also known as <b>daemons</b>, communicate via interfaces known as <b>ports</b>. These ports are potentially a security weakness, so it is best to only run daemons that are really needed, and to block unused ports with the firewall. It is highly recommended to tick the <b>Firewall enabled</b> checkbox.')${CR}${CR}$(gettext 'EasyShare configures the firewall automatically. There is also a GUI frontend for the firewall in the menu at bottom-left of the screen, <b>Setup</b> category.')${CR}${CR}<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'The Firewall install script is /usr/sbin/firewall_ng, and the installed firewall is /etc/init.d/rc.firewall. There is a configuration file /root/.config/firewall/fw.conf, that is written to by EasyShare and read by the firewall installer.')"
 export DLG_HELP_FW="<window resizable=\"false\" title=\"${H_fw1}\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_fw2}\"</label><variable>DLG_HELP_FW</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_FW</action></button></hbox></vbox></window>"
 FWflg="$(cat /var/local/easyshare/auto-firewall-flag)" #true or false
 M_fw1="$(gettext 'Firewall enabled')"
 FWxml="
 <checkbox>
  <default>${FWflg}</default>
  <variable>FWchk</variable>
  <label>${M_fw1}</label>
  <action>echo -n \$FWchk > /var/local/easyshare/auto-firewall-flag</action>
  <action>auto_firewall_func</action>
 </checkbox>
 <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_FW</action></button>"
 
 SETUPxml="
 ${CUPSMODExml}
 ${FILEMODExml}"
 H_setup1="$(gettext 'Help: File and printer sharing setup')"
 if [ "$HAVESAMBA" == "yes" ];then
  H_setup2b="$(gettext 'Note: the radiobuttons are for choosing how, or if, local folders are to be served over the network. The Network shares checkbox is not affected by the radiobuttons -- EasyShare will automatically detect any folders shared by Samba or SSH servers on other computers.')"
 else
  H_setup2b="$(gettext 'Note: the Samba radiobutton is disabled. This is because the <b>smbd</b> daemon is missing. You can still connect to SMB servers on other computers, just unable to serve folders from this computer. If you do need the full Samba package, it is available via the Package Manager.')"
 fi
 H_setup2="$(gettext 'This is the setup section of EasyShare, for choosing whether you want to share printers or files, and whether to serve local printers or files, or access network printers or files, or both.')${CR}${CR}<big>$(gettext 'Printer sharing')</big>${CR}$(gettext '<b>Disable printing:</b> completely disable local and network printing.')${CR}$(gettext '<b>Private local:</b> do not share local printers over network.')${CR}$(gettext '<b>Shared local:</b> share local printers over network.')${CR}$(gettext '<b>Network printer:</b> use printers on other computers.')${CR}$(gettext 'Note: current restriction, cannot have local printers if select network printers.')${CR}${CR}<big>$(gettext 'File sharing')</big>${CR}$(gettext '<b>none:</b> do not share any local folders over the network.')${CR}$(gettext '<b>Samba:</b> use Samba server to share local folders over network.')${CR}$(gettext '<b>SSHFS:</b> use the SSH server to share local folders over the network.')${CR}$(gettext '<b>Network shares:</b> enable or disable accessing shared folders on other computers on the network.')${CR}${CR}${H_setup2b}"
 export DLG_HELP_SETUP="<window resizable=\"false\" title=\"${H_setup1}\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_setup2}\"</label><variable>DLG_HELP_SETUP</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_SETUP</action></button></hbox></vbox></window>"
 #display a message informing user of current choices
 setup_info_func #writes to /tmp/easyshare/setup-info
fi

###android usb section### 180812
android_usb_stop_func() {
 echo -n false > /var/local/easyshare/android-usb-flag
 grep 'fuse\.adbfs' /proc/mounts >/dev/null
 if [ $? -eq 0 ];then
  rox -D /mnt/phone
  sync
  fusermount -u /mnt/phone
 fi
 pidof adb >/dev/null
 [ $? -eq 0 ] && adb kill-server
}
android_usb_scan_func() {
 M_au1="$(gettext 'Please wait, probing for Android phone connected via USB...')"
 popup "background=#FFE080|<b><big>${M_au1} </big></b>"
 #200710 easyos 2.3.2 buster, running "adb devices" with my lg nexus 5 phone:
 #List of devices attached
 #0b7f501402b1b4c8	unauthorized
 #need to click accept popup in phone, then get:
 #List of devices attached
 #0b7f501402b1b4c8 device
 M_nothing="$(gettext 'nothing')"
 ADB_PROBE="$(LANG=C adb devices | tr '\t' ' ')"
 DROID_DEV_UNAUTH="$(echo "${ADB_PROBE}" | grep ' unauthorized$' | cut -f 1 -d ' ')" #ex: 0b7f501402b1b4c8
 if [ "$DROID_DEV_UNAUTH" ];then
  ANDROID_DEVICE=""
  M_nothing="$(gettext 'unauthorized')"
 else
  ANDROID_DEVICE="$(echo "${ADB_PROBE}" | grep ' device$' | cut -f 1 -d ' ')" #ex: 0b7f501402b1b4c8
 fi
 if [ "$ANDROID_DEVICE" ];then
  grep 'fuse\.adbfs' /proc/mounts >/dev/null
  if [ $? -eq 0 ];then
   AUFNDxml="
    <hbox><button><input file>/usr/local/lib/X11/pixmaps/folder48.png</input><action>rox -x /mnt/phone</action><action>rox -x /mnt/phone/sdcard</action><action>rox -d /mnt/phone/sdcard & </action></button></hbox>
    <hbox><button><label>$(gettext 'Unmount')</label><action>EXIT:adbfs_unmount</action></button></hbox>"
  else #not mounted
   AUFNDxml="<text use-markup=\"true\"><label>\"<b>${ANDROID_DEVICE}</b>\"</label></text>
    <hbox><button><label>$(gettext 'Mount')</label><action>EXIT:adbfs_mount</action></button></hbox>"
  fi
 else
  AUFNDxml="<text use-markup=\"true\"><label>\"<b>-${M_nothing}-</b>\"</label></text>"
 fi
 killall popup
}
export -f android_usb_scan_func
export -f android_usb_stop_func

#TICK_AU=true}|false    checkbox, whether to probe for android usb phone.
#VIS_AU=true|false      whether android-usb section is visible in gui.
H_au1="$(gettext 'Help: Android phone connected via USB')"
H_au2="$(gettext 'Files may be transfered to and from an Android phone, connected by USB cable.')${CR}${CR}$(gettext '<b>Developer mode</b> must be activated on the phone.')${CR}${CR}$(gettext 'In Settings: Developer mode, there is an option to enable <b>USB debugging</b>, or in some versions of Android it is named <b>Android debugging</b>. This must be enabled.')${CR}${CR}$(gettext 'The first time this is done, EasyShare might report nothing found -- there should be a confirmation dialog on the phone screen -- accept this, then click the rescan button in EasyShare.')${CR}${CR}$(gettext 'If scan reports <b>unauthorized</b>, then a phone was found, but you have not acknowledged the confirmation dialog in the phone.')${CR}${CR}<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'In an Android phone, to turn on Developer mode, go to <i>Settings: About phone:</i> and tap on <i>Build number</i> seven times. Note, after the first few taps, a countdown will display.')${CR}${CR}<span fgcolor='red'><b>$(gettext 'Warning')</b>${CR}$(gettext 'Many USB cables are for power charging only. You need one that supports data transfer. If your phone is not recognised, try changing cables.')</span>"
export DLG_HELP_AU="<window resizable=\"false\" title=\"${H_au1}\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_au2}\"</label><variable>DLG_HELP_AU</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_AU</action></button></hbox></vbox></window>"
if [ "$VIS_AU" == "true" ];then
 android_usb_scan_func #creates AUFNDxml
 #if [ "${ANDROID_DEVICE}" ];then
  VIS_AU_FILES=true
 #else
 # VIS_AU_ASK=true
 #fi
fi

###local section###
M_slf="$(gettext 'Shared local folder:')"
VIS_LOCAL_VIEW1='false'; VIS_LOCAL_VIEW2='false'; VIS_LOCAL_VIEW3='false'
echo -n 'false' > /tmp/easyshare/local-view1-flg
echo -n 'false' > /tmp/easyshare/local-view2-flg
echo -n 'false' > /tmp/easyshare/local-view3-flg
touch /tmp/easyshare/local-folder1
touch /tmp/easyshare/local-folder2
touch /tmp/easyshare/local-folder3
if [ "$VIS_LOCAL" == "true" ];then
 if [ "$VIS_LOCAL_FILES" == "true" ];then
  local_folders_func #180126
  #...sets everything up, so the local folders xml can be refreshed.
 fi
 M_l1="$(gettext 'Other computers will detect this hostname on the local network, and will request username and password to connect. The username is <b>root</b>. and the password default is <b>woofwoof</b> unless you changed it in the <b>Samba/SSHFS Setup</b>.')"
 H_local1="$(gettext "If one or more folder buttons are displayed, the Samba or SSH daemon has made it/them available for other computers on the network to access.")${CR}${CR}$(gettext 'The hostname assigned to this computer is:')${CR}<b>${LOCALHOSTNAME}</b>${CR}${CR}${M_l1}${CR}$(gettext 'In the latter case, you will need to remember the password, as it is encrypted and not retreivable.')${CR}${CR}<b>$(gettext 'Local printers')</b>${CR}$(gettext 'Local printers may be shared over the network. Click the <b>Share local printer</b> radiobutton. To setup and modify a local printer, point the web browser to the CUPS web interface, <b>http://localhost:631</b>, or via the menu <b>Setup -> CUPS Printer Wizard</b>.')${CR}${CR}<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'For Windows to print to a printer on a Linux PC, it must be in <b>raw</b> mode -- see the online help, via the <b>Help</b> button at bottom of EasyShare window.')"
 export DLG_HELP_LOCAL="<window resizable=\"false\" title=\"$(gettext 'Help: Local shares')\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_local1}\"</label><variable>DLG_HELP_LOCAL</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_LOCAL</action></button></hbox></vbox></window>"
fi


###remote section###
REMOTEFILESxml="<text><label>remote files</label></text>"
if [ "$VIS_REMOTE" == "true" ];then
 
 if [ "$VIS_REMOTE_FILES" == "true" ];then
  #180125 scan for remote sshfs or samba servers...
  scan_file_servers_func
  #...creates /var/local/easyshare/remote-ip-hn
  #180125
  remote_folders_func
  #...creates $REMOTEFILESxml
 fi
 
 H_remote1="$(gettext 'Other computers on the network may share folders and printers.')${CR}${CR}$(gettext 'Network shared folders may be mounted by clicking the <b>Mount</b> button, and unmounted by clicking the <b>Unmount</b> button. When mounted, files on the other computer may be read on this computer.')${CR}${CR}$(gettext 'Network shared printers are available locally just like any normal local printer.')${CR}${CR}<b>Technical notes</b>${CR}$(gettext 'Samba is set to mount folders read-only. This is due to issues that Samba has with Linux file permissions and ownership.')${CR}$(gettext 'SSHF mounts folders read-write, and is the best choice for Linux-to-Linux sharing.')"
 export DLG_HELP_REMOTE="<window resizable=\"false\" title=\"$(gettext 'Help: Network shares')\" icon-name=\"${HELPICON}\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${H_remote1}\"</label><variable>DLG_HELP_REMOTE</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP_REMOTE</action></button></hbox></vbox></window>"

 if [ "$VIS_REMOTE_FILES" == "false" ];then
  VIS_REMOTE_SCAN_FILES="false" #scan button
 else
  VIS_REMOTE_SCAN_FILES="true"
 fi
 if [ "$VIS_REMOTE_CUPS" == "false" ];then
  VIS_REMOTE_SCAN_CUPS="false" #scan button
 else
  VIS_REMOTE_SCAN_CUPS="true"
 fi
fi

while [ 1 ];do #180813 so can reenter the main dlg.
export ES_MAIN_DLG="
<window title=\"$(gettext 'EasyShare')\" icon-name=\"gtk-network\" resizable=\"false\">
 <vbox>
  <text use-markup=\"true\"><label>\"<b>$(gettext 'Share files and printers over a local network')</b>\"</label></text>
  <hseparator></hseparator>
  
  <hbox>
   ${FWxml}
   <variable>BOX_FIREWALL</variable>
  </hbox>
 
  <vbox visible=\"${VIS_SETUP}\">
   <hseparator></hseparator>
   <hbox>
    ${SETUPxml}
    <text width-chars=\"20\" use-markup=\"true\"><label>...</label><input file>/tmp/easyshare/setup-info</input><variable>TXT_SETUP</variable></text>
    <vbox>
     <hbox>
      <pixmap><width>56</width><input file>/tmp/easyshare/setup-icon.svg</input><variable>PIX_SETUP</variable></pixmap>
      <vbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_SETUP</action></button></vbox>
     </hbox>
     <button visible=\"${VIS_SETUP_BTN}\">
      <input file>/tmp/easyshare/share-setup.png</input>
      <variable>BTN_SETUP_RUN</variable>
      <action function=\"exit\">filesetup</action>
     </button>
    </vbox>
   </hbox>
   <variable>BOX_SETUP</variable>
  </vbox>
 
  <vbox visible=\"${VIS_LOCAL}\">
   <hseparator></hseparator>
   <hbox>
    <text use-markup=\"true\">
     <label>\"<span fgcolor='DarkBlue'>$(gettext 'Local hostname:')
<b>${LOCALHOSTNAME}</b>
$(gettext 'Local IP-address:')
<b>${LOCALIPADDRESS}</b></span>\"</label>
    </text>
    <vbox visible=\"${VIS_LOCAL_CUPS}\">
     <text><label>local cups</label><input file>/tmp/easyshare/title-local-printers</input><variable>TITLE_LOCAL_CUPS</variable></text>
     <text use-markup=\"true\"><label>\"<b>local cups</b>\"</label><input file>/tmp/easyshare/local-printers</input><variable>PRINTERS_LOCAL_CUPS</variable></text>
     <variable>BOX_LOCAL_CUPS</variable>
    </vbox>
    <vbox visible=\"${VIS_LOCAL_FILES}\">
     <text><label>${M_slf}</label></text>
     
     <hbox>
      <vbox visible=\"${VIS_LOCAL_VIEW1}\">
       <text use-markup=\"true\"><label>\"<b>folder1</b>\"</label><input file>/tmp/easyshare/local-folder1</input></text>
       <hbox><button><input file>/usr/local/lib/X11/pixmaps/folder48.png</input><action>/tmp/easyshare/rox-local-folder1 & </action></button></hbox>
       <variable>LOCAL_VIEW1</variable>
      </vbox>
      <vbox visible=\"${VIS_LOCAL_VIEW2}\">
       <text use-markup=\"true\"><label>\"<b>folder2</b>\"</label><input file>/tmp/easyshare/local-folder2</input></text>
       <hbox><button><input file>/usr/local/lib/X11/pixmaps/folder48.png</input><action>/tmp/easyshare/rox-local-folder2 & </action></button></hbox>
       <variable>LOCAL_VIEW2</variable>
      </vbox>
      <vbox visible=\"${VIS_LOCAL_VIEW3}\">
       <text use-markup=\"true\"><label>\"<b>folder3</b>\"</label><input file>/tmp/easyshare/local-folder3</input></text>
       <hbox><button><input file>/usr/local/lib/X11/pixmaps/folder48.png</input><action>/tmp/easyshare/rox-local-folder3 & </action></button></hbox>
       <variable>LOCAL_VIEW3</variable>
      </vbox>
     </hbox>
     
     <variable>BOX_LOCAL_FILES</variable>
    </vbox>
    <vbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_LOCAL</action></button></vbox>
   </hbox>
   <variable>BOX_LOCAL</variable>
  </vbox>
  
  <vbox visible=\"${VIS_AU}\">
   <hseparator></hseparator>
   <hbox>
    <vbox visible=\"${VIS_AU_FILES}\">
     <text><label>$(gettext 'Android phone:')</label></text>
     ${AUFNDxml}
     <variable>BOX_AU_FILES</variable>
    </vbox>
    <vbox visible=\"${VIS_AU_ASK}\">
     <text><label>$(gettext 'Android phone:')</label></text>
     <text width-chars=\"16\" use-markup=\"true\"><label>\"<span fgcolor='DarkRed'>$(gettext 'Unknown, please press the <b>Scan</b> button:')</span>\"</label></text>
     <variable>BOX_AU_ASK</variable>
    </vbox>
    <vbox>
     <hbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_AU</action></button></hbox>
     <button>
      <width>56</width>
      <input file>/usr/share/pixmaps/easyshare/scan-android.svg</input>
      <action>exit:androidusb</action>
     </button>
    </vbox>
   </hbox>
   <variable>BOX_AU</variable>
  </vbox>
  
  <vbox visible=\"${VIS_REMOTE}\">
   <hseparator></hseparator>
   <hbox>
    <vbox visible=\"${VIS_REMOTE_CUPS}\">
     <text><label>remote cups</label><input file>/tmp/easyshare/title-remote-printers</input><variable>TITLE_REMOTE_CUPS</variable></text>
     <text use-markup=\"true\"><label>\"<b>remote cups</b>\"</label><input file>/tmp/easyshare/remote-printers</input><variable>PRINTERS_REMOTE_CUPS</variable></text>
     <variable>BOX_REMOTE_CUPS</variable>
    </vbox>
    <vbox visible=\"${VIS_REMOTE_FILES}\">
     ${REMOTEFILESxml}
     <variable>BOX_REMOTE_FILES</variable>
    </vbox>
    <vbox visible=\"false\">
     <text><label>$(gettext 'Remote folders:')</label></text>
     <text width-chars=\"16\" use-markup=\"true\"><label>\"<span fgcolor='DarkRed'>$(gettext 'Unknown, please press the <b>Scan</b> button:')</span>\"</label></text>
     <variable>BOX_REMOTE_ASK</variable>
    </vbox>
    <vbox>
     <hbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP_REMOTE</action></button></hbox>
     <button visible=\"${VIS_REMOTE_SCAN_FILES}\">
      <width>56</width>
      <input file>/usr/share/pixmaps/easyshare/scan-files.svg</input>
      <variable>REMOTE_SCAN_FILES</variable>
      <action>scan_file_servers_func</action>
      <action condition=\"command_is_true( cat /tmp/easyshare/scan_change_flg )\">/bin/sh /tmp/easyshare/msg-no-change</action>
      <action condition=\"command_is_false( cat /tmp/easyshare/scan_change_flg )\">EXIT:rescan</action>
     </button>
     <button visible=\"${VIS_REMOTE_SCAN_CUPS}\">
      <width>56</width>
      <input file>/usr/share/pixmaps/easyshare/scan-cups.svg</input>
      <variable>REMOTE_SCAN_CUPS</variable>
      <action>scan_cups_servers_func</action>
      <action condition=\"command_is_true( cat /tmp/easyshare/scan_change_flg )\">/bin/sh /tmp/easyshare/msg-cups-no-change</action>
      <action condition=\"command_is_false( cat /tmp/easyshare/scan_change_flg )\" type=\"refresh\">TITLE_REMOTE_CUPS</action>
      <action condition=\"command_is_false( cat /tmp/easyshare/scan_change_flg )\" type=\"refresh\">PRINTERS_REMOTE_CUPS</action>
     </button>
    </vbox>
   </hbox>
   <variable>BOX_REMOTE</variable>
  </vbox>
 
  <hseparator></hseparator>
  <hbox>
   <button><label>$(gettext 'Help')</label><action>defaultbrowser https://easyos.org/apps/easyshare-super-easy-network-file-and-printer-sharing.html & </action></button>
   <button><label>$(gettext 'Exit')</label><action function=\"exit\">quit</action></button>
  </hbox>
 </vbox>
</window>"

echo "$ES_MAIN_DLG" > /tmp/easyshare/es-main-dlg #TEST
RETVALS="$(gtkdialog --program=ES_MAIN_DLG --center)"

#cleanup retvals...
xRETVALS="$(echo "$RETVALS" | grep '^[a-zA-Z0-9_]*=".*"$')"

echo "$xRETVALS" > /tmp/easyshare-retvals-test #TEST
eval "$xRETVALS"
DEF_FILE_MODE="$(cat /var/local/easyshare/file-mode)"
DEF_CUPS_MODE="$(cat /var/local/easyshare/cups-mode)"

if [ "$EXIT" == "firewall" ];then
 [ -e /etc/init.d/rc.firewall ] && chmod 755 /etc/init.d/rc.firewall
 /usr/sbin/firewallinstallshell
 exec /usr/local/EasyShare/easyshare
fi

if [ "$EXIT" == "filesetup" ];then
 /tmp/easyshare/setup
 exec /usr/local/EasyShare/easyshare
fi

if [ "$EXIT" == "rescan" ];then
 exec /usr/local/EasyShare/easyshare
fi

if [[ $EXIT == mount_* ]];then
 REMSERV="$(echo -n "$EXIT" | cut -f 2 -d '_')" #SMB or DNSMASQ  180807 or IDENT
 case "$REMSERV" in
  SMB) DEF_FILE_MODE='1' ;;
  *)   DEF_FILE_MODE='2' ;;
 esac
 if [ "$DEF_FILE_MODE" == "1" ];then
  DEFUSER='root' #samba
 else
  DEFUSER='root' #sshfs #180811
 fi
 remIP="$(echo -n "$EXIT" | cut -f 3 -d '_' | tr 'x' '.')"
 remHN="$(grep ":${remIP}|" /var/local/easyshare/remote-ip-hn | cut -f 2 -d '|')" #180207 180730
 export ES_MNT_DLG="<window title=\"$(gettext 'EasyShare: mount remote share')\" icon-name=\"gtk-network\">
  <vbox>
   <text><label>\"$(gettext 'Please enter the username and password to mount the remote share.')${CR}$(gettext '(leave password field empty if not required)')\"</label></text>
   <text use-markup=\"true\"><label>\"$(gettext 'Remote hostname:') <b>${remHN}</b>\"</label></text>
   <hbox>
    <text><label>$(gettext 'Username:')</label></text>
    <entry><default>${DEFUSER}</default><variable>aUSERNAME</variable></entry>
   </hbox>
   <hbox>
    <text><label>$(gettext 'Password:')</label></text>
    <entry><variable>aPASSWORD</variable></entry>
   </hbox>
   <hbox>
    <button cancel></button>
    <button ok></button>
   </hbox>
  </vbox>
  </window>"
 RETVALS="$(gtkdialog --program=ES_MNT_DLG --center)"
 eval "$RETVALS"
 
 [ "$EXIT" != "OK" ] && exec easyshare restart
 
 case "$DEF_FILE_MODE" in
  1) #samba
   #note, -U "root%" works, no password required. if just hav -U "root" asks for password...
   SHARENAMES="$(smbclient -L ${remHN} -U "${aUSERNAME}%${aPASSWORD}" 2>/dev/null | tr '\t' ' ' | tr -s ' ' | grep ' Disk ' | grep -v '\$' | cut -f 2 -d ' ')"
   #181222 maybe better to also use -I param? ...
   SHARENAMES="$(smbclient -I ${remIP} -L ${remHN} -U "${aUSERNAME}%${aPASSWORD}" 2>/dev/null | tr '\t' ' ' | tr -s ' ' | grep ' Disk ' | grep -v '\$' | cut -f 2 -d ' ')"
   #181221 above is failing, odd, but this works...
   [ ! "$SHARENAMES" ] && SHARENAMES="$(smbclient -L ${remIP} -U "${aUSERNAME}%${aPASSWORD}" 2>/dev/null | tr '\t' ' ' | tr -s ' ' | grep ' Disk ' | grep -v '\$' | cut -f 2 -d ' ')"
   echo "SHARENAMES='${SHARENAMES}'" #TEST
   snCNT=0
   for aSN in $SHARENAMES
   do
    [ ! "$aSN" ] && continue
    snCNT=$(($snCNT+1))
    aMNTPT="/mnt/${remHN}_${snCNT}"
    mkdir -p $aMNTPT
    echo "EXECUTING: mount -t cifs -o vers=2.0,username="${aUSERNAME}",password="${aPASSWORD}",cifsacl,ro //${remIP}/${aSN} ${aMNTPT}" #TEST
    mount -t cifs -o vers=2.0,username="${aUSERNAME}",password="${aPASSWORD}",cifsacl,ro //${remIP}/${aSN} ${aMNTPT}
    if [ $? -eq 0 ];then
     rox -x ${aMNTPT}
     rox -d ${aMNTPT}
    else
     export ES_FAIL_MNT_DLG="<window title=\"$(gettext 'EasyShare: Failed mount remote share')\" icon-name=\"gtk-network\">
     <vbox>
      <text><label>$(gettext 'Something is wrong, failed to mount the remote share.')</label></text>
      <text use-markup=\"true\"><label>\"$(gettext 'Remote hostname:') <b>${remHN}</b>${CR}$(gettext 'Remote IP-address:') <b>${remIP}</b>${CR}$(gettext 'Remote sharename:') <b>${aSN}</b>\"</label></text>
      <text use-markup=\"true\"><label>\"<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'Refer to this script:')${CR}/usr/sbin/easyshare${CR}$(gettext 'This is the command that failed:')${CR}mount -t cifs -o username=\"${aUSERNAME}\",password=\"${aPASSWORD}\",cifsacl,ro //${remIP}/${aSN} ${aMNTPT}${CR}$(gettext 'The most likely cause of failure is that the sharename is wrong. The sharename was extracted by parsing output of this command:')${CR}smbclient -L ${remHN} -U \"${aUSERNAME}%${aPASSWORD}\"${CR}$(gettext 'Let Barry know at bkauler@gmail.com if you have a failure here, especially if you have found out why.')\"</label></text>
      <hbox>
       <button ok></button>
      </hbox>
     </vbox>
     </window>"
     RETVALS="$(gtkdialog --program=ES_FAIL_MNT_DLG --center)"
    fi
   done
  ;;
  *) #sshfs
   MNTPT="/mnt/${remHN}"
   mkdir -p $MNTPT
   mkdir -p /root/.ssh

   #this fixes an error msg at first login...
   #ref: https://stackoverflow.com/questions/3663895/ssh-the-authenticity-of-host-hostname-cant-be-established
   mkdir -p /root/.ssh
   SSHKEYGENflg="$(ssh-keygen -F ${remIP} | wc -l)"
   if [ "$SSHKEYGENflg" == "0" ]; then
    ssh-keyscan -H -p 22 ${remIP} >> /root/.ssh/known_hosts
   fi

   #180810 Rox gave error when copied a file, unable to change ownership root->rover. add "nomap=ignore"
   #180811 no longer using rover, nor ssh0 container... 180816 remove $MYPATHremote...
   #echo "${aPASSWORD}" | sshfs root@${remIP}:${MYPATHremote} ${MNTPT} -p 22 -o allow_root,password_stdin #allow_other,password_stdin,nomap=ignore
   echo "${aPASSWORD}" | sshfs root@${remIP}: ${MNTPT} -p 22 -o allow_root,password_stdin #allow_other,password_stdin,nomap=ignore
   if [ $? -eq 0 ];then
    rox -x ${MNTPT}
    #rox -d ${MNTPT}
    #180813 launch rox, as well as easyshare gui...
    rox_delayed_func ${MNTPT} &
    remote_folders_func #...creates $REMOTEFILESxml
    continue #rerun gui.
   else
    export ES_FAIL_MNT_DLG="<window title=\"$(gettext 'EasyShare: Failed mount remote share')\" icon-name=\"gtk-network\">
    <vbox>
     <text><label>$(gettext 'Something is wrong, failed to mount the remote share.')</label></text>
     <text use-markup=\"true\"><label>\"$(gettext 'Remote hostname:') <b>${remHN}</b>${CR}$(gettext 'Remote IP-address:') <b>${remIP}</b>${CR}$(gettext 'Remote sharename:') <b>${aSN}</b>\"</label></text>
     <text use-markup=\"true\"><label>\"<b>$(gettext 'Technical notes')</b>${CR}$(gettext 'Refer to this script:')${CR}/usr/sbin/easyshare${CR}$(gettext 'This is the command that failed:')${CR}
    echo ${aPASSWORD} | sshfs root@${remIP}:${MYPATHremote} ${MNTPT} -p 22 -o allow_root,password_stdin${CR}${CR}$(gettext 'Let Barry know at bkauler@gmail.com if you have a failure here, especially if you have found out why.')\"</label></text>
     <hbox>
      <button ok></button>
     </hbox>
    </vbox>
    </window>"
    RETVALS="$(gtkdialog --program=ES_FAIL_MNT_DLG --center)"
   fi
  ;;
 esac
fi

if [[ $EXIT == unmount_* ]];then #ex: unmount_SMB_DESKTOP-4IHIEA_1
 REMSERV="$(echo -n "$EXIT" | cut -f 2 -d '_')" #SMB or DNSMASQ  180807 or IDENT
 MNTfolder="$(echo -n "$EXIT" | cut -f 3- -d '_')"
 rox -D /mnt/${MNTfolder}
 sync
 if [ "$REMSERV" == "SMB" ];then #180807
  umount /mnt/${MNTfolder}
 else
  fusermount -u /mnt/${MNTfolder} #sshfs
 fi
 if [ $? -eq 0 ];then
  rmdir /mnt/${MNTfolder}
  M3_c="$(gettext 'The network share has been unmounted')"
  pupdialog --background "#80FF80" --backtitle "EasyShare" --msgbox "${M3_c}" 0 0
  exit
 else
  M3_c="$(gettext 'Failed to unmount network share')"
  pupdialog --background "#FF8080" --backtitle "EasyShare" --msgbox "${M3_c}" 0 0
  exec easyshare restart
 fi
fi

if [ "$EXIT" == "androidusb" ];then #180812
 exec easyshare restart #note, not doing anything with that "restart" param.
fi

if [ "$EXIT" == "adbfs_mount" ];then #180812
 adbfs /mnt/phone > /tmp/easyshare/adbfs-mount-log 2>&1
 grep 'device unauthorized' /tmp/easyshare/adbfs-mount-log >/dev/null
 if [ $? -eq 0 ];then
  M_am1="$(gettext 'phone error')"
  M_am2="$(gettext 'There should be a confirmation dialog on your phone screen. Confirm, then click OK button:')"
  pupdialog --background "#FF8080" --backtitle "EasyShare: ${M_am1}" --msgbox "${M_am2}" 0 0
  adbfs /mnt/phone > /tmp/easyshare/adbfs-mount-log2 2>&1
 fi
 android_usb_scan_func
 if [ "${ANDROID_DEVICE}" ];then
  VIS_AU_FILES=true
  VIS_AU_ASK=false
  rox -x /mnt/phone
  rox -x /mnt/phone/sdcard
  rox_delayed_func /mnt/phone/sdcard &
 else
  VIS_AU_FILES=false
  VIS_AU_ASK=true
  rox -D /mnt/phone
 fi
 continue
fi

if [ "$EXIT" == "adbfs_unmount" ];then #180812
 grep 'fuse\.adbfs' /proc/mounts >/dev/null
 if [ $? -eq 0 ];then
  rox -D /mnt/phone
  sync
  fusermount -u /mnt/phone
 fi
 android_usb_scan_func
 continue
fi

 break
done #180813 main dlg

###finish###
#HAVESAMBA=yes|no                  yes if have smbd and nmbd daemons.
#REMOTE_PROBE_METHOD=smbd|dnsmasq|fakeidentd  local server used by other pcs to probe this pc. (to acquire hostname)
#DEF_FILE_MODE=0|1|2               0=disabled, 1=samba, 2=sshfs how files are to be shared from this pc
#DEF_CUPS_MODE=0|1|2|3             0=disabled, 1=private local printer, 2=shared local printer, 3=network printer
case "${DEF_FILE_MODE}" in
 0)
  /etc/init.d/${INITSCRIPT} stop 2>/dev/null #INITSCRIPT is 'rc.samba' or 'dnsmasq'
  chmod 644 /etc/init.d/${INITSCRIPT}
 ;;
esac
###END###
