#!/bin/bash

bin="/opt/interm/bin"
conf="/opt/interm/conf"

if [ $# -eq 0 ]; then
	echo -e "Usage: restart_conf [system|network|audio|pio|serial|serial1|time|ddns|upgrade|reboot|passwd|factory|systemcheck]"
	exit 1
fi

case "$1" in
	"system" )
		echo -e "restart system task"
		;;
	"network" )
		echo -e "restart network task"
		/etc/rc.d/init.d/network restart
		;;
	"audio" )
		echo -e "restart audio task"
		killall -SIGTERM run_audioserver
		killall -SIGTERM run_audioclient
		killall -SIGTERM ffserver
		killall -SIGTERM ffmpeg	
		killall -SIGTERM alsa_server
		killall -SIGTERM alsa_client

		$bin/run_audioserver >/dev/null 2>&1 &
		$bin/run_audioclient >/dev/null 2>&1 &
	;;
	"pio" )
		echo -e "restart pio task"

		killall -SIGTERM run_pioserver
		killall -SIGTERM run_pioclient
		killall -SIGTERM pioclient
		killall -SIGTERM pioserver

		$bin/run_pioserver >/dev/null 2>&1 &
		$bin/run_pioclient >/dev/null 2>&1 &
	;;
	"serial" )
		echo -e "restart serial task"

		killall -SIGTERM run_uartserver
		killall -SIGTERM run_uartclient
		killall -SIGTERM uartserver
		killall -SIGTERM uartclient

		$bin/run_uartserver >/dev/null 2>&1 &
		$bin/run_uartclient >/dev/null 2>&1 &
	;;
	"serial1" )
		echo -e "restart serial1 task"

		killall -SIGTERM run_uartserver1
		killall -SIGTERM run_uartclient1
		killall -SIGTERM uartserver1
		killall -SIGTERM uartclient1

		$bin/run_uartserver1 >/dev/null 2>&1 &
		$bin/run_uartclient1 >/dev/null 2>&1 &
	;;	
	"time" )
		echo -e "restart time task"

		if [ -x "$bin/xml_util" ]; then
			zone=`$bin/xml_util -r -f $conf/time.xml -t /config/timezone`
			echo -e $?
			if [ $? -ne 0 ]; then
				zone="GMT0"
			fi
			echo -e $zone
			if [ -x "$bin/lntz" ]; then
				su -c "$bin/lntz $zone"
			fi
		else 
			exit 1
		fi
	;;	
	"ddns" )
		echo -e "restart ddns task"
		$bin/run_dipclient
	;;	
	"upgrade" )
		echo -e "excute upgrade"

		if [ -x "$bin/xml_util" ]; then
			tftpen=`$bin/xml_util -r -f $conf/upgrade.xml -t /config/tftp/enable`
			if [ $? -ne 0 ]; then
				exit 1;
			fi

			if [ $tftpen = "yes" ]; then
				$tftpip=`$bin/xml_util -r -f $conf/upgrade.xml -t /config/tftp/ip`
				$tftpfname=`$bin/xml_util -r -f $conf/upgrade.xml -t /config/tftp/filename`
				$tftppath=`$bin/xml_util -r -f $conf/upgrade.xml -t /config/path`
				$fname = ${tftppath}${tftpfname}
				tftp -r $tftpfname -l $fname -g $tftpip >/dev/null 2>&1
				[ $? -ne 0 ] && exit 1
			else 
				if [ $# -eq 2 ]; then
					fname=$2
				else
					exit 1
				fi
			fi

			if [ -f $fname ]; then
				path='/opt/interm'
				tarpath=`cd $path; tar -zvxf $fname 2>/dev/null | head -n1`
				path=${path}/

				if [ $? -eq 0 ]; then
					tarpath=`echo $tarpath | sed 's/(.*\/)/$1/'`
					tarpath="${tarpath%\\n}"
					echo -e $tarpath

					if [ -x "${path}${tarpath}" ]; then
						${path}${tarpath} ${path}
						rm -rf ${path}${tarpath}
						rm -rf $fname

						exit 0
					fi
				fi	
			fi
		fi
		exit 1
	;;
	"passwd" )
		echo -e "change password\n"

		if [ $# -eq 4 ]; then
			if [ $2 = 'admin' ]; then
				$bin/htdigest -u admin -r all -p $3 -v -f $conf/lighttpd-htdigest.user
				if [ $? -eq 0 ]; then
				$bin/htdigest -u admin -r all -p $4 -f $conf/lighttpd-htdigest.user
				echo -e "password changed.\n"
				exit 0
				fi
			fi
			if [ $2 = 'clock' ]; then
				$bin/htdigest -u clock -r all -p $3 -v -f $conf/lighttpd-htdigest.user
				if [ $? -eq 0 ]; then
				$bin/htdigest -u clock -r all -p $4 -f $conf/lighttpd-htdigest.user
				echo -e "password changed.\n"
				exit 0
				fi
			fi
			if [ $2 = 'trigger' ]; then
				$bin/htdigest -u trigger -r all -p $3 -v -f $conf/lighttpd-htdigest.user
				if [ $? -eq 0 ]; then
				$bin/htdigest -u trigger -r all -p $4 -f $conf/lighttpd-htdigest.user
				echo -e "password changed.\n"
				exit 0
				fi
			fi
		fi
		exit 1
	;;
	"factory" )
		echo -e "excute factory set\n"

		su -c "touch /.factory_set"
		su -c "/sbin/reboot"
	;;
	"reboot" )
		echo -e "excute reboot\n"

		su -c "/sbin/reboot"
	;;
	"systemcheck" )
		echo -e "excute system restart\n"

		su -c "$bin/run_systemcheck"
	;;
	* )
		echo -e "Invalid argument\n"
		echo -e "Usage: restart_conf [system|network|audio|pio|serial|serial1|time|ddns|upgrade|reboot|passwd|factory|systemcheck]"
		exit 1
	;;
esac

exit 0

