#!/bin/sh
VDR_DISABLED_FILE="/_config/status/.vdrdisabled"
VDR_CONSOLE=8
X_CONSOLE=7

if [ "$1" = "X" ] ; then
   echo "VDR wird disabled"
   touch $VDR_DISABLED_FILE 2> /dev/null
   chvt $X_CONSOLE
   sh /_config/bin/setscart.sh TVOUT
#   chvt  $X_CONSOLE
else
   if [ "$1" = "VDR" ] ; then
      echo "VDR wird enabled"
      rm -f $VDR_DISABLED_FILE 2> /dev/null
      chvt $VDR_CONSOLE
      sh /_config/bin/setscart.sh DVB
   else
      if [ -f $VDR_DISABLED_FILE ] ; then
         if [ "$(fgconsole)" = "$VDR_CONSOLE" ] ; then
            sleep 2
            if [ "$(fgconsole)" = "$VDR_CONSOLE" ] ; then
               rm -f $VDR_DISABLED_FILE 2> /dev/null
               sh /_config/bin/setscart.sh DVB
            fi
         fi
      else
         if [ "$(fgconsole)" = "$X_CONSOLE" ] ; then
            while [ "$(fgconsole)" = "$X_CONSOLE" ] ; do
               logger -s "Switching to console $VDR_CONSOLE"
               echo "chvt $VDR_CONSOLE" | at now
               sleep 1
            done
            sh /_config/bin/setscart.sh DVB
         fi
      fi
   fi
#   chvt  $VDR_CONSOLE
fi

exit
