#!/bin/bash
#Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#Barry Kauler www.puppylinux.com
#Updated for Puppy 4.00, April 2008.
#v405 july 2008 updated.
#v409 add both Pppoeconf and Roaring Penguin PPPOE to menus.
#v411 remove pppoeconf buttons from gui if pkg not installed.
#v411 anythng on commandline suppresses default-connect gui (see wizardwizard).
#v423 menu modification, cater to dialup wireless modem.
#091218 button for ipinfo.
#100227 overhaul, Network Wizard
#100310 added Simple Network Setup tool.
#100412 sns related bug fix.
#v433 rerwin: Add message for previously selected modem not present.
#100902 new pupdial_wizard_helper script.
#101002 rerwin: Wait for initialization scripts to complete. 101122 breakout.
#101206 pupdial_wizard_helper now called from pupdial.
#111022 added hostname-set button.
#111028 put hostname entry box directly in this wizard. internationalised.
#111106 do not use $HOSTNAME, as hostname-set may have just been run.
#120201 one gettext translation added.
#120823 rerwin: add frisbee.
#120823 rerwin: get correct pid in wait loop.
#121112 rerwin: change indicator of frisbee.
#130111 rerwin: change name of frisbee invocation for integrated version, add disable when appropriate.
#131214 zigbert: gui (gtkdialog) improvements.
#160120 rerwin: add use of new pgprs and frisbee interfaces.
#170308 rerwin: use only new pgprs (2.0+) and frisbee (1.4.x) interfaces; remove gkdial check.
#170309 rerwin: retain choice in case multiple setups tried; disconnect any unchosen network setup.
#170418 rerwin: leave frisbee state unchanged after choosing new defaultconnect.
#170510 rerwin: replace 170309 implemention with connectwizard_exec; kill other connectwizard dialogs, to avoid conflict; exit if dialog killed; support optional pupdial.
#180919 add peasywifi support.
#200817 remove change current exec to pgprs because pgprs 3.0 does it before a connect.
#200910 position windows at center of screen; resolve shellcheck warnings.
#210122 restore determination of default tool, by testing for connman-gtk separately; limit wget max wait.
#230918 EasyOS compatability - bypass connectwizard for NetworkManager.
#230926 Removed "BarryK" from text message.
#231212 Add connman support.
#240309 Use gtk+ 2 version of gtkdialog, to work around gtk+ 3 sizing and theming issues.
#240604 Reorder Desktop/Tray "Connect action" list & clarify text in CHOOSEDEFAULT.

#For NetworkManager, bypass connectwizard altogether. #230918...
if [ -x /etc/init.d/rc.networkmanager ];then
    exec nm-setup
fi

export TEXTDOMAIN=connectwizard
export OUTPUT_CHARSET=UTF-8

firewall_func() {
    if which firewall_ng >/dev/null 2>&1;then
        firewall_ng &
    else
        rxvt -e /usr/sbin/firewallinstallshell &
    fi
}
export -f firewall_func

GTKDIALOG='gtkdialog' #240309...
which gtk2dialog >/dev/null \
  && GTKDIALOG='gtk2dialog' && GTKDIALOG_BUILD=GTK2 #force gtk2 theming

WIZARDPIDS="$(pgrep -f 'gtk2?dialog -p (Network_Connection_Wizard|InternetConnectionWizard)' | tr '\n' ' ')" #170510 (Avoid -d & -a options not in busybox pgrep.)
# shellcheck disable=SC2086 #Ensure individual PIDs to kill.
[ -n "$WIZARDPIDS" ] && kill $WIZARDPIDS #170510

# test conectivity
if wget -q --spider --timeout=5 http://distro.ibiblio.org ;then # returns other than 0 we're not connected #210122
    ONLINE='true'
    FIREWALL_LABEL=$(gettext "Setup a firewall")
else
    ONLINE='false'
    FIREWALL_LABEL=$(gettext "Firewall can be setup after connection")
fi

#v411 commandline param, this section irrelevant...
DEFAULTCONNECT="$(tail -n 1 /usr/local/bin/defaultconnect | tr -s " " | cut -f 2 -d " ")"
#radiobuttons...
DEFGNOMEPPP="no"
DEFPUPDIAL="no"
DEFRPPPPOE="no"
DEFMTGPRS="no"
DEFICW="no"
DEFPPPOECONF="no" #v409
DEFFRISBEE="no" #120823
DEFNETWIZARD="no" #100227
DEFSNS="no" #100310
DEFPWF="no" #180919
DEFCONNMAN="no" #231212

