#!/bin/sh
#
# psman       This shell script takes care of starting and stopping
#             the PowerSoft UPS monitoring daemon.

PROGRAM_PATH=/usr/local
POWERPID=/var/run/power.pid
PSPID=/var/run/powersoft.pid
NOLOGIN=/etc/nologin

if [ $(whoami) != "root" ]; then
    printf "Musisz miec prawa administratora, aby uruchomic ten skrypt-($0).\n"
    exit 1
fi

case "$1" in
    init)
	rm -f $POWERPID
	rm -f $NOLOGIN
	if [ -f $PROGRAM_PATH/sbin/powersoft ]; then
	    printf "\nUruchomienie PowerSoft...\n"
	    exec $PROGRAM_PATH/sbin/powersoft
	else
	    exit 1
	fi
    ;;
    stop)
	if [ -f /var/run/powersoft.pid ]; then
	    printf "\nZatrzymanie PowerSoft...\n"
	    kill $(cat $PSPID)
	    rm -f $PSPID
	    rm -f $POWERPID
	else
	    printf "\nPowerSoft nie jest uruchomiony!\n"
	fi
    ;;
    restart)
	if [ -f /var/run/powersoft.pid ]; then
	    printf "\nRestartowanie PowerSoft...\n"
	    kill $(cat $PSPID)
	    rm -f $PSPID
	    rm -f $POWERPID
	    rm -f $NOLOGIN
	    if [ -x $PROGRAM_PATH/sbin/powersoft ]; then
		exec $PROGRAM_PATH/sbin/powersoft
	    else
		exit 1
	    fi
	else
	    printf "\nPowerSoft nie jest uruchomiony!\n"
	fi
    ;;
    status)
	if [ -x $PROGRAM_PATH/sbin/psstat ]; then
	    exec $PROGRAM_PATH/sbin/psstat
	else
	    exit 1
	fi
    ;;
esac

exit 0
