#!/bin/bash
#(c) Copyright 2010 Barry Kauler bkhome.org
#Lesser GPL licence v2 (/usr/share/doc/legal/lgpl-2.1.txt)
#called from 'connectwizard'
#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.
#131216 zigbert: gui (gtkdialog) improvements.
#160120 rerwin: add use of new frisbee interface; remove irrelevant dialup code.
#170308 rerwin: use only new frisbee (1.4+) & pgprs (2.0+) interfaces; remove gkdial check; remove unused 'CURRCHOICE code.
#170309 rerwin: retain choice in case multiple setups tried; disconnect any unchosen network setup.
#170418 rerwin: leave defaultconnect unchanged when trying another tool.
#170510 rerwin: exit if executable/dialog killed.
#180624 add check for predictable network interface device names (e.g. enp0s25).
#180919 add peasywifi support.
#180923 replace specific exec paths with 'which'; move network wizard.
#200206 replace deprecated 'ifconfig' with busybox 'ip'; replace 180624.
#200910 position windows at center of screen; resolve shellcheck warnings.
#230926 Removed "Puppy" and "BarryK" from text messages.
#231212 Add connman support.
#231219 Enable messages from legacy network managers, to allow debug tracing.
#240309 Use gtk+ 2 version of gtkdialog, to work around gtk+ 3 sizing issue.
#240604 Move ConnMan to top of manager list; change SNS bolded statement.
#240607 Update feedback messages and button to acommodate onnman and peasywifi.

export TEXTDOMAIN=connectwizard_2nd
export OUTPUT_CHARSET=UTF-8

function cn_2nd() { #EXIT ICON.SVG TITLE MSG
    #BORDER='border-width="7"'
    echo '
    <hbox '"${BORDER}"' space-expand="true" space-fill="true">
      <text space-expand="false" space-fill="false"><label>""</label></text>
      <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'"${3}"'"</label></text>
      <vbox space-expand="false" space-fill="false">
        <text space-expand="false" space-fill="false"><label>""</label></text>
        <button>
          '"$(/usr/lib/gtkdialog/xml_button-icon "${2}" huge)"'
          <action type="exit">'"${1}"'</action>
        </button>
      </vbox>
    </hbox>
    <hseparator></hseparator>'
}

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

