#!/bin/sh
#(c) Copyright 2010 Barry Kauler bkhome.org
#Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
#called from 'connectwizard'
#choose between old Network Wizard, new Network Wizard and Pwireless2.
#100227 BK initial creation of script.
#100306 added Barry's Simple Network Setup. 100810 moved SNS to top choice, improved messages.
#120201 rodin.s: internationalized.
#121029 01micko, rerwin: added/integrated Frisbee.
#121122 rerwin: change indicator of frisbee; ensure frisbee stopped if not chosen.
#130103 rerwin: change name of frisbee invocations for integrated version.
#151105 change gtkdialog3 to gtkdialog.
#180127 rerwin: use interfaces for frisbee 1.4, if installed
#181124 BK: handle NetworkManager.
#190211 BK: handle UltraSNS.  190216
#190805 may have nm-connection-editor (in network-manager-applet pkg) instead of nmtui (in network-manager pkg).
#190922 BK: 190805: now "nm-setup" in defaultconnect.
#190926 BK: call scripts /usr/sbin/nm-enable, nm-disable
#190926 BK: removed string "Puppy" from messages. make sure networkmanager disabled.

export TEXTDOMAIN=connectwizard_2nd
export TEXTDOMAINDIR=/usr/share/locale
export OUTPUT_CHARSET=UTF-8
. gettext.sh
export LANGORG=$LANG

if pidof NetworkManager >/dev/null ;then #181124
 /etc/init.d/rc.networkmanager stop
fi

#100306
SNS=""
if [ -d /usr/local/simple_network_setup ];then
 SNS="<frame $(gettext 'Simple Network Setup') (SNS)>  <hbox>
   <text><label>$(gettext 'This is a simple network setup tool created by BarryK. Not as many features as the Network Wizard, for example requires the network to have a DHCP server (the vast majority of cases). The greatest feature is extreme ease of use. Recommended for most people.')</label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGSNS</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

USNS="" #190211...
#if [ -d /usr/local/UltraSNS ];then
if [ -f /sbin/wsupp ];then #190216
 USNS="<frame $(gettext 'Ultra Simple Network Setup') (UltraSNS)>  <hbox>
<text><label>$(gettext 'UltraSNS is designed by BarryK, using utilities created by Alex Suykov. It is almost completely automatic, and is now the default at first bootup.')</label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGUSNS</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

PWIRELESS=""
if [ -d /usr/local/Pwireless2 ];then #Jemimah's wireless connection gui.
 PWIRELESS="<frame Pwireless2>  <hbox>
   <text><label>$(gettext 'This is a tool developed by Jemimah. Mostly for wireless connectivity for laptops, but also can handle ethernet connection. Very good for wireless roaming. Under very active development (see forum).')</label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGPWIRELESS</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

FRISBEE="" #121029...
if [ -f /usr/local/bin/frisbee ];then #Jemimah's wireless connection gui. 121122 130103
 FRISBEE="<frame Frisbee>  <hbox>
   <text><label>$(gettext 'This is a tool developed by Jemimah. Mostly for wireless connectivity for laptops, but also can handle ethernet connection. Very good for wireless roaming.')</label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGFRISBEE</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

NETWIZARD=""
if [ -f /usr/sbin/net-setup.sh ];then #wizard developed by Dougal.
 NETWIZARD="<frame $(gettext 'Network Wizard')>  <hbox>
   <text><label>`gettext \"This is a sophisticated tool for network setup. It is based on the 'Classic' Network Wizard originally written by BarryK, but greatly enhanced by Dougal, Rarsa, Shinobar and others. It is for both wired (ethernet) and wireless connectivity, with more configuration features than Pwireless and SNS.\"`</label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGNETWIZARD</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

NETWIZCLASSIC=""
if [ -f /usr/local/net_setup/usr/sbin/net-setup.sh ];then #wizard used in older puppies.
 NETWIZCLASSIC="<frame $(gettext 'Network Wizard (Classic)')>  <hbox>
   <text><label>$(gettext 'The Network Wizard was originally developed by Barry Kauler, with considerable input from others.') </label></text>
   <vbox>
   <button>
    <input file>/usr/local/lib/X11/mini-icons/pc-2x.xpm</input>
    <action type=\"exit\">FLAGNETWIZCLASSIC</action>
   </button>
   </vbox>
  </hbox>
  </frame>
