trap "echo PowerMon Installation Aborted;exit" 2 3 15
cfg=0
scounix=0
ver=3
if [ -f /usr/5bin/uname ]
then
   uname=/usr/5bin/uname
elif [ -f /usr/bin/uname ]
then
   uname=/usr/bin/uname
else
   uname=uname
fi
oem=`$uname -a | grep "oem=3000"`
if [ "$oem" = "" ]
then
   oem=`$uname | grep "SunOS"`
   if [ "$oem" = "" ]
   then
      oem=`$uname -a | grep "i386"`
      if [ "$oem" = "" ]
      then
         oem=`$uname -a | grep "3B2"`
         if [ "$oem" = "" ]
         then
             oem=`$uname | grep "AIX"`
             if [ "$oem" = "" ]
             then
  	        type=0
             else
  	        type=6
             fi
         else
            type=4
         fi
      else
         oem=`$uname -a | grep "scosysv"`
         if [ "$oem" = "" ]
	 then
            type=2
	 else
	    type=2
	    scounix=1
	 fi
      fi	
   else
      echo "PowerMon Installation for Sun"
      echo "3-Sun 3 Series"
      echo "4-Sun 4 Series (includes SPARCstation)"
      while true
      do  
         echo 'Your Choice? '
         read ver
         if [ "$ver" = "" ]
         then
            ver=0
         elif [ $ver -lt 3 -o $ver -gt 4 ]
         then
            ver=0
         else	
            break
         fi
      done
      type=3
   fi
else
   type=1
fi

if [ $cfg -ne 1 ]
then
    if [ "$TERM" = "ansi" -o "$TERM" = "ansic" ]
    then
       echo "\033[2J"
       echo "\r"  
    else
       echo ""
       echo ""   
    fi
    echo "                          PowerMon for UPS Systems"
    echo "                  Copyright (c) 1989, 1990 By CompuSci, Inc."
fi    

if [ $cfg -eq 1 -o $type -eq 1 -o $scounix -eq 1 ]
then
   echo "                        INSTALLING FOR SCO UNIX/XENIX"
elif [ $type -eq 2 ]
then
   echo "                           INSTALLING FOR AT&T UNIX"
elif [ $type -eq 3 ]
then
   echo "                           INSTALLING FOR SUN UNIX"
elif [ $type -eq 4 ]
then
   echo "                            INSTALLING FOR AT&T 3B2"
elif [ $type -eq 5 ]
then
   echo "                            INSTALLING FOR UNISYS"
elif [ $type -eq 6 ]
then
   echo "                       INSTALLING FOR IBM AIX for the RS6000"
fi

if [ $cfg -ne 0 ]
then
   echo " "
elif [ $type -eq 0 ] 
then   
   echo "Please Enter the Version of Unix/Xenix that You are Using\n\r"
   echo "   1-SCO Xenix for 286/386"
   echo "   2-AT&T or SCO Unix 386"
   echo "   3-SunOS"
   echo "   4-AT&T 3B2"
   echo "   5-UniSys"
   echo "   6-IBM AIX for RS6000"
   echo " "

   while true
   do  
      echo 'Your Choice? '
      read type
      if [ "$type" = "" ]
      then
         type=0
      elif [ $type -lt 1 -o $type -gt 6 ]
      then
         type=0
      elif [ $type -eq 3 ]
      then
         echo "PowerMon Installation for Sun"
         echo "3-Sun 3 Series"
         echo "4-Sun 4 Series (includes SPARCstation)"
         while true
         do  
            echo 'Your Choice? '
            read ver
            if [ "$ver" = "" ]
            then
               ver=0
            elif [ $ver -lt 3 -o $ver -gt 4 ]
            then
               ver=0
            else
               break
            fi
         done
         type=3
      else
         break
      fi
   done
fi   

if [ $type -eq 1 ]
then
   mv power_mon.sco power_mon
   rm power_mon.sun4
   rm power_mon.sun3
   rm power_mon.i386
   rm power_mon.3b2       
   rm power_mon.uni
   rm power_mon.rs6
elif [ $type -eq 2 ]
then
   mv power_mon.i386 power_mon
   rm power_mon.sco
   rm power_mon.sun4
   rm power_mon.sun3
   rm power_mon.3b2       
   rm power_mon.uni
   rm power_mon.rs6
