#!/bin/sh
#Update Skript V0.7
#Erstellt von HelAu und Martin

# EasyVDR Version
. /etc/vdr/static.conf
################################################################
URL1="http://ftp.gwdg.de/pub/linux/easyvdr/$EASYVDRVERS/online-updates"
###################################################
#Beta URL
URL2="http://$USER:$PASSWORD@www.easy-vdr.de/downloads/dev/online-updates/$EASYVDRVERS"


MD5_FILE="updates-$EASYVDRVERS.md5"
UPDATES=/_config/online-updates
mkdir $UPDATES  >/dev/null 2>&1
cd $UPDATES
rm -f  /_config/pre-update.sh  >/dev/null 2>&1
rm -f  /_config/post-update.sh  >/dev/null 2>&1

rm -f $MD5_FILE >/dev/null 2>&1
echo "starte easyVDR Update"
/usr/bin/svdrpsend.pl mesg !! Update wird gestartet !!
sleep 2

stopvdr


wget "${URL2}/${MD5_FILE}"

if [ -f $MD5_FILE ] ; then
       URL=$URL2
       date >>/etc/updates/betatest
       echo "Downloadquelle: Betatest" 
else
       wget "${URL1}/${MD5_FILE}"
       URL=$URL1
fi




ARCHS=$(cat $MD5_FILE | grep "\.tar\.bz2" | cut -f 3 -d " ")

for i in $ARCHS ; do
   if [ -f ${i} ] ; then
      echo "Checking md5sum <${i}> ..."
      MD5=$(md5sum ${i} | cut -f 1 -d " ")
      if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" != "" ] ; then
         echo "$i bereits verarbeitet"
         continue
      else
         rm -f ${i}
      fi    
   fi
   echo "Downloading <${i}> ..."
   wget ${URL}/${i}
   MD5=$(md5sum ${i} | cut -f 1 -d " ")
   if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" = "" ] ; then
      logger -s "Fehler bei Download von ${i}"
      exit -1
   fi
   logger -s "Entpacke <$i>"            
   tar -xjf $i _config/pre-update.sh
   sh _config/pre-update.sh 
   tar -C / -xjvf $i
   tar -xjf $i _config/post-update.sh  
   sh _config/post-update.sh
   logger -s "Starte <$INST_SCRIPT>"            

done
rm -f  /_config/pre-update.sh >/dev/null 2>&1
rm -f  /_config/post-update.sh >/dev/null 2>&1
rm -r  /_config/online-updates/_config >/dev/null 2>&1

echo "easyVDR Update fertiggestellt"

startvdr

sleep 20

if [ "$REBOOT" = "yes" ]; then
echo "Update finished - Reboot in 30 Seconds"
/usr/bin/svdrpsend.pl mesg !! Update fertig - Reboot in 30 Sekunden !!
sleep 30
reboot

else
/usr/bin/svdrpsend.pl mesg !! Update fertig - Kein Reboot erforderlich !!
echo "Kein Reboot erforderlich - Habe fertig"
fi









REBOOT=yes

sleep 5