#!/bin/sh
# Version 00.00.03
# script fuer CTI  :
# Netzwerk IP pruefen
# wegen dhcp und Switch bootet zu lange, <Erklaerung noch schreiben>
# Anpassung fuer WLAN, wenn Funkverbindung nicht aktiv ist
# eigentlich starten nach Link is up/Cable plug in
# Debug Log zugefuegt


SCRIPTNAME=`basename "$0"`

IPCHECKSTATE=1

ERRORLOGFILE=/var/log/check_dhcp.log
DEBUG_ENABLED_FILE=/home/cti/wlan_debug_enable

#--------------
ReadDebugMode()
{
   if [ ! -e $DEBUG_ENABLED_FILE ]; then
      return 0
   else
      return 1
   fi
}
#--------------
PrintDebug()
{
   if [ $2 -gt 0 ]; then
      echo "[DEBUG]:---- $(date) ---- $1" >>$ERRORLOGFILE
   fi
}
#--------------
RestartIF()
{
   PrintDebug "restart IF $1, force down $2" $3
   #NOTE: $1 - Interface, $2 - force ifdown
   if [ -r /var/run/udhcpc.$1.pid ]; then
      kill -9 $(cat /var/run/udhcpc.$1.pid)
   fi
   if [ $2 -eq 1 ]; then
#note ifdown beendet udhcp
      ifdown $1
   fi
   ifup $1
}
#--------------
CheckAdapter()
{
   INTERFACE=$1
   DEBUGVALUE=$2
   CurrentIP=`ip addr show $INTERFACE |grep "inet " |grep -v 127.0.0. |head -1|cut -d" " -f6|cut -d/ -f1`
   CurrentOperstate=`cat /sys/class/net/$INTERFACE/operstate`
   case "$CurrentOperstate" in
   up)
      CurrentIP=`ip addr show  $INTERFACE |grep "inet " |grep -v 127.0.0. |head -1|cut -d" " -f6|cut -d/ -f1`
      if [ -z "$CurrentIP" ] ; then
         logger -s "$SCRIPTNAME [$INTERFACE] Missing IP -> Restart if(down/up) "
         RestartIF $INTERFACE 1 $DEBUGVALUE
      else
      # IP ist vorhanden, pruefe WPA status
         /home/cti/shellscripts/network_helper.sh $INTERFACE CheckWPAStatus 2>&1
         ReturnValue=$?
         PrintDebug "IP:  $CurrentIP , state: $CurrentOperstate und online? - > CheckWPAStatus, RetVat: $ReturnValue" $DEBUGVALUE
         if [ $ReturnValue -eq 2 ]; then
            PrintDebug "Error:Operation not supported, no wpa for if $INTERFACE -> Ignored " $DEBUGVALUE
            return
         fi
         if [ $ReturnValue -eq 1 ]; then
            PrintDebug "Error: [$INTERFACE]CheckWPAStatus -> no connection, drop" $DEBUGVALUE
            RestartIF $INTERFACE 1 $DEBUGVALUE
         fi
         sleep 5
      fi
   ;;
   dormant)
      PrintDebug "[$INTERFACE] dormant State -> ifup" $DEBUGVALUE
      # dormant - Funkverbindung ist nicht ok, bzw. AP nicht erreichbar, neu versuchen
      RestartIF $INTERFACE 0 $DEBUGVALUE
   ;;
   *)
      # hier ist if down, aber wlan(supplicant) aktiv
      if [ -r /var/run/wpa_supplicant.$INTERFACE.pid ]; then
         PrintDebug " ---- IF not UP || not DORMANT : <$CurrentOperstate >, pidof wpa_supplicant: $(cat /var/run/wpa_supplicant.$INTERFACE.pid) -> restart IF <$INTERFACE>" $DEBUGVALUE
         #   wpa_supplicant ist aktiv
         RestartIF $INTERFACE 1 $DEBUGVALUE
         sleep 60
      fi
   ;;
   esac
}
#-------------
start()
{
   logger -s "$SCRIPTNAME : DHCP IP Check started"
   sleep 30
   while [ $IPCHECKSTATE -gt 0 ]; do
      # wenn existent und lesbar
      ReadDebugMode
      DEBUGENABLED=$?
#         echo "dhcp debug: $DEBUGENABLED"
      if [ -r /sys/class/net/eth0 ]; then
       CheckAdapter "eth0" $DEBUGENABLED
      fi
      if [ -e /home/cti/wireless ]; then
         if [ -r /sys/class/net/wlan0 ]; then
            CheckAdapter "wlan0" $DEBUGENABLED
         fi
      fi
# Pruefung  alle 5 Minuten
   sleep 300
   done
}
#-------------
mytest()
{
      ReadDebugMode
      DEBUGENABLED1=$?
      echo "dhcp debug: $DEBUGENABLED1"
      PrintDebug " ---- debug enabled!" $DEBUGENABLED1
      RestartIF "hatnich" 0 $DEBUGENABLED1


}
#-------------
stop()
{
   IPCHECKSTATE=0
}
#-------------
restart()
{
   stop
   start
}
#====================
# main case ...
#====================
case "$1" in
start)
   start
   ;;
stop)
   stop
   ;;
restart)
   restart
   ;;
mytest)
   mytest
   ;;
*)
   echo "Usage: $0 {start | stop | restart }"
   exit 1
esac

exit $?

