#!/bin/bash2

#/opt/phion/modules/box/boxnet/bin/wipevpnc
#/etc/phion/bin/wipevpnc
#arg1 device name
#
#
#author: K. M. Gheri phion IT Gmb, Copyright 2002-2007, phion AG 2007
# All rights reserved
#
#script assumes that handed over parameters are meaningful
#i.e. it relies on a consistency check having been performed by the caller

# KmgVersionString 4.0.0.1





set -f



DEV=${1:-tap3}


ARG0="wipevpnc"
ARGB="wipevpnc"
ARGS="$1"

TRIES=2
NEXTCHECK=10	#in secs
STANDOFF=60	#in secs

IP=/sbin/ip
IFCFG=/sbin/ifconfig
#VPNC=/etc/phion/bin/vpnc
VPNC3=/etc/phion/bin/vpnc3

#PIDFILE=/var/run/vpnc.pid
PIDFILE3=/var/run/vpnc3.pid
TABLE=vpn2mc

######## 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_vpn2mc:-"box_Network_vpn2mc"}"

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

export H=1




######## phion utility functions end ##############


function linkandrulesoff
	{
	#arg1: device

	local ARG0="$(SUB1) linkandrulesoff"
        local ARGS="$1"

	local DEV=${1:-tap3}
	local RULES=
	local TBL=
	local PRIO
	local line
	
	killall -q -KILL openvpnc
	

	#[ -x $VPNC ] && $VPNC stop &>/dev/null
	[ -x $VPNC3 ] && $VPNC3 stop &>/dev/null
	
	sleep 1
	
	#killall -q -KILL $(basename $VPNC)
	#[ -f $PIDFILE ] && /bin/rm -f $PIDFILE
	
	killall -q -KILL $(basename $VPNC3)
	[ -f $PIDFILE3 ] && /bin/rm -f $PIDFILE3
	
	

	

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

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

		set -- $line
		PRIO=${1%:*}	
		
		case $4 in
			$DEV )
				$IP rule del from $2 table $4 pref $PRIO &>/dev/null
				;;
			* )
				:
				;;
		esac
	
	done
	}
	
	$IP route flush table $TABLE &>/dev/null
	
	$IP ro flush cache &>/dev/null #flush routing cache to obtain clean state

	
	#now get rid of ip addr on dhcp
	$IP a flush dev $DEV &>/dev/null
	

	#finally get rid of device itself if still up

	($IP link show | grep $DEV |grep UP ) |{
		while read line; do
			set -- $line
			$IP link set ${2%:} down &>/dev/null
		done
		}	

		
	
	return 0
	}



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

$logerror "Info" "[0141806] ${ARGB}(${ARGS}): -------- START ----------------------"
$logerror "Info" "[0140100]  ${ARG0}(${ARGS}): dismantling vpn based management link [wipevpnc():$$]"

if [ -e /var/run/openvpnc.pid ]; then
	CPID=$(cat /var/run/openvpnc.pid)
	/bin/kill -TERM -${CPID} &>/dev/null
	sleep 2
	/bin/rm -f /var/run/openvpnc.pid
	$logerror "Info" "[0140100]  ${ARG0}(${ARGS}): exiting, removing /var/run/openvpnc.pid (pid=${CPID})"
	/usr/bin/killall -TERM openvpnc
fi

#clean up to make sure all remnants are gone

echo "0 management tunnel is off" >/opt/phion/run/network_vpnc_state
linkandrulesoff $DEV 

$logerror "Info" "[0141808]  ${ARG0}(${ARGS}): VPN management link (RAM) removed, exiting"
$logerror "Info" "[0141807] ${ARGB}(${ARGS}): --------- END -----------------------"
exit 0

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

