#! /bin/bash
#
# Author: Manfred Paulus
#
# /etc/init.d/upsd_configure
#
### BEGIN INIT INFO
# Provides: upsd_configure
# Required-Start: $local_fs
# Required-Stop:  $local_fs
# Should-Start: upsd
# Should-Stop:  upsd
# Default-Start:  1 2 3 4 5
# Default-Stop:   0 6
# Short-Description: Configure and start all components needed by nut
# Description:
#   Read all parameters that are relevant for UPS from haipsetup.xml,
#   change the configuration files needed for nut according to these parameters
#   and start the components needed for nut.
#
### END INIT INFO

export PATH=/usr/bin:/bin:$PATH

WEBSERVICE=/opt/webservice
GET_HA_ENTRY=$WEBSERVICE/scripts/gethaipsetupentry.sh
UPSD_CONFIG_SRC=$WEBSERVICE/config/upsd
UPSD_CONFIG_DEST=/etc/ups
SED_CMD=""

LOGFILE="/var/log/webservice/startup.log"

my_min_runtime="30"

. /etc/rc.status

rc_reset

printUsage() {
  echo "Called $#" 1>&2
  echo "Usage:" 1>&2
  echo "$0 start [master|slave minMinutes masterIP]" 1>&2
  echo "$0 [status|reload|restart|stop]" 1>&2
}

touch $LOGFILE
echo `date` "--- upsd_configure - Received command:" $* "---" >> $LOGFILE

