#!/bin/sh
#Connect/AppRun - Invoked by netmon_wce for legacy network managers and connectwizard_switcher for connman.
#100228 BK support default network tool.
#100325 BK support Simple Network Setup.
#16aug10 shinobar: netchoice. BK: but only if defaultconnect still set to 'exec connectwizard'.
#110505 support sudo for non-root user.
#130104 rerwin: add frisbee
#130117 rerwin: remove redundant CURREXEC test, per shinobar
#160120 rerwin: add use of new pgprs and frisbee interfaces; correct frisbee check; ignore frisbee if not active.
#170308 rerwin: update filename in frisbee-active test; use only new frisbee (1.4+) and pgprs (2.0+) interfaces; remove gkdial check.
#170309 rerwin: retain choice in case multiple setups tried; end use of non-default exec when starting default exec.
#170515 rerwin: add checks for netwiz & sns installed.
#170724 rerwin: accommodate absence of a current exec, for default eth0, to connect/disconnect without using network wizard component.
#180919 add peasywifi support.
#180923 remove ethernet tool-specific exec paths; use package rc.network.
#200910 resolve shellcheck warnings.
#210415 Remove connectwizard cases to allow connect/disconnect of ethernet default.
#230919 BK: /usr/local/simple_network_setup/rc.network renamed to rc.snsnetwork
#230928 BK: /usr/local/network-wizard/rc.network renamed to rc.wiznetwork.
#231212 Add connman support.
#240311 Update peasywifi support for version 4.8.1+ only.
#240523 Determine tool only for connect.

[ "$(whoami)" != "root" ] && exec sudo -A "${0}" "${@}" #110505 #200910 

CURRENT_EXEC='' #170309... 170724
if [ -f /root/.connectwizardrc ];then
# shellcheck disable=SC1091
    . /root/.connectwizardrc #sets CURRENT_EXEC
fi

RUNMODE="$1" #240523
NETCHOICE="" #240523
if [ "$RUNMODE" = '--connect' ] \
  && [ "$CURRENT_EXEC" = "connectwizard" ];then #BK, 170309 end #240523
    #try determine which tool was used to setup networking...
    if which connman-gtk >/dev/null 2>&1 \
      && [ -x /etc/init.d/connman ];then #231212
        NETCHOICE='connman-gtk' #231212
    elif which frisbee >/dev/null 2>&1 && frisbee --test_active; then #130104 160120 170308
        NETCHOICE='frisbee' #130104 160120
    elif which sns >/dev/null 2>&1 \
      && [ -s /etc/simple_network_setup/connections ];then #100306 160120 170515
        NETCHOICE='sns'
    elif which net-setup.sh >/dev/null 2>&1 \
      && [ "$(ls -1 /etc/network-wizard/network/interfaces 2>/dev/null)" ];then #170515
        NETCHOICE='net-setup.sh'
    elif which peasywifi >/dev/null 2>&1 \
      && { [ -n "$(ls -1 /etc/pwf/wifi 2>/dev/null)" ] \
          || [ -n "$(ls -1 /etc/pwf/ethernet 2>/dev/null)" ]; };then #180919 #200910
        NETCHOICE='peasywifi' #180919
    elif which bbwireless.sh >/dev/null 2>&1 \
      && [ -e /etc/init.d/01bbwireless.sh ] ;then #191109
        NETCHOICE='bbwireless.sh'
    fi
    [ -n "$NETCHOICE" ] && CURRENT_EXEC="$NETCHOICE" #170309
fi
#16aug10 shinobar: end

case $RUNMODE in
    --wizard) exec /usr/sbin/connectwizard 1>&2 ;;
    --connect)
        case "$CURRENT_EXEC" in #connect using default tool. 170309
            peasywifi) /etc/init.d/rc.network-start start ;; #180919 #240311
            net-setup.sh) exec /usr/local/network-wizard/rc.wiznetwork connect 1>&2 ;; #Dougal. 180923 #230928
            pgprs) pgprs --connect >/dev/null ;; #160120 170308
            pupdial) pupdial ;;
            pppoe_gui) pppoe_gui ;;
            sns) /usr/local/simple_network_setup/rc.snsnetwork ;; #230919
            frisbee) frisbee --connect ;; #130104 160120 170308
            bbwireless.sh) /etc/init.d/01bbwireless.sh start >/dev/null 2>&1 ;; #191109
            connman-gtk) /etc/rc.d/rc.network_connman start ;; #231212
            *) exec /etc/rc.d/rc.network_eth ;; #170724
        esac
        ;;
    --disconnect) #disconnect using default tool.
        case "$CURRENT_EXEC" in #170309
            peasywifi) #180919 #240311...
                /etc/init.d/rc.network-start stop #peaswifi's partial stop
                /etc/init.d/rc.network-stop stop #connectwizard's completion
                ;;
            net-setup.sh) exec /usr/local/network-wizard/rc.wiznetwork stop 1>&2 ;; #Dougal. 180923 #230928
            pgprs) pgprs --disconnect >/dev/null ;;  #160120 170308
            pupdial) killall wvdial; killall pppd ;;
            pppoe_gui) pppoe_gui ;;
            sns) /usr/local/simple_network_setup/rc.snsnetwork stop ;; #230919
            frisbee) frisbee --disconnect ;; #130104 160120 170308
            bbwireless.sh) /etc/init.d/01bbwireless.sh stop >/dev/null 2>&1 ;; #191109
            connman-gtk) /etc/rc.d/rc.network_connman stop ;; #231212
            *) networkdisconnect --current-exec ;; #170724
        esac
        ;;
    *) exec /usr/local/bin/defaultconnect 1>&2 ;;
esac

###END###