if which sns >/dev/null 2>&1;then
    SNS="$(cn_2nd FLAGSNS network.svg "$(gettext '<b><span color='"'blue'"'>Simple Network Setup</span></b>.
This is a simple network setup tool. Not as many features as the Network Wizard, for example requires the network to have a DHCP server (the vast majority of cases). <b>Recommended for most people.</b>')")"
    which connman-gtk >/dev/null 2>&1 \
      && SNS="$(cn_2nd FLAGSNS network.svg "$(gettext '<b><span color='"'blue'"'>Simple Network Setup</span></b>.
This is a simple network setup tool. Not as many features as the Network Wizard, for example requires the network to have a DHCP server (the vast majority of cases). <b>User-friendly legacy network tool.</b>')")"
fi

if which frisbee >/dev/null 2>&1;then #Jemimah's wireless connection gui. 121122 130103 160120
    FRISBEE="$(cn_2nd FLAGFRISBEE internet_connect_yes.svg "$(gettext '<b><span color='"'blue'"'>Frisbee</span></b>.
This is a tool developed by Jemimah. Mostly for wireless connectivity for laptops, but also can handle ethernet connection. <b>Good for wireless roaming.</b>')")"
fi

if which net-setup.sh >/dev/null 2>&1;then #wizard developed by Dougal.
    NETWIZARD="$(cn_2nd FLAGNETWIZARD network_connect.svg "$(gettext '<b><span color='"'blue'"'>Network Wizard</span></b>.
This is a sophisticated tool for network setup. It is for both wired (ethernet) and wireless connectivity, <b>with more configuration features</b> than SNS.')")"
fi

if which peasywifi >/dev/null 2>&1;then #rcrsn51's wifi connection manager  180919...
    PEASYWIFI="$(cn_2nd FLAGPEASYWIFI wireless.svg "$(gettext '<b><span color='"'blue'"'>PeasyWiFi</span></b>.
This is a tool developed by rcrsn51. Mostly for wireless connectivity, but also can handle ethernet connections. <b>Good for precise control and multiple networks.</b>')")"
fi

if which bbwireless.sh >/dev/null 2>&1;then #CLI wireless connection manager
    BBWIRELESS="$(cn_2nd FLAGBBW terminal.svg "$(gettext '<b><span color='"'blue'"'>BBWireless</span></b>.
This is a tool developed for the command line interface for wireless connectivity. A virtual terminal will open when you click the button and run <b>bbwireless.sh</b> in that window. NOTE: experimental')")"
fi

if which connman-gtk >/dev/null 2>&1;then #connman connection manager  #231212...
    CONNMAN="$(cn_2nd FLAGCONNMAN wireless.svg "$(gettext '<b><span color='"'blue'"'>ConnMan</span></b>.
This is a simple and fast tool that provides IPv4 and IPv6 connectvity via Ethernet, WiFi, WiFi Direct, Bluetooth, mobile/cellular (needs ofono), VPN (needs openvpn) and USB. <b>Recommended.</b>')")"
fi

# shellcheck disable=SC2155
export Network_Connection_Wizard='
<window title="'$(gettext 'Network Connection Wizard')'" icon-name="gtk-connect" resizable="false">
<vbox space-expand="true" space-fill="true">
  '"$(/usr/lib/gtkdialog/xml_info fixed network_connect.svg 48 "$(gettext 'These are the choices of GUI tool that you can use to connect to a network (and Internet)...')")"' 
  <vbox space-expand="true" space-fill="true">
   '${CONNMAN}'
   '${SNS}'
   '${NETWIZARD}'
   '${FRISBEE}'
   '${PEASYWIFI}'
   '${BBWIRELESS}'
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"$(/usr/lib/gtkdialog/xml_button-icon cancel)"'
      <action>exit:Cancel</action>
    </button>
  </hbox>
</vbox>
</window>' #180919

# shellcheck disable=SC1091
. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
RETSTRING="$($GTKDIALOG -p Network_Connection_Wizard --center --styles=/tmp/gtkrc_xml_info.css)" 
[ $? -gt 2 ] && exit #170510 dialog killed

if [ "$(echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG')" != "" ];then
    CHOSENWIZ="$(echo "$RETSTRING" | grep '^EXIT' | grep 'FLAG' | cut -f 2 -d \")"
    case $CHOSENWIZ in
        FLAGFRISBEE) #121029
            NWEXEC='frisbee' #130103
            NWCHOICE="Jemimah's Frisbee"
            ;;
        FLAGNETWIZARD) #net-setup.sh
            NWEXEC='net-setup.sh'
            NWCHOICE="Network Wizard"
            ;;
        FLAGSNS)
            NWEXEC='sns'
            NWCHOICE="Simple Network Setup"
            ;;
        FLAGPEASYWIFI) #180919...
            connectwizard_wrapper #for immediate use after install
            NWEXEC='peasywifi'
            NWCHOICE="PeasyWiFi"
            ;;
        FLAGBBW)
            NWEXEC='bbwireless.wrap.sh'
            NWCHOICE="CLI base Wireless Connect"
            ;;
        FLAGCONNMAN) #231212...
            connectwizard_wrapper #for immediate use after install
            NWEXEC='connman-gtk'
            NWCHOICE="Connection Manager"
            ;;
    esac

    case "$NWEXEC" in #231219...
#      sns|net-setup.sh|frisbee|connman-gtk|peasywifi) $NWEXEC ;; #DEBUG
      sns|net-setup.sh|frisbee) $NWEXEC ;;
      *) $NWEXEC > /dev/null 2>&1 ;;
    esac
    [ $? -gt 2 ] && exit #170510 exec killed

    #Display 'Set as default' button and appropriate instruction if desktop 'connect' icon exists or network monitor is legacy, if default is currently different.
    . /root/.connectwizardrc #sets CURRENT_EXEC #240607...
    case "$CURRENT_EXEC" in
        connman-gtk)  NETMGRNAME="$(gettext 'Connection Manager')" ;;
        sns)          NETMGRNAME="$(gettext 'Simple Network Setup')" ;;
        net-setup.sh) NETMGRNAME="$(gettext 'Network Wizard')" ;;
        frisbee)      NETMGRNAME="$(gettext 'Frisbee')" ;;
        peasywifi)    NETMGRNAME="$(gettext 'PeasyWiFi')" ;;
        *)            NETMGRNAME="$(gettext '(unknown)')" ;;
    esac
    IFSUP="$(ip link show | grep -B 1 'link/ether' | grep -w 'UP' | cut -f 2 -d ' '  | tr -d :)" #200206
    if [ "$IFSUP" != "" ];then
        IFSUP="$(echo "$IFSUP" | cut -f 1 -d ' ' | tr '\n' ' ')"
        MSG1="$(gettext 'These interfaces are active:')