case $DEFAULTCONNECT in
    gnome-ppp)      DEFGNOMEPPP="yes"  ;;
    pupdial)        DEFPUPDIAL="yes"   ;;
    pppoe_gui)      DEFRPPPPOE="yes"   ;;
    pppoeconf)      DEFPPPOECONF="yes" ;;
    frisbee)        DEFFRISBEE="yes"   ;;
    net-setup.sh)   DEFNETWIZARD="yes" ;;
    sns)            DEFSNS="yes"       ;;
    pgprs)          DEFMTGPRS="yes"    ;; #170308
    peasywifi)      DEFPWF="yes"       ;; #180919
    connman-gtk)    DEFCONNMAN="yes"   ;; #231212
    *)              DEFICW="yes"       ;;
esac

#101002 rerwin: Wait for initialization scripts to complete... 101122 breakout.
BRKCNT=0 ; BRKPID=0
while ps -C rc.services >/dev/null;do
    if [ $BRKCNT -eq 1 ];then
        /usr/lib/gtkdialog/box_splash -placement center -close never -fontsize large -text "Please wait..." &
        BRKPID=$! #120823
    fi
    sleep 1
    [ $((++BRKCNT)) -gt 10 ] && break
done
[ $BRKPID -ne 0 ] && kill $BRKPID

#v405
MSGNETIFS="$(gettext 'No network interfaces detected')"
M_if1=$(gettext 'Available network interfaces:')
SHOWNETIFS="$(find /sys/class/net | cut -f 5 -d / | grep -vwE '^lo|^wmaster' | tr '\n' ' ')"
if [ "$(echo -n "$SHOWNETIFS" | wc -w)" -gt 0 ];then
    MSGNETIFS="${M_if1} $SHOWNETIFS"
fi

#v433...
MSGMODEM="$(gettext 'No dialup modem detected')"
if [ -h /dev/modem ];then
    DEVMODEM="$(readlink /dev/modem)"
    if [ -e /dev/modem ];then
        MSGMODEM="$(gettext 'Dialup modem detected at port') $DEVMODEM"
    else
        MSGMODEM="$(gettext 'Dialup modem at port') $DEVMODEM $(gettext 'not found')"
    fi
fi

FLAGGNOMEPPP=""
if [ ! "$(which gnome-ppp 2>/dev/null)" = "" ];then
    FLAGGNOMEPPP="<radiobutton><label>$(gettext 'GnomePPP (modem dialup)')</label><variable>RADIOGNOMEPPP</variable><default>$DEFGNOMEPPP</default></radiobutton>"
fi

FLAGPUPDIAL="" #170510...
CONNECTPUPDIAL=""

if [ "$(which gnome-ppp 2>/dev/null)" != "" ];then
    CONNECTPUPDIAL='
 <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
     '"$(/usr/lib/gtkdialog/xml_button-icon modem.svg big)"'
     <action>connectwizard_exec gnome-ppp</action>
     <action>gnome-ppp &</action>
     <action type="exit">true_exit</action>
   </button>
   <text space-expand="false" space-fill="false"><label>'$(gettext "Dialup analog or wireless modem (2g/3g)")'</label></text>
   <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>'
elif [ "$(which pupdial 2>/dev/null)" != "" ];then
    FLAGPUPDIAL="<radiobutton><label>$(gettext "PupDial (modem dialup)")</label><variable>RADIOPUPDIAL</variable><default>$DEFPUPDIAL</default></radiobutton>"
    CONNECTPUPDIAL='
 <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
     '"$(/usr/lib/gtkdialog/xml_button-icon modem.svg big)"'
     <action>connectwizard_exec pupdial</action>
     <action>/usr/sbin/pupdial & </action>
     <action type="exit">true_exit</action>
   </button>
   <text space-expand="false" space-fill="false"><label>'$(gettext "Dialup analog or wireless modem (2g/3g)")'</label></text>
   <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>'
fi #170510 end

