#!/bin/sh

#
# Copyright (C) 2016-2020, Lantronix, Inc. All Rights Reserved.
#            7535 Irvine Center Drive, Suite 100
#            Irvine, CA 92618 USA

#  Permission to use, copy, modify this software for any
#  purpose is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.

#  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
#  SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
#  OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

. /lib/functions.sh

# configavailable=0

checkstatus() {
	local recheckcount=0

	while true
	do
		local count=0
		while [ $count -lt 10 ]
		do
			# echo $count
			count=$(expr "$count" + 1)
			sleep 1
		done

		vpnipstatus=`ifstatus $1 |  jsonfilter -e '@["ipv4-address"][0].address'`
		if [ -z "$vpnipstatus" ];then 
		{
			recheckcount=$(expr "$recheckcount" + 1)
			/usr/bin/logger -t PPTPwatchdog -p info  "$1 status is offline"
			
			if [ "$recheckcount" -eq 120 ];then 
			{
				echo "PPTP WatchDog $1 reboot at " `date` > /etc/rebootcause/reboot_pptp
				/usr/bin/logger -t PPTPwatchdog -p info "Restarting Due to pptp interface $1 offline"
				# touch /etc/dummy_file && touch /etc/crontabs/root #FIXEME: Ashish Dont know usage of this
				reboot
			}
			else
			{
				/usr/bin/logger -t PPTPwatchdog -p info  "$1 recheckcount:$recheckcount"
			}
			fi
		}	
		else
		{
			/usr/bin/logger -t PPTPwatchdog -p info  "$1 status is online"
			recheckcount=0
		}
		fi		
	done
}

update(){
	# /usr/bin/logger -t PPTPwatchdog -p info  "update: $@"
	config_get protocol "$1" proto
	[ "$protocol" = "pptp" ] && {
		# configavailable=1
		/usr/bin/logger -t PPTPwatchdog -p info  "Interface $1 is pptp interface"
		checkstatus $1 &
	}
}

watch_pptp(){
	# /usr/bin/logger -t PPTPwatchdog -p info  "updateall_noreload: $@"
	config_load network
	config_foreach update interface
}


commd="$1"

option="${commd}"

/usr/bin/logger -t PPTPwatchdog -p info  "commd:$commd"

case ${option} in 
	boot)
		watch_pptp
	;;
esac


# while [ 1 ] 
# do
# 	count=0
# 	while [ $count -lt 1200 ]
# 	do
# 		echo $count
# 		count=$(expr "$count" + 1)
# 		sleep 1
# 	done
# 	conf_pptp=`cat /etc/config/network | grep config | grep pptp | awk '{print$3}' | cut -d\' -f2`
# 	[ -z $conf_pptp ] || {
# 		for var in $conf_pptp 
# 		do
# 			ifc_pptp=`ifconfig | grep -o pptp-$var`
# 			echo $ifc_pptp
# 			[ -z $ifc_pptp ] || {
# 				echo "Interface present"
# 				prsnt="pptp"
# 				break
# 			}
# 		done
# 		[ -z $prsnt ] && {
# 			echo "PPTP WatchDog reboot at " `date` > /etc/rebootcause/reboot_pptp
# 			/usr/bin/logger -t PPTPwatchdogwatchdog -p info "Restarting Due to all pptp interface offline"
# 			touch /etc/dummy_file && touch /etc/crontabs/root
# 			reboot
# 		}
# 	}
# done
