#! /bin/sh

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

DATADIR=$1
echo "getcontent.sh DATADIR = $DATADIR"
# ----- RESULT ----- //
BULIRESULTURL="http://linpop.zdf.de/sport/buli/head.php"
GET1="wget -O - -q"
CONTENT=`$GET1 $BULIRESULTURL |sed  -e '/.* - .*/!d' -e '/.* - .*/{n;!p;}' |sed -e 's/.*<td.*0">//' -e 's/<\/a><\/td>//'  -e 's/.*<td.*">//' -e 's/<.*\/td>//' -e 's/\(.*:.*\)/ \1\n/' |grep -v "<.*>"` 
BULIRESULTFILE="$DATADIR/ergebnisse.txt"
#
#
# ----- GET RESULTS ----- //
IFS=""
#
for i in $CONTENT
do
  START=`echo "$i"|grep \-`
  if [ "$START"x != "x" ]
  then
    echo ""
    echo "$i"
  else
    echo "$i"
    echo ""
  fi
done
#
echo $CONTENT > $BULIRESULTFILE
#
#
#
# ----- RANKING ----- //
BULIRANKINGURL="http://linpop.zdf.de/sport/buli/blitztabelle.htm"
BULIRANKINGFILE="$DATADIR/blitztabelle.htm"
CMD=`wget -O  $BULIRANKINGFILE $BULIRANKINGURL`
#
# ----- GET RANKING ----- //
#
sed -e \
's/&nbsp;9/09/' -e \
's/&nbsp;8/08/' -e \
's/&nbsp;7/07/' -e \
's/&nbsp;6/06/' -e \
's/&nbsp;18/18/' -e \
's/&nbsp;17/17/' -e \
's/&nbsp;16/16/' -e \
's/&nbsp;15/15/' -e \
's/&nbsp;14/14/' -e \
's/&nbsp;13/13/' -e \
's/&nbsp;12/12/' -e \
's/&nbsp;11/11/' -e \
's/&nbsp;10/10/' -e \
's/&nbsp;5/05/' -e \
's/&nbsp;4/04/' -e \
's/&nbsp;3/03/' -e \
's/&nbsp;2/02/' -e \
's/&nbsp;1/01/' -e \
's/Schalke 04/Schalke 04             /' -e \
's/MSV Duisburg/MSV Duisburg              /' -e \
's/Stuttgart/Stuttgart             /' -e \
's/ln/ln                 /' -e \
's/Bremen/Bremen             /' -e \
's/nchen/nchen         /' -e \
's/Hamburger SV/Hamburger SV              /' -e \
's/Leverkusen/Leverkusen       /' -e \
's/Dortmund/Dortmund         /' -e \
's/Wolfsburg/Wolfsburg             /' -e \
's/Bielefeld/Bielefeld         /' -e \
's/Hannover 96/Hannover 96               /' -e \
's/nberg/nberg             /' -e \
's/Mainz 05/Mainz 05            /' -e \
's/Bochum/Bochum               /' -e \
's/Rostock/Rostock         /' -e \
's/Freiburg/Freiburg      /' -e \
's/Berlin/Berlin         /' -e \
's/Kaiserslautern/Kaiserslautern       /' -e \
's/Frankfurt/Frankfurt       /' -e \
's/gladbach/gladbach       /' -e \
's/Aachen/Aachen        /' -e \
's/Cottbus/Cottbus         /' -e \
's/<img.*.">//' -e 's/<td.*.">//' -e 's/<td>//' -e 's/<.*.td>//' -e 's/.*<tr.*">//' -e 's/<.*\/tr>//' -e \
's/<tr>//' -e 's/<table.*>//' -e 's/<*.table>//' -e 's/<meta.*>//' -e 's/<!DOCTYPE HTML.*>//' -e 's/<html.*>//' -e 's/<*.head.*>//' -e \
's/<link.*>//' -e 's/<body.*>//' -e 's/-Ticker.*>//' -e 's/<title.*>//' -e 's/Champion.*e//' -e 's/CL-Qualif.*n//' -e 's/UEFA-Cup//' -e \
's/Abstieg//' -e 's/<*.body>//' -e 's/<*.html>//' -e 's/Blitztabelle//' -e 's/Mannschaft//' -e 's/Sp//' -e 's/Tore//' -e 's/P//' -e \
's/ZD.*l//g' $DATADIR/blitztabelle.htm > $DATADIR/test2.txt 
#
sed -e '/^[^0-9a-zA-Z!-\/]*$/d' $DATADIR/test2.txt > $DATADIR/test3.txt
#
if [ -s $DATADIR/test3.txt ]; then
 rm $DATADIR/tabelle.txt > /dev/null
 a5="GO"
 line=1
 while [ ! -z "$a5" ]
 do
 a1=`sed -n "$line"p $DATADIR/test3.txt` # Platzierung
 line=`expr "$line" + 1`
 a2=`sed -n "$line"p $DATADIR/test3.txt` # Club
 line=`expr "$line" + 1`
 a3=`sed -n "$line"p $DATADIR/test3.txt` # Spiele
 line=`expr "$line" + 1`
 a4=`sed -n "$line"p $DATADIR/test3.txt` # Torverhältnis
 line=`expr "$line" + 1`
 a5=`sed -n "$line"p $DATADIR/test3.txt` # Punkte
 line=`expr $line + 1`
 echo "$a1 $a2 $a3 $a4 $a5" >> $DATADIR/test4.txt
 done
rm $DATADIR/blitztabelle.htm
rm $DATADIR/test2.txt
rm $DATADIR/test3.txt
 sed '19,20d' $DATADIR/test4.txt > $DATADIR/tabelle.txt
rm $DATADIR/test4.txt
fi
cat $DATADIR/tabelle.txt
