#!/bin/bash

HOST="192.168.168.37"
PORT="554"
#STREAM="?src=1&fe=1&freq=11493&pol=h&ro=35&mtype=8PSK&msys=dvbs2&sr=22000&fec=23&pids=0,5101,5102,5103,5106,5104,5105"
STREAM="?src=1&fe=1&freq=11836&sr=27500&pol=h&msys=dvbs&pids=0,100,101,102,103,106"
CSEQ=1
STREAMID=0
SESSIONID=""
echo "---------------------------------------------------------------" >> req.out


rtsp_OPT() {
    echo "rtsp_OPT"
    echo "-------" >> req.out
    echo "Request:" >> req.out
    if [ M${SESSIONID} == "M" ]
    then
	printf "OPTIONS rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nConnection:close\r\n\r\n" >> req.out
	printf "OPTIONS rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nConnection:close\r\n\r\n" | netcat ${HOST} ${PORT}
    else
	printf "OPTIONS rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n" >> req.out
	printf "OPTIONS rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n" | netcat ${HOST} ${PORT}
    fi
}


rtsp_DESC() {
    echo "rtsp_DESC"
    echo "-------" >> req.out
    echo "Request:" >> req.out
    printf "DESCRIBE rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nAccept: application/sdp\r\nConnection:close\r\n\r\n" >> req.out
    printf "DESCRIBE rtsp://${HOST}:${PORT}/ RTSP/1.0\r\nCSeq:${CSEQ}\r\nAccept: application/sdp\r\nConnection:close\r\n\r\n" | netcat ${HOST} ${PORT}
}

rtsp_SETUP() {
    echo "rtsp_SETUP"
    echo "-------" >> req.out
    echo "Request:" >> req.out
    printf "SETUP rtsp://${HOST}:${PORT}/${STREAM} RTSP/1.0\r\nCSeq:${CSEQ}\r\nTransport:RTP/AVP;unicast;client_port=1234-1235\r\nConnection:close\r\n\r\n" >> req.out
    printf "SETUP rtsp://${HOST}:${PORT}/${STREAM} RTSP/1.0\r\nCSeq:${CSEQ}\r\nTransport:RTP/AVP;unicast;client_port=1234-1235\r\nConnection:close\r\n\r\n" | netcat ${HOST} ${PORT}
}

rtsp_PLAY() {
    echo "rtsp_PLAY"
    echo "-------" >> req.out
    echo "Request:" >> req.out
    printf "PLAY rtsp://${HOST}:${PORT}/stream=${STREAMID} RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n" >> req.out
    printf "PLAY rtsp://${HOST}:${PORT}/stream=${STREAMID} RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n" | netcat ${HOST} ${PORT}
}

rtsp_TEARD() {
    echo "rtsp_TEARD"
    echo "-------" >> req.out
    echo "Request:" >> req.out
    printf "TEARDOWN rtsp://${HOST}:${PORT}/stream=${STREAMID} RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n"  >> req.out
    printf "TEARDOWN rtsp://${HOST}:${PORT}/stream=${STREAMID} RTSP/1.0\r\nCSeq:${CSEQ}\r\nSession:${SESSIONID}\r\nConnection:close\r\n\r\n"  | netcat ${HOST} ${PORT}
}

rtsp_RESULT() {

    CMD=`echo ${1} | awk '{print $1}'` 
#    echo "Command: $CMD" 

    echo "\"${1}\"" 
    echo "Result:"  >> req.out
    echo "\"${1}\"" >> req.out

    
    case ${CMD} in
	rtsp_OPT)
	;;
	rtsp_DESC)
	    # src=<srcID>;
	    #tuner=<feID>,<level>,<lock>,<quality>,<frequency>,<polarisation>,<system>,<type>,<pilots>,<roll_off>,<symbol_rate>,<fec_inner>;
	    #pids=<pid0>,…,<pidn>
	    t_resp=`echo ${1} | awk '{for(i=1;i<NF;i++){if($i~/ver=/) print $i}}'`
	    SRC=`echo ${t_resp}| awk -F";" '{print substr($2,index($2,"=")+1)}'` 
	    TUNER=`echo ${t_resp}| awk -F";" '{print substr($3,index($3,"=")+1)}'`
	    PIDS=`echo ${t_resp}| awk -F";" '{print substr($4,index($4,"=")+1)}'`
	    
	    
	    echo "SRC:\"$SRC\" TUNER:\"$TUNER\" PIDS:\"$PIDS\""
	     read feID level lock quality frequency polarisation system type pilots roll_off symbol_rate fec_inner <<< $(echo ${TUNER} | sed 's/,/ /g') 
	    echo ${feID},${level},${lock},${quality},${frequency},${polarisation},${system},${type},${pilots},${roll_off},${symbol_rate},${fec_inner}
	;;
	rtsp_SETUP)
	if [ `echo ${1}| grep -c "streamID:"` -gt 0 ]
	then
	    echo "STREAMID"
	    STREAMID=`echo ${1} | awk '{if($0~/streamID:/)printf"%d", substr($0,index($0,"streamID:")+9)}'`
	    echo "streamID: \"${STREAMID}\""
	fi 
        if [ `echo ${1}| grep -c "Session:"` -gt 0 ]
	then
	    SESSIONID=`echo ${1} | awk '{if($0~/Session:/)printf"%s", substr($0,index($0,"Session:")+9,index(substr($0,index($0,"Session:")+9),";")-1)}'`
	    echo "SessionID: \"${SESSIONID}\""
	fi 
	;;
	rtsp_PLAY)
	;;
	rtsp_TEARD)
	;;
    esac

