#!/bin/sh

#get ascii char                          
ascii() {                   
    printf \\$(printf '%03o' $1)
    echo ''                     
}      

disconnectManually() {
	ps -e  | grep pppoe-connect | grep -v grep > /dev/null
	if [ $? -eq 0 ]; then
		#means an instance of pppoe-connect still running (possibly a connection still active)
		pids="`ps -e  | grep pppoe-connect | grep -v grep | awk '{print $1}'`"
		for pid in $pids
		do
			kill $pid > /dev/null
		done

		#kill pppd process which would also be running with this
		ps -e  | grep pppd | grep -v grep | grep rp-pppoe > /dev/null
		if [ $? -eq 0 ]; then
			pids="`ps -e  | grep pppd | grep -v grep | grep rp-pppoe | awk '{print $1}'`"
			for pid in $pids
			do
				kill $pid > /dev/null
			done
		fi
	fi
	rm -rf /etc/resolv.conf
}

#drop pppoe connections
drop_pppoe() {
	ls -1 /var/run/*-pppoe.pid > /dev/null 2>&1

	ret=$?

	if [ $ret -eq 0 ]; then
		connections="`ls -1 /var/run/*-pppoe.pid`"

		for connection in $connections
		do
			connection="`echo $connection | sed s/'\/var\/run\/'//`"
			connection="`echo $connection | sed s/'-pppoe.pid'//`"
			/opt/pppoe-mgr/pppoe-mgr disconnect $connection
			sleep 2
		done
		rm -rf /etc/resolv.conf
	fi
	disconnectManually
}

notify() {
	echo ""
	#if [ $1 = "connect" ]; then
	#	rm -rf /opt/pppoe-mgr/events/manualdisconnect > /dev/null 2>&1
	#	rm -rf /opt/pppoe-mgr/events/disconnect > /dev/null 2>&1
	#elif [ $1 = "manualdisconnect" ]; then
	#	rm -rf /opt/pppoe-mgr/events/connect > /dev/null 2>&1
	#	rm -rf /opt/pppoe-mgr/events/disconnect > /dev/null 2>&1
	#elif [ $1 = "disconnect" ]; then
	#	rm -rf /opt/pppoe-mgr/events/connect > /dev/null 2>&1
	#	rm -rf /opt/pppoe-mgr/events/manualdisconnect > /dev/null 2>&1
	#fi
	#touch /opt/pppoe-mgr/events/$1 > /dev/null 2>&1
	#echo "$1" >> /opt/pppoe-mgr/events/$1	> /dev/null 2>&1
}

