#!/bin/bash
# Apply all Gibraltar patch packages after fetching them automatically
# Copyright Rene Mayrhofer 2003

LICENSE=/etc/gibraltar/license.key

if [ ! -r $LICENSE ]; then
  echo "No valid license found, online update aborted."
  exit 100
fi

if [ $# -gt 1 ] || [ $# -eq 1 -a "$2" != "skip-hooks" ]; then
  echo "Usage: $0 [skip-hooks]"
  exit 1
fi

version=`cat /system/etc-static/gibraltar/version`

wget -q -O - "http://www.gibraltar.at/getAllPatches.php?version=$version" \
	| sed 's/;/ /g' |
while read patch desc; do
  if [ ! -f /system/patches/$patch -a ! -f /etc/gibraltar/patches/$patch ];
  then
    wget -q -O /etc/gibraltar/patches/$patch \
    	--post-data=`cat $LICENSE` \
        "http://www.gibraltar.at/getPatch.php?version=$version&patch=$patch"
    if /sbin/patchme "/etc/gibraltar/patches/$patch" $1 >"/tmp/$patch.log" 2>&1; then
      echo "Patch file '$patch' applied successfully: $desc"
    else
      echo "Patch file '$patch' failed: errorcode $?:"
      cat "/tmp/$patch.log"
      rm "/etc/gibraltar/patches/$patch"
    fi
    rm "/tmp/$patch.log"
    # TODO: cleanup older patches in /etc/gibraltar/patches/
  fi
done

exit 0
