#!/bin/sh
#Barry Kauler, May 2012, license GPL3 (ref: /usr/share/doc/legal)
#this code is taken out of /etc/rc.d/rc.sysinit
#called from /usr/bin/xwin, if file /tmp/simple_network_setup/network_default_reconnect_required_flag exists.
#that 'flag' file is created in /usr/sbin/hostname-set, if the hostname was changed and the network connection brought down.
#170718 this script now called from /etc/rc.d/rc.sysinit
#180117 rerwin: clear resolv.conf at bootup.
#180127 rerwin: add frisbee
#181118 BK: NetworkManager
#190207 BK: support UltraSNS.
#190805 may have nm-connection-editor (in network-manager-applet pkg) instead of nmtui (in network-manager pkg).
#190922 BK: 190805: now "nm-setup" in defaultconnect.

#100227 choose default network tool...
DEFAULTCONNECT="`cat /usr/local/bin/defaultconnect | tail -n 1 | tr -s " " | cut -f 2 -d " "`"
[ "`grep 'gprs' /usr/local/bin/defaultconnect`" != "" ] && DEFAULTCONNECT='pgprs-connect'
[ "$DEFAULTCONNECT" = "gkdial" ] && DEFAULTCONNECT="pupdial" #for older pups.
[ "`grep 'nmtui' /usr/local/bin/defaultconnect`" != "" ] && DEFAULTCONNECT='nmtui' #181118 networkmanager
[ "`grep 'nm\-connection\-editor' /usr/local/bin/defaultconnect`" != "" ] && DEFAULTCONNECT='nm-setup' #190805 networkmanager. 190922
[ "`grep 'nm\-setup' /usr/local/bin/defaultconnect`" != "" ] && DEFAULTCONNECT='nm-setup' #190922

NETCHOICE="$DEFAULTCONNECT"	#101007 shinobar
case $DEFAULTCONNECT in
 nmtui) #181118
  NETCHOICE='nmtui'
 ;;
 nm-setup) #190805 190922
  NETCHOICE='nm-setup'
 ;;
 Pwireless2)
  NETCHOICE='Pwireless2'
 ;;
 net-setup.sh)
  NETCHOICE='net-setup.sh'
 ;;
 net_wiz_classic)
  NETCHOICE='net_wiz_classic'
 ;;
 sns)
  NETCHOICE='sns'
 ;;
 usns) #190207
  NETCHOICE='usns'
 ;;
 connectwizard|frisbee) #try determine which tool was used to setup networking... 101007 180127...
  if [ -x /usr/local/bin/frisbee ] \
    && grep -q 'frisbee_mode=1' /etc/frisbee/frisbee.conf; then
   NETCHOICE='frisbee'
  elif [ -s /etc/simple_network_setup/connections ];then #100306 180127 end
   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
 ;;
esac

if [ "$NETCHOICE" != "nmtui" -a "$NETCHOICE" != "nm-setup" ];then #181118 190805 190922
 if pidof NetworkManager >/dev/null ;then
  /etc/init.d/rc.networkmanager stop
 fi
 chmod 644 /etc/init.d/rc.networkmanager
fi

if [ ! -f /tmp/.initial_resolv.conf_cleared ];then #180117...
 echo -n '' > /etc/resolv.conf
 touch /tmp/.initial_resolv.conf_cleared
fi

[ -f /etc/init.d/Pwireless2 ] && chmod 644 /etc/init.d/Pwireless2 #prevent jemimah's script from running. 100304 100513
case $NETCHOICE in
 Pwireless2)
  #this only sets up interface 'lo'...
  /etc/rc.d/rc.network_basic
  #jemimah's script is in /etc/init.d/Pwireless2
  chmod 755 /etc/init.d/Pwireless2 #make executable so it will run.
  #i want to run it right now, as a separate process (rc.services will ignore it)...
  /etc/init.d/Pwireless2 start &
 ;;
 net-setup.sh)
  /etc/rc.d/rc.network &
 ;;
 net_wiz_classic)
  #note, old wizard is located in /usr/local/net_setup.
  /usr/local/net_setup/etc/rc.d/rc.network &
 ;;
 sns) #100306
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /usr/local/simple_network_setup/rc.network &
 ;;
 usns) #190207
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /usr/local/UltraSNS/rc.network start &
 ;;
 connectwizard) #101007 shinobar
  #161215 rewritten...
  /etc/rc.d/rc.network_basic #this only sets up interface 'lo'.
  /etc/rc.d/rc.network_eth &   #test for wired network.
 ;;
 nmtui|nm-setup) #181118 190805 190922
  /etc/rc.d/rc.network_basic 
  /etc/rc.d/rc.network_eth_nm &
  chmod 755 /etc/init.d/rc.networkmanager
  if ! pidof NetworkManager >/dev/null ;then
   /etc/init.d/rc.networkmanager start
  fi
 ;;
 *) #101007 shinobar
  /etc/rc.d/rc.network_basic &
 ;;
esac
###end###
