#!/bin/sh
#set -x
URL_BASE="http://www.htpc-forum.de/gen2vdr/2.0"
URL="$URL_BASE/fixes"

MD5_FILE="g2v_md5_fixes"
OPTS="--no-cache "
CD_MD5_FILE="g2v_inst/_config/update/$MD5_FILE"
UP_MD5_FILE="g2v_md5_cdupdate"

rm -f $MD5_FILE $UP_MD5_FILE g2v_inst/$MD5_FILE >/dev/null 2>&1
wget $OPTS "${URL}/${UP_MD5_FILE}"
if [ ! -f $UP_MD5_FILE ] ; then
   OPTS=""
   wget "${URL}/${UP_MD5_FILE}"
fi

wget $OPTS "${URL}/${MD5_FILE}"
cat $UP_MD5_FILE >> $MD5_FILE

echo "Hole Docs"
for i in README INSTALL HISTORY ; do
   [ -f $i ] && rm $i
   wget $OPTS "$URL_BASE/$i"
done   

mkdir -p g2v_inst/_config/update
[ -f $CD_MD5_FILE ] && rm -f $CD_MD5_FILE
ARCHS=$(cat $MD5_FILE | cut -f 3 -d " ")
#set -x
for i in $ARCHS ; do
   found=""
   if [ -f ${i} ] ; then
      echo "Checking md5sum <${i}> ..."
      MD5=$(md5sum ${i} | cut -f 1 -d " ")
      ORG_MD5=$(grep "${i}" ${MD5_FILE}| cut -f 1 -d " ")
      if [ "$MD5" = "$ORG_MD5" ] ; then
         found="1"
      else
         rm -f ${i}
      fi
   fi
   if [ "$found" = "" ] ; then
      echo "Downloading <${i}> ..."
      wget $OPTS ${URL}/${i}
      MD5=$(md5sum ${i} | cut -f 1 -d " ")
      if [ "$(grep "${i}" "${MD5_FILE}" | grep $MD5)" = "" ] ; then
         echo "Error getting ${i}"
         exit -1
      fi
   fi
   if [ "$i" = "g2v_cdupdate.tar.bz2" ] ; then
      bzip2 -d -c $i > g2v_cdupdate.tar
      tar -xvf g2v_cdupdate.tar
      rm g2v_cdupdate.tar
   else
      cp -f $i g2v_inst/_config/update
      echo "$MD5  $i" >> $CD_MD5_FILE
   fi
done