<b>${IFSUP}</b>"
    else
        MSG1="$(gettext 'No interfaces are active.')"
    fi
    MSG1="$MSG1
$(gettext 'This network tool will be used for future boots:')
<b>${NETMGRNAME}</b>"
    if ! 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" ];then
        VISIBILITY='visible="false"'
    else
        if grep -qw "$CURRENT_EXEC" /usr/local/bin/defaultconnect;then
            VISIBILITY='visible="false"'
        else
            VISIBILITY='visible="true"'
            if [ -L /etc/xdg/autostart/netmon.desktop ] \
              && [ "$(basename "$(readlink /etc/xdg/autostart/netmon.desktop)")" \
              != "netmon_wce.desktop" ];then
                MSG1="$MSG1

$(gettext "To start this tool from the connect desktop icon, click the <u>Set as default</u> button.")"
            elif ! grep -qs '/usr/local/apps/Connect' ~/Choices/ROX-Filer/PuppyPin;then
                MSG1="$MSG1

$(gettext "To start this tool from the 'Setup networking' option of the tray network monitor icon, click the <u>Set as default</u> button.")"
            else
                MSG1="$MSG1

$(gettext "To start this tool from the connect desktop icon or the 'Setup networking' option of the tray network monitor icon, click the <u>Set as default</u> button.")"
            fi
        fi
    fi #240607 end

# shellcheck disable=SC2155
    export Network_Connection_Wizard='
<window title="'$(gettext 'Network Connection Wizard')'" icon-name="gtk-connect" resizable="false">
<vbox space-expand="true" space-fill="true">
  '"$(/usr/lib/gtkdialog/xml_info fixed network_connect.svg 60 "$(gettext 'You have finished running')" "<b>${NWCHOICE}</b>")"' 
  <vbox space-expand="true" space-fill="true">
    <frame>
      '"$(/usr/lib/gtkdialog/xml_pixmap dialog-complete.svg popup)"' 
    <text use-markup="true"><label>"'${MSG1}'

<u>'$(gettext "Try a different tool")'</u> '$(gettext "if you would like to try one of the other network setup tools.")'"</label></text>
   </frame>
   <hbox space-expand="false" space-fill="false">
     <button space-expand="false" space-fill="false">
       '"$(/usr/lib/gtkdialog/xml_button-icon refresh)"'
       <label>'$(gettext 'Try a different tool')'</label>
       <action type="exit">FLAGTRYDIFF</action>
     </button>
     <text space-expand="true" space-fill="true"><label>""</label></text>
     <button '$VISIBILITY'>
       '"$(/usr/lib/gtkdialog/xml_button-icon apply)"'
       <label>'$(gettext 'Set as default')'</label>
       <action type="exit">FLAGYES</action>
     </button>
     <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>
</vbox>
</window>'

    RETSTRING="$($GTKDIALOG -p Network_Connection_Wizard --center --styles=/tmp/gtkrc_xml_info.css)" 
    [ $? -gt 2 ] && exit #170510 dialog killed

    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
                echo "<b>${NWCHOICE} $(gettext "now set as default network tool.</b>
Here are some technical notes that you might find useful:
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'.
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.
<b>Geek stuff</b>: 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.")" > /tmp/box_help

                /usr/lib/gtkdialog/box_help "$(gettext 'Network Connection Wizard')" info.svg &
                ;;
            FLAGTRYDIFF)
                exec connectwizard
                ;;
        esac
    fi
 
fi

###END###
