#!/bin/sh

if [ -e /usr/sundtek/mediasrv.bz2 ] && [ ! -e /tmp/mediasrv ]; then
  bzip2 -cd /usr/sundtek/mediasrv.bz2 > /tmp/mediasrv
  chmod 777 /tmp/mediasrv
fi
if [ -e /usr/sundtek/mediaclient.bz2 ] && [ ! -e /tmp/mediaclient ]; then
  bzip2 -cd /usr/sundtek/mediaclient.bz2 > /tmp/mediaclient
  chmod 777 /tmp/mediaclient
fi
if [ -f /usr/sundtek/mediasrv ] || [ -e /usr/sundtek/mediasrv.bz2 ]; then
  DRIVERPATH=/usr/sundtek
else
  DRIVERPATH=/opt/bin
fi

LINE="--------------------------------------"
# end

if [ -e /bin/nice ]; then
	NICECMD="nice -n -15 "
else
	NICECMD=""
fi

re_enigma () {
	if [ ! -e /usr/bin/enigma2-environment ] && [ "$dorestart" = "restart" ]; then
		echo $LINE
		echo "Enigma restart"
		echo $LINE
		init 4
		sleep 2
		init 3
	fi
}

dorestart=$2

usepsax=1

ps ax > /dev/null 2>&1
if [ "$?" != "0" ]; then
  usepsax=0   
fi

if [ -f /usr/bin/enigma2.sh ] && [ "`grep libmediaclient /usr/bin/enigma2.sh -c`" = "0" ]; then
	sed -i 's/LIBS=\/usr\/lib\/libopen.so.0.0.0/LIBS="\/opt\/lib\/libmediaclient.so \/usr\/lib\/libopen.so.0.0.0"/g' /usr/bin/enigma2.sh
	sed -i 's/LIBS="$LIBS \/usr\/lib\/libopen.so/LIBS="$LIBS \/opt\/lib\/libmediaclient.so \/usr\/lib\/libopen.so/g' /usr/bin/enigma2.sh
fi



