#!/bin/bash
# Autopoweroff init script.

test -f /lib/lsb/init-functions || exit 1
. /lib/lsb/init-functions

logfile="/var/log/autopoweroff.log"

start()
{
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

test -f /etc/ltsp_chroot || exit 0

. /lib/lsb/init-functions
. /usr/lib/ltsp/ltsp_config

boolean_is_true() {
    case "$(echo $1 | tr 'A-Z' 'a-z')" in
       true|y|yes) return 0 ;;
       *) return 1 ;;
    esac
}

if boolean_is_true "$AUTOPOWEROFF" ; then

cat <<-EOF > /etc/autopoweroff.conf

[NO_SHUTDOWN_TIME_RANGE]
StartHour=${AUTOPOWEROFF_START}
EndHour=${AUTOPOWEROFF_END}

[TIMEOUTS]
StartupDelay=${AUTOPOWEROFF_STARTUP_DELAY}
IdleTime=${AUTOPOWEROFF_IDLE_TIME}

[DEPENDANTS]
Hosts=

EOF

  log_begin_msg "Starting Autopoweroff..."
  start_daemon /usr/sbin/autopoweroffd >>${logfile} 2>&1
  log_end_msg $?
fi
}

stop()
{
  log_begin_msg "Stopping Autopoweroff..."
  killproc autopoweroffd
  log_end_msg 0
}


case "$1" in
  start)    start;;
  stop)     stop;;
  restart)  stop;start;;
  *)        echo "Usage: $0 {start|stop|restart}" ;;
esac
