#! /bin/sh
# Copyright (c) 1995-2000 ???
#
# Author: Sandro Wefel  <wefel@informatik.uni-halle.de>
#
# init.d/upsd
#
#   and symbolic its link
#
# /sbin/upsd
#
# System startup script for the upsd
#
### BEGIN INIT INFO
# Provides: upsd
# Required-Start: route 
# Required-Stop:  
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start upsd 
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

# Source SuSE config
. /etc/rc.config


# POWERDOWNFLAG *must* match that in upsmon.conf
DRIVERPATH=/usr/local/ups/bin
POWERDOWNFLAG=/etc/killpower
UPSDCONF=/usr/local/ups/etc/upsd.conf
UPSDPATH=/usr/local/ups/sbin

rc_reset

# if there is no config file, bail out
if [ ! -f $UPSDCONF ]; then
  echo -n "No config file $UPSDCONF ! "
  return=$rc_failed
  echo -e "$return"
  exit 1
fi 
	

# See how we are called.
case "$1" in
  start)
  	echo -n "Starting upsd"
	NUM=0
	for cmd_line in "`grep '^UPS' $UPSDCONF| cut -d' ' -f4-`"
	do
	    if [ -n "$cmd_line" ]
	    then
		DAEMON=`echo $cmd_line | cut -d' ' -f1`
		ARGUMENTS=`echo $cmd_line | cut -d' ' -f2-`
		echo "NUT Starting model driver $DAEMON" 
		startproc $DRIVERPATH/$DAEMON $ARGUMENTS || return=$rc_failed 
		NUM=`expr $NUM + 1`
	    fi
	done
	if [ $NUM -eq 0 ]
	then
	    echo "NUT No UPS drivers were configured"
            return=$rc_failed
	    echo -e "$return"
	    exit 1  	    
	else
	    echo "NUT Starting UPS daemon" 
	    startproc $UPSDPATH/upsd || return=$rc_failed 
	fi
	touch /var/lock/subsys/upsd
	rc_status -v
	;;
  stop)
	echo -n "NUT Stopping UPS daemon: "
	killproc $UPSDPATH/upsd
	echo
	for DAEMON in `awk '/^UPS/ {print $4}' $UPSDCONF`
	do
	    echo -n "NUT Stopping model driver $DAEMON: "
	    killproc $DAEMON
	done
	rm -f /var/lock/subsys/upsd
	rc_status -v
	;;
  powerdown)
	# what fun - cut is in /usr/bin, we'll use awk & sed
	# we also use runcmd - action needs things not available at this stage
	# the first part of the sed replaces tabs with spaces - mind the raw tab in this script
	if [ -f $POWERDOWNFLAG ]; then
	    for cmd_line in "`grep '^UPS' $UPSDCONF |\
		sed -e 'y/	/ /;s/^UPS  *[^ ][^ ]*  *[^ ][^ ]*  *//'`"
	    do
		if [ -n "$cmd_line" ]
		then
		    DAEMON=`echo $cmd_line | awk '{print $1}'`
		    # Arguments are munged to insert -k before device name (last arg)
		    ARGUMENTS=`echo $cmd_line | sed -e 's/^[^ ][^ ]*  *//;s/\( *[^ ][^ ]* *\)$/ -k \1/'`
		    echo "NUT Powerdown model driver $DAEMON"  
		    startproc $DRIVERPATH/$DAEMON -d0 $ARGUMENTS || return=$rc_failed 
		fi
	    done
	fi
	rc_status -v
	;;
  restart)
	$0 stop
	$0 start
	rc_status
	;;
  status)
	# ugh - this is painful - maybe it should all be made one function to do this parsing
	for DAEMON in `awk '/^UPS/ {print $4}' $UPSDCONF`
	do
	  checkproc $DAEMON; rc=$?
	  if test $rc = 0; then echo "OK $DAEMON"
	  else echo "No process $DAEMON" 
	  fi
	done
	checkproc $UPSDPATH/upsd; rc=$?
	if test $rc = 0; then echo "OK $UPSDPATH/upsd"
	else echo "No process $UPSDPATH/upsd" 
	fi
	;;
  *)
	echo "Usage: upsd {start|stop|powerdown|restart|status}"
	exit 1
esac
rc_exit

