#!/bin/bash
# $Id: vdrshutdown,v 1.21 2004/08/23 01:22:35 bistr-o-math Exp $
#
# adapted to SUSE scripts and packages by <gernot@hillier.de>

NVRAMCMD=/usr/bin/nvram-wakeup
SVDRPCMD=/usr/bin/vdr13-svdrpsend.pl
LOGGER=/bin/logger
CHECKSCRIPT=/bin/true

next_timer="$1"         # time in seconds of next timer
until_timer="$2"        # time in seconds until next timer (may be negative)
timer_channel="$3"      # channel recorded by next timer
timer_fname="$4"        # filename for next recording
manual_shutdown="$5"    # 1 if shutdown reqested by user

test -f /etc/sysconfig/vdr13  && . /etc/sysconfig/vdr13

if [ "$VDR_SHUTDOWN" = "fast" ]; then
HALT_OPTIONS="-f"
fi

#################
##
## For this script to work as user "vdr", you need to add the following
## lines in "visudo":
##
## vdr   ALL=NOPASSWD: /sbin/halt
## vdr   ALL=NOPASSWD: /sbin/reboot
## vdr   ALL=NOPASSWD: /sbin/modprobe nvram
## vdr   ALL=NOPASSWD: /usr/bin/nvram-wakeup
## vdr   ALL=NOPASSWD: /usr/sbin/grubonce
##
## If you need to reboot your machine, the following 2-line section in
## /boot/grub/menu.lst is also required:
##
## title Shutdown
##    halt

#################
##  if the script $CHECKSCRIPT thinks that we should     shutdown,
##     it must not print anything on stdout.
##  if it                      thinks that we should NOT shutdown,
##     it must print a one-line message on stdout describing the reason.
##
##  note that all parameters passed to vdrshutdown will also be passed to $CHECKSCRIPT
##  so it might use them (e.g. $5 -eq 0 below)
##
##  it might look like this:
##  --------------------------
##    #!/bin/bash
##  
##    pgrep 2divx >/dev/null 2>&1       && { echo Divx-Conversion is still running;  exit; }
##
##    test -a /some/file                && { echo /some/file exists;                 exit; }
##
##    USERCOUNT=`who | wc -l`;
##    test $5 -eq 0 -a $USERCOUNT -gt 0 && { echo "$USERCOUNT users are logged in."; exit; }
##
##  --------------------------
##
##  now if $CHECKSCRIPT exists and is executable, the message will be passed through
##  SVDRP to the tv screen.
##

test -x $CHECKSCRIPT && {
     msg=`$CHECKSCRIPT "$@"` 
     test "$msg" != "" && {
          $SVDRPCMD MESG $msg &
          $LOGGER -t `basename $0` "shutdown aborted: $msg"
          exit 1
     }
}

#################
## Do EPG update for analog VDR if needed
##
# max. age of Nextview DB before we update in days
MAX_AGE_NEXTVIEWDB=3
# where Nextview EPG will store its database
PATH_TO_NEXTVIEWDB="/var/tmp/nxtvdb/"

current_database=`find ${PATH_TO_NEXTVIEWDB} -type f -name "nxtvdb*" -mtime -$MAX_AGE_NEXTVIEWDB`
if [ "$current_database" = "" ]; then
	$SVDRPCMD MESG "Nextview DB older than $MAX_AGE_NEXTVIEWDB days. Updating..." &
	$0.epg $@ &
	# we will be called again by vdrshutdown.epg so shutdown will continue later
	exit 0
fi

#################

# Add here needed options like --configfile=...
# (read 'man nvram-wakeup' and 'man nvram-wakeup.conf' for more details)
sudo /sbin/modprobe nvram
sudo $NVRAMCMD --syslog --settime $1 

# if you are going to use the set_timer script instead of nvram-wakeup,
# comment out the line above and uncomment the following line.
# (read the comments inside the script for more details)
# $PATH_TO_SET_TIMER/set_timer $1 $2


case $PIPESTATUS in
     0) # all went ok - new date and time set
        sudo /sbin/halt $HALT_OPTIONS
        EXITSTATUS=0
        ;;
     1) # all went ok - new date and time set.
        #
        # *** but we need to reboot. ***
        #
        # for some boards this is needed after every change.
        #
        # for some other boards, we only need this after changing the
        # status flag, i.e. from enabled to disabled or the other way.
        
        sudo /usr/sbin/grubonce `sudo /usr/sbin/grubonce | grep Shutdown | sed -e s/:.*//g`

	sudo /sbin/reboot $HALT_OPTIONS      
        EXITSTATUS=0
        ;;
     2) # something went wrong
        # don't do anything - just exit with status 1
        EXITSTATUS=1
        ;;
esac

# exit with 0 if everything went ok.
# exit with 1 if something went wrong.
exit $EXITSTATUS
