#!/bin/sh
checkpid(){
	echo "######################################################"
	echo "checkpid"
	pid=`echo $$` 
	pname=`echo $0`
	mypidfile=/tmp/JXJSQ-service.pid
	JXJSQpidfile=/tmp/JXJSQ.pid
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		for pfalg in `ps|grep "$expid"|grep "$pname"|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
}

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

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

checkJXJSQenable(){
	echo "###############################################################"
	echo "Check JXJSQ enable..."
	uci get JXJSQ.@JXJSQ[0].enable >/dev/null 2>&1
	if [ "$?" = "0" ];then
		enable=$(uci get JXJSQ.@JXJSQ[0].enable)
		if [ "$enable" = "1" ];then
			checkrun
			echo "Pass!"
		else
			echo "JXJSQ is disabled. Please enable JXJSQ and try again."
			/usr/bin/JXJSQ 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 JXJSQ.@JXJSQ[0].enable=0
				echo "JXJSQ is disabled. Please enable JXJSQ and try again."
				JXJSQ stop
				sleep 30
			else
				uci set JXJSQ.@JXJSQ[0].enable=1
				checkrun
				echo "Pass!"
			fi
			uci commit JXJSQ
		else
			uci set JXJSQ.@JXJSQ[0].enable=1
			uci commit JXJSQ
			checkrun
			echo "Pass!"
		fi
	fi
}

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

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

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

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

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

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

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