#!/bin/sh

binary=/usr/sbin/sw-cp-serverd
name=sw-cp-server
pidfile=/var/run/$name.pid

. /etc/rc.status

configtest() {
	if $binary -q -t >/dev/null 2>&1; then
		return 0
	else
		rc_status -v
		$binary -t $DAEMON_OPTS
		rc_exit
	fi
}

case "$1" in
	upgrade)
		echo -n "Starting new $name process "
		configtest
		/sbin/killproc -p $pidfile -USR2 $binary
		rc_status -v
		sleep 1
		if [ -f $pidfile.oldbin -a -f $pidfile ]; then
			echo -n "Graceful shutdown of old $name "
			/sbin/killproc -p $pidfile.oldbin -QUIT $binary
			rc_status -v
		else
			echo "$name upgrade failed! Attempting hard restart."
			/sbin/systemctl restart ${name}.service
			rc_status
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|upgrade}"
		exit 1
		;;
esac
rc_exit
