#!/bin/bash

# this script is called before template-expand
# then another script is called to run /usr/libexec/nut-driver-enumerator.sh and start/stop drivers
# then traditionnal services2adjust

# calling this script to configure unit and drivers
if [[ $(/sbin/e-smith/config getprop nut status || echo "disabled") == "disabled" ]] ; then
  # disable server
  /sbin/e-smith/config setprop nut-server status disabled
  /sbin/e-smith/config setprop nut access localhost
  # disable monitor
  /sbin/e-smith/config setprop nut-monitor status disabled
  # TODO get_all_by_prop filter nut-driver@ and foreach $UPS disabled
  #config keys|grep nut-driver
  for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver)
  do
    /sbin/e-smith/config setprop $OUTPUT status disabled
  done

  exit 0;
fi
# nut is enabled

# we set services depending on Mode
Mode=$(/sbin/e-smith/config getprop nut Mode || echo "standalone")
if [[ $Mode == "netserver" ]] ; then
    /sbin/e-smith/config setprop nut-server status enabled
    /sbin/e-smith/config setprop nut access private
elif [[ $Mode == "netclient" ]] ; then
    /sbin/e-smith/config setprop nut-server status disabled
    /sbin/e-smith/config setprop nut access localhost
else
    /sbin/e-smith/config setprop nut-server status enabled
    /sbin/e-smith/config setprop nut access localhost
fi
# enable nut-monitor
/sbin/e-smith/config setprop nut-monitor status enabled

# get_all_by_prop filter nut-driver@ and foreach $UPS enable.
if [[ $Mode == "netclient" ]] ; then
  for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver)
  do
    /sbin/e-smith/config setprop $OUTPUT status disabled
  done
else
  for OUTPUT in $(/sbin/e-smith/config keys|grep nut-driver)
  do
    /sbin/e-smith/config setprop $OUTPUT status enabled
  done
fi

