#!/bin/bash2

#/opt/phion/modules/box/boxnet/bin/wipexdhcp
#/etc/phion/bin/wipexdhcp
#arg1 device name
#
#
##author: K. M. Gheri 
#Copyright phion IT GmbH  2005-2007, phion AG 2007-2010, Barracuda Networks AG 2010-2015
#					
#
#script assumes that handed over parameters are meaningful
#i.e. it relies on a consistency check having been performed by the caller

set -f

PATH="/etc/phion/bin:/opt/phion/bin:/sbin:/bin:/usr/sbin/:/usr/bin/"

ARG0="wipexdhcp"
ARGB="wipexdhcp"



IP=$(which ip) || exit 1
MODPROBE=$(which modprobe) || exit 1
MODULES=""
#MODULES="ppp_deflate bsd_comp ppp slhc"

######## phion utility functions begin ##############

if [ -f /etc/phion/phion.sh ]; then
        source /etc/phion/phion.sh
fi

PMODPATH="/opt/phion/modules/box/boxnet"
[ -f ${PMODPATH}/moddefs.conf ] && source ${PMODPATH}/moddefs.conf  
MYLOGFLE="${logfile_xdhcp:-"box_Network_dhcp"}"

export H=1

if [ -f /etc/phion/functions ]; then
        source /etc/phion/functions
        #MYLOGFLE="box_network_axdhcp"
        logerror="LogError $MYLOGFLE"
else
        logerror="echo"
fi

MAXLINK=18


function cleanup {

	local PRIO TBL line i j MODULES

	local PID=${CPID:-2}	
	if [[ ${PID} != 2 ]]; then
		/bin/ps -g ${PID} | {
			while read -t 3 line; do
				set -- ${line}
				_PID=${1} _PROC="${4}" 
				[[ _"${_PROC#xdhcp}" != "_${_PROC}" ]] && {
					kill -TERM ${_PID}
					$logerror "Info" "${ARG0}[$$]: sent SIGTERM to process ${_PROC}, pid=${_PID}"
				}
			done
		}		
	fi			
	
	for i in $(seq 1 ${MAXLINK}); do
			
		for j in 3 2 1 0; do
			killall -q -TERM dhcpcd_xdhcp${i}.$j
			killall -q -TERM xdhcp${i}
		done
	
	done		
	
	killall -q -KILL openxdhcp

	cnt=0
	while pgrep -x 'dhcpcd_xdhcp[[:digit:]]\.[[:digit:]]' > /dev/null
	do
		sleep 1
		cnt=$((cnt+1))
		if [ "$cnt"  -gt 15 ]; then
			$logerror "Info" "${ARG0}[$$]: timeout waiting for DHCP client processes to terminate"
			break
		fi
	done

	sleep 1

	TBL=""
	($IP rule  show|/bin/grep dhcp) | {
		while read line ; do

			TBL="$TBL ${line#* lookup}"

			set -- $line
			PRIO=${1%:*}	

			case $4 in
				dhcp* )
					$IP rule del from $2 table $4 pref $PRIO
					;;
				* )
					:
					;;
			esac

		done
	}
	for i in $(seq 1 ${MAXLINK}); do

		$IP route flush table dhcp${i} &>/dev/null	
		
		DEV=dhcp${i}
		for j in main default; do
			
			#handle device routes with proto equal kernel
			($IP ro list table $j scope link proto kernel|/bin/grep " ${DEV}:") | {
			while read line; do
				$IP ro del $line table $j &>/dev/null
			done
			}

			#handle device routes with proto not kernel
			($IP ro list table $j scope link|/bin/grep " $DEV:") | {
			while read line; do
				$IP ro del $line table $j &>/dev/null
			done
			}

			#handle gateway routes
			($IP ro list table $j|/bin/grep " $DEV:") | {
			while read line; do
				$IP ro del $line table $j &>/dev/null
			done
			}

		done
		
		$IP addr flush dev $DEV &>/dev/null
		$IP li set $DEV down &>/dev/null
	done
	
	$IP ro flush cache 	#flush routing cache to obtain clean state
	

	
	return 0
	}


######### begin main loop ##############

RUNDIR="/var/phion/run/boxnet/DHCP"



$logerror "Info" "${ARG0}[$$]: START, cleaning up DHCP links"
if [ -e ${RUNDIR}/openxdhcp.pid ]; then
	CPID=$(/bin/cat ${RUNDIR}/openxdhcp.pid 2>/dev/null)
	ps axww|grep openxdhcp &>/dev/null && {
		/bin/kill -TERM ${CPID} &>/dev/null
		$logerror "Info" "${ARG0}[$$]: sent SIGTERM to running openxdhcp daemon, pid=${CPID}"
		sleep 1; sleep 1
	}
	/bin/rm -f ${RUNDIR}/openxdhcp.pid
	$logerror "Info" "${ARG0}[$$]: removed ${RUNDIR}/openxdhcp.pid"
	killall -TERM openxdhcp
fi


cleanup

echo "0 dhcp link state OFF" >/opt/phion/run/network_dhcp_state

$logerror "Info" "${ARG0}[$$]: END, DHCP link cleaned up"

exit 0

######## end main loop ################

