#!/bin/bash2

#/opt/phion/modules/box/boxnet/bin/wipeumts
#/etc/phion/bin/wipeumts
#arg1 device name
#
#
#author: K. M. Gheri 
#Copyright phion IT GmbH  2006-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

# KmgVersionString 5.4.0.3

set -f

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

ARG0="wipeumts"
ARGB="wipeumts"



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_umts:-"box_Network_umts"}"

export H=1

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

DEVOFFSET=4


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#umts}" != "_${_PROC}" ]] && {
					kill -TERM ${_PID}
					$logerror "Info" "${ARG0}[$$]: sent SIGTERM to process ${_PROC}, pid=${_PID}"
				}
			done
		}		
	fi	
	
	

	
	for i in 1 2; do
			
		for j in 0; do
			killall -q -TERM pppd_umts${i}.${j}
		done
		
	
	done		
	
	sleep 1


	for i in 1 2; do
			
		for j in 0; do
			killall -q -TERM chat_umts${i}.$j
		done

		
	
	done		
	

	killall -q -KILL openumts

	
	for i in 1 2; do

		$IP route flush table umts${i} &>/dev/null	

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

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

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

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

			done
		}
		
		for j in main default; do
			DEV=ppp$((i+DEVOFFSET))
			#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
		
	done
	
	$IP ro flush cache 	#flush routing cache to obtain clean state
	
	
	return 0
	}


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

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



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


cleanup

echo "0 umts link state OFF" >/opt/phion/run/network_umts_state

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

exit 0

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

