#!/bin/sh
# Tiivitaavin nukutus
# Kimmo Tykkälä, 20.7.2004
#

#
# If the following file exists, shutdown will not be done
#
NO_SHUTDOWN_FILE=/tmp/vdr_noshutdown

exec >> /tmp/vdrstandby.log

if [ -f ${NO_SHUTDOWN_FILE} ] ; then
  echo ${NO_SHUTDOWN_FILE} exists, exiting
  exit 0
fi
#
# We don't want the watchdog to start vdr while we are going down
#
/usr/local/bin/vdr-watchdog-allowed.sh OFF

echo VDR-standby-script called with parameters $1 $2 $3 $4 $5

# Second parameter is number of seconds til the next recording
# Let's convert it into hours and minutes

SECONDS=$2

if [ ${SECONDS} -lt 1800 ] ; then
  echo En ota alle puolen tunnin torkkuja!
  exit 0
fi

#
# Let's start the machine 5 minutes before the recording
# will start
#

SECONDS=`expr ${SECONDS} - 300`

HOURS=`expr ${SECONDS} / 3600`
S_LEFT=`expr ${SECONDS} % 3600`
MINUTES=`expr ${S_LEFT} / 60`

if [ ${MINUTES} -lt 10 ] ; then
  MINUTES=0${MINUTES}
fi

echo Next startup will be in ${HOURS}:${MINUTES}

/usr/bin/apmsleep "+${HOURS}:${MINUTES}"