#! /bin/sh
#
# This script should be called by upssched via the CMDSCRIPT directive.
# 
# Here is a quick example to show how to handle a bunch of possible
# timer names with the help of the case structure.
#
# This script may be replaced with another program without harm.
#
# The first argument passed to your CMDSCRIPT is the name of the timer
# from your AT lines.

upsdrvctl=/usr/sbin/upsdrvctl
upsd=/usr/sbin/upsd
upsmon=/usr/sbin/upsmon
upsconf=/etc/nut/ups.conf
upsconf_cache=/etc/ups.config

stopUps() {
    killall upsmon > /dev/null 2>&1
    killall upssched > /dev/null 2>&1
    killall upsd > /dev/null 2>&1
    $upsdrvctl stop

    waitStop

    return 0
}

forceStopUps() {
    killall -9 upsmon > /dev/null 2>&1
    killall -9 upssched > /dev/null 2>&1
    killall -9 upsd > /dev/null 2>&1
    $upsdrvctl stop
    sleep 3
}

waitStop() {
    for i in `seq 1 1 3`; do
        sleep 3
        local P_COUNT=`ps -aux |grep -E "(upsd|upsmon|a ups)"|grep -cv grep`
        if [ 0 -eq $P_COUNT ]; then
            return 0
        fi
    done
    forceStopUps
    return 255
}

in_Array(){
	local keyword=$1
	for name in $MUSTAPP;
	do
		[ "$keyword" = "$name" ] && return 0
	done
	return 1
}

stopAllServices() {
	rm $onbattfile

	#if need shutdown ups device
	local driver=`iniparse -c -f $upsconf -s ups -k driver`
	local isShutdownUPS=`iniparse -c -f $upsconf_cache -s ups_con -k shutdown_ups`
	if [ "$driver" != "snmp-ups" -a $isShutdownUPS = 1 ]; then
		stopUps
		$upsdrvctl shutdown
		/sbin/poweroff
		exit 0
	fi
	
	#There is no need to shutdown ups device
	#1、sync data
	sync
	sleep 3
	
	#2、kill process
	local rspid=`ps -ef | awk '/restart_service/ {if($8 != "awk") print $2}'`
	kill -9 $rspid
	local nipid=`ps -ef | awk '/sbin\/nasips/ {if($8 != "awk") print $2"}'`
	kill -9 $nipid
	local nipid=`ps -ef | awk '/sbin\/nasips/ {if($8 != "awk") print $2"}'`
        killall -9 syscontrol > /dev/null 2>&1
	
	MUSTAPP="
	nfs
	samba
	ftp
	iscsid
	netatalk
	mt-daapd
	iscsi-scst
	nginx
	"
	for svrname in $MUSTAPP;
	do
		/etc/init.d/$svrname stop >/dev/null 2>&1
	done

	sleep 1
	for svrname in `ls /etc/sc.d/`
	do
		[ "$svrname" = "ups" ] && continue
		in_Array "$svrname"
		[ $? -eq 0 ] && continue
		scriptshell="/etc/init.d/$svrname"
		[ -f $scriptshell ] && $scriptshell stop >/dev/null 2>&1
	done
	
	#3、sync data again
	sync
	sleep 1
	
	#4、Hard disk sleep、CPU Frequency reduction
#	/usr/bin/cpupower frequency-set -g powersave
	/sbin/hdparm -Y /dev/sd*
}

onbattfile=/tmp/onbatt.ups
onBatt() {
#	echo UPSMODEBAT > /var/msg.fifo &

	if [ ! -f $onbattfile ]; then
		touch $onbattfile
	fi
}

restart() {
	echo UPSMODEAC > /var/msg.fifo &

	if [ -f $onbattfile ]; then
		rm $onbattfile
		exit 0
	fi
	/sbin/reboot
}

case $1 in
	online)
		restart
		;;
	onbatt)
		onBatt
		;;
	fsd)
		stopAllServices
#		$upsmon -c fsd
		;;
	lowbatt)
#		echo UPSBATLOW > /var/msg.fifo &
		stopAllServices
		;;
	nocomm)
		echo UPSDISCON > /var/msg.fifo &
		;;
	*)
		logger -t upssched-cmd "Unrecognized command: $1"
		;;
esac


