#!/bin/sh
recording=$2
update=$3

out="/dev/null"
if grep -q 'ttyS0' /var/tuxbox/boot/boot.conf; then
	out="/dev/console"
fi

case "$1" in

emu_stop)
	if pidof camd2 > /dev/null ; then
		killall camd2
		rm /tmp/camd.socket
	fi	
	if pidof multicamd2 > /dev/null ; then
		killall multicamd2
		rm /tmp/camd.sock04
	fi	
	if pidof scam > /dev/null ; then
		killall scam
		rm /tmp/camd.socket
	fi
	if [ "update" = "$update" ] ;then
		if [ -e /tmp/.rdgc ] ; then
			killall rdgc
			killall rdgd
			rm /tmp/rdgd*
			[ -e /tmp/camd.socket ] && rm /tmp/camd.socket
		fi;
		if [ -e /tmp/.mbox ] ; then
			touch /tmp/mbox.kill
			i=5
			while expr $i != 0 > /dev/null; do
				if pidof mbox > /dev/null; then
					echo "Warte auf Mbox (max. $i seconds)" > $out
				else
					break;
				fi
				i=`expr $i - 1`
				sleep 1
			done
				if pidof mbox > /dev/null; then
					echo "Sende KILL-Signal zu Mbox" > $out
					killall -9 mbox
				fi
			rm /tmp/share*
			[ -e /tmp/pid.info ] && rm /tmp/pid.info
		fi;
	fi
	if pidof evocamd > /dev/null ; then
		killall evocamd
		[ -e /tmp/pid.info ] && rm /tmp/pid.info
		[ -e /tmp/server.ok ] && rm /tmp/server.ok
	fi
	if pidof mgcamd > /dev/null ; then
		kill $( cat /tmp/mgcamd.pid )
		[ -e /tmp/ca_cache.list ] && cp /tmp/ca_cache.list  /var/keys/replace.list
		[ -e /tmp/mgstat.info ] && rm /tmp/mgstat.info
		[ -e /tmp/mgshare.info ] && rm /tmp/mgshare.info
		rm /tmp/pid.info
		[ -e /tmp/ca_cache.list ] && rm /tmp/ca_cache.list
	fi
	if [ -e /tmp/.gbox -o -e /tmp/gbox.ver ];then
		touch /tmp/gbox.kill
		rm /tmp/gbox.ver
		rm /tmp/online.log
		rm /tmp/pid.info
		rm /tmp/share*
	fi
	if pidof camd3 > /dev/null ; then
		[ -e /tmp/camd.socket -a ! -e /tmp/camd.sock01 ] && /bin/emu/plugins/pcamd -s /tmp/camd.socket -kill
		[ -e /tmp/camd.sock01 ] && /bin/emu/plugins/pcamd -s /tmp/camd.sock01 -kill
		i=5
		while expr $i != 0 > /dev/null; do
			if pidof camd3 > /dev/null; then
				echo "Warte auf camd3 (max. $i seconds)" > $out
			else
				break;
			fi
			i=`expr $i - 1`
			sleep 1
		done
		if pidof camd3 > /dev/null; then
			echo "Sende KILL-Signal zu Camd3" > $out
			killall -9 camd3
		fi
	fi
	if pidof newcamd > /dev/null ; then
		kill $( cat /tmp/newcamd.pid )
	fi
	if pidof camx > /dev/null ; then
		killall camx
		[ -e /tmp/camd.sock05 ] && rm /tmp/camd.sock05
	fi
	if pidof cccam > /dev/null ; then
		killall cccam
		killall capmtserver
		rm /tmp/.CCcam.nodeid 
		rm /tmp/.listen.camd.socket 
		rm /tmp/camd.socket
		rm /tmp/ecm0.info 
	fi
;;
emu_start)
	[ "update" != "$update" ] && [ "nozap" != "$recording" ] && [ -e /tmp/.mbox -a -e /var/etc/.emu ] && pzapit -lsb && exit 0
	if [ ! -e /tmp/.mbox ] ;then
		VENDOR_ID=`/bin/tuxinfo -v`
	
		rmmod cam_napi
		rmmod cam
		sleep 1

		if [ $VENDOR_ID -eq 2 ]; then
			# Philips
			insmod cam mio=0xC040000 firmware=/var/tuxbox/ucodes/cam-alpha.bin
		else
			#Nokia und Sagem
			insmod cam mio=0xC000000 firmware=/var/tuxbox/ucodes/cam-alpha.bin
		fi;

		insmod cam_napi.o
		sleep 1
	fi
	if [ -e /tmp/.camd2 ] ; then
		if [ -e /var/etc/.au_slot3 ]; then
			/bin/emu/camd2
			else
			/bin/emu/camd2 --noemm
		fi;
	fi
	if [ -e /tmp/.camd2 ] ; then
		if [ -e /var/etc/.au_slot3 ]; then
			/bin/emu/multicamd2
			else
			/bin/emu/multicamd2 --noemm
		fi;
	fi
	if [ -e /tmp/.scam ] ; then
		/bin/emu/scam &
	fi
	if [ -e /tmp/.evocamd ] ; then
		/bin/emu/evocamd
	fi
	if [ -e /tmp/.mgcamd ] ; then
		/bin/emu/mgcamd
	fi
	if [ -e /tmp/.gbox ] ; then
		/bin/emu/gbox &
	fi
	if [ -e /tmp/.camd3 ] ; then
		/bin/emu/camd3
	fi
	if [ -e /tmp/.newcamd ] ; then
		/bin/emu/newcamd
	fi
	if [ -e /tmp/.camx ] ; then
		/bin/emu/camx &
	fi
	if [ -e /tmp/.cccam ] ; then
		/bin/emu/capmtserver >/dev/null &
		sleep 1
		/bin/emu/cccam &
	fi
	if [ "update" = "$update" ] ;then
		if [ -e /tmp/.mbox ] ; then
			/bin/emu/mbox /var/keys/mbox.cfg &
			sleep 2
		fi
	fi
	if [ "nozap" = "$recording" ] ;then
		if [ -e /tmp/.rdgc ] ; then
			if ! pidof rdgd > /dev/null ; then
				/var/emu/rdgd -d 0
			fi
		fi
	else
		sleep 2
		pzapit -lsb
		[ "recording" = "$recording" ] && touch /tmp/.rezap
	fi
;;
esac
