#!/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

[ "`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.

#16aug10 shinobar: netchoice... same code in rc.init
if [ "$CURREXEC" = "connectwizard" ];then #BK
  NETCHOICE=""
  #try determine which tool was used to setup networking...
  if [ -s /etc/simple_network_setup/connections ];then #100306
   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
     CHECKFRISBEE="`ls -1 /etc/frisbee/interfaces 2>/dev/null`" #130104...
     if [ "$CHECKFRISBEE" != "" ];then
      NETCHOICE='frisbee'
     else #130104 end
      [ -f /usr/local/Pwireless2/interface ] && NETCHOICE='Pwireless2' #100304
     fi
    fi
   fi
  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) connectwizard ;;
    Pwireless2) Pwireless2 ;;
    net_wiz_classic) net_wiz_classic ;;
    sns) /usr/local/simple_network_setup/rc.network ;;
    frisbee) [ -f /usr/local/bin/frisbee ] && /usr/local/frisbee/connect ;; #130104
   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) connectwizard ;;
    Pwireless2) Pwireless2 ;;
    net_wiz_classic) net_wiz_classic ;;
    sns) /usr/local/simple_network_setup/rc.network stop ;;
    frisbee) [ -f /usr/local/bin/frisbee ] && /usr/local/frisbee/disconnect ;; #130104
   esac
   ;;
  *) exec /usr/local/bin/defaultconnect 1>&2  ;;
esac

###END###
