#!/bin/bash

# this is a generic wrapper to use with mms (My Media System)
# when you need to launch an external player that forks
# See: http://mms.kicks-ass.org/

LOGFILE=""


help ()
{
   echo "
   This is a generic wrapper for mms (My Media System)
   you can use to launch external players that fork()
      
   Usage: ${0/*\/} [OPTION1] <PROGRAM> [OPTION2]...

   OPTION1 can be one of the following:
   -h      Show this help
   -l <file>   Redirect stdout and stderr to <file>

   OPTION2 is/are passed to <PROGRAM>
            
"
}

disclaimer ()
{
   echo "
   My Media System is an application that manages, displays
   and plays media content such as videos, music, pictures,
   and more. MMS runs perfectly on anything from a Set-Top-Box
   connected to your TV-Set, to your specially tailored
   multimedia PC and HD display.

   See: http://mms.sunsite.dk/

   "
}

# we need *at least* one parameter, otherwise we
# show an error message
if [ $# -eq 0 ] ; then

   echo "Error: no executable specified"
   help
   exit  1

fi

case "$1" in
   -h)
      disclaimer
         help
      exit 0
   ;;
   
        -l)     #activate logging
      if [ -z "$2" ]; then
         echo "-l option needs a filename as argument"
         help
         exit 1
      fi
      LOGFILE="$2"
      shift
      shift
   ;;
   -*)   
      echo "Error: unknown option $1"
      help
      exit 1
   ;;
      
esac

CMD="$1"
CMDNAME="${1/*\/}"
shift
[ -n "$LOGFILE" ]&&LOGFILE=" >$LOGFILE 2>&1"

sh -c  "$CMD $* $LOGFILE"

while ((1)) ; do
   pidof $CMDNAME > /dev/null || exit    
   sleep 1
done

exit
