#!/bin/sh

################################################################################
#      Copyright (C) 2009-2010 OpenELEC.tv
#      http://www.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, 675 Mass Ave, Cambridge, MA 02139, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

[ -f /etc/update.conf ] && . /etc/update.conf
[ -f /storage/.config/update.conf ] && . /storage/.config/update.conf
[ -f /etc/repo.conf ] && . /etc/repo.conf || exit 0
[ -n "$KEYFILE" ] && SCP_ARG="-i $KEYFILE"

download () {
  case "$DL_METHOD" in
          wget)
            wget -c $1 -P /tmp
            ;;
          scp)
            scp $SCP_ARG "$1" /tmp
            ;;
        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" = "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`"
    THIS_MAJOR="`echo "$THIS_VERSION" | cut -d "." -f1`"
    THIS_MINOR="`echo "$THIS_VERSION" | cut -d "." -f2`"
    THIS_PATCH="`echo "$THIS_VERSION" | cut -d "." -f3`"

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

    NEW_IMAGE="`cat /tmp/latest |grep "$THIS_DISTRIBUTION-$THIS_ARCH"`"
    NEW_VERSION="`echo "$NEW_IMAGE" | cut -d "-" -f3`"

    rm -rf /tmp/latest

  # compare installed version with latest released version
    NEW_MAJOR="`echo "$NEW_VERSION" | cut -d "." -f1`"
    NEW_MINOR="`echo "$NEW_VERSION" | cut -d "." -f2`"
    NEW_PATCH="`echo "$NEW_VERSION" | cut -d "." -f3`"

    if [ "$THIS_MAJOR" -lt "$NEW_MAJOR" -o \
         "$THIS_MINOR" -lt "$NEW_MINOR" -o \
         "$THIS_PATCH" -lt "$NEW_PATCH" ]; then

      if [ "$AUTOUPDATE" = "manually" ]; then

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

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

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

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

      # downloading the new version
        download "$UPDATEURL/$NEW_IMAGE.tar.bz2"

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

      # move KERNEL and SYSTEM to an temporary file
        mkdir -p /storage/.update

        [ -f /tmp/$NEW_IMAGE/target/KERNEL ] && \
          mv /tmp/$NEW_IMAGE/target/KERNEL /storage/.update/KERNEL.tmp

        [ -f /tmp/$NEW_IMAGE/target/SYSTEM ] && \
          mv /tmp/$NEW_IMAGE/target/SYSTEM /storage/.update/SYSTEM.tmp
        sync

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

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

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

      fi
    fi
  fi
fi