#    if [ `echo ${1}| grep -c "CSeq:"` -gt 0 ]
#    then
#	echo "CSEQ"
#	_CSEQ=`echo ${1} | awk '{if($0~/CSeq:/)printf"%d", substr($0,index($0,"CSeq:")+5)}'`
#	echo "CSeq: \"${_CSEQ}\""
#    fi 

    CSEQ=`expr $CSEQ + 1`
}

echo "SatIP_RTSP start: $#:${@}"

STREAM="?src=1&fe=1&freq=10744&pol=h&ro=0.35&msys=dvbs&mtype=qpsk&plts=off&sr=22000&fec=56&pids=0,400,401,402"

WAIT=5

while [ $# -gt 0 ]
do

PAR=$1
RPT=1

if [ `echo $1 | grep -c "="` -gt 0 ]
then
    echo $1
    eval $1 
    RPT=0
elif [ `echo $1 | grep -c ","` -gt 0 ]
then
    PAR=`echo $1 | awk -F"," '{print $1}'`
    RPT=`echo $1 | awk -F"," '{print $2}'`
fi


for (( i=1;i<=$RPT;i++))
do

case $PAR in
    opt)
	echo "OPTIONS"
	rtsp_RESULT "$(rtsp_OPT | sed 's/\r//')"
	;;
    desc)
	echo "DESCRIBE"
	rtsp_RESULT "$(rtsp_DESC | sed 's/\r//')"
	;;
    setup)
	echo "SETUP"
	rtsp_RESULT "$(rtsp_SETUP | sed 's/\r//')"
        ;;
    play)
	echo "PLAY"
	rtsp_RESULT "$(rtsp_PLAY | sed 's/\r//')"
        ;;
    teard)
	echo "TEARDOWN"
	rtsp_RESULT "$(rtsp_TEARD | sed 's/\r//')"
        ;;
esac
sleep $WAIT

done

    shift    

done

exit






exit

rtsp_RESULT "$(rtsp_OPT | sed 's/\r//')"
echo "---------------------------------------------------------------"
exit
rtsp_RESULT "$(rtsp_DESC | sed 's/\r//')"
echo "---------------------------------------------------------------"
sed 's/\r//g' req.out
rtsp_RESULT "$(rtsp_SETUP | sed 's/\r//')"
echo "---------------------------------------------------------------"
rtsp_RESULT "$(rtsp_DESC | sed 's/\r//')"
echo "---------------------------------------------------------------"
rtsp_RESULT "$(rtsp_PLAY | sed 's/\r//')"
echo "---------------------------------------------------------------"
rtsp_RESULT "$(rtsp_DESC | sed 's/\r//')"
echo "---------------------------------------------------------------"
count=0
touch runrunrun
while ( [ -e runrunrun ] )
do
	count=$(( count + 1 ))
#echo "---------------------------------------------------------------"
	echo "count: $count $(( $count % 10))"
    	if [ $(( $count % 10 )) -eq 0 ]
	then
	    echo "mod funzt:"
	    
	    if [ `echo $STREAM | grep -c "11836"` -gt 0 ]
	    then
		echo "switch to ZDF"
		STREAM="?src=1&fe=1&freq=11953&sr=27500&pol=h&msys=dvbs&pids=0,100,110,120,121,122,125"
	    else
		echo "switch to ARD"
		STREAM="?src=1&fe=1&freq=11836&sr=27500&pol=h&msys=dvbs&pids=0,100,101,102,103,106"

	    fi
	    
	    rtsp_RESULT "$(rtsp_SETUP | sed 's/\r//')"
	    
	    
	fi
echo "---------------------------------------------------------------"
	rtsp_RESULT "$(rtsp_OPT | sed 's/\r//')"
	rtsp_RESULT "$(rtsp_DESC | sed 's/\r//')"
echo "---------------------------------------------------------------"
	 
	sleep 10
done


