#!/bin/sh
# @(#)xpowerchute	1.12
#         Copyright 1992, American Power Conversion, Inc.
#
# NAME: xpowerchute
#
# PATH: /usr3/SCCS/BREAKOFF/UNIX/PCPLUS4.2/s.xpowerchute
#
# REV: 1.12
#
# LAST MODIFIED: 10/6/95 14:05:55
#
# DESCRIPTION: 
#  
#	This script will automatically determine the windown manager running
# and automatically invoke the correct powerchute binary
#
# REFERENCES:
#
# NOTES:
#
# REVISIONS:
# ajr15Jun94 Created.
# ajr21Jun94 Fixed bug with $? interpetation...
# ajr08Jul94 Lets throw away the meaninglyess xvt warings when oxpwrchute 
#            starts.
# ajr08Jul94 Need to use different method of determining running wm for 
#            Solaris 1.X and 2.X
# ajr11Jul94 Forgot to take debugging info out...
# ajr23Sep94 Make it so you can execute FE scripts from outside of installdir
# ajr04Nov94 Drop OpenWindows support from script.
# djs28Jun95 Add OpenWindows support for Solaris2.x only
# djs26Sep95 Sent XVT errors to /dev/null
# mds30Sep98 Port to Unixware 7 - set DISPLAY environment variable to localhost
# mds07Sep98 Make sure above fix is only done on SCO Unixware 7 and not SCO 
#            Openserver 5
# mds24Nov98 TMPDIR is now $PWRCHUTE/.tmp
# ckuiawa08Apr99  Added check to see if we are running client in local mode.
#                 If we are, then don't run.  Otherwise, run client.
# twenisch 14Apr99 Linux must check for other PC instances with
#                  ps aux instead of ps e.  Used grep instead of awk.
#

. ./what_os.sh

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

PWRCHUTE=/usr/lib/powerchute
TMPDIR=$PWRCHUTE/.tmp
export TMPDIR
export PWRCHUTE


# The following DISPLAY setting is needed on SCO Unixware 7 for our front-end
# to execute correctly. This was recommended in SCO's release notes. If it is 
# not done, our front-end will not open

if [ $OS = "$UWARE7" ]
then
	DISPLAY=localhost:0.0
	export DISPLAY
	xhost localhost >/dev/null
fi

	

TRUE=0
FALSE=1

OPENWIN=2
MOTIF=3

WIN_MAN=

SolarisDetermineWinMan() {

	# whodo? Why, I do, damn you!
	whodo | awk '/mwm/ {print $1}' > ./mwm.flag
        whodo | awk '/dtwm/ {print $1}' > ./dtwm.flag
	whodo | awk '/olwm/ {print $1}' > ./olwm.flag
}


Sun4DetermineWinMan() {

	ps -aux | grep olwm | awk '{ if ($11 ~ /olwm/) { print $11 }}' > ./olwm.flag
	ps -aux | grep mwm | awk '{ if ($11 ~ /mwm/) { print $11 }}' > ./mwm.flag
}
	

	

DetermineWindowManager() {

	os=$1
	rval=

	if [ $os = "$SUNOS4" ]
	then
		Sun4DetermineWinMan
		
	elif [ $os = "$SOLARIS2X" ] || [ $os = "$SOLARIS2X86" ]
	then
		SolarisDetermineWinMan
	else 
		echo "ERROR: Should not be in DetermineWindowManager()"
		UnableToDetermineWM_Error
		exit
	fi

	if [ -s ./mwm.flag ] || [ -s ./dtwm.flag ] 
	then
		rval=$MOTIF
	else
		rval=$OPENWIN
	fi

	rm -f ./mwm.flag
	rm -f ./olwm.flag
        rm -f ./dtwm.flag

	return $rval
}


UnableToDetermineWM_Error() {
	echo "Could not automatically determine windowing manager!  Try"
	echo "providing the command line switch '-motif' or '-openwin'"
	echo "Quitting."
}

cd $PWRCHUTE

if [ "$1" = "-openwin" ]
then
  if [ $OS = $SOLARIS2X ]
  then
    ./_oxpwrchute 2>/dev/null
  else
    echo "OpenWindows not supported in the release."
  fi
elif [ "$1" = "-motif" ]
then
    ./_xpwrchute #2>/dev/null
else

	. ./what_os.sh

	case "$OS" in
		$SOLARIS2X|$SOLARIS2X86|$SUNOS4)
	
			DetermineWindowManager $OS
			WIN_MAN=$?
	
			if [ $WIN_MAN -eq $MOTIF ]
			then
				./_xpwrchute #2>/dev/null

			elif  [ $WIN_MAN -eq $OPENWIN ]
			then
			   if [ $OS = $SOLARIS2X ]
                           then
                                 ./_oxpwrchute 2>/dev/null
                           else
                                echo "OpenWindows not supported in the release."
                           fi
			else
				UnableToDetermineWM_Error
			fi
			;;
		*)

            # we need to determine if powerchute is running
            # and if it is, are we in local or network mode?

	    . ./what_os.sh
	    if [ "$OS" = "$LINUX" ]
	    then 
		RUN_CHECK=`ps aux > tmp.pid`
	    else
		RUN_CHECK=`ps e > tmp.pid`
	    fi

	    $RUN_CHECK
	    grep pwrchu tmp.pid > tmp2.pid
            if [ -s tmp2.pid ]   #file exists; size > 0
               then

               #powerchute is running
               #check to see if usetcp is set to no

               rm -f tmp.pid
	       rm -f tmp2.pid
               grep -i usetcp powerchute.ini > tmp.pid
               grep -i no tmp.pid > tmp2.pid

               if [ -s tmp2.pid ]
                  then

                  # usetcp is set to no.
                  echo "Can't run more than one version of "
                  echo "PowerChute Client in Local Mode."
                  
                  else
                     ./_xpwrchute 2>/dev/null
                  fi
               else

                  # powerchute is not running.  Go ahead and
                  # launch it.
                  ./_xpwrchute 2>/dev/null
            fi

			rm -f tmp.pid
			rm -f tmp2.pid

			;;
	esac
fi




