#!/bin/sh
UPD_DIR="/_config/update"
UPD_LOG="${UPD_DIR}/update.log"
URL_MAIN="www.htpc-forum.de/gen2vdr/2.0/fixes"
URL_TEST="www.htpc-forum.de/test/2.0/fixes"
URL=""
BLACKLIST=$(cat ${UPD_DIR}/.blacklist 2>/dev/null)

is_blacklisted() {
   for i in $BLACKLIST ; do
      case "$1" in
         *$i*) return 0;;
      esac
   done
   return 1
}

logmsg() {
   logger -s "$*" 2>&1 | tee -a $UPD_LOG
}

MD5_FILE="g2v_md5_fixes"

if [ "$1" = "--help" ] ; then
   echo "Syntax: $0 [-media|-t]"
   echo "       -media installiert Updates von CD/DVD/USB"
   echo "       -t     installiert Updates vom Testserver"
   echo "       -f     installiert trotz anderer VDR Version"
   exit
fi   

logmsg "$(date) - Starte $0"
cd $UPD_DIR
#Check for newer update script
UPD_SCRIPT=${0##*/}
[ -f ${UPD_SCRIPT}.new ] && rm -f ${UPD_SCRIPT}.new

if [ "$1" = "-media" ] ; then
   MEDIA_MD5=$(find -L /media -name $MD5_FILE -type f)
   [ "$MEDIA_MD5" == "" ] && echo "$MD5_FILE nicht vorhanden" && exit 1
   rm -f $MD5_FILE >/dev/null 2>&1
   MEDIA_PATH=$(dirname $MEDIA_MD5)
   cp -f "$MEDIA_MD5" .
   [ -f ${MEDIA_PATH}/${UPD_SCRIPT} ] && cp ${MEDIA_PATH}/${UPD_SCRIPT} ./${UPD_SCRIPT}.new
else
   if  [ "$1" = "-t" ] ; then
      URL=$URL_TEST
   else
      URL=$URL_MAIN
   fi            
   rm -f $MD5_FILE >/dev/null 2>&1
   wget --no-cache -O ${UPD_SCRIPT}.new "${URL}/${UPD_SCRIPT}"
   wget --no-cache "${URL}/${MD5_FILE}"
fi
if [ -s ${UPD_SCRIPT}.new  ] ; then
   if [ "$(diff -uN ${UPD_SCRIPT}.new ${UPD_SCRIPT})" != "" ] ; then
      chmod +x ${UPD_SCRIPT}.new 
      logmsg "Update Script wurde geaendert - Neustart des Updates"
      screen sh -c "cd $UPD_DIR; cp ${UPD_SCRIPT}.new ${UPD_SCRIPT}; sh ${UPD_SCRIPT} $1"
      exit 
   fi 
fi

#if [ "$1" != "-f" ] && [ "$1" != "-t" ] && [ "$(readlink /usr/local/src/VDR |grep "1.4.7-ext40")" = "" ] ; then
#   echo "Updates werden nur fuer die Standard VDR Variante unterstuetzt"
#   exit
#fi   

VS="$(pidof vdr)"

ARCHS=$(grep "\.tar\.bz2" g2v_md5_fixes | tr -s " " | cut -f 2- -d " " | sed -e "s%.*/%%" | sort)

for i in $ARCHS ; do
   if ( is_blacklisted "$i" ) ; then
      logmsg "<$i> ist auf der schwarzen Liste"
      continue
   fi
   BASENAME="${i/.tar.bz2/}"
   if [ "$1" != "-t" ] && [ -e ${BASENAME} -o -e ${BASENAME}.README ] ; then
      logmsg "$BASENAME wurde bereits verarbeitet"
      continue
   fi
   [ -f ${BASENAME}.sh ] && rm -f ${BASENAME}.sh

   GET_IT=1
   if [ -f ${i} ] ; then
      echo "Checking md5sum <${i}> ..."
      MD5=$(md5sum ${i} | cut -f 1 -d " ")
      if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" != "" ] ; then
         logmsg "$i bereits vorhanden"
         GET_IT=0
      else
         rm -f ${i}
	 logmsg "$i wird erneut geladen"
      fi    
   fi
   if [ "$GET_IT" = "1" ] ; then
      if [ "$URL" != "" ] ; then
         logmsg "Downloading <${i}> ..."
         wget ${URL}/${i}
      else
         if [ "$1" = "-media" ] ; then
            cp -f $MEDIA_PATH/$i /_config/update/
	 fi      
      fi   
      if [ -f $i ] ; then
         MD5=$(md5sum ${i} | cut -f 1 -d " ")
         if [ "$(grep ${i} ${MD5_FILE} | grep $MD5)" = "" ] ; then
            logmsg "Fehler bei Download/Kopieren von ${i}"
            continue
         fi
      else
         logmsg "Fehler bei Download/Kopieren von ${i}"
         continue
      fi
   fi 
   mkdir $BASENAME 2>/dev/null
   FILES=""
   UNDO_FILES=""
   for j in $(tar -tjf $i) ; do
      if ( ! is_blacklisted "$j" ) ; then
         FILES="$FILES $j" 
         [ -f /${j} ] && UNDO_FILES="$UNDO_FILES /${j}" 
      else
         logmsg "<$j> ist auf der schwarzen Liste"
      fi      
   done
   if [ "$UNDO_FILES" != "" ] && [ ! -f ${BASENAME}/${BASENAME}_undo.tar.bz2 ] ; then
      logmsg "Sichere <$UNDO_FILES>"
      tar -cjvf ${BASENAME}/${BASENAME}_undo.tar.bz2 $UNDO_FILES 2>&1 | tee -a $UPD_LOG
   else
      logmsg "Nichts zu sichern"
   fi
   if [ "$FILES" != "" ] ; then
      logmsg "Entpacke <$FILES>"
      tar -C / -xjvf ${i} $FILES 2>&1 | tee -a $UPD_LOG
   else
      logmsg "Nichts zu entpacken"
   fi
   if [ -f ${BASENAME}.sh ] ; then
      mv ${BASENAME}.sh ${BASENAME}/
      logger -s "Starte <${BASENAME}/${BASENAME}.sh>"            
      sh ${BASENAME}/${BASENAME}.sh 2>&1 | tee -a $UPD_LOG
   fi
done

if [ "$VS" != "" -a "$(pidof vdr)" = "" ] ; then
   /etc/init.d/vdr start
fi
logmsg "Update beendet"
logmsg ""
   
