#!/bin/sh
#
# NUT:
#   Install/Upgrade/Remove the network UPS tools client
#
# chkconfig: on 16 31
# description: NUT client post-install script
#

#
# Log action
#
NUT_log() {
   echo "$1"
   logger -t NUT "$1"
   return 0
}

/etc/init.d/hostd status
HOSTD_RUNNING=$?

add_advcfg_string() {
   esxcfg-advcfg -q -g "/UserVars/$1" 2>/dev/null
   if [ $? = 1 ]
   then
      esxcfg-advcfg -A "$1" -T string -E "$2" -F "$3"
      if [ $? = 0 ]
      then
         NUT_log "Created string advcfg $1"
         if [ "$HOSTD_RUNNING" -eq 0 ]
         then                                              
            # make hostd refresh its cached list of options  
            esxcli system settings advanced set -o "/UserVars/$1" -d        
         fi                                                               
      else
         NUT_log "Failed to create string advcfg $1"
      fi
   else
      DESC="$(esxcfg-advcfg -l | grep "/UserVars/$1" | cut -d: -f2- | sed -e 's/^ //')"
      if [ "${DESC}" != "$2" ]
      then
         NUT_log "Upgrading existing string advcfg $1 and keeping value"
         VALUE="$(esxcfg-advcfg -q -g "/UserVars/$1")"
         esxcfg-advcfg -L "$1" && esxcfg-advcfg -A "$1" -T string -E "$2" -F "$3"
         esxcfg-advcfg -q -s "${VALUE}" "/UserVars/$1"
      else
         NUT_log "Using existing string advcfg $1"
      fi
   fi
}

add_advcfg_int() {
   esxcfg-advcfg -q -g "/UserVars/$1" 2>/dev/null
   if [ $? = 1 ]
   then
      esxcfg-advcfg -A "$1" -T int -E "$2" -N "$3" -M "$4" -F "$5"
      if [ $? = 0 ]
      then
         NUT_log "Created int advcfg $1"
         if [ "$HOSTD_RUNNING" -eq 0 ]
         then
            # make hostd refresh its cached list of options
            esxcli system settings advanced set -o "/UserVars/$1" -d
         fi
      else
         NUT_log "Failed to create int advcfg $1"
      fi
   else
      DESC="$(esxcfg-advcfg -l | grep "/UserVars/$1" | cut -d: -f2- | sed -e 's/^ //')"
      if [ "${DESC}" != "$2" ]
      then 
         NUT_log "Upgrading existing int advcfg $1 and keeping value"
         VALUE="$(esxcfg-advcfg -q -g "/UserVars/$1")"
         esxcfg-advcfg -L "$1" && esxcfg-advcfg -A "$1" -T int -E "$2" -N "$3" -M "$4" -F "$5"
         esxcfg-advcfg -q -s "${VALUE}" "/UserVars/$1"
      else
         NUT_log "Using existing int advcfg $1"
      fi
   fi
} 

del_advcfg() {
   esxcfg-advcfg -L "$1" && NUT_log "Deleted advcfg $1" || NUT_log "Failed to delete advcfg $1"
}

upsmon_install() {
   add_advcfg_string NutUpsName 'NUT remote ups name (eg: upsname@nutserver) use space as a separator for multiple upses' upsname@nutserver
   add_advcfg_string NutUser 'NUT username to connect to remote ups' upsuser
   add_advcfg_string NutPassword 'NUT password to connect to remote ups' upspassword
   add_advcfg_string NutMailTo 'NUT send mail notification to this address' root@domain
   add_advcfg_string NutSmtpRelay 'NUT optional smtp relay to send mail notifications' 'none'
   add_advcfg_int NutFinalDelay 'NUT seconds to wait after low battery event before shutting down' 0 3600 5
   add_advcfg_int NutSendMail 'NUT send mail notification (0=no 1=yes 2=verbose)' 0 2 0
   add_advcfg_int NutMinSupplies 'NUT number of power supplies needed to keep this system running' 1 10 1
   add_advcfg_int NutOnBatteryDelay 'NUT seconds to run on battery before shutting down (0=wait for low battery event)' 0 86400 0
}

upsmon_remove() {
   del_advcfg NutUpsName
   del_advcfg NutUser
   del_advcfg NutPassword
   del_advcfg NutMailTo
   del_advcfg NutSmtpRelay
   del_advcfg NutFinalDelay
   del_advcfg NutSendMail
   del_advcfg NutMinSupplies
   del_advcfg NutOnBatteryDelay
}

for action in "$@"; do
   echo "Running '${action}' action";

   case "$action" in
      start)
         NUT_log "Verify NUT client installation"
         upsmon_install
         ;;

      stop)
         true               
         ;;

      status)
         true
         ;;

      install)
         NUT_log "Installing NUT client"
         upsmon_install
         ;;

      upgrade)
         NUT_log "Upgrading NUT client"
         upsmon_install
         ;;

      remove)
         NUT_log "Removing NUT client"
         upsmon_remove
         ;;

      *)
         echo "Usage: `basename "$0"` {start|stop|status|restart|install|upgrade|remove}"
         exit 1
   esac
done

exit 0

