#!/bin/sh
checkpid(){
	echo "######################################################"
	echo "checkpid"
	pid=`echo $$` 
	pname=`echo $0`
	mypidfile=/tmp/uswc-service.pid
	uswcpidfile=/tmp/uswc.pid
	/etc/init.d/uswc-service enable
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		for pfalg in `ps|grep -v grep|grep "$pname"|awk '{print $1}'|grep "$expid"|wc -l`;do
			if [ "0" -lt "$pfalg" ];then
				echo "The process $pname is already exists ! pid:$expid."
				exit 0
			else
				echo $pid > $mypidfile
			fi
		done
	else
		echo $pid > $mypidfile
	fi
}

inituswc(){
	echo "######################################################"
	echo "inituswc"
	rm -f /etc/init.d/uswc >/dev/null 2>&1
	uci get uswc.@uswc[0] >/dev/null 2>&1
	if [ "$?" != "0" ];then
		uci get uswc
		if [ "$?" != "0" ];then
			touch /etc/config/uswc
			uci add uswc uswc
			uci commit uswc
		fi
	fi

	uci get uswc.@uswc[1] >/dev/null 2>&1
	if [ "$?" = "0" ];then
		uci del uswc.@uswc[1]
		uci del uswc.@uswc[1]
		uci del uswc.@uswc[1]
		uci commit uswc
	fi
	version=$(opkg list-installed|grep uswc|awk '{print $3}')
	uci set uswc.@uswc[0].version=$version
	uci commit uswc
}

checkuswcenable(){
	echo "###############################################################"
	echo "Check uswc enable..."
	uci get uswc.@uswc[0].enable >/dev/null 2>&1
	if [ "$?" = "0" ];then
		enable=$(uci get uswc.@uswc[0].enable)
		if [ "$enable" = "1" ];then
			checkrun
			echo "Pass!"
		else
			echo "uswc is disabled. Please enable uswc and try again."
			/usr/bin/uswc stop
			sleep 30
		fi
	else
		cat /proc/mtd | grep spec
		if [ "$?" = "0" ];then
			mtdspec=$(cat /proc/mtd |grep spec|awk -F ":" '{print $1}')
			enable=$(cat /dev/$mtdspec | awk -v var="$var1" -F'=' '{if($1==var)print $2}')
			if [ "$enable" = "0" ];then
				uci set uswc.@uswc[0].enable=0
				echo "uswc is disabled. Please enable uswc and try again."
				uswc stop
				sleep 30
			else
				uci set uswc.@uswc[0].enable=1
				checkrun
				echo "Pass!"
			fi
			uci commit uswc
		else
			uci set uswc.@uswc[0].enable=1
			uci commit uswc
			checkrun
			echo "Pass!"
		fi
	fi
}

checkuptime(){
	echo "######################################################"
	echo "checkuptime"
	chmod +x /usr/bin/uswc
	/usr/bin/uswc -u
}

checkrun(){
	echo "######################################################"
	echo "checkrun"
	if test -f "$uswcpidfile";then
		exuswcpid=`cat $uswcpidfile`
		echo $exuswcpid
		pfalg=$(ps|grep -v grep|grep /usr/bin/uswc|grep -v /usr/bin/uswc-service|grep $exuswcpid|awk '{print $1}')
		echo $pfalg
		if [ "$exuswcpid" != "$pfalg" ];then
			chmod +x /usr/bin/uswc
			/usr/bin/uswc start &
		fi
	else
		chmod +x /usr/bin/uswc
		/usr/bin/uswc start &
	fi
}

main(){
	echo "######################################################"
	echo "main"
	while true
	do
		checkuswcenable
		checkuptime
		sleep 10
	done
}

startservice(){
	echo "######################################################"
	echo "startservice"
	sleep 1
	checkpid
	inituswc
	main
}

stopservice(){
	for servicepid in `ps|grep -v grep|grep /usr/bin/uswc-service|awk '{print $1}'`;do
		{
		kill -9 $servicepid > /dev/null 2>&1
		}&
	done
	echo "uswc-service has stopped!"
}

action=$1
[  -z $1 ] && action=start
case "$action" in
start)
    startservice
    ;;
stop)
    stopservice
    ;;
*)
    echo "
Usage: uswc [<OPTIONS>]

Options:
        start
        stop
        -v, --version Report version number
"
    ;;
esac
