#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  This Program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

TMP_DIR="$HOME/.xbmc/temp"

if [ -f /etc/update.conf ]; then
  . /etc/update.conf
elif [ -f /var/config/settings.conf ]; then
  . /var/config/settings.conf
  AUTOUPDATE="$UPDATE_AUTO"
else
  exit 0
fi

if [ -f /etc/repo.conf ]; then
  . /etc/repo.conf
  if [ -n "$KEYFILE" ]; then
    SCP_ARG="-i $KEYFILE"
  fi
else
  exit 0
fi

download () {
  case "$DL_METHOD" in
    wget)
      wget -U "$THIS_DISTRIBUTION ($THIS_ARCH): $THIS_VERSION" \
           -c ${1}?sysid=$SYSTEMID \
           -O $2 > /dev/null 2>&1
      ;;
    scp)
      scp $SCP_ARG "$1" $TMP_DIR
      ;;
  esac
}

send_message () {
  xbmc-send --host=127.0.0.1 -a "Notification(Automatic update service:,$1,20000)"
}

if [ ! -f /var/lock/update.lock ]; then

  if [ "$AUTOUPDATE" = "manually" -o "$AUTOUPDATE" = "manual" -o "$AUTOUPDATE" = "auto" ]; then

  # sleep a bit, maybe we have a lot of work ;-)
    usleep 30000000

  # getting this version
    THIS_DISTRIBUTION="`cat /etc/distribution`"
    THIS_ARCH="`cat /etc/arch`"
    THIS_VERSION="`cat /etc/version`"

  # get infofile with the latest released version
    rm -rf $TMP_DIR/latest
    download "$UPDATEURL/latest" "$TMP_DIR/latest"

    NEW_IMAGE="`cat $TMP_DIR/latest |grep "$THIS_DISTRIBUTION-$THIS_ARCH"`"
    NEW_VERSION="`echo "$NEW_IMAGE" | cut -d "-" -f5 | tr -d "r"`"

    rm -rf $TMP_DIR/latest

  # compare installed version with latest released version
    THIS_VERSION="`echo "$THIS_VERSION" | cut -d "-" -f3 | tr -d "r"`"

    if [ -z $NEW_VERSION ]; then
      NEW_VERSION="$THIS_VERSION"
    fi

    if [ "$THIS_VERSION" -lt "$NEW_VERSION" ]; then

      if [ "$AUTOUPDATE" = "manually" -o "$AUTOUPDATE" = "manual" ]; then

      # show a message if a new version is avaible
        send_message "New update avaible: r$NEW_VERSION - please update manually"

      elif [ "$AUTOUPDATE" = "auto" ]; then

      # show a message if a new version is avaible
        send_message "New update avaible: r$NEW_VERSION - downloading and extracting the new version..."

      # locking autoupdate
        touch /var/lock/update.lock

      # downloading the new version
        rm -rf $TMP_DIR/$NEW_IMAGE.tar.bz2
        download "$UPDATEURL/$NEW_IMAGE.tar.bz2" "$TMP_DIR/$NEW_IMAGE.tar.bz2"

      # extract the image
        rm -rf $TMP_DIR/$NEW_IMAGE
        tar -xjvf $TMP_DIR/$NEW_IMAGE.tar.bz2 -C $TMP_DIR

      # move KERNEL and SYSTEM to an temporary file
        mkdir -p /storage/.update
        if [ -f $TMP_DIR/$NEW_IMAGE/target/KERNEL -a -f $TMP_DIR/$NEW_IMAGE/target/KERNEL.md5 ]; then
          mv $TMP_DIR/$NEW_IMAGE/target/KERNEL /storage/.update/KERNEL.tmp
          mv $TMP_DIR/$NEW_IMAGE/target/KERNEL.md5 /storage/.update/KERNEL.md5.tmp
        fi
        if [ -f $TMP_DIR/$NEW_IMAGE/target/SYSTEM -a -f $TMP_DIR/$NEW_IMAGE/target/SYSTEM.md5 ]; then
          mv $TMP_DIR/$NEW_IMAGE/target/SYSTEM /storage/.update/SYSTEM.tmp
          mv $TMP_DIR/$NEW_IMAGE/target/SYSTEM.md5 /storage/.update/SYSTEM.md5.tmp
        fi
        sync

      # move KERNEL and SYSTEM to the right place
        mv /storage/.update/KERNEL.tmp /storage/.update/KERNEL
        mv /storage/.update/KERNEL.md5.tmp /storage/.update/KERNEL.md5
        mv /storage/.update/SYSTEM.tmp /storage/.update/SYSTEM
        mv /storage/.update/SYSTEM.md5.tmp /storage/.update/SYSTEM.md5
        sync

      # cleanup tmp files
        rm -rf $TMP_DIR/$NEW_IMAGE
        rm -rf $TMP_DIR/$NEW_IMAGE.tar.bz2
        rm -rf /storage/.update/*.tmp

      # we are ready (hopefully)
        send_message "Update r$NEW_VERSION - downloaded and extracted - please reboot to install"

      fi
    fi
  fi
fi
