#! /bin/bash
AGENT="Mozilla/5.0 (X11; U; Linux x86; it; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.5.7"
STREAM=$1
OS=`uname`
if [[ "$OS" == 'Darwin' ]]; then
  VLC=/Applications/VLC.app/Contents/MacOS/VLC
else
  VLC=vlc
fi
ID=`expr "$STREAM" : '.*=\([0-9]*\)'`
DATE=`curl -A "$AGENT" http://videowall.rai.it/cgi-bin/date`
DATE=(`echo $DATE | sed -e 's/[:-]/ /g'`)
DAY=${DATE[0]}
MONTH=${DATE[1]}
YEAR=${DATE[2]}
HOUR=${DATE[3]}
MIN=${DATE[4]}
SEC=${DATE[5]}
giveRND() {
	expr $(expr $(echo $RANDOM) + 1) % 10
}
TOKENSTRING=`echo "${YEAR};${ID};${DAY}-${MONTH}-$(giveRND)$(giveRND)$(giveRND)-${HOUR}-${MIN}-${SEC}-$(giveRND)$(giveRND)$(giveRND)"`
MASKEDSTRING=`python << EOF
a="$TOKENSTRING"
i=0
x=""
for ch in a:
  x+=(chr(ord(ch)+i))
  i=i+3
print x
EOF`
TOKEN=`echo "$MASKEDSTRING" | openssl enc -base64`
curl -A "$AGENT" -d -H "Connection: keep-alive" -H "Keep-Alive: 300" -H "viaurl: http://www.rai.tv" -H "ttAuth: $TOKEN" -H "Content-Length: 0" $STREAM | $VLC --http-user-agent="$AGENT" -

