#!/bin/sh
#Play HLSv5/HLSv7 Streams on DreamOS2.5 from bouquet.tv
#copyright by dam72
#feel free to use, modify, adapt, improve

#set -x # debugging

killall -9 ffmpeg
killall -9 journalctl
killall -9 udpxy

mkdir -p /tmp/tmp/hlsv5;

udpxy -a lo -p 32034 &

journalctl -fu enigma2 -o cat > /tmp/tmp/hlsv5/jctl &

while true
      do
    	all=`cat /tmp/tmp/hlsv5/jctl | grep "playing" | tail -n1`
	var0=`echo "$all" | grep -Eo "(prox/http|prox/https)%3a//[a-zA-Z0-9./?=_%:-]*(:)" | tail -n1 | sed 's,:,,g' | sed 's,prox/https%3a,https:,g' | sed -n "1p"`
           
			if [ "$var0" != "$old_var0" ] && echo 'url changed'
			then
			kill -15 "$ffmpeg_pid"
											                  
				if [ -n "$var0" ] # URL nicht leer
                    then
                sleep 1
                mfest=`curl -L -k "$var0"`
			   var1=`echo "$mfest" | sed -n '/RESOLUTION=1/{n;p}'| sed -n "1p"`
			   var2=`echo "$mfest" | sed '/DEFAULT=YES[^[]*URI="/!d;s//&\n/;s/.*\n//;:a;/"/bb;$!{n;ba};:b;s//\n&/;P;D' | sed -n "1p"`				                      
        		   /usr/bin/ffmpeg -re -thread_queue_size 4 -i "$var1" -i "$var2" -strict 2 -c copy -loglevel error -f rtp_mpegts "rtp://227.0.0.0:92?pkt_size=1316" & ffmpeg_pid=$!
				old_var0=$(echo "$var0")                                    
                 fi
			fi
		sleep 1
done