#!/bin/bash # # upseye 0.2.0 installation script # # # 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" ScoDir="/etc/init.d" # the installation directory will be: DestDir/DestSubDir # if 'DestDir' does not exists, user will be warned! DestDir="/usr/sbin" SubDir='microdowellCL' ScriptFile='rcupseyeCL' 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 if [ "$OSname" == "SCO_SV" ] ; then # '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 # 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 if [ "$OSname" == "SCO_SV" ] ; then # I don't need to check the subdirs: they don't exist!!! InitDir=$ScoDir else #clear echo Unable to find one of the boot scripts directories 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 fi } function ResetVariables() { BATT_CMD="" BATT_TIME=120 TMP=`type -p shutdown` if [ "$OSname" == "SunOS" ] ; then SHDWN_CMD="$TMP now" else if [ "$OSname" == "SCO_SV" ] ; then SHDWN_CMD="$TMP -y now" else SHDWN_CMD="$TMP -h now" fi fi SHDWN_TIME=180 UPS_MODEL="NOT DEFINED" UPS_LINE="Enterprise" UPS_CHARGE=0 MASTER_MODE="" IPaddress="NOT DEFINED" opt="" RET="" ScriptDir=$PWD return } 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 } 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 ; } 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 } 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 } function ChkPerlVersion() { perl -e 'if ($] < 5.6.0) { 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 newer echo echo =================================================================== echo return 0 else return 1 fi } 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/S92$ScriptFile ] ; then rm $InitDir/../rc3.d/S92$ScriptFile FlagRemove=1 fi if [ -f $InitDir/$ScriptFile ] ; then rm $InitDir/$ScriptFile FlagRemove=1 fi else if [ "$OSname" == "SCO_SV" ] ; then if [ -f $InitDir/../rc2.d/S92$ScriptFile ] ; then rm $InitDir/../rc2.d/S92$ScriptFile FlagRemove=1 fi if [ -f $InitDir/$ScriptFile ] ; then rm $InitDir/$ScriptFile 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/S02$ScriptFile ] ; then rm $InitDir/rc5.d/S02$ScriptFile FlagRemove=1 fi if [ -f $InitDir/rc3.d/S02$ScriptFile ] ; then rm $InitDir/rc3.d/S02$ScriptFile FlagRemove=1 fi if [ -f $InitDir/rc5.d/S92$ScriptFile ] ; then rm $InitDir/rc5.d/S92$ScriptFile FlagRemove=1 fi if [ -f $InitDir/rc3.d/S92$ScriptFile ] ; then rm $InitDir/rc3.d/S92$ScriptFile FlagRemove=1 fi if [ -f $InitDir/$ScriptFile ] ; then rm $InitDir/$ScriptFile FlagRemove=1 fi fi fi if [ "$FlagRemove" == "1" ] ; then echo files removed else echo no files found: none removed! fi echo echo " press to continue..." read } function EditTemplates() { # in this function i modify the template script files # according to the user preferencies # generation of the sed script to modify '$ScriptFile' 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" $ScriptFile file if [ "$OSname" == "SCO_SV" ] ; then cat $ScriptFile.template.sco | sed -f SedScript > $ScriptFile else cat $ScriptFile.template | sed -f SedScript > $ScriptFile fi # deletion of script file rm SedScript # change the generated script file to "executable" chmod +x $ScriptFile # generation of the sed script to modify 'enterprise.ini' 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\|__IP_ADDRESS__\|$IPaddress\|g >> SedScript if [ "$MASTER_MODE" == "MASTER" ] ; then echo s\|__MASTER_PORT__\|18881\|g >> SedScript if [ "$UPS_MODEL" == "UpsEye Server (PC/Unix/Linux)" ] ; then echo s\|__MASTER_VALUE__\|0\|g >> SedScript else echo s\|__MASTER_VALUE__\|1\|g >> SedScript fi else echo s\|__MASTER_PORT__\|18882\|g >> SedScript echo s\|__MASTER_VALUE__\|0\|g >> SedScript fi if [ "$OSname" == "SunOS" ] ; then # MsgCmd=wall -a echo s\|wall\|wall -a\|g >> SedScript fi # modification of the "template" $ScriptFile file cat enterpriseCL.template | sed -f SedScript > enterprise.ini # deletion of script file rm SedScript # change the generated script file to "executable" chmod +x enterprise.ini } function InstallFiles() { if [ "$IPaddress" = "NOT DEFINED" ] ; then clear echo echo "Server IP address not defined:" echo " Please set it!!!!" echo echo echo " press to continue..." read return 0 fi if [ "$UPS_MODEL" = "NOT DEFINED" ] ; then clear echo echo "'Type of connection' not defined:" echo " Please set it!!!!" echo echo echo " press to continue..." read return 0 fi # modify the script template for # 'enterprise.ini' # and # '$ScriptFile' # 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/$ScriptFile $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/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/S92$ScriptFile ] ; then rm $InitDir/../rc3.d/S92$ScriptFile fi else if [ "$OSname" == "SCO_SV" ] ; then if [ -f $InitDir/../rc2.d/S92$ScriptFile ] ; then rm $InitDir/../rc2.d/S92$ScriptFile fi else if [ -f $InitDir/rc5.d/S02$ScriptFile ] ; then rm $InitDir/rc5.d/S02$ScriptFile fi if [ -f $InitDir/rc3.d/S02$ScriptFile ] ; then rm $InitDir/rc3.d/S02$ScriptFile fi # it exists: i copy the $ScriptFile file if [ -f $InitDir/rc5.d/S92$ScriptFile ] ; then rm $InitDir/rc5.d/S92$ScriptFile fi if [ -f $InitDir/rc3.d/S92$ScriptFile ] ; then rm $InitDir/rc3.d/S92$ScriptFile fi fi fi echo -n copying $ScriptFile script in [$InitDir]: cp $ScriptDir/$ScriptFile $InitDir echo OK if [ "$OSname" == "SunOS" ] ; then echo -n making links in directories [rc3.d and/or rc5.d]: cd $InitDir/../rc3.d ln -s $InitDir/$ScriptFile S92$ScriptFile else echo -n making link in directory [rc2.d]: if [ "$OSname" == "SCO_SV" ] ; then cd $InitDir/../rc2.d ln -s $InitDir/$ScriptFile S92$ScriptFile else echo -n making links in directories [rc3.d and/or rc5.d]: cd $InitDir/rc5.d ln -s ../$ScriptFile S92$ScriptFile cd $InitDir/rc3.d ln -s ../$ScriptFile S92$ScriptFile fi 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/$ScriptFile or" echo " $DestDir/$SubDir/$ScriptFile" echo echo cd $DestDir/$SubDir ; exit 0 } function GetMasterSlave() { clear echo "If you are connecting to a TCP/IP module, ONLY one 'MASTER' can" echo "manage all of the UPS functions" echo "The other connections MUST be made in 'SLAVE' mode: you will be" echo "able to receive the UPS status, but some of the UPS functions" echo "(shutdown, scheduling, ...) are disabled." echo "If you set more than one 'MASTER', only the first client will contact" echo "the UPS, the others will not be able to communicate with it!" echo echo "Are you connecting to:" echo echo "[1] UpsEye Server (PC/Unix/Linux)" echo "[2] TCP/IP module [MASTER mode]" echo "[3] TCP/IP module [SLAVE mode] " echo echo "[q] quit without modifying CONNECTION method" echo echo -n "Select option: " read opt1 if [ "$opt1" == "" ] ; then return fi case $opt1 in 1) UPS_MODEL="UpsEye Server (PC/Unix/Linux)" ; MASTER_MODE="MASTER" ; return ;; 2) UPS_MODEL="TCP/IP module" ; MASTER_MODE="MASTER" ; return ;; 3) UPS_MODEL="TCP/IP module" ; MASTER_MODE="SLAVE" ; return ;; q) return ;; esac } # ========================================================================= # ========================================================================= # ========================================================================= # ========================================================================= # ========================================================================= ResetVariables # set variables to default values # now i check for the required program: 'make' and 'perl' ChkProgram perl -v && exit 1 ChkPerlVersion && exit 1 # main loop while : do clear echo "[0] destination directory (script location) [$DestDir]" echo "[1] Server IP address [$IPaddress]" 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]" echo "[6] Type of connection: [$UPS_MODEL, $MASTER_MODE]" 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 "Server-TCP/IP module IP address" "$IPaddress" ; IPaddress=$RET ;; 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 ;; 6) GetMasterSlave ;; d) ResetVariables ;; u) RemoveBootFiles ;; s) InstallFiles ;; q) echo "" ; exit 10 ;; esac done exit