elif [ $type -eq 3 ]
then
   if [ $ver -eq 3 ]
   then
      mv power_mon.sun3 power_mon
      rm power_mon.sun4
   else
      mv power_mon.sun4 power_mon
      rm power_mon.sun3
   fi
   rm power_mon.sco
   rm power_mon.i386
   rm power_mon.3b2
   rm power_mon.uni
   rm power_mon.rs6
elif [ $type -eq 4 ] 
then
   mv power_mon.3b2 power_mon
   rm power_mon.sco
   rm power_mon.i386
   rm power_mon.sun4
   rm power_mon.sun3	
   rm power_mon.uni
   rm power_mon.rs6
elif [ $type -eq 5 ] 
then
   mv power_mon.uni power_mon
   rm power_mon.sco
   rm power_mon.i386
   rm power_mon.sun4
   rm power_mon.sun3	
   rm power_mon.3b2
   rm power_mon.rs6
elif [ $type -eq 6 ] 
then
   mv power_mon.rs6 power_mon
   rm power_mon.sco
   rm power_mon.i386
   rm power_mon.sun4
   rm power_mon.sun3
   rm power_mon.uni
   rm power_mon.3b2
fi
chmod u+x power_mon

if [ $type -eq 1 ]
then
   echo "/etc/power_mon.sd /dev/console">>/tmp/power_mon.halt
elif [ $type -eq 2 ]
then
   echo "/etc/shutdown -g0 -i0 -y >/dev/console 2>/dev/console">>/tmp/power_mon.halt
elif [ $type -eq 3 ]
then
   echo "sync;sleep 5;/etc/shutdown -h now">>/tmp/power_mon.halt
elif [ $type -eq 4 ]
then
   echo "/etc/shutdown -g0 -i0 -y >/dev/console 2>/dev/console">>/tmp/power_mon.halt
elif [ $type -eq 5 ]
then
   echo "/etc/shutdown -g0 -i0 -y >/dev/console 2>/dev/console">>/tmp/power_mon.halt
elif [ $type -eq 6 ]
then
   echo "/etc/shutdown -F 0 >/dev/console 2>/dev/console">>/tmp/power_mon.halt
fi
chmod u+x /tmp/power_mon.halt 

echo "(cd /etc;./power_mon)">/tmp/power_mon.init
chmod u+x /tmp/power_mon.init     
     
if [ -d /etc/rc2.d ]
then
    mv /tmp/power_mon.init /etc/rc2.d/S91.power_mon
    echo "Automatic startup file in /etc/rc2.d/S91.power_mon"
elif [ -d /etc/rc1.d ]
then
    mv /tmp/power_mon.init /etc/rc1.d/S91.power_mon
    echo "Automatic startup file in /etc/rc1.d/S91.power_mon"
elif [ -d /etc/rc.d ]
then
    if [ -d /etc/rc.d/7 ]
    then
       mv /tmp/power_mon.init /etc/rc.d/7/S91.power_mon
       echo "Automatic startup file in /etc/rc.d/7/S91.power_mon"
    else
       mv /tmp/power_mon.init /etc/rc.d/S91.power_mon
       echo "Automatic startup file in /etc/rc.d/S91.power_mon"
    fi
elif [ -f /etc/rc ]
then
   cp /etc/rc /etc/rc.old
   if [ $type -eq 6 ]
   then
      ct=`grep "mount all" /etc/rc | wc -l`
   else
      ct=`grep cron /etc/rc | wc -l`
   fi
   if [ $ct -lt 1 ]
   then
      echo "To automatically activate PowerMon after rebooting your computer,"
      echo "you must manually insert the following line into your /etc/rc file"
      echo "   (cd /etc;./power_mon)"
   else
      mv /tmp/power_mon.init /etc
      cp /etc/rc /etc/rc.old.pm
      if [ $type -eq 6 ]
      then
         sed </etc/rc -f sed.ibm >/etc/rc.new
      else
         sed </etc/rc -f sed.pm >/etc/rc.new
      fi
      mv /etc/rc.new /etc/rc
      chmod u+x /etc/rc
      echo "/etc/rc modified.  old version is in /etc/rc.old.pm"
   fi
else
   echo "To automatically activate PowerMon after rebooting your computer,"
   echo "you must manually copy the power_mon.init file from the /etc directory"
   echo "to the appropriate startup directory.  Refer to your system "
   echo "documentation for details."
fi   
cd /etc
cp /tmp/power_mon* .
./power_mon
echo "PowerMon Installed"