case "$1" in
     start_c)
        echo $LINE
        echo "DVB-C Tuner start"
        echo $LINE
        if [ "$usepsax" = "1" ]; then
	  if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; sleep 1; fi
        else
	  if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; sleep 1; fi
        fi
	#wait for devices will block until all devices are initialized
        $NICECMD $DRIVERPATH/mediasrv -d --no-nodes --wait-for-devices
	sleep 2
        $DRIVERPATH/mediaclient --loglevel=off

	FRONTEND=`$DRIVERPATH/mediaclient -e |grep -A 5 '\[DVB' -m 1 | grep -m 1 FRONTEND: | sed 's#.*/dev#/dev#g'`

	if [ "$FRONTEND" = "" ]; then
		echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" No Sundtek device Detected" >> /var/log/mediasrv.log
		exit 1 
	fi
	if [ -e /etc/sundtek.conf ] && [ "`grep -c dreambox_support_fe1 /etc/sundtek.conf`" != "0" ]; then
           echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" Using /etc/sundtek.conf for deviceconfiguration" >> /var/log/mediasrv.log
        else
           $DRIVERPATH/mediaclient -D DVBC -d $FRONTEND
           $DRIVERPATH/mediaclient --enabledreambox=$FRONTEND
	fi
        if [ "$usepsax" = "1" ]; then
          if [ `ps ax | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
          else
             echo $LINE
          fi
        else
          if [ `ps | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
          else
             echo $LINE
          fi
        fi
        ;;
    start_t2)
        echo $LINE
        echo "DVB-T2 Tuner start"
        echo $LINE
        if [ "$usepsax" = "1" ]; then
	   if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        else
	   if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        fi
        $NICECMD $DRIVERPATH/mediasrv -d --no-nodes --wait-for-devices
	sleep 2
        $DRIVERPATH/mediaclient --loglevel=off
	FRONTEND=`$DRIVERPATH/mediaclient -e |grep -A 5 '\[DVB' -m 1 | grep -m 1 FRONTEND: | sed 's#.*/dev#/dev#g'`

	if [ "$FRONTEND" = "" ]; then
		echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" No Sundtek device Detected" >> /var/log/mediasrv.log
		exit 1 
	fi
	if [ -e /etc/sundtek.conf ] && [ "`grep -c dreambox_support_fe1 /etc/sundtek.conf`" != "0" ]; then
           echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" Using /etc/sundtek.conf for deviceconfiguration" >> /var/log/mediasrv.log
        else
           $DRIVERPATH/mediaclient -D DVBT2 -d $FRONTEND 
           $DRIVERPATH/mediaclient --enabledreambox=$FRONTEND
	fi
        if [ "$usepsax" = "1" ]; then
           if [ `ps ax | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        else
           if [ `ps | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        fi
        ;;
    start_t)
        echo $LINE
        echo "DVB-T Tuner start"
        echo $LINE
        if [ "$usepsax" = "1" ]; then
	   if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        else
	   if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        fi
        $NICECMD $DRIVERPATH/mediasrv -d --no-nodes --wait-for-devices
	sleep 2
        $DRIVERPATH/mediaclient --loglevel=off
	FRONTEND=`$DRIVERPATH/mediaclient -e |grep -A 5 '\[DVB' -m 1 | grep -m 1 FRONTEND: | sed 's#.*/dev#/dev#g'`
	if [ "$FRONTEND" = "" ]; then
	FRONTEND=`$DRIVERPATH/mediaclient -e |grep -A 5 '\[ATSC' -m 1 | grep -m 1 FRONTEND: | sed 's#.*/dev#/dev#g'`
	fi

	if [ "$FRONTEND" = "" ]; then
		echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" No Sundtek device Detected" >> /var/log/mediasrv.log
		exit 1 
	fi
	if [ -e /etc/sundtek.conf ] && [ "`grep -c dreambox_support_fe1 /etc/sundtek.conf`" != "0" ]; then
           echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" Using /etc/sundtek.conf for deviceconfiguration" >> /var/log/mediasrv.log
        else
           $DRIVERPATH/mediaclient -D DVBT -d $FRONTEND 
           $DRIVERPATH/mediaclient --enabledreambox=$FRONTEND
	fi
        if [ "$usepsax" = "1" ]; then
           if [ `ps ax | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        else
           if [ `ps | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        fi
        ;;
    start_s2)
        echo $LINE
        echo "DVB-S/S2 Tuner start"
        echo $LINE
        if [ "$usepsax" = "1" ]; then
	   if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        else
	   if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1 ;fi
        fi
        $NICECMD $DRIVERPATH/mediasrv -d --no-nodes --wait-for-devices
	sleep 2
	FRONTEND=`$DRIVERPATH/mediaclient -e |grep -A 5 '\[DVB' -m 1 | grep -m 1 FRONTEND: | sed 's#.*/dev#/dev#g'`

	if [ "$FRONTEND" = "" ]; then
		echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" No Sundtek device Detected" >> /var/log/mediasrv.log
		exit 1 
	fi
	if [ -e /etc/sundtek.conf ] && [ "`grep -c dreambox_support_fe1 /etc/sundtek.conf`" != "0" ]; then
           echo `date +"%Y-%m-%d %H:%M:%S [$$]"`" Using /etc/sundtek.conf for deviceconfiguration" >> /var/log/mediasrv.log
	else
          $DRIVERPATH/mediaclient --loglevel=off
          $DRIVERPATH/mediaclient --enabledreambox=$FRONTEND
	fi
        if [ "$usepsax" = "1" ]; then
           if [ `ps ax | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        else
           if [ `ps | grep enigma2 | grep -v boblight-enigma2 | grep -v grep | wc -l` -gt 0 ];then
                re_enigma
           else
              echo $LINE
           fi
        fi
        ;;
     start_net)
           echo $LINE
           if [ "$usepsax" = "1" ]; then
	      if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; fi
           else
	      if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; fi
           fi
           # new as per 12. oct 2013, network devices are also part of wait-for-devices now
           $NICECMD $DRIVERPATH/mediasrv -d --no-nodes --wait-for-devices
	   sleep 2 
        ;;
     start_netdvb)
           echo $LINE
           echo "DVB Net Tuner start"
           echo $LINE
	   $DRIVERPATH/mediaclient --shutdown
           if [ "$usepsax" = "1" ]; then
	      if [ `ps ax | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; fi
           else
	      if [ `ps | grep mediasrv | grep -v grep | wc -l` -gt 0 ];then killall -9 mediasrv >/dev/null 2>&1; fi
           fi
           $NICECMD $DRIVERPATH/mediasrv -d --no-nodes
	   sleep 5
        ;;
     stop)
        echo $LINE
        echo "DVB Tuner $1"
        echo $LINE
        killall -9 mediasrv >/dev/null 2>&1
        rmmod usbkhelper 2>&1
        ;;
     info)
        echo $LINE
        echo  "DVB Tuner $1"
        echo $LINE
        $DRIVERPATH/mediaclient -e
        cat /proc/bus/nim_sockets
        ;;
     autostart_c | enable_c)
        echo $LINE
        echo "DVB-C Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_C_Tuner_starten.sh /etc/rc3.d/S99tuner.sh
	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     autostart_t | enable_t)
        echo $LINE
        echo "DVB-T Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_T_Tuner_starten.sh /etc/rc3.d/S99tuner.sh
	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     autostart_t2 | enable_t2)
        echo $LINE
        echo "DVB-T2 Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_T2_Tuner_starten.sh /etc/rc3.d/S99tuner.sh
	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     autostart_s2 | enable_s2)
        echo $LINE
        echo "DVB-S/S2 Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_S2_Tuner_starten.sh /etc/rc3.d/S99tuner.sh
	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     autostart_net | enable_net)
        echo $LINE
        echo "DVB-C Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_C_netTuner_starten.sh /etc/rc3.d/S99tuner.sh
	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     autostart_netdvb | enable_netdvb)
        echo $LINE
        echo "DVB-Net Tuner $1"
        echo $LINE
        ln -sfn /usr/script/DVB_netTuner_starten.sh /etc/rc3.d/S99tuner.sh

	if [ -e /lib/systemd/system/enigma2.service ]; then
	   if [ "`grep -c sundtek /lib/systemd/system/enigma2.service`" = "0" ]; then
	     sed -i 's/enigma2-environment.service/enigma2-environment.service sundtek.service/g' /lib/systemd/system/enigma2.service
	   fi
	   cp /usr/sundtek/sundtek.service /lib/systemd/system/
	   systemctl daemon-reload
	fi
        ;;
     noautostart | disable)
        echo $LINE
        echo "DVB-Tuner autostart disable"
        echo $LINE
	if [ -e /lib/systemd/system/sundtek.service ]; then
		rm -rf /lib/systemd/system/sundtek.service
		if [ -e /lib/systemd/system/enigma2.service ]; then
			sed -i 's/ sundtek.service//g' /lib/systemd/system/enigma2.service
		fi
	fi
        rm /etc/rc3.d/S99tuner.sh > /dev/null 2>&1
        ;;
     *)
        echo "Usage: $0 start_c|start_t|stop|info|enable_c|enable_t|enable_s2|enable_net|enable_netdvb|disable"
        exit 1
        ;;
esac

exit 0

