File: set_timer

package info (click to toggle)
nvram-wakeup 1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,052 kB
  • ctags: 242
  • sloc: ansic: 1,943; sh: 309; ruby: 190; makefile: 102
file content (106 lines) | stat: -rwxr-xr-x 3,174 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/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