#!/bin/sh
### BEGIN INIT INFO
# Provides:          softcam
# Required-Start:    $local_fs
# Required-Stop:     
# Should-Start:      $time $remote_fs cardserver
# Should-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: oscam
# Description:       oscam SoftCAM, card reader and server.
### END INIT INFO



case "$1" in
start)
	wget -q "http://localhost/web/about" -O /tmp/openwebif_about.txt
    OPENATV_ACTUAL_IMAGE_VER=`cat /tmp/openwebif_about.txt|sed -n 's/<e2imageversion>\(.*\)<\/e2imageversion>/\1/p'|sed -e 's/[[:space:]]//g'`
	major_version="${OPENATV_ACTUAL_IMAGE_VER%%.*}"
	echo $major_version
	
	if [[ "$major_version" == "7" ]];then #If it is OATV v7.x
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=174; #Exit
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=103; #Go one UP
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=352; #Press OK
	else
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=174; #Exit
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=174; #Exit
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=174; #Exit
		wget -O /dev/null -q http://localhost/web/remotecontrol?command=174; #Exit
	fi
	/usr/script/oscam-update_cam.sh
	rm -fr /tmp/openwebif_about.txt
	;;
stop)
	echo -n "Stopping $DESC: "
	pkill -f oscam 2> /dev/null
	;;
restart|reload)
	$0 stop
	sleep 1
	$0 start
	;;
version)
	$CAM -V | grep 'Version\|IPv6' | sed 's/Version:[ ]*//' | sed 's/IPv6.*yes/with IPv6/g' | sed 's/IPv6.*no/IPv4-only/g' | sed ':a;N;$!ba;s/\n/ /g'
	;;
info)
	$CAM -V | grep 'Version\|IPv6' | sed 's/Version:[ ]*//' | sed 's/IPv6.*yes/with IPv6/g' | sed 's/IPv6.*no/IPv4-only/g' | sed ':a;N;$!ba;s/\n/ /g'
	;;
*)
	echo "Usage: $0 start|stop|restart"
	exit 1
	;;
esac
exit 0
