#!/bin/sh
# @(#)upsd	1.13
#         Copyright 1992, American Power Conversion, Inc.
#
# NAME: upsd
#
# PATH: /usr3/SCCS/BREAKOFF/UNIX/PCPLUS4.2/s.upsd
#
# REV: 1.13
#
# LAST MODIFIED: 10/6/95 14:05:45
#
# DESCRIPTION: 
#
#	This script has become sufficiently complex to warrent having it not
# written by INSTALL.
#
# REFERENCES:
#
# NOTES:
#
# REVISIONS:
# ajr01Jun94: Created.
# ajr03Jun94: Added some logic to test if the child is still running
# ajr06Jun94: Had to better specify the daemon name, or shell would show up in
#              RUN_CHECK
# ajr06Jun94: Fixed bug with RUN_CHECK return value
# ajr31Oct94: Added TZ env var stuff for Unixware 1.X
# djs31Aug95: Added /tmp cleanup after FIFO connections
# djs26Sep95: Sent errors to /dev/null
# mds23Nov98: TMPDIR now is $PWRCHUTE/.tmp
# msamson23Mar99: Redhat Linux complains about -e for ps command. It only 
#                 wants ps e instead of ps -e.
# msamson31Mar99: No longer use 'ps e' for Linux - now user 'ps aux'
# msamson02Apr99: Made minor syntax corrections for Linux
#

Echo() {
	string="$1"
	if [ -r /usr/5bin/echo ]
	then
		/usr/5bin/echo "$string"
	else
		echo "$string"
	fi
}


echo_line() {
	Echo "----------------------------------------------------------------------------"
}

beep() {
	Echo "\\c"
}

# UnixWare 1.X users will need to uncomment and supply the following:
#TZ=
#export TZ

PWRCHUTE=/usr/lib/powerchute
export PWRCHUTE

TMPDIR=$PWRCHUTE/.tmp
export TMPDIR

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

# On HP systems, we need to get the TZ environment variable as part of our
# startup environment
if [ -r /etc/src.sh ]
then
	. /etc/src.sh
fi


. ./what_os.sh

case "$OS" in
	$NCR|$UWARE|$OLIV|$SOLARIS2X|$SOLARIS2X86|$USYS|$SINIX)
		shutfile="/sbin/rc0"
		search_string="POWERCHUTE"
		rstring=`grep $search_string $shutfile 2>/dev/null`
		if [ -z "$rstring" ]
		then
			beep
			Echo
			echo_line
			Echo "$shutfile does not contain modifications for PowerChute. Reinstall PowerChute"
			echo_line
			Echo
			beep
			sleep 2
		fi
		;;
esac

rm -f TMPDIR/*apc*
rm -f TMPDIR/a_client.*
rm -f TMPDIR/d_client.*

./_upsd $1 2>/dev/null&


sleep 4

if [ -r upsd.2 ]
then
	Echo "Only one copy of $0 may be running at a time!"
	rm -f upsd.2
	exit
fi

. ./what_os.sh
if [ "$OS" = "$LINUX" ]
then 
    RUN_CHECK=`ps aux | awk '/_upsd/ {print $1}'`
else
    RUN_CHECK=`ps -e | awk '/_upsd/ {print $1}'`
fi

if [ -z "$RUN_CHECK" ] && [ -r ./powerchute.err ]
then
	echo_line
	Echo "upsd has exited on an error condition.  The last entrys to powerchute.err are:"
	echo_line
	tail ./powerchute.err
fi

rm -f ./.upsd.nexit







