#!/bin/sh
#
# usage: mp3iterget [-s secs] [-n segments] [-o output_prefix] URL
#
# defaults: segment_length = 900
#           segments       = 4
#
# Download an mp3 stream iteratively from a streaming media server.
# This is for protection against disconnection during long downloads
# because of unreliable connectivity or server policy does not permit
# a single connection of such long duration.  Using the defaults, one hour
# of streaming media is downloaded in 4 15 minute chunks. Each segment
# has 10 seconds of overlap, so that each each segment may be aligned
# and reassembled, permitting the original stream to be  seamlessly 
# reconstructed.  The Python align.py utility performs this assembly.
#
#  Copyright (C) 2005, 2006 Adam Bernstein
#  All rights reserved.
# 
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Library General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
# 
#  This library 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
#  Library General Public License for more details.
# 
#  You should have received a copy of the GNU Library General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#  Boston, MA  02111-1307  USA.


usage_func()
{
  echo
  echo "usage: mp3iterget [-s secs] [-n segments] [-o output_prefix] URL"
  echo "       defaults: secs:          900"
  echo "                 segments:      4"
  echo "                 output_prefix: mp3iterget"
  exit 1
}


# Call mp3get in the background.
# 
mp3get_func()
{
  echo mp3get -o "$FILE" -S $secs $URL
       mp3get -o "$FILE" -S $secs $URL
  echo download done...
}

PATH=$PATH:/usr/local/bin
export PATH

sec=900
segments=4
output_prefix="mp3iterget"

tmptst=`which mp3get | grep '^/'`
if [ -z "$tmptst" ]; then
  echo "ERROR: mp3get is not installed or not in PATH environment"
  exit 1
fi

while [ -n "$1" ]; do
  if [ `echo " $1" | grep -c ' -'` -eq 1 ]; then
#    echo "debug '$1'"
    if [ " $1" = ' -s' ]; then
      shift
      if [ -z "$1" ]; then
        echo "ERROR: -s requires an argument"
        usage_func
      else
        sec="$1"
        shift
      fi
    elif [ " $1" = ' -n' ]; then
      shift
      if [ -z "$1" ]; then
        echo "ERROR: -n requires an argument"
        usage_func
      else
        segments="$1"
        shift
      fi
    elif [ " $1" = ' -o' ]; then
      shift
      if [ -z "$1" ]; then
        echo "ERROR: -o requires an argument"
        usage_func
      else
        output_prefix="$1"
        shift
      fi
    else
      echo "ERROR: Unknown option '$1'"
      usage_func
    fi
  elif [ -n "$1" ]; then
    URL=$1
    shift
  fi
done

if [ -z "$URL" ]; then
  echo "ERROR: no URL specified"
  usage_func
fi

echo sec=$sec
echo segments=$segments
echo output_prefix=$output_prefix
echo URL=$URL

secs=`expr $sec + 10`
echo $secs

cnt=1

# Prime the download pump
#
FILE="$output_prefix`date +%j_%H%M`.mp3"

mp3get_func "$FILE" -S $secs $URL &
child1="$!"

while [ $cnt -lt $segments ]; do
  echo "iteration: $cnt"
  sleep $sec
  FILE="$output_prefix`date +%j_%H%M`.mp3"
  mp3get_func "$FILE" -S $secs $URL &
  child2="$!"
  child1=$child2
  cnt=`expr $cnt + 1`
done
echo "iteration: $cnt"
echo "waiting for last download to finish..."
wait $child1
exit 0