FLAGROARINGPENGUIN=""
CONNECTROARINGPENGUIN="" #v409
if [ "$(which pppoe_gui 2>/dev/null)" != "" ];then
    FLAGROARINGPENGUIN="<radiobutton><label>$(gettext 'Roaring Penguin (PPPoE)')</label><variable>RADIORPPPPOE</variable><default>$DEFRPPPPOE</default></radiobutton>"
    CONNECTROARINGPENGUIN='
 <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
     '"$(/usr/lib/gtkdialog/xml_button-icon internet_connect.svg big)"'
     <action>connectwizard_exec pppoe_gui</action>
     <action>/usr/sbin/pppoe_gui &</action>
     <action type="exit">exit</action>
   </button>
   <text space-expand="false" space-fill="false"><label>'$(gettext "'Roaring Penguin' PPPOE")'</label></text>
   <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>' #v409
fi

#v411...
FLAGPPPOECONF=""
CONNECTPPPOECONF=""
if [ "$(which pppoeconf 2>/dev/null)" != "" ];then
    FLAGPPPOECONF="<radiobutton><label>$(gettext 'Pppoeconf (PPPOE)')</label><variable>RADIOPPPOECONF</variable><default>$DEFPPPOECONF</default></radiobutton>"
    CONNECTPPPOECONF='
 <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
     '"$(/usr/lib/gtkdialog/xml_button-icon internet_connect.svg big)"'
     <action>/usr/sbin/pppoeconf &</action>
     <action type="exit">exit</action>
   </button>
   <text space-expand="false" space-fill="false"><label>'$(gettext "'Pppoeconf' PPPOE")'</label></text>
   <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>'
fi

FLAGMTGPRS=""
if which pgprs >/dev/null 2>&1;then #160120
    CONNECTMTGPRS='
 <hbox space-expand="true" space-fill="true">
   <button space-expand="false" space-fill="false">
     '"$(/usr/lib/gtkdialog/xml_button-icon wireless.svg big)"'
     <action>/usr/sbin/pgprs --setup &</action>
     <action type="exit">exit</action>
   </button>
   <text space-expand="false" space-fill="false"><label>'$(gettext "Wireless GPRS modem")'</label></text>
   <text space-expand="true" space-fill="true"><label>""</label></text>
 </hbox>' #160120 170308
    FLAGMTGPRS="<radiobutton><label>$(gettext "GPRS Connect")</label><variable>RADIOMTGPRS</variable><default>$DEFMTGPRS</default></radiobutton>"
fi

if which frisbee >/dev/null 2>&1;then #100227 121112 130111 Jemimah's wireless connection gui. 160120
    FLAGFRISBEE="<radiobutton><label>$(gettext "Frisbee (wireless networking)")</label><variable>RADIOFRISBEE</variable><default>$DEFFRISBEE</default></radiobutton>"
fi

if which net-setup.sh >/dev/null 2>&1;then #100227 wizard developed by Dougal.
    FLAGNETWIZARD="<radiobutton><label>$(gettext "Network Wizard")</label><variable>RADIONETWIZARD</variable><default>$DEFNETWIZARD</default></radiobutton>"
fi

if which sns >/dev/null 2>&1;then #100310
    FLAGSNS="<radiobutton><label>$(gettext "Simple Network Setup")</label><variable>RADIOSNS</variable><default>$DEFSNS</default></radiobutton>"
fi

if which peasywifi >/dev/null 2>&1;then #180919
    FLAGPWF="<radiobutton><label>$(gettext "PeasyWiFi Connection Manager")</label><variable>RADIOPWF</variable><default>$DEFPWF</default></radiobutton>"
fi

if which connman-gtk >/dev/null 2>&1;then #231212
    FLAGCONNMAN="<radiobutton><label>$(gettext "ConnMan Connection Manager")</label><variable>RADIOCONNMAN</variable><default>$DEFCONNMAN</default></radiobutton>"
fi


