From 3df4252b00aca3d018cd3362ad2aab9b969d9e46 Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Tue, 18 Sep 2012 10:24:15 -0500
Subject: linhes-scripts: idle.sh: Check if frontends are playing. closes #851.

Add -d option that will include mythshutdown daily wake in system busy calculation. Default excludes daily wake in system busy calculation. This is needed to use idle.sh as the PreCheck for system shutdown.

Add -w option to check for open windows. Before checking for windows was default but in some cases the user won't want the check. Adding window check as an option makes it consistant with the login check.
---
 abs/core/linhes-scripts/PKGBUILD |  6 ++--
 abs/core/linhes-scripts/idle.sh  | 63 ++++++++++++++++++++++++++++++++--------
 2 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/abs/core/linhes-scripts/PKGBUILD b/abs/core/linhes-scripts/PKGBUILD
index 4ffdc6f..6f70ebe 100644
--- a/abs/core/linhes-scripts/PKGBUILD
+++ b/abs/core/linhes-scripts/PKGBUILD
@@ -3,12 +3,12 @@
 
 pkgname=linhes-scripts
 pkgver=7
-pkgrel=48
+pkgrel=49
 pkgdesc="Various scripts that help to make LinHES, LinHES."
 arch=('i686' 'x86_64')
 license=('GPL2')
 depends=('xosd' 'cpulimit' 'screen' 'mencoder' 'tablet-encode' 'mplayer'
-         'normalize' 'handbrake-cli' 'mkvtoolnix')
+         'normalize' 'handbrake-cli' 'mkvtoolnix' 'gnu-netcat')
 url="http://linhes.org/"
 install="linhes-scripts.install"
 source=(
@@ -85,7 +85,7 @@ md5sums=('f56985b2d602e11dc1e10d3e7848b2a5'
          '3b776bbff68906ddc2f62b7e0dde3fe4'
          'a57641fb63d7bb6d92a438f1ea779a57'
          '3d0adf26280cde55a0c47188fff34826'
-         '25dd5b8ec0482d561b481bfce051d648'
+         '439026b38a9d3d27478078887639152b'
          'b2c3dd48a4abb976eda2d5fbf22a173a'
          'b527b01d119d3bc33b8fa69bdf1082bb'
          '9d077c24d102fa02e4f417d639d8b00a'
diff --git a/abs/core/linhes-scripts/idle.sh b/abs/core/linhes-scripts/idle.sh
index 70db6f0..7733b27 100755
--- a/abs/core/linhes-scripts/idle.sh
+++ b/abs/core/linhes-scripts/idle.sh
@@ -1,12 +1,14 @@
 #!/bin/bash
 
 usage () {
-    echo "Usage: $0 [-h] [-s] [-t <minutes_needed>] [-l] [-v]"
+    echo "Usage: $0 [-h] [-s] [-t <minutes_needed>] [-l] [-w] [-d] [-v]"
     echo
     echo "-h - Print this help/usage message and quit"
     echo "-s - Run silently (default is verbose)"
     echo "-t - Minutes of idle time needed (default is 20)"
     echo "-l - Check for user logins (default: false - do not check)"
+    echo "-w - Check for open windows (default: false - do not check)"
+    echo "-d - Include mythshutdown daily wake in system busy (default: daily wake is system idle)"
     echo "-v - Be more verbose for debugging"
     echo
     echo "Silent mode is recommended for use in cron jobs or scripts."
@@ -24,15 +26,19 @@ mysql_cmd () {
 # Command line argument handling
 VERBOSE=1
 LOGINS=0
+WINDOWS=0
+DAILY=0
 TIME_BEFORE=20
 TIME_AFTER=5    # Only adjustable by editing here
 
-while getopts "hslt:v" OPT ; do
+while getopts "hslwdt:v" OPT ; do
     case $OPT in
     h) usage 0 ;;
     s) VERBOSE=0 ;;
     t) TIME_BEFORE=$OPTARG ;;
     l) LOGINS=1 ;;
+    w) WINDOWS=1 ;;
+    d) DAILY=1 ;;
     v) VERBOSE=2 ;;
     *) usage 1 ;;
     esac
@@ -41,14 +47,17 @@ done
 shift $(($OPTIND - 1))
 [ -n "$*" ] && usage 1
 
-msg "Checking what MythTV is doing now or plans within $TIME_BEFORE minutes..."
+msg "Checking what MythTV is doing now or plans to within $TIME_BEFORE minutes..."
 msg
 
 /usr/bin/mythshutdown -s 1
 BUSY="$?"
 msg "mythshutdown returned $BUSY"
-# Ignore certain non-zero flag values
-BUSY=$(($BUSY & 0x2F))
+if [ "$DAILY" -eq 0 ] ; then
+    msg "  including daily wake (64) as system idle"
+    # Ignore certain non-zero flag values
+    BUSY=$(($BUSY & 0x2F))
+fi
 
 SCHEMALOCK=$(mysql_cmd "select count(*) from schemalock")
 msg "schemalock $SCHEMALOCK"
@@ -95,24 +104,54 @@ if [ "$VERBOSE" -ge 2 ] ; then
         }'
 fi
 
+# Check all frontends if they are playing
+HOSTNAMES=$(mysql_cmd "select hostname from settings where value = 'FrontendIdleTimeout'")
+PLAYING=0
+for HOST in $HOSTNAMES
+do
+    if [ "$VERBOSE" -ge 2 ]; then
+        msg "Checking if $HOST is playing recordings or videos..."
+    fi
+ncOUTPUT=$(nc $HOST 6546  << EOF
+query location
+quit
+EOF
+)
+    if [[ "$ncOUTPUT" == *"# Playback "* ]]; then
+        PLAYING=$(( $PLAYING + 1 ))
+        msg "$HOST is playing a recording or video"
+    else
+        msg "$HOST is NOT playing a recording or video"
+    fi
+done
+
+# Check for users logged in
 if [ "$LOGINS" -ge 1 ] ; then
   USERS=`/usr/bin/last | /bin/grep "still logged in"  | awk '{ print $1 }'`
   if [ -n "$USERS" ] ; then
     LOGINS=1
-    msg "The following user(s) are still logged in: ${USERS}"
+    msg "The following user(s) are still logged in:"
+    msg "${USERS}"
   else
     LOGINS=0
-    msg "No user(s) are logged in..."
+    msg "No users are logged in"
   fi
 fi
 
 # Check for open windows
-/usr/LH/bin/xwin_find.sh -q '.*(mythtv@|xterm|Firefox|Namoroka|Opera).*'
-WINDOWS="$?"
-WINDOWS=$(($WINDOWS == 0))
-msg "windows $WINDOWS"
+FOUNDWINDOWS=0
+if [ "$WINDOWS" -ge 1 ] ; then
+    /usr/LH/bin/xwin_find.sh -q '.*(mythtv@|xterm|Firefox|Namoroka|Opera|Chromium).*'
+    FOUNDWINDOWS="$?"
+    FOUNDWINDOWS=$(($FOUNDWINDOWS == 0))
+    if [ "$FOUNDWINDOWS" -eq 0 ] ; then
+        msg "no application windows open"
+    else
+        msg "one or more application windows open"
+    fi
+fi
 
-activities=$(($BUSY + $SCHEMALOCK + $JOBS + $INUSE + $UPCOMING + $LOGINS + $WINDOWS))
+activities=$(($BUSY + $SCHEMALOCK + $JOBS + $INUSE + $UPCOMING + $PLAYING + $LOGINS + $FOUNDWINDOWS))
 msg
 if [ "$activities" -eq 0 ] ; then
     msg "System is idle"
-- 
cgit v0.12