"
fi

export MAIN_DIALOG="
<window title=\"$(gettext 'Network Connection Wizard')\" icon-name=\"gtk-connect\">
<vbox>
 <text><label>$(gettext 'Here is a choice of GUI tools that you can use to connect to a network (and Internet)...')</label></text>

 ${USNS}
 ${SNS}
 ${PWIRELESS}
 ${FRISBEE}
 ${NETWIZARD}
 ${NETWIZCLASSIC}

 <hbox>
  <button cancel></button>
 </hbox>
</vbox>
</window>
" #121029

RETSTRING="`gtkdialog --program=MAIN_DIALOG --center`" 
#[ "`echo "$RETSTRING" | grep 'EXIT' | grep 'OK'`" = "" ] && return
#gtkdialog3 --program=MAIN_DIALOG --center

[ "`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG'`" == "" ] && exit #190926

#190926 none of the choices in this script are for networkmanager, so make
#sure it is disabled...
/usr/sbin/nm-disable

CHOSENWIZ="`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG' | cut -f 2 -d '"'`" #'geany

if [ "$FRISBEE" ];then #130103...
 if [ "$CHOSENWIZ" != "FLAGFRISBEE" ];then #180127...
  if [ -x /usr/local/frisbee/frisbee-main ];then
   frisbee --deactivate
  else
   frisbee_mode_disable
  fi
 fi
fi
 
if [ "$CHOSENWIZ" != "FLAGUSNS" ];then #190211
 if pidof ifmon wsupp >/dev/null ;then #UltraSNS daemons
  /usr/local/UltraSNS/rc.network stop
 fi
fi

case $CHOSENWIZ in
 FLAGPWIRELESS) #Pwireless2
  chmod 755 /etc/init.d/Pwireless2 #100304
  /etc/init.d/Pwireless2 start #100304
  /usr/local/bin/Pwireless2 > /dev/null 2>&1
  NWEXEC='Pwireless2'
  NWCHOICE="Jemimah's Pwireless2"
 ;;
 FLAGFRISBEE) #121029
  /usr/local/bin/frisbee > /dev/null 2>&1 #130103
  NWEXEC='frisbee' #130103
  NWCHOICE="Jemimah's Frisbee"
 ;;
 FLAGNETWIZARD) #net-setup.sh
  /usr/sbin/net-setup.sh > /dev/null 2>&1
  NWEXEC='net-setup.sh'
  NWCHOICE="Dougal's Network Wizard"
 ;;
 FLAGNETWIZCLASSIC) #net_wiz_classic
  /usr/sbin/net_wiz_classic > /dev/null 2>&1
  NWEXEC='net_wiz_classic'
  NWCHOICE="Barry's Network Wizard Classic"
 ;;
 FLAGSNS)
  /usr/sbin/sns
  NWEXEC='sns'
  NWCHOICE="Barry's Simple Network Setup"
 ;;
 FLAGUSNS) #190211
  /usr/sbin/usns
  NWEXEC='usns'
  NWCHOICE="Ultra Simple Network Setup"
 ;;
esac
 
IFSUP="`ifconfig | grep -E '^eth|^wlan'`"
if [ "$IFSUP" != "" ];then
 IFSUP="`echo "$IFSUP" | cut -f 1 -d ' ' | tr '\n' ' '`"
 MSG1="$(gettext 'These interfaces are active:')
$IFSUP

`gettext \"...Satisfied? Got online ok? If yes, click 'YES SET AS DEFAULT' button, so it will be automatically used at future bootups.\"`"
else
 MSG1="`gettext \"No interfaces are active, so was the tool unsatisfactory? If it actually was OK, click the 'YES SET AS DEFAULT' button. But, it seems not, so...\"`"
fi
 
