#!/bin/ash
# Returns '<if> connected' or '<if> no Internet' or 'inactive'
#100808 TazOC lhpup.org license: GPL version 3 or any later version
# Try to detect active network interface (<if> means network interface)
#110202 Report whether an Internet connection is available; use Frisbee DHCP_CONF; bring down <if> that are denied/ignored. Assume net is active if tested within 10s, fallback domain.
#111115 Use Google Public DNS
#120204 Refined test for active <if>, RX-OK is not zero
#120704 use exec sudo if non-root, test for ignorelist before bringing <if> down.

#wifi="`iwgetid -a | gawk '{print $1}'`"
#if [ -n "$wifi" ]; then # active wireless interface
# echo "$wifi"
export USER="`id -un`"
if [ "$USER" != "root" ]; then #110202 run as root
 if [ "`which sudo`" ]; then
  exec sudo "$0" "$@" #120704 use exec
 fi
 exit # End script
fi
export LANG=C
SUCCESS_FILE="/var/local/internetsuccess"
[ -s /usr/local/Frisbee/config ] && . /usr/local/Frisbee/config

WIFI_IF=""
[ -s /usr/local/Frisbee/interface ] && WIFI_IF=`cat /usr/local/Frisbee/interface`

[ -z "$DHCP_CONF" ] && DHCP_CONF="/usr/local/Frisbee/dhcpcd.conf"

[ ! -s "$SUCCESS_FILE" ] && echo $((`date +%s` - 10)) >"$SUCCESS_FILE"

#Find active network interfaces
IGNORELIST=""
[ -e $DHCP_CONF ] && IGNORELIST="`cat $DHCP_CONF | grep denyinterfaces | cut -f2 -d' ' | tr , " "`"
ACTIVES=""
IF="`netstat -i | grep "^[epvwar]" | grep -v 'wmaster' | gawk '{print $1}'`"
 #110202 e=eth v=vboxnet w=wlan a=ath r=ra p=pan/ppp
 #		 wired virtual   wireless          ?
if [ -n "$IF" ]; then # length of string is non-zero
 for ONE_IF in `echo $IF`
 do
  # Workaround for bug in network tray icons
  #[ -n "`echo "$ONE_IF" | grep 'vbox'`" ] && ! pidof VBoxSVC 1>/dev/null && ifconfig $ONE_IF down &>/dev/null
  ONESTAT="`netstat -i | grep "$ONE_IF"`"
  if [ "`echo "$ONESTAT" | cut -c 16`" != "" ] || [ "`echo "$ONESTAT" | gawk '{print $4}'`" != "0" ]; then #120204 If RX-OK is not zero

   if [ -z "`echo "$IGNORELIST" | grep "$ONE_IF"`" ]; then
    [ "`netstat -i | grep "$ONE_IF" | grep R`" = "" ] && continue # Ignore if not running
    [ "$ONE_IF" = "$WIFI_IF" ] && [ "`$WPA_CLI -i $WIFI_IF status 2>&1 | grep DISCONNECTED >/dev/null`" ] || [ $? -eq 0 ] && continue # or disconnected
    if [ -n "$ACTIVES" ]; then
     ACTIVES="${ACTIVES} ${ONE_IF}"
    else ACTIVES="${ONE_IF}"
    fi
   else # bring down if interface is denied in DHCP_CONF
    [ -n "$IGNORELIST" ] && ifconfig `ifconfig | grep "^$ONE_IF" | cut -f1 -d' '` down #120704 
   fi
   
  fi
 done
fi

if [ -n "$ACTIVES" ]; then
 echo -en "$ACTIVES"

 #Now see if Internet connection is live
 if [ $((`date +%s` - `cat "$SUCCESS_FILE"`)) -le 10 ]; then # already connected within last 10s
  echo " connected"
 else
  #ping -c 1 64.233.169.103 &>/dev/null #google
  ping -c 1 8.8.4.4 &>/dev/null || ping -c 1 8.8.8.8 &>/dev/null #111115 Google Public DNS
  if [ $? -eq 0 ]; then # connected
   echo " connected"
   echo `date +%s` >"$SUCCESS_FILE"
  else
   wget -T 12 -O - -q icanhazip.com &>/dev/null # fallback -T timeout secs
   if [ $? -eq 0 ]; then # connected
    echo " connected"
    echo `date +%s` >"$SUCCESS_FILE"
   else echo " no Internet"
   fi
  fi
 fi
  
else
 pidof frisbee_tray 1>/dev/null && killall -SIGALRM frisbee_tray
 echo "inactive"
fi

exit 0 # END SCRIPT ####
