151b6f6
#!/bin/bash
151b6f6
151b6f6
# Set RTC wakeup for next VDR timer event
151b6f6
151b6f6
shopt -s extglob
151b6f6
151b6f6
# Check timestamp set by shutdown script.
151b6f6
nexttimer=$( cat /var/run/vdr/next-timer 2>/dev/null )
151b6f6
rm -f /var/run/vdr/next-timer
151b6f6
151b6f6
if [[ $nexttimer != +([0-9]) ]] ; then
151b6f6
    # Next timer timestamp not set by shutdown script or bogus,
151b6f6
    # try to get it via SVDRP.
151b6f6
    nexttimer=$( svdrpsend NEXT abs 2>/dev/null | \
151b6f6
        sed -rne 's/^250[[:space:]]+[0-9]+[[:space:]]+([0-9]+).*/\1/p' )
151b6f6
fi
151b6f6
24fb62f
# TODO: nexttimer may be in the past if there's an ongoing recording
24fb62f
# (see svdrpsend HELP NEXT). In that case we should set *something* or
24fb62f
# the box won't autowakeup at all
151b6f6
if [[ $nexttimer && $nexttimer -gt $( date +%s ) ]] ; then
151b6f6
    [[ -f /etc/sysconfig/vdr ]] && . /etc/sysconfig/vdr
151b6f6
    when=$(( $nexttimer - ${WAKEUP_BEFORE_RECORDING:-10} * 60 ))
151b6f6
    hrwhen=$( date -d "1970-01-01 $when sec UTC" )
151b6f6
    echo "Setting wakeup time for next recording: $hrwhen"
151b6f6
    /usr/sbin/rtcwake -m no -t $when >/dev/null
151b6f6
fi