CURREXEC="`cat /usr/local/bin/defaultconnect | tail -n 1 | tr -s " " | cut -f 2 -d " "`"
[ "`grep 'gprs' /usr/local/bin/defaultconnect`" != "" ] && CURREXEC='pgprs-connect'
[ "$CURREXEC" = "gkdial" ] && CURREXEC="pupdial" #for older pups.
[ "`grep 'nmtui' /usr/local/bin/defaultconnect`" != "" ] && CURREXEC='nmtui' #190926
case $CURREXEC in
 Pwireless2)
  CURRCHOICE="Jemimah's Pwireless2"
 ;;
 frisbee) #121029 130103
  CURRCHOICE="Jemimah's Frisbee"
 ;;
 net-setup.sh)
  CURRCHOICE="Dougal's Network Wizard"
 ;;
 net_wiz_classic)
  CURRCHOICE="Barry's Network Wizard Classic"
 ;;
 sns)
  CURRCHOICE="Barry's Simple Network Setup"
 ;;
 usns) #190211
  CURRCHOICE="Ultra Simple Network Setup"
 ;;
 *)
  CURRCHOICE="${CURREXEC}"
 ;;
esac

[ "$NWEXEC" == "$CURREXEC" ] && exit

export POST_DIALOG="
<window title=\"$(gettext 'Network Connection Wizard')\" icon-name=\"gtk-connect\">
<vbox>
 <text use-markup=\"true\"><label>\"<b>$(gettext 'You have finished running') '${NWCHOICE}'</b>\"</label></text>
 <text><label>\"${MSG1}\"</label></text>
 <text><label>`gettext \"Click 'TRY A DIFFERENT TOOL' if you would like to try one of the other network setup tools...\"`</label></text>
 <text><label>`gettext \"Click 'Cancel' button to quit without setting a default tool...\"`</label></text>
 <hbox>
   <button><input file>/usr/local/lib/X11/mini-icons/mini-tick.xpm</input><label>$(gettext 'YES SET AS DEFAULT')</label><action type=\"exit\">FLAGYES</action></button>
   <button><input file>/usr/local/lib/X11/mini-icons/mini-cross.xpm</input><label>$(gettext 'TRY A DIFFERENT TOOL')</label><action type=\"exit\">FLAGTRYDIFF</action></button>
   <button cancel></button>
 </hbox>
</vbox>
</window>
"

RETSTRING="`gtkdialog --program=POST_DIALOG --center`" 
if [ "`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG'`" != "" ];then
 CHOSENACTION="`echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG' | cut -f 2 -d '"'`" #'geany
 case $CHOSENACTION in
  FLAGYES)
   echo -e "#!/bin/sh\nexec ${NWEXEC}" > /usr/local/bin/defaultconnect
   export INFO_DIALOG="
<window title=\"$(gettext 'Network Connection Wizard')\" icon-name=\"gtk-connect\">
<vbox>
 <text use-markup=\"true\"><label>\"<b>'${NWCHOICE}' $(gettext 'now set as default network tool. Here are some technical notes that you might find useful:')</b>\"</label></text>
 <text><label>`eval_gettext \"The default network tool is specified in file /usr/local/bin/defaultconnect, and if you were to look in that file now you will see that it contains '\\\${NWEXEC}'. If you ever wanted to manually change that back to the original setting, you could edit the file and in place of '\\\${NWEXEC}' put 'connectwizard'.\"`</label></text>
  <text><label>`eval_gettext \"If you click on the 'connect' icon on the desktop, it will directly start the '\\\${NWEXEC}' tool. In fact, it starts whatever has been specified in /usr/local/bin/defaultconnect. You can right-click on the 'connect' icon for a menu with other run options.\"`</label></text>
 <text><label>`gettext \"EXPERTS ONLY: If you really want to know in-depth about the inner workings, specifically how the default tool is activated at bootup, look in /etc/rc.d/rc.sysinit. Two other relevant scripts are /usr/sbin/connectwizard and /usr/sbin/connectwizard_2nd.\"`</label></text>
 <hbox><button ok></button></hbox>
</vbox>
</window>"
   gtkdialog --program=INFO_DIALOG --center
  ;;
  FLAGTRYDIFF)
   echo -e '#!/bin/sh\nexec connectwizard' > /usr/local/bin/defaultconnect
   exec connectwizard
  ;;
 esac
fi
 
#181124 190805 190922 190926
if grep -E 'nm\-connection\-editor|nm\-setup|nmtui' /usr/local/bin/defaultconnect >/dev/null ;then
 /usr/sbin/nm-enable
else
 /usr/sbin/nm-disable
fi
 
###end###
