#!/bin/sh
#
# adzapcheck.sh
#
# source: vdr-plugin adzap

# CONFIG START
  TIMEOUT="10"  # timeout in minutes
# CONFIG END

PATH=$PREFIX/bin:$PREFIX/sbin:$PATH

rm -f /tmp/adzap.run

case $1 in
    "NEUN LIVE Television")
	MAP_NAME="9Live"
	;;
    "Das Erste")
	MAP_NAME="ARD"
	;;
    "DSF")
	MAP_NAME="$1"
	;;
    "Eurosport")
	MAP_NAME="EuroSport"
	;;
    "KABEL1")
	MAP_NAME="Kabel 1"
	;;
    "n-tv")
	MAP_NAME="NTV"
	;;
    "ProSieben")
	MAP_NAME="Pro7"
	;;
    "RTL Television")
	MAP_NAME="RTL"
	;;
    "RTL2")
	MAP_NAME="RTL II"
	;;
    "SAT.1")
	MAP_NAME="Sat.1"
	;;
    "Super RTL")
	MAP_NAME="$1"
	;;
    "VOX")
	MAP_NAME="$1"
	;;
    *)
	echo "svdrpsend.pl -p ${VDR_PORT:-2001} MESG no support for $1..." | at now
	exit 1
	;;
esac

at now <<EOT
if ping -c 1 werbesignal.dyndns.org ; then
    cmd(){ lynx -connect-timeout=1 -source werbesignal.dyndns.org/Werbesignal_client.php | grep "COLOR=lightpink><B>$MAP_NAME<"; }
    if [ -n "\$(cmd)" ] ; then
	date > /tmp/adzap.run
	while [ -a /tmp/adzap.run -a -n "\$(cmd)" -a $((TIMEOUT*60+$(date +%s))) -gt \$(date +%s) ] ; do
	    sleep 1s
	done
	if [ -a /tmp/adzap.run ] ; then
	    svdrpsend.pl -p ${VDR_PORT:-2001} CHAN "$1"
	    rm -f /tmp/adzap.run
	fi
    fi
else
     svdrpsend.pl -p ${VDR_PORT:-2001} MESG werbesignal.dyndns.org not available...
fi \
>> /dev/null 2>&1
EOT

# for {isdn,analog} try 5s or higher...
sleep 4s
