#!/bin/bash
#
# vdrshutdown: shutdown script for vdr
# Load VDR configuration
. /etc/default/vdr
# Load Shutdown configuration
. $VDRCONFDIR/shutdown.conf
while read i ; do
CMD=$(eval $i 2>/dev/null)
if [ $? -eq 0 ] ; then
if [ -n "$CMD" ] ; then
logger -i "${0##*/} -- stop shutdown, because $CMD..."
logger -i "${0##*/} -- waiting period $WAITING_PERIOD minutes up to the next shutdown..."
echo "svdrpsend.pl MESG 'Shutdown abgebrochen, weil $CMD'"| at now
fi
sleep 1
echo "svdrpsend.pl MESG 'Erneuter Shutdown Versuch in $WAITING_PERIOD Minuten.'"| at now
sleep 1
echo "svdrpsend.pl HITK Power"| at +$WAITING_PERIOD minutes
exit 1
fi
done < <(echo "$CHECK_PROCS" | sed '/^[ ]*$/d')
if [ "$ACPI_WAKEUP" = "1" -a -f /proc/acpi/alarm ] ; then
WAKEUPTIME=$(($1 + 7200 ))
# echo `unix2iso8601 -u $(($WAKEUPTIME - 120))` >/proc/acpi/alarm
sudo $VDRSCRIPTDIR/vdr2root acpi-wakeup $WAKEUPTIME
sudo $VDRSCRIPTDIR/vdr2root poweroff
exit 0
fi
#if which nvram-wakeup >/dev/null 2>&1 ; then
if [ "$NVRAM_WAKEUP" = "1" ] ; then
# check MAX_POWEROFF_TIME for special command and set the maximal value
if [ $MAX_POWEROFF_TIME = "MAXIMAL" ] ; then
# wenn dieser Monat keinen 31. Tag hat, setze den wakeup auf den 31. naechsten Monats.
MONTH_NOW=`date +%-m`
YEAR_NOW=`date +%Y`
if [ $MONTH_NOW==2 ]||[ $MONTH_NOW==4 ]||[ $MONTH_NOW==6 ]||[ $MONTH_NOW==9 ]||[ $MONTH_NOW==11 ] ; then
NEXT_WAKEUP=`date -d "$YEAR_NOW-$(($MONTH_NOW+1))-31 23:59:59" +%s`
MAX_POWEROFF_TIME=$(( ($NEXT_WAKEUP-`date +%s`) / 60 ))
else
# sonst schlafe genau einen Monat.
MAX_POWEROFF_TIME=44640
fi
fi
# calculate, at what time the machine should be powered on
NOW=$((`date +%s`))
if [ $MAX_POWEROFF_TIME -eq 0 ] ; then
WAKEUPTIME=$1
else
WAKEUPTIME=$(( $NOW + $MAX_POWEROFF_TIME * 60 ))
if [ $WAKEUPTIME -gt $1 ] && [ $1 -gt 0 ] ; then
WAKEUPTIME=$1
fi
fi
if [ $WAKEUPTIME -gt 0 ] ; then
MIN_WAKEUPTIME=$(( $NOW + 10 * 60 + 1 ))
if [ $MIN_WAKEUPTIME -gt $WAKEUPTIME ] ; then
MINUTEN=$(( ($WAKEUPTIME - $NOW) / 60 ))
echo "svdrpsend.pl MESG 'Aufnahme: \"$4\" in $MINUTEN Minuten.'"| at now
sleep 1
echo "svdrpsend.pl MESG 'Shutdown abgebrochen.'"| at now
exit 1
fi
VAR="-s $WAKEUPTIME $NVRAM_OPT ${NVRAM_CONFIG:+-C $NVRAM_CONFIG} ${NVRAM_IWNAME:+-I $NVRAM_IWNAME}"
# sudo $VDRSCRIPTDIR/vdr2root nvram-wakeup -d
sudo $VDRSCRIPTDIR/vdr2root nvram-wakeup "$VAR"
sudo $VDRSCRIPTDIR/vdr2root lilo
sudo $VDRSCRIPTDIR/vdr2root reboot
exit 0
else
# falls wakeuplan nur mit dem poweroff-kernel funktioniert
echo ""
sudo $VDRSCRIPTDIR/vdr2root nvram-wakeup -d
# sudo $VDRSCRIPTDIR/vdr2root lilo
# sudo $VDRSCRIPTDIR/vdr2root reboot
# exit 0
fi
fi
echo "svdrpsend.pl MESG 'Shutdown now!'"| at now
#### hardware wakeup-script
if [ "$HW_WAKEUP" = "1" ] ; then
. /etc/vdr/vdr-addon-hw-wakeup.conf
# Defaults:
[ -z "$ENABLED" ] && export ENABLED="yes"
[ -z "$WAKEUP_PORT" ] && export WAKEUP_PORT="/dev/ttyS0"
[ -z "$KILL_LIRC" ] && export KILL_LIRC="yes"
[ -z "$KILL_LCDD" ] && export KILL_LCDD="no"
[ -z "$MIN_PRE_TIMER" ] && export MIN_PRE_TIMER=5
[ -z "$MAX_POWEROFF_TIME" ] && export MAX_POWEROFF_TIME=0
[ -z "$MAX_TRY_AGAIN" ] && export MAX_TRY_AGAIN=0
#For testing
#MAX_POWEROFF_TIME=60
#DAILY_TIMER_STD=4
#DAILY_TIMER_MIN=30
#KILL_LIRC="no"
#KILL_LCDD="no"
# Const
NEVER=2147483647
if [ $MAX_TRY_AGAIN -gt 0 ]
then
# another script wants to retry shutdown later
logger -t vdr-addon-hw-wakeup "hw-wakeup not called - shutdown suspended by other script"
EXITCODE=0
else
if [ $ENABLED = "no" ]
then
# vdr-addon-hw-wakeup is disabled, so just exit
logger -t vdr-addon-hw-wakeup "hw-wakeup functionality is disabled"
EXITCODE=0
else
# calculate, at what time the machine should be powered on
# VDR-Timer
NOW=$((`date +%s`))
echo "NOW " `date -d "1970-01-01 $NOW seconds UTC" "+%H:%M %d.%m.%Y"` $NOW
if [ -z $1 ] [ $1 -eq 0 ]
then
NEXTTIMER=$NEVER
else
NEXTTIMER=`expr $1 - $MIN_PRE_TIMER \* 60`
fi
echo "TIMER " `date -d "1970-01-01 $NEXTTIMER seconds UTC" "+%H:%M %d.%m.%Y"` $NEXTTIMER
# Daily Timer
if [ -z $DAILY_TIMER_STD ] [ -z $DAILY_TIMER_MIN ]
then
NEXTDAILYTIMER=$NEVER
else
TODAY=`date +"%m/%d/%Y"`
NEXTDAILYTIMER=`date -d "$TODAY $DAILY_TIMER_STD:$DAILY_TIMER_MIN" +%s`
if [ $NOW -gt $NEXTDAILYTIMER ]
then
NEXTDAILYTIMER=`expr $NEXTDAILYTIMER + 86400`
fi
fi
echo "DAILYTIMER " `date -d "1970-01-01 $NEXTDAILYTIMER seconds UTC" "+%H:%M %d.%m.%Y"` $NEXTDAILYTIMER
# MaxPowerOff Time
if [ $MAX_POWEROFF_TIME -eq 0 ]
then
WAKEUPTIME=$NEVER
else
WAKEUPTIME=`expr $NOW + $MAX_POWEROFF_TIME \* 60`
fi
# echo "MAXPOWEROFF " `date -d "1970-01-01 $WAKEUPTIME seconds UTC" "+%H:%M %d.%m.%Y"` $WAKEUPTIME
# Who comes first?
if [ $WAKEUPTIME -gt $NEXTTIMER ] && [ $NEXTDAILYTIMER -gt $NEXTTIMER ]
then
WAKEUPTIME=$NEXTTIMER
else
if [ $WAKEUPTIME -gt $NEXTDAILYTIMER ]
then
WAKEUPTIME=$NEXTDAILYTIMER
fi
fi
# echo "WAKEUPTIME " `date -d "1970-01-01 $WAKEUPTIME seconds UTC" "+%H:%M %d.%m.%Y"` $WAKEUPTIME
if [ $WAKEUPTIME -eq $NEVER ]
then
WAKEUPSTR="00000000"
else
WAKEUPSTR=`date -d "1970-01-01 $WAKEUPTIME seconds UTC" +%H%M%d%m`
fi
TIMESTR=`date +%H%M%S%d%m%y%w`
# Kill LIRC
if [ $KILL_LIRC = "yes" ]
then
logger -t vdr-addon-hw-wakeup "Shutting down LIRC"
/etc/init.d/lirc stop
setserial $WAKEUP_PORT uart 16550A
sleep 1
fi
# Kill LCDd
if [ $KILL_LCDD = "yes" ]
then
/etc/init.d/LCDd stop
sleep 1
echo -n -e "\317" > $WAKEUP_PORT
fi
logger -t vdr-addon-hw-wakeup "echo ATS$WAKEUPSTR > $WAKEUP_PORT"
echo ATS$WAKEUPSTR > $WAKEUP_PORT
logger -t vdr-addon-hw-wakeup "echo RTS$TIMESTR > $WAKEUP_PORT"
echo RTS$TIMESTR > $WAKEUP_PORT
EXITCODE=0
fi
fi
fi
#### ich hoffe es klappt!!!
sudo $VDRSCRIPTDIR/vdr2root lilo -R PowerOff
sudo $VDRSCRIPTDIR/vdr2root reboot
exit 0