#!/bin/sh
port="$2"
path="/usr/lib/enigma2/python/Plugins/Extensions/sispmctl"
file="/tmp/sispmctlstatus.txt"
e2settings="/etc/enigma2/settings"
WebIFport=`cat $e2settings | grep config.plugins.sispmctl.WebIFport | cut -d "=" -f2`
Time1=`cat $e2settings | grep config.plugins.sispmctl.socket1delay | cut -d "=" -f2`
Time2=`cat $e2settings | grep config.plugins.sispmctl.socket2delay | cut -d "=" -f2`
Time3=`cat $e2settings | grep config.plugins.sispmctl.socket3delay | cut -d "=" -f2`
Time4=`cat $e2settings | grep config.plugins.sispmctl.socket4delay | cut -d "=" -f2`
aon1=`cat $e2settings |grep config.plugins.sispmctl.socket1auton | cut -d "=" -f2`
aon2=`cat $e2settings |grep config.plugins.sispmctl.socket2auton | cut -d "=" -f2`
aon3=`cat $e2settings |grep config.plugins.sispmctl.socket3auton | cut -d "=" -f2`
aon4=`cat $e2settings |grep config.plugins.sispmctl.socket4auton | cut -d "=" -f2`
aoff1=`cat $e2settings |grep config.plugins.sispmctl.socket1autoff | cut -d "=" -f2`
aoff2=`cat $e2settings |grep config.plugins.sispmctl.socket2autoff | cut -d "=" -f2`
aoff3=`cat $e2settings |grep config.plugins.sispmctl.socket3autoff | cut -d "=" -f2`
aoff4=`cat $e2settings |grep config.plugins.sispmctl.socket4autoff | cut -d "=" -f2`
    case "$1" in
    'toggle')
            s1=`$path/sispmctl -g $port | grep on`
            if [ -z "$s1" ]
                    then
                            $path/sispmctl -o $port
                    else
                            $path/sispmctl -f $port
            fi
    ;;
    'status')
	echo > $file
	for n in $(seq 4);do
        s=`$path/sispmctl -g $n | grep on`
        if [ -z "$s" ]
        then
        echo 0 >> $file
        else
        echo 1 >> $file
        fi
        done
    ;;
    'WebIFstart')
		[ -z $(pidof sispmctl) ] && $path/sispmctl -q -p $WebIFport -l
    ;;	
    'WebIFstop')
            s1=`pidof sispmctl|head -n1`
            if [ "$s1" ]
                    then
                        killall sispmctl
            fi
    ;;	
    'WebIFautostart')
            s1=`cat $e2settings | grep config.plugins.sispmctl.WebIF=1`
            if [ "$s1" ]
                    then
                        /etc/init.d/sispmctl WebIFstart
            fi
	;;
	'start')
		twu=`$path/timerwakeup.py`
			
				if ( [ $aon1 -gt 1 ] ) && ( [ `grep socket1ignev=1 $e2settings` ] || [ "$twu" != "True" ] ) 
				then
						sleep $Time1 && $path/sispmctl -o1 &
				fi
				if ( [ $aon2 -gt 1 ] ) && ( [ `grep socket2ignev=1 $e2settings` ] || [ "$twu" != "True" ] )
				then
						sleep $Time2 && $path/sispmctl -o2 &
				fi
				if ( [ $aon3 -gt 1 ] ) && ( [ `grep socket3ignev=1 $e2settings` ] || [ "$twu" != "True" ] )
				then
						sleep $Time3 && $path/sispmctl -o3 &
				fi
				if ( [ $aon4 -gt 1 ] ) && ( [ `grep socket4ignev=1 $e2settings` ] || [ "$twu" != "True" ] )
				then
						sleep $Time4 && $path/sispmctl -o4 &
				fi
			
			/etc/init.d/sispmctl WebIFautostart
    ;;		
	'stop')
				if [ $aoff1 -gt 1 ]
				 then
						sleep $Time1
						$path/sispmctl -f1
				fi
				if [ $aoff2 -gt 1 ]
				 then
						sleep $Time2
						$path/sispmctl -f2
				fi
				if [ $aoff3 -gt 1 ]
				 then
						sleep $Time3
						$path/sispmctl -f3
				fi
				if [ $aoff4 -gt 1 ]
				 then
						sleep $Time4
						$path/sispmctl -f4
				fi
    ;;		
    esac
    exit 0