if [ "$(pidof NetworkManager)" == "" ]; then
    PROXYSETUP='<hbox space-expand="true" space-fill="true">
                <text space-expand="false" space-fill="false"><label>'$(gettext "Setup a proxy server")'</label></text>
                <button space-expand="false" space-fill="false">
                  '"$(/usr/lib/gtkdialog/xml_button-icon shield_internet.svg big)"'
                  <action>proxy-setup &</action>
                  <action type="exit">exit</action>
                </button>
              </hbox>'

    MAINSETUP='<hbox space-expand="true" space-fill="true">
                <button space-expand="false" space-fill="false">
                  '"$(/usr/lib/gtkdialog/xml_button-icon network_connect.svg big)"'
                  <action>/usr/sbin/connectwizard_2nd & </action>
                  <action type="exit">true_exit</action>
                </button>
                <text space-expand="false" space-fill="false"><label>'$(gettext "Wired or wireless LAN")'</label></text>
                <text space-expand="true" space-fill="true"><label>""</label></text>
              </hbox>'
          
else
    CONNECTMTGPRS=""
    CONNECTPPPOECONF=""
    CONNECTROARINGPENGUIN=""
    PROXYSETUP=""
    MAINSETUP=""
fi

#Display 'Desktop/Tray' tab if desktop 'connect' icon exists or network monitor is legacy. #240129...
! grep -qs '/usr/local/apps/Connect' ~/Choices/ROX-Filer/PuppyPin \
  && [ -L /etc/xdg/autostart/netmon.desktop ] \
  && [ "$(basename "$(readlink /etc/xdg/autostart/netmon.desktop)")" \
  != "netmon_wce.desktop" ] \
  && VISIBILITY='visible="false"' \
  || VISIBILITY='visible="true"'
#v411...
CHOOSEDEFAULT='
<vbox '$VISIBILITY' space-expand="true" space-fill="true" margin="8">
'"$(/usr/lib/gtkdialog/xml_info fixed internet_connect.svg 60 "$(gettext "What you choose here will start immediately next time you click either a 'connect' icon on the desktop or the 'Setup networking' option of the legacy network icon in the tray.")")"'
      <vbox space-expand="true" space-fill="true">
  <frame '$(gettext "Connect action")'>
      <vbox space-expand="true" space-fill="true">
        '$FLAGCONNMAN'
        '$FLAGSNS'
        '$FLAGNETWIZARD'
        '$FLAGFRISBEE'
        '$FLAGPWF'
        '$FLAGMTGPRS'
        '$FLAGPUPDIAL'
        '$FLAGGNOMEPPP'
        '$FLAGPPPOECONF'
        '$FLAGROARINGPENGUIN'
        <radiobutton><label>'$(gettext "Internet Connection Wizard")'</label><variable>RADIOICW</variable><default>'$DEFICW'</default></radiobutton>
      </vbox>
  </frame>
      </vbox>
</vbox>'
[ -n "$1" ] && CHOOSEDEFAULT=""

HOSTNAME="$(cat /etc/hostname)" #111106 do not use $HOSTNAME, as hostname-set may have just been run.

