#!/bin/bash
#
#   $Id: set_timer 908 2008-11-23 13:29:13Z tiber $
#
# This script was made by Hans-Hermann Redenius (redenius@gmx.de)
#
# This file is for bioses nvram-wakeup couldn't handle, because there is
# no possibility to change the Wake on RTC time in the nvram
# The wake up time must be set to 23:59:59 the day must be set to 31
# if your board supports a month setting for the wakeup, set it to July
#
# The System should not update the hardware clock in the shutdown process.
# In Debian Woody set HWCLOCKACCESS=no in /etc/default/rcS
#
# in vdrshutdown replace: 
# 
#    $NVRAMCMD -ls $1
#
# by 
#
#    $PATH_TO_SET_TIMER/set_timer $1 $2
#
# and add the following line to your start script before vdr is started
# 
#    $PATH_TO_SET_TIMER/set_timer
#
# The time command from nvram-wakeup is needed (set the path bwlow)
#
# Thanks to Sergei Haller for nvram-wakeup and the time and ideas to develop
# this script


# programs to use:
HWCLOCK=/sbin/hwclock
TIME=/usr/local/sbin/time

# files to use:
TIME_DIFF=/video/time_diff


if [ ! -x $HWCLOCK ] ; then 
  echo "$0 needs hwclock" 
  exit 1
fi

if [ ! -x $TIME ] ; then
  echo "$0 needs the time command from nvram-wakeup"
  exit 1
fi

# current time
cur_time=`date +%s`

# how many seconds before the next event the system should be booted
boot_time=300

# if a time_diff file exists the clock is set to the wakeup time and
# must be fixed before any further action
#
if [ -f $TIME_DIFF ] ; then
  
  # the difference stored 
  time_diff=`cat $TIME_DIFF`
  
  # add the difference to the curent time and change it in the date format
  set_time=$(( $cur_time + $time_diff ))
  set_date=`$TIME $set_time | grep "^(local" | cut -f2- -d')' `
  
  # restore the correct time and if all is right remove the $TIME_DIFF file
  # because there is no longer a difference
  $HWCLOCK --set --date "$set_date" && $HWCLOCK --hctosys && rm $TIME_DIFF
fi  

# if there is a parameter given, it is assumed the system schould boot 
# after $boot_time seconds 
if [ -n "$2" ] ; then

  if [ $1 -eq 0 ] ; then # no timer present
    sleep_time=$(( 61 * 24 * 60 * 60 - 1 + $boot_time )) # 61 days - 1s
                                                         # + boot_time
  else # timer present
    if [ $2 -ge $(( $boot_time + 30 )) ] ; then # need 30 seconds for shutdown
      sleep_time=$2
    else
      sleep_time=$(( $boot_time + 30 )) 
      # or exit 1 so vdrshutdown would do nothing 
      # because the next timer is less than boot_time + 30 seconds in the future
      # or currently recording
      # I suggest a short timeout, because the user really asked for it
    fi
  fi

  # wakeup time  
  wake_time=996616799 # date -d "Jul 31 23:59:59 2001" +%s 
  
  # calculate the right time and the time difference
  set_time=$(( $wake_time - $sleep_time + $boot_time ))
  time_diff=$(( $cur_time - $set_time ))

  # convert time to date format
  set_date=`$TIME $set_time | grep "^(local" | cut -f2- -d')' `
  
  # set date and if it is correct write the time_diff file
  # date --set="$set_date" && \ # add this if a script updates the hwclock 
  $HWCLOCK --set --date "$set_date" && echo $time_diff > $TIME_DIFF
fi
