#!/bin/bash
#
# Bash script for playing Live streams of Olympic Games.
#
# Thanks to: Martin Beránek (mousehouse) - providing URL extraction method
#            hikikomori82 - compatible sed with older version
#            Jakub Lucký - various code enhancements (dumpstream)
#            Marián Kyral - Use direct links provided by CT
#
#  Copyright (c) 2010 Radek Novacek
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# Replace with your favorite player
COMMAND="smplayer -playlist"


help() {
    echo "Missing argument. Use one of following: CT4OH, CT2OH, CTHD, ZOH1, ZOH2, ZOH3, ZOH4, ZOH5, ZOH6"
    echo "Usage: zoh.sh VIDEO [dump [outfile]]"
    exit 1
}


getPlaylist() {
ID="$1"

case $ID in
  CT2OH) CID=ct2  ;;
  CT4OH) CID=ct4  ;;
   CTHD) CID=zohHD  ;;
   ZOH1) CID=zoh1  ;;
   ZOH2) CID=zoh2  ;;
   ZOH3) CID=zoh3  ;;
   ZOH4) CID=zoh4  ;;
   ZOH5) CID=zoh5  ;;
   ZOH6) CID=zoh6  ;;
      *) echo "Unknown ID: $ID"; return 1;;
esac

COOKIE=$(mktemp)

echo $(curl -L -H "Content-Type: text/xml; charset=utf-8" \
     http://zoh.ct24.cz/ctdir/links.asp?$CID | \
     sed 's/</\n</g' | grep "ClientPlaylist.aspx" | sed 's/>/\n/'| grep "ClientPlaylist.aspx")

rm -f $COOKIE
}

playChanel() {
  ID="$1"
  PLAYLIST=$(getPlaylist "$ID" )

  echo "===================================================================================="
  echo "Playlist: $PLAYLIST"
  echo "===================================================================================="
  echo

  if [ -z "$PLAYLIST" ]
  then
    echo "Error: Playlist url is incorrect"
    return 1
  else
    $COMMAND "$PLAYLIST"
  fi

}

if [ $# -lt 1 ];
then
  help
else
  if [ $(echo "$1" |egrep -c "CT4OH|CT2OH|ZOH1|ZOH2|ZOH3|ZOH4|ZOH5|ZOH6|CTHD") -gt 0 ]
  then
    ID=$1;
  else
    help
  fi
fi

if [ $# -gt 1 ]
then
  if [  "$2" = "dump" -a  -n "$3"  ];
  then
      COMMAND="mplayer -dumpfile $3 -dumpstream -playlist "
  elif [ "$2" == "dump" ];
  then
      COMMAND="mplayer -dumpstream -playlist "
  fi
fi

playChanel $ID