# shellcheck disable=SC2155
export InternetConnectionWizard='
<window title="'$(gettext "Internet Connection Wizard")'" icon-name="gtk-connect" resizable="false">
<vbox space-expand="true" space-fill="true">
  <notebook labels="'$(gettext 'Connection')'|'$(gettext 'Desktop / Tray')'">
    <vbox space-expand="true" space-fill="true" margin="8">
      '"$(/usr/lib/gtkdialog/xml_info fixed internet_connect.svg 60 "$MSGNETIFS" "$MSGMODEM" "<b>$(gettext "Help button for more info")</b>")"'
      <hbox space-expand="true" space-fill="true">
        <vbox space-expand="true" space-fill="true">
          <frame '$(gettext "Connect to Internet by")'>
            <vbox space-expand="false" space-fill="false">
              '${MAINSETUP}'
              '${CONNECTPUPDIAL}'
              '${CONNECTMTGPRS}'
              '${CONNECTPPPOECONF}'
              '${CONNECTROARINGPENGUIN}'
            </vbox>
          </frame>
        </vbox>
        <vbox space-expand="true" space-fill="true">
          <frame '$(gettext 'Tools')'>
            <vbox space-expand="false" space-fill="false">
              <hbox space-expand="true" space-fill="true">
                <text space-expand="false" space-fill="false"><label>'"$FIREWALL_LABEL"'</label></text>
                <button space-expand="false" space-fill="false" sensitive="'"$ONLINE"'">
                  '"$(/usr/lib/gtkdialog/xml_button-icon firewall.svg big)"'
                  <action>firewall_func</action>
                  <action type="exit">exit</action>
                </button>
              </hbox>
              '${PROXYSETUP}'
              <hbox space-expand="true" space-fill="true" tooltip-text="'$(gettext 'This gives detailed network interface information')'">
                <text space-expand="false" space-fill="false"><label>'$(gettext "Network information")'</label></text>
                <button space-expand="false" space-fill="false">
                  '"$(/usr/lib/gtkdialog/xml_button-icon info.svg big)"'
                  <action>ipinfo & </action>
                </button>
              </hbox>
              <hbox space-expand="true" space-fill="true" tooltip-text="'$(gettext 'Type your computer name to identify in the network. Alpha-numeric without spaces. Then click button to apply the change.')'">
                <text width-request="30" space-expand="true" space-fill="true"><label>""</label></text>
                <vbox>
                  <text space-expand="false" space-fill="false"><label>'$(gettext 'Change hostname')'</label></text>
                  <entry width-chars="15">
                    <input>echo -n "'$HOSTNAME'"</input>
                    <variable>ENTRY_HOSTNAME</variable>
                  </entry>
                </vbox>
                <button space-expand="false" space-fill="false">
                  '"$(/usr/lib/gtkdialog/xml_button-icon apply.svg big)"'
                  <action>/usr/sbin/hostname-set $ENTRY_HOSTNAME</action>
                </button>
              </hbox>
           </vbox>
        </frame>
      </vbox>
    </hbox>
  </vbox>
  '${CHOOSEDEFAULT}'
  </notebook>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Help")'</label>
      '"$(/usr/lib/gtkdialog/xml_button-icon help)"'
      <action>/usr/local/apps/Connect/connect_help & </action>
    </button>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"$(/usr/lib/gtkdialog/xml_button-icon ok)"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'

# shellcheck disable=SC1091
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETSTR="$($GTKDIALOG -p InternetConnectionWizard --center --styles=/tmp/gtkrc_xml_info.css)" 

[ $? -gt 2 ] && exit #170510
[ -n "$1" ] && exit #v411
[ "$(echo "$RETSTR" | grep 'true_exit')" != "" ] && exit #100412 after return from sns. 100902 and pupdial.

RADIOBUT="$(echo "$RETSTR" | grep '^RADIO' | grep '"true"' | cut -f 1 -d '=')"
[ "$RADIOBUT" = "RADIOGNOMEPPP" ] && echo -e '#!/bin/sh\nexec gnomepppshell' > /usr/local/bin/defaultconnect
[ "$RADIOBUT" = "RADIOPUPDIAL" ] && echo -e '#!/bin/sh\nexec pupdial' > /usr/local/bin/defaultconnect
[ "$RADIOBUT" = "RADIORPPPPOE" ] && echo -e '#!/bin/sh\nexec pppoe_gui' > /usr/local/bin/defaultconnect
[ "$RADIOBUT" = "RADIOMTGPRS" ] && ( echo -e '#!/bin/sh\nexec pgprs --connect' > /usr/local/bin/defaultconnect ) #160120 170308
[ "$RADIOBUT" = "RADIOICW" ] && echo -e '#!/bin/sh\nexec connectwizard' > /usr/local/bin/defaultconnect
[ "$RADIOBUT" = "RADIOPPPOECONF" ] && echo -e '#!/bin/sh\nexec pppoeconf' > /usr/local/bin/defaultconnect #v409
[ "$RADIOBUT" = "RADIOFRISBEE" ] && echo -e '#!/bin/sh\nexec frisbee' > /usr/local/bin/defaultconnect #120823 130111
[ "$RADIOBUT" = "RADIONETWIZARD" ] && echo -e '#!/bin/sh\nexec net-setup.sh' > /usr/local/bin/defaultconnect #100227
[ "$RADIOBUT" = "RADIOSNS" ] && echo -e '#!/bin/sh\nexec sns' > /usr/local/bin/defaultconnect #100227
[ "$RADIOBUT" = "RADIOPWF" ] && echo -e '#!/bin/sh\nexec peasywifi' > /usr/local/bin/defaultconnect #180919
[ "$RADIOBUT" = "RADIOCONNMAN" ] && echo -e '#!/bin/sh\nexec connman-gtk' > /usr/local/bin/defaultconnect #231212

###end###
