#!/bin/sh

#
#Emu Start 1.5
#Written for starting NewCS + MGcamd
#

# Add library path for libcrypto.so.0.9.7
export LD_LIBRARY_PATH=/tmp:/var/lib:/lib:

FAKECHAN="1:0:1:fab:451:35:c11100:0:0:0:"

#
# Getting the current stream information
#
CURRCHAN=`wget -O - -q http://root:dreambox@127.0.0.1/cgi-bin/streaminfo | grep '<!-- .*:-->' | sed 's/.*<!-- \(.*\)-->/\1/'`

#first we kill possible running emu's
killall -9 newcs.ppc dccamd mgcamd cardserver.dream
#killall -9 dccamd
#cp /usr/bin/dccamd /usr/bin/dccamd.old
#rm /usr/bin/dccamd
#remove ecm info file
rm -f /tmp/ecm.info /tmp/camd.socket			
#starting newcs card server
/usr/bin/newcs.ppc &
sleep 5
#starting mgcamd emulator
/usr/bin/mgcamd &
sleep 3

#
# zapping back
#
[ "$CURRCHAN" != "$FAKECHAN" ] && wget -O /tmp/runemu.temp -q http://root:dreambox@127.0.0.1/cgi-bin/zapTo?path=$CURRCHAN

sleep 1

#
# Removing temporary files
#
rm /tmp/runemu.*

exit;