#!/bin/sh
#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
#180127 rerwin: use interfaces for frisbee 1.4, if installed
#180207 rerwin: make frisbee check first
#180809 BK: if active eth was autodetected at bootup, selecting disconnect here did not work.
#181119 BK: NetworkManager.

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

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`" != "" ] && CURRENTEXEC='nmtui' #181119 networkmanager

#16aug10 shinobar: netchoice... same code in rc.init
if [ "$CURREXEC" = "connectwizard" ];then #BK
  NETCHOICE=""
  #try determine which tool was used to setup networking...
  if grep -qs 'frisbee_mode=1' /etc/frisbee/frisbee.conf; then #180207
   NETCHOICE='frisbee' #180207
  elif [ -s /etc/simple_network_setup/connections ];then #100306 180207
   NETCHOICE='sns'
  else
   CHECKOLDWIZ="`ls -1 /etc/*[0-9]mode 2>/dev/null`" #ex: eth0mode, wlan0mode.
   if [ "$CHECKOLDWIZ" != "" -a -d /usr/local/net_setup ];then
    NETCHOICE='net_wiz_classic'
   else
    CHECKNEWWIZ="`ls -1 /etc/network-wizard/network/interfaces 2>/dev/null`"
    if [ "$CHECKNEWWIZ" != "" ];then
     NETCHOICE='net-setup.sh'
    else
     [ -f /usr/local/Pwireless2/interface ] && NETCHOICE='Pwireless2' #100304
    fi
   fi
  fi
  if [ -x /etc/init.d/rc.networkmanager ];then #181119
   NETCHOICE="nmtui"
  fi
  [ "$NETCHOICE" != "" ] && CURREXEC="$NETCHOICE"
fi
#16aug10 shinobar: end

RUNMODE="$1"

case $RUNMODE in
  --wizard) exec /usr/sbin/connectwizard 1>&2 ;;
  --connect)
   case $CURREXEC in #connect using default tool.
    net-setup.sh)  exec /etc/rc.d/rc.network connect 1>&2 ;; #Dougal.
    pgprs-connect) pppd call gprs-disconnect-chatmm; killall pppd; pgprs-connect ;; #guessing, i need help here.
    pupdial) pupdial ;;
    pppoe_gui) pppoe_gui ;;
    connectwizard) #180809
     if [ -s /tmp/apps-connect-was-eth-ifup ];then
      /etc/rc.d/rc.network_eth
     else
      connectwizard
     fi
    ;;
    Pwireless2) Pwireless2 ;;
    net_wiz_classic) net_wiz_classic ;;
    sns) /usr/local/simple_network_setup/rc.network ;;
    frisbee) #130104 #180127...
     if [ -f /usr/local/bin/frisbee ];then
      if [ -x /usr/local/frisbee/frisbee-main ];then
       frisbee --connect
      else
       /usr/local/frisbee/connect #130104
      fi
     fi
    ;; #180127
    nmtui) #181119
     chmod 755 /etc/init.d/rc.networkmanagr
     if ! pidof NetworkManager >/dev/null ;then
      /etc/init.d/rc.networkmanagr start
     else
      /etc/init.d/rc.networkmanagr restart
     fi
     urxvt -e nmtui
    ;;
   esac
  ;;
  --disconnect) #disconnect using default tool.
   case $CURREXEC in
    net-setup.sh)  exec /etc/rc.d/rc.network stop 1>&2 ;; #Dougal.
    pgprs-connect) pppd call gprs-disconnect-chatmm; killall pppd ;; #guessing, i need help here.
    pupdial) killall wvdial; killall pppd ;;
    pppoe_gui) pppoe_gui ;;
    connectwizard) #180809
     IFup="$(ifconfig | grep '^eth[0-9].*encap:Ethernet' | cut -f 1 -d ' ')"
     echo -n "$IFup" > /tmp/apps-connect-was-eth-ifup
     if [ "$IFup" ];then
      /usr/sbin/networkdisconnect
     else
      connectwizard
     fi
    ;;
    Pwireless2) Pwireless2 ;;
    net_wiz_classic) net_wiz_classic ;;
    sns) /usr/local/simple_network_setup/rc.network stop ;;
    frisbee) #130104 180127...
     if [ -f /usr/local/bin/frisbee ];then
      if [ -x /usr/local/frisbee/frisbee-main ];then
       frisbee --disconnect
      else
       /usr/local/frisbee/disconnect #130104
      fi
     fi
    ;; #180127
    nmtui) #181119
     if pidof NetworkManager >/dev/null ;then
      /etc/init.d/rc.networkmanagr stop
     fi
    ;;
    *) #181119
     /usr/sbin/networkdisconnect
    ;;
   esac
  ;;
  *) exec /usr/local/bin/defaultconnect 1>&2  ;;
esac

###end###
