#!/bin/sh
URL_FILES="http://ftp.gwdg.de/pub/linux/mediaportal/helau/1.1/files"
URL_FIXES="http://www.htpc-forum.de/gen2vdr/1.1/fixes"
OPTS=""
MD5_FILE="g2v_md5_files"

rm -f $MD5_FILE >/dev/null 2>&1
wget "${URL_FILES}/${MD5_FILE}"

ARCHS=$(cat $MD5_FILE | cut -f 3 -d " ")
if [ "${ARCHS}" = "" ] ; then
   echo "Error getting ${MD5_FILE}"
   exit -1
fi

for i in $ARCHS ; do
   WP=""
   if [ -f ${i} ] ; then
      echo "Checking md5sum <${i}> ..."
      MD5=$(md5sum ${i} | cut -f 1 -d " ")
      if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" != "" ] ; then
         continue
      else
         WP="-c "
      fi
   fi
   echo "Downloading <${i}> ..."
   wget $WP ${URL_FILES}/${i}
   MD5=$(md5sum ${i} | cut -f 1 -d " ")
   if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" = "" ] ; then
      echo "Error getting ${i}"
      exit -1
   fi
done

if [ -f g2v_boot.tar.bz2 ] ; then
   if [ ! -f g2v_boot.tar ] ; then
      bzip2 -d -c g2v_boot.tar.bz2 > g2v_boot.tar
   fi
   tar -xvf g2v_boot.tar
   rm g2v_boot.tar
fi

if [ -e g2v_inst ] ; then
   rm -rf g2v_inst > /dev/null 2>&1
fi
mkdir g2v_inst

if [ -f g2v_inst.tar.bz2 ] ; then
   if [ ! -f g2v_inst.tar ] ; then
      bzip2 -d -c g2v_inst.tar.bz2 > g2v_inst.tar
   fi
   tar -xvf g2v_inst.tar
   rm g2v_inst.tar
fi

rm -f g2v_cdupdate.sh > /dev/null 2>&1
wget "${URL_FIXES}/g2v_cdupdate.sh" > /dev/null 2>&1

if [ -f g2v_cdupdate.sh ] ; then
   . ./g2v_cdupdate.sh ${URL_FIXES}
fi
