#!/bin/bash # # upseye 0.3.6 installation script # #[ElioCor 2003/11/21] Changed script to upgrade perl module to 'Device::SerialPort 0.22' # # # by default the boot scripts are installed in '/etc/init.d' # if not found, the script tries to install in '/etc/rc.d' # if even '/etc/rc.d' does not exists, the script will terminate # asking for a modification of the install script. # check 'ChkInstDir' to see the algorithm InitDir="/etc/init.d" AltInitDir="/etc/rc.d" SolarisDir="/etc/init.d" # the installation directory will be: DestDir/DestSubDir # if 'DestDir' does not exists, user will be warned! DestDir="/usr/sbin" SubDir='microdowell' PidFile="thepid" # name of the OS OSname=`uname` if [ "$OSname" == "SunOS" ] ; then # 'bash' executable location DIRbash=`type bash` # 'sh' executable location DIRsh=`type sh` # 'csh' executable location DIRcsh=`type csh` # 'perl' executable location DIRperl=`type perl` # 'cat' executable location DIRcat=`type cat` else # 'bash' executable location DIRbash=`type -p bash` # 'sh' executable location DIRsh=`type -p sh` # 'csh' executable location DIRcsh=`type -p csh` # 'perl' executable location DIRperl=`type -p perl` # 'cat' executable location DIRcat=`type -p cat` fi # directory with the serial port perl module SerialPortDir="Device-SerialPort-0.22" #===================================================================== # 'ChkBootScriptDir()' checks where the boot scripts are. #===================================================================== # check of the boot script directory function ChkBootScriptDir() { # does 'InitDir' exists? if ! [ -d $InitDir/rc5.d ] ; then # no, i will try for "/etc/rc.d" if [ -d $AltInitDir/rc5.d ] ; then # yes: i set 'InitDir' to /etc/rc.d InitDir=$AltInitDir else # I check if i'm on SOLARIS if [ "$OSname" == "SunOS" ] ; then # I don't need to check the subdirs: they don't exist!!! InitDir=$SolarisDir else #clear echo Unable to find the boot scripts directory echo echo " $InitDir/rc5.d or $AltInitDir/rc5.d" echo echo please edit this installation script echo and modify the variable according to your needs echo echo " 'InitDir'" echo echo exit 2 fi fi fi } #===================================================================== # 'ResetVariables()' sets some variables to their default value #===================================================================== function ResetVariables() { # SOLARIS? i set a different serial port name! if [ "$OSname" == "SunOS" ] ; then UPS_PORT="/dev/cua/b" else UPS_PORT="/dev/ttyS1" fi BATT_CMD="" BATT_TIME=120 TMP=`type -p shutdown` if [ "$OSname" == "SunOS" ] ; then SHDWN_CMD="$TMP now" else SHDWN_CMD="$TMP -h now" fi SHDWN_TIME=180 UPS_MODEL=3 UPS_LINE="Enterprise" UPS_CHARGE=0 opt="" RET="" ScriptDir=$PWD return } #===================================================================== # GetUpsModel() asks for the model of the UPS (currently not used) #===================================================================== function GetUpsModel() { clear echo echo select UPS family echo echo "[1] HiBox (USB/Advanced/USB 700VA)" echo "[2] BBox (B.50)" echo "[3] BBox (B.80/B.100/B.140)" echo "[4] BBoxPro (H.80/H.100/H.125/H.150/H.200/H.300)" echo "[5] BBoxPro RACK (RH.100/RH.150/RH.200/RH.300)" echo "[6] Enterprise Line (every model)" echo echo "[q] quit without modifying UPS model" echo read opt1 if [ "$opt1" == "" ] ; then return fi case $opt1 in 1) UPS_MODEL=1 ; UPS_LINE="HiBox" ; return ;; 2) UPS_MODEL=1 ; UPS_LINE="BBox B.50" ; return ;; 3) UPS_MODEL=2 ; UPS_LINE="BBox" ; return ;; 4) UPS_MODEL=2 ; UPS_LINE="BBoxPro" ; return ;; 5) UPS_MODEL=2 ; UPS_LINE="BBoxPro RACK" ; return ;; 6) UPS_MODEL=3 ; UPS_LINE="Enterprise" ; return ;; q) return ;; esac } #===================================================================== # GetCommunicationPort() asks for the communication port (Serial/USB) #===================================================================== function GetCommunicationPort() { # 1) Get "serial port connected to the UPS" $UPS_PORT ; ChkDevice || UPS_PORT=$RET ;; clear echo echo select UPS communication port: echo echo "[1] USB" echo "[2] Serial port" echo echo "[q] quit without modifying UPS communication port" echo read opt1 if [ "$opt1" == "" ] ; then return fi case $opt1 in 1) UPS_PORT="usb" ; return ;; 2) Get "define the serial port connected to the UPS [eg. /dev/ttyS1]" $UPS_PORT ; ChkDevice || UPS_PORT=$RET ; return ;; q) return ;; esac } #===================================================================== # Get() asks for a variable: if keeps the default value [...] #===================================================================== function Get() { echo echo -n "$1 [$2]: " Ret=$2 read RET if [ "$RET" = "" ] ; then return 0 ; # pressed return: not modify else return 1 ; # new value fi } #===================================================================== # ChkLimit() checks if the variable is in the defined limits #===================================================================== function ChkLimit() { if [ "$RET" == "" ]; then return 0 fi if [ $RET -lt "$1" ]; then echo "" echo "Value UNDER limits [ < $1 ]: not accepted" echo " press to continue" read return 0 ; fi if [ $RET -gt "$2" ]; then echo "" echo "Value OVER limits [ > $2 ]: not accepted" echo " press to continue" read return 0 ; fi return 1 ; } #===================================================================== # ChkDevice() checks if the parameter is really a device #===================================================================== function ChkDevice() { if [ -c "$RET" ]; then return 1 ; else echo "" echo "device [ $RET ] not found: not accepted" echo " press to continue" read return 0 ; fi } #===================================================================== # ChkDir() checks if the parameter is really a directory #===================================================================== function ChkDir() { if [ -d "$RET" ]; then return 1 ; else echo "" echo "directory [ $RET ] not found: not accepted" echo " press to continue" read return 0 ; fi } #===================================================================== # ChkProgram() checks if the parameter is really a program #===================================================================== function ChkProgram() { `$1 $2 >/dev/null 2>/dev/null` if [ $? == 127 ] ; then clear echo =================================================================== echo echo command echo " $1" echo not found. echo [$1] is needed by this installation script: please install it. echo echo =================================================================== echo return 0 else return 1 fi } #===================================================================== # ChkPerlVersion() checks the version of the perl interpreter #===================================================================== function ChkPerlVersion() { perl -e 'if ($] < 5.006) { exit(127) ; } else { exit(1) } ;' if [ $? == 127 ] ; then clear echo =================================================================== echo echo Your perl version: echo echo --------- echo `perl -v` echo --------- echo echo is too old: please upgrade it to 5.6 or major echo echo =================================================================== echo return 0 else return 1 fi } #===================================================================== # RemoveBootFiles() removes the startup scripts of UpsEye #===================================================================== function RemoveBootFiles() { FlagRemove=0 echo echo -n "removing files... " # on SOLARIS it is a bit different... if [ "$OSname" == "SunOS" ] ; then if [ -f $InitDir/../rc3.d/S92rcupseye ] ; then rm $InitDir/../rc3.d/S92rcupseye FlagRemove=1 fi if [ -f $InitDir/rcupseye ] ; then rm $InitDir/rcupseye FlagRemove=1 fi else # delete the old files which was started TOO early # they were installed by the previous release 0.1.0 # of this script if [ -f $InitDir/rc5.d/S02rcupseye ] ; then rm $InitDir/rc5.d/S02rcupseye FlagRemove=1 fi if [ -f $InitDir/rc3.d/S02rcupseye ] ; then rm $InitDir/rc3.d/S02rcupseye FlagRemove=1 fi if [ -f $InitDir/rc5.d/S92rcupseye ] ; then rm $InitDir/rc5.d/S92rcupseye FlagRemove=1 fi if [ -f $InitDir/rc3.d/S92rcupseye ] ; then rm $InitDir/rc3.d/S92rcupseye FlagRemove=1 fi if [ -f $InitDir/rcupseye ] ; then rm $InitDir/rcupseye FlagRemove=1 fi fi if [ "$FlagRemove" == "1" ] ; then echo files removed else echo no files found: none removed! fi echo echo " press to continue..." read } #===================================================================== # ChkPerlModule() checks if the right 'SerialPort' module is loaded #===================================================================== function ChkPerlModule() { # i check if the 'SerialPort' module has been already installed... #[ElioCor 2003/11/21] Changed script to upgrade perl module to 'Device::SerialPort 0.22' perl -e 'eval "use Device::SerialPort 0.22" ; exit 127 if ($@) ; exit 0 ;' if [ $? == 127 ] ; then # module not installed: install it # check if Perl Module directory exists if ! [ -d $SerialPortDir ] ; then echo echo Unable to find directory [$ScriptDir/$SerialPortDir] echo installation aborted. echo exit 4 fi # if there is an instance of upseye running, stop it if [ -f $InitDir/rcupseye ] ; then Ret=`ps -o pid -o cmd -e | grep -qc upseye.pl | grep perl` if [ "$Ret" != "0" ] ; then Ret=`ps -o pid -o cmd -e | grep upseye.pl | grep perl` ProcID=${Ret%* * -I*} if [ "$ProcID" != "" ] ; then kill -9 $ProcID echo 'upseye' daemon stopped... fi fi fi cd $SerialPortDir clear echo Installation of the \'SerialPort\' perl module: echo please be sure NO devices are connected to echo echo " $UPS_PORT" echo echo otherwise tests can be erroneous! echo echo " press to continue..." read clear echo ----------------------------------------------------- echo \'SerialPort\' perl module installation. Please wait... echo ----------------------------------------------------- echo echo Generating makefile, please wait \(about 1 minute\)... # remove old logfile if [ -f ../ErrLog ] ; then rm ../ErrLog fi perl Makefile.PL >> ../ErrLog 2>>../ErrLog echo Running makefile, please wait \(about 1 minute\)... make >> ../ErrLog 2>> ../ErrLog echo Testing \'SerialPort\' module, please wait \(about 1 minute\)... make test $UPS_PORT >> ../ErrLog 2>> ../ErrLog echo Installing module... make install >> ../ErrLog 2>> ../ErrLog echo . perl -e 'eval "use Device::SerialPort" ; exit 127 if ($@) ; exit 0 ;' if [ $? == 127 ] ; then make clean >> ../ErrLog 2>> ../ErrLog cd .. clear cat ErrLog echo echo ---------------------------------------------------------- echo Errors found during the installation: installation halted. echo echo if you are unable to correct the problem, contact the echo MicroDowell software support [research@microdowell.com] echo and send them an email with attached the error file echo " ErrLog" exit 3 else make clean >> /dev/null cd .. rm ErrLog fi fi } #===================================================================== # EditTemplates() modifies the template files #===================================================================== function EditTemplates() { # in this function i modify the template script files # according to the user preferencies # generation of the sed script to modify 'rcupseye' if [ -f SedScript ] ; then rm SedScript fi if [ "$OSname" == "SunOS" ] ; then echo s\|/bin/bash\|$DIRbash\|g > SedScript echo s\|==\|=\|g >> SedScript fi echo /__START__/,/__STOP__/ { >> SedScript echo s\|__SCRIPTPATH__\|$DestDir/$SubDir\|g >> SedScript echo s\|__OPTIONS__\|\|g >> SedScript echo s\|__PERLOPTIONS__\|-I $DestDir/$SubDir \|g >> SedScript echo s\|__PERLPATH__\|$DIRperl\|g >> SedScript echo s\|__CATPATH__\|$DIRcat\|g >> SedScript echo s\|__PIDFILE__\|$DestDir/$SubDir/$PidFile\|g >> SedScript echo } >> SedScript # modification of the "template" rcupseye file cat rcupseye.template | sed -f SedScript > rcupseye # deletion of script file rm SedScript # change the generated script file to "executable" chmod +x rcupseye # generation of the sed script to modify 'enterprise.ini' echo s\|__PORT__\|$UPS_PORT\|g > SedScript echo s\|__SDDELAY__\|$SHDWN_TIME\|g >> SedScript echo s\|__SHUTDOWNCMD__\|$SHDWN_CMD\|g >> SedScript echo s\|__E_BAT_CMDFILE__\|$BATT_CMD\|g >> SedScript echo s\|__E_BAT_SHUTDOWNDELAY__\|$BATT_TIME\|g >> SedScript echo s\|__UPSTYPE__\|$UPS_MODEL\|g >> SedScript if [ "$OSname" == "SunOS" ] ; then # MsgCmd=wall -a echo s\|wall\|wall -a\|g >> SedScript fi # modification of the "template" rcupseye file cat enterprise.template | sed -f SedScript > enterprise.ini # deletion of script file rm SedScript # change the generated script file to "executable" chmod +x enterprise.ini } #===================================================================== # InstallFiles() copies the needed files in the destination directory #===================================================================== function InstallFiles() { # do i need to install the requested perl module? # check for the presence of the 'SerialPort' module... ChkPerlModule # modify the script template for # 'enterprise.ini' # and # 'rcupseye' # EditTemplates # create installation dir echo if ! [ -d $DestDir/$SubDir ] ; then echo creating script directory: $DestDir/$SubDir mkdir $DestDir/$SubDir fi # copy file to destination dir ONLY if i'm not in the same directory if [ "$ScriptDir" != "$DestDir/$SubDir" ] ; then echo -n "copying files to installation dir [$DestDir/$SubDir]: " cp $ScriptDir/install $DestDir/$SubDir cp $ScriptDir/rcupseye $DestDir/$SubDir cp $ScriptDir/*.pl $DestDir/$SubDir cp $ScriptDir/*.pm $DestDir/$SubDir cp $ScriptDir/*.ini $DestDir/$SubDir cp $ScriptDir/*.pdf $DestDir/$SubDir cp $ScriptDir/*.template $DestDir/$SubDir cp $ScriptDir/BBoxDriver $DestDir/$SubDir cp $ScriptDir/BBoxUSBTest $DestDir/$SubDir #cp $ScriptDir/msg_all $DestDir/$SubDir #cp $ScriptDir/installDaemon $DestDir/$SubDir echo OK fi # delete the old files which was started TOO early # they were installed by the previous release 0.1.0 # of this script if [ "$OSname" == "SunOS" ] ; then if [ -f $InitDir/../rc3.d/S92rcupseye ] ; then rm $InitDir/../rc3.d/S92rcupseye fi else if [ -f $InitDir/rc5.d/S02rcupseye ] ; then rm $InitDir/rc5.d/S02rcupseye fi if [ -f $InitDir/rc3.d/S02rcupseye ] ; then rm $InitDir/rc3.d/S02rcupseye fi # it exists: i copy the rcupseye file if [ -f $InitDir/rc5.d/S92rcupseye ] ; then rm $InitDir/rc5.d/S92rcupseye fi if [ -f $InitDir/rc3.d/S92rcupseye ] ; then rm $InitDir/rc3.d/S92rcupseye fi fi echo -n copying 'rcupseye' script in [$InitDir]: cp $ScriptDir/rcupseye $InitDir echo OK echo -n making links in directories [rc3.d and/or rc5.d]: if [ "$OSname" == "SunOS" ] ; then cd $InitDir/../rc3.d ln -s $InitDir/rcupseye S92rcupseye else cd $InitDir/rc5.d ln -s ../rcupseye S92rcupseye cd $InitDir/rc3.d ln -s ../rcupseye S92rcupseye fi echo OK cd $ScriptDir echo . echo . echo . echo upseye files has been installed in [$DestDir/$SubDir] : echo if you need, you can modify echo " $DestDir/$SubDir/enterprise.ini" echo to better suite your needs: see documentation files for more info echo echo the daemon will start at the first reboot of the system. echo if you want to start/stop/restart it by hand, please use the command echo " $InitDir/rcupseye or" echo " $DestDir/$SubDir/rcupseye" echo echo PLEASE REMEMBER TO CONNECT THE UPS TO THE [$UPS_PORT] PORT !!! echo } # ========================================================================= # ========================================================================= # ========================================================================= # ========================================================================= # ========================================================================= #===================================================================== # MAIN routine #===================================================================== ResetVariables # set variables to default values ChkBootScriptDir # check if the boot script directory exists, otherwise exit # now i check for the required program: 'make' and 'perl' ChkProgram make && exit 1 ChkProgram perl -v && exit 1 ChkPerlVersion && exit 1 # main loop while : do clear echo "[0] destination directory (script location) [$DestDir]" echo "[1] UPS communication port [$UPS_PORT] " echo "[2] maximum battery runtime (seconds) BEFORE shutdown [$BATT_TIME]" echo "[3] UPS shutdown delay (seconds) [$SHDWN_TIME]" echo "[4] command to shutdown system [$SHDWN_CMD]" echo "[5] command to be run when the UPS goes in battery mode [$BATT_CMD]" # if [ "$UPS_MODEL" == "3" ] ; then # echo "[8] UPS restarts only when charged more than [$UPS_CHARGE%]" # fi # echo "[9] UPS model/family [$UPS_LINE]" echo echo "[d] restore to defaults" echo "[q] quit without installing " echo "[u] remove files from boot script dir (remove upseye daemon)" echo echo "[s] install 'upseye' and save modification" echo echo -n "Select option: " read opt case $opt in 0) Get "destination directory (script location)" "$DestDir" ; ChkDir || DestDir=$RET ;; # 1) Get "serial port connected to the UPS" $UPS_PORT ; ChkDevice || UPS_PORT=$RET ;; 1) GetCommunicationPort ;; 2) Get "maximum battery runtime (seconds) BEFORE shutdown" $BATT_TIME ; ChkLimit 1 900 || BATT_TIME=$RET ;; 3) Get "UPS shutdown delay (seconds)" $SHDWN_TIME ; ChkLimit 60 900 || SHDWN_TIME=$RET ;; 4) Get "command to shutdown system ( = none)" "$SHDWN_CMD" ; SHDWN_CMD=$RET ;; 5) Get "command to be run when the UPS goes in battery mode ( = none)" "$BATT_CMD" ; BATT_CMD=$RET ;; # 8) [ "$UPS_MODEL" == "3" ] ; Get "UPS restarts only when battery are charged more than" "$UPS_CHARGE%" ; ChkLimit 0 99 || UPS_CHARGE=$RET ;; # 9) GetUpsModel ;; d) ResetVariables ;; u) RemoveBootFiles ;; s) InstallFiles ; cd $DestDir/$SubDir ; exit ;; q) echo "" ; exit 10 ;; esac done exit