#!/bin/sh
#######################
# Update Check Script #
#######################
#
# (C) by BPanther
# 07.03.2011, Version 1.00
#
# Port des WebServers (yWeb) - fuer OSD
# Standard
HTTP_Port="80"
# Automatisch aus Datei nhttpd.conf lesen (Pfad ggf. anpassen)
if [ -e "/var/tuxbox/config/nhttpd.conf" ]; then
   HTTP_Port=$(grep -i "port=" /var/tuxbox/config/nhttpd.conf | cut -d "=" -f 2)
   if [ ! "$HTTP_Port" ]; then
      HTTP_Port="80"
   fi
fi

# Meldungen: 0 = Aus, 1 = Console, 2 = Bildschirm
OSD="0"
#################################

Ausgabe() {
   if [ "$OSD" != "0" ]; then
      if [ "$OSD" == "1" ]; then
         echo "$MSG"
      fi
      if [ "$OSD" == "2" ]; then
         wget -q -O - http://127.0.0.1:$HTTP_Port/control/message?popup="$MSG" > /dev/null
      fi
   fi
}

Ausgabe2() {
   if [ "$OSD" != "0" ]; then
      if [ "$OSD" == "1" ]; then
         echo "$MSG"
      fi
      if [ "$OSD" == "2" ]; then
         wget -q -O - http://127.0.0.1:$HTTP_Port/control/message?nmsg="$MSG" > /dev/null
      fi
   fi
}

#################################

cd /tmp
updurl=`sed -n '1p' /var/etc/update.urls`
localversion=$(grep -i "version=" /var/etc/.version | cut -d "=" -f 2)
remoteversion=`wget -q -O - $updurl | awk '{print $2}' | head -n1`
if [ "$localversion" != "" ] && [ "$remoteversion" != "" ]; then
   localversion=${localversion:6}
   remoteversion=${remoteversion:6}
   result=`expr $localversion - $remoteversion`
   if [ "$result" -gt "-1" ]; then
      MSG=$MSG"Keine neue Version verfuegbar."
      rm -f /tmp/.update_avail
   else
      MSG=$MSG"Neue Version verfuegbar."
      touch /tmp/.update_avail
   fi
   Ausgabe2
else
   if [ "$localversion" == "" ]; then
      MSG=$MSG"Fehler: Konnte Box-Version nicht finden."
      rm -f /tmp/.update_avail
   fi
   if [ "$remoteversion" == "" ]; then
      MSG=$MSG"Fehler: Konnte Server-Version nicht finden."
      rm -f /tmp/.update_avail
   fi
   Ausgabe2
fi
exit 0