#!/bin/sh
if (! env | grep -qE "^HD_APK")
then
HD_HTTP_LOCAL_PORT="80"
FS_PREFIX=""
fi

CURL=""
source "$FS_PREFIX/tmp/run/versions.txt"
if (echo $platform_kind | grep -E -q "87.."); then
CURL="../../bin/curl.87xx"
elif (echo $platform_kind | grep -E -q "864."); then
CURL="../../bin/curl.864x"
elif (echo $platform_kind | grep -E -q "867."); then
CURL="../../bin/curl.867x"
elif (echo $platform_kind | grep -E -q "865."); then
CURL="../../bin/curl.865x"
else
CURL="$FS_PREFIX/firmware/bin/curl"
fi


echo -e "Content-Type: application/vnd.apple.mpegURL\n"

HTTP_USER_AGENT="WINK/1.55.1 (AndroidTV/9) HlsWinkPlayer"
if [[ "$QUERY_STRING" == *.m3u8* ]]; then
BASE_URL="http://127.0.0.1:$HD_HTTP_LOCAL_PORT$SCRIPT_NAME?"${QUERY_STRING%\/*}"/"
M3U8=$($CURL -k -s  --location $QUERY_STRING --user-agent "$HTTP_USER_AGENT" -H "Origin: https://sansa.allarknow.online")

	if (echo "$M3U8" | grep -E -q "^http"); then
	echo "$M3U8" | sed "s|^http|http://127.0.0.1:$HD_HTTP_LOCAL_PORT$SCRIPT_NAME?http|"
	else
	echo "$M3U8" | sed "s|^.*m3u8|${BASE_URL}&|; s|^.*ts|${BASE_URL}&|"
	fi
	
else
$CURL -k -s --location  $QUERY_STRING --user-agent "$HTTP_USER_AGENT" -H "Origin: https://sansa.allarknow.online"
fi
exit;



