#!/bin/sh
PROTOCOL_VER="63"
PROTOCOL_TOKEN="3875641D"
HOSTNAME="localhost"
CONTROL_PORT="6543"
STATUS_PORT="6544"

PID=$1
ADAPTERS=`ps aux | awk -v PID=$PID '$2==PID{print $13FS$14}'`
set -- $ADAPTERS
ADAPTER=$1
NEW_ADAPTER=$2
RECORDER_ID=`wget http://localhost:$STATUS_PORT/ -O - -o /dev/null | \
	awk -v ENCODER=${NEW_ADAPTER}/frontend0 '$6==ENCODER{print $2}'`
if [ ! -z $RECORDER_ID ]
then
	expect -c "
	set timeout 20
	spawn telnet localhost $CONTROL_PORT
	expect \"\n\"
	expect \"\n\"
	expect \"\n\"
	send \"33       MYTH_PROTO_VERSION $PROTOCOL_VER $PROTOCOL_TOKEN\n\"
	expect \"ACCEPT\"
	expect \"$PROTOCOL_VER\"
	send \"\n\"
	send \"23       ANN Monitor $HOSTNAME 0\n\"
	expect \"OK\"
	send \"87      QUERY_RECORDER $RECORDER_ID\[\]:\[\]SPAWN_LIVETV\[\]:\[\]release\[\]:\[\]0\n\"
	send \"87      QUERY_RECORDER $RECORDER_ID\[\]:\[\]SPAWN_LIVETV\[\]:\[\]release\[\]:\[\]0\n\"
	expect \"ok\"
	send \"87      QUERY_RECORDER $RECORDER_ID\[\]:\[\]STOP_LIVETV\n\"
	send \"87      QUERY_RECORDER $RECORDER_ID\[\]:\[\]STOP_LIVETV\n\"
	send \"87      QUERY_RECORDER $RECORDER_ID\[\]:\[\]STOP_LIVETV\n\"
	expect \"ok\"
	send \"\n\n\n\n\"
	" > /dev/null
	sleep 1
fi
umount $NEW_ADAPTER
if [ $? -eq 0 ]
then
	nice --4 /usr/local/bin/fuse_b25 --target $ADAPTER $NEW_ADAPTER -o allow_other
	exit 0
else
        exit 1
fi


