updated on Tue Jan 24 08:00:27 UTC 2012
[aur-mirror.git] / vdr-devel / vdr-shutdown.sh
blobdcc01bca9d2a14e8fec9f5778fd2d99c215be309
1 #!/bin/bash
3 # Shut the computer down, setting ACPI wakeup time if VDR passes a timer
4 # event.
6 # Arguments passed by VDR
7 # $1 = Time (UTC, seconds from epoch) of the next timer event ('0' if none)
8 # $2 = Number of seconds until the next timer event ('0' if none)
9 # $3 = Number of the channel that will be recorded
10 # $4 = File name of the recording as defined in the timer
11 # $5 = '0' for automatic shutdown or '1' for requested shutdown
13 # 1. To enable this script, pass it to vdr with the -s option. If using
14 # runvdr-extreme then it can be defined in /etc/runvdr.conf:
15 # # Call SHUTDOWN to shutdown the computer.
16 # SHUTDOWN="/usr/bin/vdr-shutdown.sh"
17 # 2. If HARDWARECLOCK and TIMEZONE are not set properly in /etc/rc.conf
18 # then ACPI wakeup may not work as expected.
19 # 3. If vdr is being run as a user other than root, e.g. vdr, then this
20 # script will not work unless the vdr user is able to run it as root
21 # with sudo, without entering a password and without access to a terminal
22 # device. To set all that up in /etc/sudoers, run visudo and enter:
23 # Defaults:vdr !requiretty
24 # vdr ALL=(root) NOPASSWD: /usr/bin/vdr-shutdown.sh
26 # If not run as root then run as sudo root
27 if [ "$(id -u)" -ne 0 ]; then
28 sudo $0 $*
29 exit $?
32 # Time for the system to start, in seconds
33 START_BUFFER="300"
35 # Add debug information to system logs, set to any value to enable logging
36 DEBUG_LOGGING=
38 # Set ACPI wakeup time for timer event
39 if [ "${1:-0}" -gt 0 ] ; then
40 BASENAME=$(basename $0)
42 ADJTIME_FILE="/var/lib/hwclock/adjtime"
43 if [ ! -e "$ADJTIME_FILE" ]; then
44 if [ -n "$DEBUG_LOGGING" ]; then
45 logger -t $BASENAME File $ADJTIME_FILE not found
47 exit 1
50 WAKEALARM_FILE="/sys/class/rtc/rtc0/wakealarm"
51 if [ ! -e "$WAKEALARM_FILE" ]; then
52 if [ -n "$DEBUG_LOGGING" ]; then
53 logger -t $BASENAME File $WAKEALARM_FILE not found
55 exit 1
57 if [ ! -w "$WAKEALARM_FILE" ]; then
58 if [ -n "$DEBUG_LOGGING" ]; then
59 logger -t $BASENAME File $WAKEALARM_FILE not writeable
61 exit 1
64 TIMEZONE=$(grep -e LOCAL -e UTC $ADJTIME_FILE)
65 if [ "$TIMEZONE" = "LOCAL" ]; then
66 UTC_OFFSET=$(($(date "+%::z" | sed -e "s/[+-]/&1*(((/" -e "s/:/)*60+/g" -e "s/$/)/")))
67 else
68 UTC_OFFSET="0"
70 WAKEUP_TIME=$(($1 + $UTC_OFFSET - $START_BUFFER))
71 echo 0 > $WAKEALARM_FILE
72 echo $WAKEUP_TIME > $WAKEALARM_FILE
74 if [ -n "$DEBUG_LOGGING" ]; then
75 logger -t $BASENAME ACPI wakeup: $(grep alrm_date /proc/driver/rtc | cut -c 13-) $(grep alrm_time /proc/driver/rtc | cut -c 13-) $TIMEZONE
76 if [ "$TIMEZONE" = "LOCAL" ]; then
77 logger -t $BASENAME Timer event: $(date -d "1970-01-01 UTC $1 seconds" "+%F %T %Z")
78 else
79 logger -t $BASENAME Timer event: $(date -u -d "1970-01-01 UTC $1 seconds" "+%F %T %Z")
84 # Shut the computer down
85 exec poweroff