#!/bin/sh
END=1

if [ "$2" != "" ]; then
   END=$2
fi

if [ "$1" = "VDR" ] ; then
   for i in `seq 1 15` ; do
      pidof vdr >/dev/null
      if [ "$?" = "0" ] ; then
         break
      fi
      sleep 1
   done
   pidof vdr >/dev/null
   if [ "$?" != "0" ] ; then
      echo "VDR ist nicht gestartet"
      exit
   fi
   FGC=8
else 
   for i in `seq 1 15` ; do
      FRV=`ps x | grep /usr/bin/freevo | grep /usr/bin/python`
      if [ "$FRV" != "" ] ; then
         break;
      fi
      sleep 1
   done
   if [ "$FRV" = "" ] ; then
      echo "FreeVo ist nicht gestartet"
      exit
   fi
   FGC=7
fi

SWITCHED=0

for i in `seq 1 "$END"` ; do
   if [ "$(fgconsole)" != "$FGC" ] ; then
      while [ "$(fgconsole)" != "$FGC" ] ; do
         logger -s "Switching to console $FGC"
         killall -9 chvt > /dev/null 2>&1
         chvt $FGC &
         sleep 1
      done
      SWITCHED=$(($SWITCHED+1))
   fi
   if [ ${i} -gt 10 ] && [ $SWITCHED -ge 1 ] ; then
      break
   fi
   sleep 1
done
