#!/bin/bash

#
# Function library 
# Phill Edwards
#

# -----------------------------------------------------------------------
# Check if any important myth functions are running, such as frontend,
# transcoding, commercial flagging, nuvexporting, listings grabbing.
# -----------------------------------------------------------------------
myth_busy()
{
FRONTEND=`ps -e | grep mythfrontend | grep -v grep | wc -l | awk '{print $1}'`
COMMFLAG=`ps -e | grep mythcommflag | grep -v grep | wc -l | awk '{print $1}'`
TRANSCODE=`ps -e | grep mythtranscode | grep -v grep | wc -l | awk '{print $1}'`
TV_GRAB=`ps -e | grep tv_grab_au.sh | grep -v grep | wc -l | awk '{print $1}'`
NUVEXPORT=`ps -e | grep nuvexport | grep -v grep | wc -l | awk '{print $1}'`

if [ $FRONTEND -eq 0 ] && [ $COMMFLAG -eq 0 ] && [ $TRANSCODE -eq 0 ] && [ $TV_GRAB -eq 0 ] && [ $NUVEXPORT -eq 0 ]
then
    return 0
else
    return 1
fi
}

# -----------------------------------------------------------------------
# Check if any of the tuners are recording by getting the backend
# status xml from port 6544 and parsing the output. 
# -----------------------------------------------------------------------
myth_recording()
{
TNRNUM=1                # Init the count of number of tuners in system
while [ $TNRNUM -le 2 ]
do
   TNRSTS=`wget --quiet --output-document=- http://elm.edwards.home:6544/xml | \
   grep "<Encoder .*id\=\"$TNRNUM\"" `

   # If the state="0" in the XML then nothing's recording on that tuner.
   echo $TNRSTS | grep --quiet state\=\"0\"
   if [ $? -gt 0 ]
   then
      return 1
   fi
   TNRNUM=`expr $TNRNUM + 1`
done

return 0
}
