#!/bin/bash
# Stefan Huber, 2010-11-28


mainpageurl="http://www.oebb.at/de/Fahrplanauskunft/Aktuelles_Kursbuch/index.jsp"
mainpage=`curl -s $mainpageurl`
echo "$mainpage" > index.html

pdfs=`echo "$mainpage" | grep -o -E \
	"/de/Fahrplanauskunft/Aktuelles_Kursbuch/.*\.pdf" | \
	tr '\n' ' '`

rm -f index.txt
rm -f _*
for p in $pdfs; do


	fn=`basename $p`
	if [ -f "$fn" ]; then
		echo "Already got $fn"
	else
		wget "http://www.oebb.at/$p"
	fi


	if [ ! -z `echo "$fn" | grep kif` ]; then

		# Get description of pdf
		desc=`echo "$mainpage" | grep "$p" -A 1 | sed -e "1d" | sed -e "s_.*<td>\(.*\)</td>.*_\1_g"`
		echo -e "$fn\n\t$desc" >> index.txt

		lnfn=`echo "$desc" | tr -cds '[a-zA-Z0-9_\-]' '_'`

		ln -s $fn "_$lnfn.pdf"

	fi

done
