Powersaving Script


 * 1) !/bin/sh
 * 2) This is a suspend extension for TV-Viewer which works as suspendhook within pm-utils.
 * 3) The name of this file is case sensitive and has to wear the '00aa_' at the beginning.
 * 4) It is executable and should be placed in: /etc/pm/sleep.d/00aa_tvcard_tv-viewer
 * 5) Please verify that your kernel modules for the tv card support s2ram and s2disk or make pm-utils load and unload
 * 6) the corresponding driver. Test by issuing pm-suspend, when the card is not used after a fresh reboot.
 * 7) You can find informations about this in the pm-utils documentation. Or ath the
 * 8) howto section in tv-viewer wiki.
 * 9) You can find some logoutput in /var/log/pm-suspend.log
 * 10) http://tv-viewer.sourceforge.net/
 * 11) ( refer to: "man pm-utils" and look at the structure: "ls /usr/lib/pm-utils/sleep.d/"
 * 1) howto section in tv-viewer wiki.
 * 2) You can find some logoutput in /var/log/pm-suspend.log
 * 3) http://tv-viewer.sourceforge.net/
 * 4) ( refer to: "man pm-utils" and look at the structure: "ls /usr/lib/pm-utils/sleep.d/"
 * 1) http://tv-viewer.sourceforge.net/
 * 2) ( refer to: "man pm-utils" and look at the structure: "ls /usr/lib/pm-utils/sleep.d/"
 * 1) ( refer to: "man pm-utils" and look at the structure: "ls /usr/lib/pm-utils/sleep.d/"

. "${PM_FUNCTIONS}"
 * 1) Load some basic pm-utils functions:

tvuser="$( stat -c "%U" /home/*/.tv-viewer/tmp/lockfile.tmp )"; #
 * 1) Get the username that has tv-viewer:

buildnr="$( grep version /home/$tvuser/.tv-viewer/log/tvviewer.log | tail -n 1 | awk -F \space: '{ print $6 }' )";
 * 1) Version von tv-viewer (Planned for backward compatibility!):
 * 2) buildnr="0.8.2b1";

timer1="sleep 1"; timer2="sleep 3";
 * 1) Set this timing if the system is under heavy load, or if the modules need longer time to be more graceful on resume!:

tvpath="$( whereis tv-viewer_lirc | awk -F \space: '{ print $2 }' | awk -F \/tv-viewer_lirc '{ print $1 }' )";
 * 1) Get the path to tv-viewer executables:
 * 2) tvpath="/usr/local/bin";

test -L /home/*/.tv-viewer/tmp/lockfile.tmp || { echo "TV-Viewer $buildnr is not running! No need to run hooks!"; if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }
 * 1) Only run hooks, if tv-viewer is actually runnung! exit 0=success exit 6 =fail;

touch /home/$tvuser/.tv-viewer/tmp/tv-viewer_plays touch /home/$tvuser/.tv-viewer/tmp/tv-viewer_done statusfile="/home/$tvuser/.tv-viewer/tmp/tv-viewer_plays"; statusfile2="/home/$tvuser/.tv-viewer/tmp/tv-viewer_done";
 * 1) Check/Create the statusfiles for this script;

ask="$( cat $statusfile )"; askD="$( cat $statusfile2 )";
 * 1) Get the bool from statusfile:

ispid="$( grep "MPlayer process id" /home/$tvuser/.tv-viewer/log/videoplayer.log | tail -n 1 | awk -F \blank: '{ print $8}' )"; pidisproc="$( ps ax -o "%p" | grep $ispid | awk -F \blank: '{ print $1 }' )"; pidisproc2="$( ps ax -o "%p" | grep $ispid | awk -F \blank: '{ print $2 }' )"; func_pidcheck { if [ "$ispid" = "$pidisproc" -o "$ispid" = "$pidisproc2" ]; then echo "need" > $statusfile; else echo "noneed" > $statusfile; fi; ask="$( cat $statusfile )"; askD="$( cat $statusfile2 )"; }
 * 1) The pid of mplayer which was started by tv-viewer from tv-viewer log as variable:
 * 1) Grep this PID from actual process list and make it a variable:
 * 1) This function compares the gotten PID information. So if mplayer is running from tv-viewer, it writes "need" to the statusfile.
 * 2) If Mplayer was not started, it writes "noneed to the statusfile".

func_gn8mo { su $tvuser -c "$tvpath/tv-viewer_lirc teleview" echo "2" > "$statusfile2" echo "Halt playback? [YES]" }
 * 1) This function is executed to stop playback if tv-viewer needs to be stopped. "need = need";

suspend_viewer { echo "Suspend function for TV-Viewer $buildnr!" if [ "$ask" = "need" ]; then func_gn8mo; elif [ "$ask" = "noneed" ]; then echo "Halt playback? ... [NO]" & echo "0" > $statusfile2; else echo "Error ins suspend detection! ... [Error]"; fi; }
 * 1) This is the main suspend function. (That is for the case, that the computer goes to sleep.):
 * 1) Some output to logfile:
 * 1) If need is need, stop playback.
 * 2) If need is noneed, give some output to log and write "0" to statusfile2;
 * 3) If both cases fail, give a Error that no condition was true.

old_version { su $tvuser -c "$tvpath/tv-viewer_lirc station_down" $timer1 su $tvuser -c "$tvpath/tv-viewer_lirc station_up" $timer2 su $tvuser -c "$tvpath/tv-viewer_lirc teleview" echo "1" > $statusfile2 echo "Done" > $statusfile } new_version { su $tvuser -c "$tvpath/tv-viewer_lirc station_prior" $timer1 su $tvuser -c "$tvpath/tv-viewer_lirc station_next" $timer2 su $tvuser -c "$tvpath/tv-viewer_lirc teleview" echo "1" > $statusfile2 echo "Done" > $statusfile }
 * 1) The final resume commands for tv-viewer to execute after a standby process:

resume_viewer { echo "Resume function for TV-Viewer $buildnr!" if [ "$askD" = "0" ]; then echo "Resume playback? ... [NO]"; elif [ "$buildnr" = "0.8.1" -a "$askD" = "2" ]; then echo "Resume $buildnr playback? ... [YES]" & old_version; elif [ "$buildnr" = "0.8.2b1" -a "$askD" = "2" ]; then echo "Resume $buildnr playback? ... [YES]" & new_version; elif [ "$buildnr" = "0.8.2b2" -a "$askD" = "2" ]; then echo "Resume $buildnr playback? ... [YES]" & new_version; else echo "Error in resume detection! ... [Error] "; fi; }
 * 1) This is the main resume function. (That is for the case, that the computer wakes up.)
 * 2) Need to get informations from statusfiles in order to know what happened before.
 * 3) Conditions which are written to the both statusfiles can be:
 * 4) - statusfile contains "need" or "noneed".
 * 5) - statusfile contains "Done" if a prior suspend process took place.
 * 6) statusfile2 contains 0 1 2:
 * 7) - 0 means, did / do not halt playback
 * 8) - 1 means, on last resume depending on version one of the hooks was executed.
 * 9) - 2 means, playback was stopped when computer went to sleep.
 * 1) Some output for the log:
 * 1) And Action! <--- PROBLEM! (geht nicht!)

case "$1" in	hibernate|suspend)		func_pidcheck && suspend_viewer		;;	thaw|resume) resume_viewer ;;	*) exit $NA		;; esac
 * 1) Execute these functions in pm-utils defined cases;