#!/bin/sh
BOXIP="http://localhost"
WGET="/usr/bin/wget"



LOG_FILE="/tmp/camstart.log"




# *******************************************************************
# CAMNAME auslesen 
# *******************************************************************

readlink -f $0 >/dev/null 2>&1
if [ $? -eq 0 ]; then
	CAM=$(basename $(readlink -f $0) | sed s#^softcam\.##)
	echo "camname 1 :$CAM " >> $LOG_FILE
else
	CAM=$(basename $0 | sed s#^softcam\.##)
	echo "camname 2 :$CAM " >> $LOG_FILE
fi
	$WGET -O -q "http://localhost/web/message?text=Cam+Start+Wait&type=1&timeout=2" /dev/null
    # timeout im wget ist die anzeigedauer , sleep stopt nur den code
	sleep 2

if [ -n "$CAM" ]; then
    echo "camname 3 check binary: $CAM" >> $LOG_FILE
    if [ -x "/usr/softcams/$CAM" ]; then
        echo "camname 4 binary OK: $CAM" >> $LOG_FILE
		
    else
        echo "Die Datei /usr/softcams/$CAM existiert nicht oder hat keine rechte." >> $LOG_FILE
        exit 1
    fi
else
    echo "Der Wert von \$CAM ist leer." >> $LOG_FILE
    exit 1
fi


remove_tmp() {
	rm -rf /tmp/*.info* /tmp/*.tmp* /tmp/*share* /tmp/*.pid* /tmp/*sbox* /tmp/oscam* /tmp/.oscam
}
echo "cam info " >> $LOG_FILE



case "$1" in
start)
	
    ulimit 1024
    remove_tmp
	echo "cam start " >> $LOG_FILE

	/usr/softcams/$CAM --daemon --config-dir /etc/tuxbox/config --pidfile /tmp/cam.pid --restart 2
	

	# Setze den Statuscode auf fehler setzen , bei 0 befehl ok , also abfrage groesser null fuer fehler
	#STATUS=1

	# ****************************************************************************************************	
    # hier wird die oscam gestartet in usr/bin/camname und hier sagt man ihm wo seine config dateien sind 
    # ****************************************************************************************************
   
    #CAM_UP_OUTPUT="$(/usr/softcams/$CAM --daemon --config-dir /etc/tuxbox/config --pidfile /tmp/cam.pid --restart 2 --utf8  2>&1)" >> $LOG_FILE
	#STATUS=$?
	#echo "Statuscode: $STATUS" >> $LOG_FILE
	#befehl="nice -n -20 /usr/softcams/$CAM --daemon --config-dir /etc/tuxbox/config --pidfile /tmp/cam.pid --restart 2 --utf8  2>&1"
	#echo "cam start befehl : $befehl " >> $LOG_FILE
	#echo "cam start gestartet befehl " >> $LOG_FILE
	#echo "CAM_UP_OUTPUT: $CAM_UP_OUTPUT" >> $LOG_FILE
    #echo "cam start gestartet befehl CAM_UP_OUTPUT " >> $LOG_FILE
	#echo "Fehler beim Starten der CAM. Exit-Code: $STATUS. Fehlergrund: $CAM_UP_OUTPUT" >> /tmp/OscamStartError.log
	#echo "cam start ende ausgabe test " >> $LOG_FILE
	#ERROR_MESSAGE=""
	
	
	
	#if [ $STATUS -gt 0 ]; then
	#	echo "cam start error message " >> $LOG_FILE
	#	ERROR_MESSAGE="Error: $CAM_UP_OUTPUT"
	#	echo "$ERROR_MESSAGE" >> $LOG_FILE
	#	ERROR="Error: $CAM_UP_OUTPUT"
	#	MESSAGE="Fehler beim Starten der CAM. Exit-Code: $STATUS. Fehlergrund: $CAM_UP_OUTPUT"
	#	$WGET -O -q "http://localhost/web/message?text=${MESSAGE// /%20}&type=0&timeout=5" /dev/null
	#	sleep 3
	#	$WGET -O -q "http://localhost/web/message?text=Cam+not+Startet+Error!!!+---+logfile+---+tmp/OscamStartError.log&type=1&timeout=10" /dev/null
	#	sleep 10
		
		
	#	echo "cam start error mesage ausgabe ende " >> $LOG_FILE
	
	#else	
	#	$WGET -O -q "http://localhost/web/message?text=Cam+Start+OK&type=1&timeout=5" /dev/null
	#	sleep 5
	
	#fi
	
	echo "cam start ende" >> $LOG_FILE
	;;
stop)
	echo "cam stop befehl " >> $LOG_FILE
    killall -9 ${CAM} 2>/dev/null
    remove_tmp
    ;;
restart | reload)
	echo "cam reload befehl " >> $LOG_FILE
    $0 stop
    sleep 1
    $0 start
    ;;
version)
    echo "${CAM}"
    ;;
info)
    echo "${CAM}"
    ;;
*)
    echo "Usage: $0 start|stop|restart"
	echo "cam exit 1 " >> $LOG_FILE
    exit 1
    ;;
esac
exit 0
