#!/bin/bash

stations='ARD ZDF Sdwest_3 Bayern_3 DRS'

[ $1 ] && TV2HTML_DIR=$1
if [ ! -d "$TV2HTML_DIR" ]; then
  echo 'Usage: gettv <directory>'
  exit 1
fi

for station in $stations; do
  echo -ne "Prepare station $station. Press <RETURN> to start or enter 's' to skip: \\a"
  read
  if [ "$REPLY" != s ]; then
    touch $TV2HTML_DIR/$station.db
    pages=
    case $station in
      ARD|ZDF|Sdwest_3|Bayern_3)
        pages="361.0 362.0 363.0 364.0 365.0 366.0 367.0";;
      DRS)
        pages="371.0 372.0 373.0 374.0 375.0 376.0 377.0";;
    esac
    if [ "$pages" = "" ]; then
      echo Unknown station: $station
    else
      vtxget -p $station -o $TV2HTML_DIR -f text $pages 2>&1 | tee $TV2HTML_DIR/vtxget.log
      if grep -q timeout $TV2HTML_DIR/vtxget.log; then
        echo -e Warning: timeouts occured:\\a
        grep timeout $TV2HTML_DIR/vtxget.log
      fi
      rm -f $TV2HTML_DIR/vtxget.log
    fi
  fi
done
