#!/bin/sh

# Copyright (C) 2010 Sergio García-Cuevas González.
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# rtve-mediateca-dl
# 
# Download a video file from the "mediateca" of RTVE:
# <http://www.rtve.es/mediateca/videos/>.
# 
# Usage:
#   download-from-rtve-mediateca ADDRESS [OUTPUT-FILE]

SUCCESS=0
FAILURE=1

NAME=rtve-mediateca-dl
VERSION=1.0

RTVE_MEDIATECA_DL=`basename "$0"`


usage() {
  printf 'Usage: %s [OPTION] ADDRESS [OUTPUT-FILE]\n' "$RTVE_MEDIATECA_DL"
  printf 'Download a video from the "mediateca" of RTVE.\n'
  printf '\n'
  printf '  -q, --quiet    quiet (no output)\n'
  printf '  -h, --help     display this help and exit\n'
  printf '      --version  output version information and exit\n'
  printf '\n'
  printf 'Mail bug reports and suggestions to <webmaster@sgcg.es>.\n'
}

version() {
  printf '%s %s\n' "$NAME" "$VERSION"
  printf 'Copyright (C) 2010 Sergio García-Cuevas González <webmaster@sgcg.es>.\n'
  printf 'License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n'
  printf '\n'
  printf 'This is free software; you are free to change and redistribute it.\n'
  printf 'There is NO WARRANTY, to the extent permitted by law.\n'
}


# Process [OPTION]
QUIET=""
case "$1" in
  -h|--help) usage
             exit $SUCCESS
             ;;
  --version) version $SUCCESS
             exit
             ;;
  -q|--quiet) QUIET=--quiet
              shift
              ;;
esac
  

# Get the address and the output file
ADDRESS="$1"
if [ "x$ADDRESS" = "x" ]; then
  usage >&2
  exit $FAILURE
fi
OUTPUT_FILE="$2"


# Extract the numeric content Id
CONTENT_ID_LINE_PATTERN='var flashcontentId ='
CONTENT_ID=`wget $QUIET "$ADDRESS" -O - \
            | sed -n "/$CONTENT_ID_LINE_PATTERN/s/[^0-9]//gp"`
if [ "x$CONTENT_ID" = x ]; then
  printf '%s: Could not extract the content Id from %s.\n' \
         "$RTVE_MEDIATECA_DL" "$ADDRESS" >&2
  exit $FAILURE
fi


# Extract the resource file URI
RESOURCE_URI_TEMPLATE=http://www.rtve.es/alacarta/player/%s.xml
RESOURCE_URI=`printf "$RESOURCE_URI_TEMPLATE" "$CONTENT_ID"`


# Extract the location of the video file
LEADING_REGEX='^.*<location>'
TRAILING_REGEX='</location>.*$'
ORIGINAL_PREFIX='rtmp://stream'
DESIRED_PREFIX='http://www'
VIDEO_URI=`wget $QUIET "$RESOURCE_URI" -O - \
           | sed "s_${LEADING_REGEX}__;s_${TRAILING_REGEX}__" \
           | sed "s_${ORIGINAL_PREFIX}_${DESIRED_PREFIX}_"`
if [ "x$VIDEO_URI" = x ]; then
  printf '%s: Could not extract the video location from %s.\n' \
         "$RTVE_MEDIATECA_DL" "$ADDRESS" >&2
  exit $FAILURE
fi


# Download the video file
if [ "x$OUTPUT_FILE" = x ]; then
  wget $QUIET "$VIDEO_URI" -c
else
  wget $QUIET "$VIDEO_URI" -c -O "$OUTPUT_FILE"
fi


# Maybe something went wrong at the last step
if [ $? != 0 ]; then
  printf '%s: Could not download video from %s\n' \
         "$RTVE_MEDIATECA_DL" "$VIDEO_URI" >&2
  exit $FAILURE
fi
