#!/bin/sh
UCI="uci -q"

yesno()
{
	if [ -n "$1" ]; then
		v=$(echo "$1" | tr [:upper:] [:lower:])
		case ${v} in
			yes|true)	echo 1		;;
			[0-9])		echo ${v}	;;
                        *)		echo 0		;;
		esac
        fi
}

stop()
{
	tag=$1
       
        for service in video telemetry status; do
                if [ -f /run/${service}-${tag}.pid ]; then
                        start-stop-daemon -q -K -s 9 -p /run/${service}-${tag}.pid
                        rm /run/${service}-${tag}.pid
                fi
        done
}

start()
{
	tag="$1"
        ip="$2"
       
        if [ ! -f /run/video-${tag}.pid ]; then
                VIDEO=$($UCI get keenfalcon.gcs.video)
                RTP=$($UCI get keenfalcon.gcs.rtp)
                FORMAT=$($UCI get keenfalcon.encoder.format)
                if [ "$FORMAT" != "h265" ]; then
                	FORMAT=h264
                fi
       
                if [ -n "${RTP}" ]; then
                        RTP=`yesno ${RTP}`
                else
                        RTP=0
                fi
       
                if [ "${RTP}" -eq 1 ]; then
                        if [ -z "${VIDEO}" ]; then
                                VIDEO=5600
                        fi
               
                        start-stop-daemon -S -b -p /run/video-${tag}.pid -m \
                                -x /usr/bin/gst-launch-1.0 -- \
                                -q udpsrc port=5000 do-timestamp=1 \! ${FORMAT}parse \! rtp${FORMAT}pay pt=96 config-interval=-1 \! queue \! udpsink port=${VIDEO} host=${ip}
                else
                        if [ -z "${VIDEO}" ]; then
                                VIDEO=5000
                        fi
               
                        start-stop-daemon -S -b -p /run/video-${tag}.pid -m \
                                -x /usr/bin/gst-launch-1.0 -- \
                                -q udpsrc port=5000 do-timestamp=1 \! udpsink port=${VIDEO} host=${ip}
                fi
        fi

        if [ ! -f /run/telemetry-${tag}.pid ]; then
                MAVLINK=$($UCI get keenfalcon.gcs.mavlink)
                if [ -n "${MAVLINK}" ]; then
	                start-stop-daemon -S -b -p /run/telemetry-${tag}.pid -m \
        	                -x /usr/bin/mavlink-routerd \
                	        -- -t 0 -p 127.0.0.1:5760 -e ${ip}:${MAVLINK}
		fi                	      
        fi

        if [ ! -f /run/status-${tag}.pid ]; then
                STATUS=$($UCI get keenfalcon.gcs.status)
                if [ -z "${STATUS}" ]; then
                        STATUS=5003
                fi
       
        	start-stop-daemon -S -b -p /run/status-${tag}.pid -m \
                        -x /usr/bin/socat \
                        -- UDP4-LISTEN:5003,reuseaddr UDP4-SENDTO:${ip}:${STATUS}
        fi
}

echo $0 $@ >> /tmp/log

case "$1" in
	renew|bound)
		if [ ${interface} = "usb0" ]; then
	                start ${interface} ${serverid}
		fi	              
		;;

	deconfig)
		if [ ${interface} = "usb0" ]; then
	                stop ${interface}
		fi
		;;

	add|old)
                start $3 $3
        	;;

	del)
                stop $3
        	;;
esac