case "$1" in
    start)
        if [ ! -f $GET_HA_ENTRY ]; then
           echo `date` "Cannot find $GET_HA_ENTRY." >> $LOGFILE
           rc_failed 1;
        fi

        my_ups_master=""
        if [ $# -eq 1 ]
        then
           multiNodeType=`$GET_HA_ENTRY system.multiNodeSystem`
           if (( $multiNodeType == 100 )) ; then
              echo `date` "First installation mode: don't configure UPS yet." >> $LOGFILE
           else
              if (( $multiNodeType == 0 || (( $multiNodeType >= 20 && $multiNodeType < 60 )) )) ; then
                 my_node="node1"
              else
                 my_name=`hostname`
                 if [ "$?" != "0" ]; then
                       echo `date` "Cannot get hostname" >> $LOGFILE
                       rc_failed 1;
                 fi
                 name_node1=`$GET_HA_ENTRY system.name-node1`
                 if [ "$?" != "0" ]; then
                       echo `date` "Didn't find system.name_node1 in haipsetup.xml" >> $LOGFILE
                       rc_failed 1;
                 fi
                 name_node2=`$GET_HA_ENTRY system.name-node2`
                 if [ "$?" != "0" ]; then
                       echo `date` "name_node2 =" $name_node2 >> $LOGFILE
                       echo `date` "Didn't find system.name_node2 in haipsetup.xml" >> $LOGFILE
                       rc_failed 1;
                 fi
                 if [ "$my_name" == "$name_node1" ]; then
                    my_node="node1"
                 else
                    if [ "$my_name" == "$name_node2" ]; then
                       my_node="node2"
                    else
                       name_nodeq=`$GET_HA_ENTRY system.nameQuorumNode`
                       if [ "$?" != "0" ]; then
                             echo `date` "Didn't find system.nameQuorumNode in haipsetup.xml" >> $LOGFILE
                             rc_failed 1;
                       fi
                       if [ "$my_name" == "$name_nodeq" ]; then
                          my_node="nodeQ"
                       fi
                    fi
                 fi
              fi
              echo `date` "I am" $my_node >> $LOGFILE
   
              my_ups_mode=`$GET_HA_ENTRY system.ups.$my_node.mode`
              if [ "$?" != "0" ]; then
                    echo `date` "Didn't find system.ups."$my_node".mode in haipsetup.xml" >> $LOGFILE
                    rc_failed 1
              fi
              my_min_runtime=`$GET_HA_ENTRY system.ups.$my_node.min.minutes`
              if [ "$?" != "0" ]; then
                    echo `date` "Didn't find system.ups."$my_node".min.minutes in haipsetup.xml" >> $LOGFILE
              fi
           fi
        else
           if [ $# -eq 4 ]
           then
              my_ups_mode=$2
              my_ups_master=$3
              my_min_runtime=$4
           else
              printUsage
              rc_failed 1
              exit 1
           fi
        fi

        if [ "$my_ups_mode" == "none" ]; then
              # todo: close tcp port
              service upsd stop
        else
           SED_CMD="-e s/MY_MODE/$my_ups_mode/g"
           if [ "$my_ups_mode" == "master" ]; then
              my_password=391a8725c6
              grep -q "upsd:" /etc/hosts.allow
              if [ "$?" != "0" ]; then
                    echo "upsd: ALL" >> /etc/hosts.allow
              fi
           else
              if [ "$my_ups_mode" == "slave" ]; then
                 my_password=f2b1275b85
                 if [ "$my_ups_master" == "" ]; then
                    my_ups_master=`$GET_HA_ENTRY system.ups.$my_node.master-ip-address`
                    if [ "$?" != "0" ]; then
                          echo `date` "Didn't find system.ups."$my_node".master-ip-address in haipsetup.xml" >> $LOGFILE
                          rc_failed 1;
                    fi
                    #SED_CMD="$SED_CMD -e s/localhost/$my_ups_master/"
                 fi
                 #OSFOURK-12931 UPS - flood messages regarding UPS connection
                 SED_CMD="$SED_CMD -e s/localhost/$my_ups_master/"
              fi
           fi
           SED_CMD="$SED_CMD -e s/MY_PASSWORD/$my_password/"
           sed $SED_CMD $UPSD_CONFIG_SRC/upsmon.conf > /tmp/upsmon.conf
           mv /tmp/upsmon.conf $UPSD_CONFIG_DEST
           chmod g+r $UPSD_CONFIG_DEST/upsmon.conf
           chgrp daemon $UPSD_CONFIG_DEST/*
           chown upsd $UPSD_CONFIG_DEST/*

           if [ "$my_ups_mode" == "master" ]; then
              insserv /etc/init.d/upsd
              service upsd restart
              command `upsc usbhid@localhost battery.runtime.low` > /dev/null
              if [ $? -eq 0 ]; then
                 sleep 1
                 echo "upsrw -s battery.runtime.low="$(($my_min_runtime * 60))" -u admin -p c868f93715 usbhid@localhost" >> $LOGFILE
                 upsrw -s battery.runtime.low=$(($my_min_runtime * 60)) -u admin -p c868f93715 usbhid@localhost
              else
                 sleep 1
                 charge=`upsc usbhid@localhost battery.charge`
                 runtime=`upsc usbhid@localhost battery.runtime`
                 my_min_charge=$(($my_min_runtime * $charge * 60 / $runtime + 1))
                 echo "upsrw -s battery.charge.low="$my_min_charge" -u admin -p c868f93715 usbhid@localhost" >> $LOGFILE
                 upsrw -s battery.charge.low=$my_min_charge -u admin -p c868f93715 usbhid@localhost
              fi
           else
              upsmon
           fi

        fi

        rc_status -v
        logger -t upsd_configure "exiting service command: $1 (rc=$_rc_status)"
        ;;
    stop)
        service upsd stop
        insserv -r /etc/init.d/upsd
        rc_reset
        logger -t upsd_configure "exiting service command: $1 (rc=$_rc_status)"
        ;;
    restart|reload)
        $0 start
        ;;
    status)
        rc_reset
        ;;
    *)
        echo `date` "Usage: $0 {start|stop|status|restart|reload}" >> $LOGFILE
        echo `date` "Wrong usage of $0" >> $LOGFILE
        rc_failed 1
        ;;
esac

# Set exit status
rc_exit
