#!/bin/sh
# Copyright (c) 2008-2010 Synology Inc. All rights reserved.

if [ "x$1" = "x" -o "x$1" = "x-h" ]; then
	echo "Copyright (c) 2008-2010 Synology Inc. All rights reserved."
	echo "Usage: `basename $0` { online | lowbatt | nocomm | fsd }"
	exit
fi

UPSMode=`/bin/get_key_value /etc/synoinfo.conf ups_mode`
case "${UPSMode}" in
[Ss][Nn][Mm][Pp] | [Uu][Ss][Bb])
	UPSMaster=1
	EnabledKey="ups_enabled"
	UPSMonServer="localhost"
	;;
*)
	UPSMaster=0
	EnabledKey="upsslave_enabled"
	UPSMonServer=`/bin/get_key_value /etc/synoinfo.conf upsslave_server`
	if [ "x$UPSMonServer" = "x" ]; then
		UPSMonServer="localhost"
	elif [ 0 -lt `echo ${UPSMonServer}|grep -c ':'` ]; then
		UPSMonServer="[${UPSMonServer}]"
	fi
	;;
esac

UPSEnabled=`/bin/get_key_value /etc/synoinfo.conf ${EnabledKey}`
case "$UPSEnabled" in
[Yy][Ee][Ss])
	;;
*)
	echo "UPS is not enable."
	exit
	;;
esac

UPSSafeShutdown=`/bin/get_key_value /etc/synoinfo.conf ups_safeshutdown`
case "${UPSSafeShutdown}" in
[Nn][Oo])
	UPSSafeShutdown=0;;
*)
	UPSSafeShutdown=1;;
esac

SZF_SAFEMODE="/tmp/ups.safedown"

SYSLOG() {
	logger -p user.err -t synoups "$1"
}

UPSStatusGet() {
	ups_stat=`/usr/syno/bin/upsc ups@${UPSMonServer} ups.status 2>/dev/null`
	if [ $? -ne 0 ]; then
		echo "ERR"
		return 255
	fi
	for status in $ups_stat ; do
		case "$status" in
		"OL"|"OB"|"LB")
			echo "$status"
			return
			;;
		esac
	done
	echo "ERR"
}

UPSShutdown() {
	if [ $UPSMaster -ne 1 ]; then
		echo "Slave waits for safe shutdown" >> $SZF_SAFEMODE
		touch /var/.NormalShutdown
		SYSLOG "UPS waits for safe shutdown."
		return
	fi
	OL=0
	LB=0
	while [ $OL -ne 2 -a $LB -ne 2 ]; do
		sleep 10
		St=`UPSStatusGet`
		if [ "$St" = "OL" ]; then
			OL=`expr $OL + 1`
			LB=0
		else
			LB=`expr $LB + 1`
			OL=0
		fi
		echo "OL=$OL LB=$LB" >> $SZF_SAFEMODE
	done
	if [ $OL -eq 2 ]; then
		synologset1 sys warn 0x11300012
		echo "UPS back to On-Line and reboot." >> $SZF_SAFEMODE
		reboot
	elif [ $LB -eq 2 ]; then
		touch /var/.NormalShutdown
		if [ $UPSSafeShutdown -eq 0 ]; then
			echo "Waiting UPS exhausted." >> $SZF_SAFEMODE
			SYSLOG "Waiting UPS exhausted."
		else
			echo "UPS safe shutdown." >> $SZF_SAFEMODE
			SYSLOG "UPS safe shutdown."
			killall upssched
			killall upsmon
			killall upsd
			/usr/syno/bin/upsdrvctl stop
			/usr/syno/bin/upsdrvctl shutdown
		fi
	fi
}

UPSSafeMode() {
	if [ -f $SZF_SAFEMODE ]; then
		return
	fi
	St=`UPSStatusGet`
	if [ "$St" = "OL" ]; then
		echo "WARNING: UPS is On-Line"
		return
	fi
	echo "$1" > $SZF_SAFEMODE

	/usr/syno/bin/synologset1 sys warn 0x11300011
	SYSLOG "UPS going to stop all services and umount all volumes."

	/usr/syno/bin/syno_poweroff_task -s
	sync; sync; sync;

	echo "Stop all services and umount all volumes" >> $SZF_SAFEMODE
	UPSShutdown
}

UPSRestart() {
	if [ ! -f $SZF_SAFEMODE ]; then
		return
	fi
	echo "UPS back on-line, so wait to reboot" >> $SZF_SAFEMODE
	sleep 30
	St=`UPSStatusGet`
	if [ "$St" = "OL" ]; then
		synologset1 sys warn 0x11300012
		SYSLOG "UPS back to On-Line and reboot."
		reboot
	fi
}

case "$1" in
online)
	UPSRestart
	;;
lowbatt | nocomm)
	UPSSafeMode $1
	;;
fsd)
	/usr/syno/sbin/upsmon -c fsd
	UPSSafeMode $1
	;;
esac

