#!/bin/bash
# Download Lage der Nation episodes as MP3
# Usage: ./download_ldn.sh [start] [end]
#   e.g.: ./download_ldn.sh 379 474

START=${1:-379}
END=${2:-474}
DIR="$(dirname "$0")/audio"
mkdir -p "$DIR"

echo "Downloading LdN$START to LdN$END into $DIR"

for i in $(seq $START $END); do
  FILE="$DIR/LdN${i}.mp3"
  if [ -f "$FILE" ]; then
    echo "EXISTS: LdN${i}.mp3"
    continue
  fi
  echo -n "LdN${i}... "
  HTTP_CODE=$(curl -sL -w "%{http_code}" -o "$FILE" "https://files.lagedernation.org/lagedernation/LdN${i}.mp3")
  if [ "$HTTP_CODE" = "200" ]; then
    SIZE=$(du -h "$FILE" | cut -f1)
    echo "OK ($SIZE)"
  else
    echo "SKIP (HTTP $HTTP_CODE)"
    rm -f "$FILE"
  fi
done

echo ""
echo "Done. Files:"
ls -lh "$DIR"/*.mp3 2>/dev/null | wc -l
du -sh "$DIR"
