#!/bin/sh

#
#  Copyright (C) 2010 Funambol, Inc.  All rights reserved.
#

# Setting FUNAMBOL_HOME and DS_HOME
# resolving links - $0 could be a softlink
PRG="$0"
APPNAME=`basename "$PRG"`

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

PRGDIR=`dirname "$PRG"`

FUNAMBOL_HOME=`cd "$PRGDIR/.." ; pwd`

if [ ! -d $FUNAMBOL_HOME/config ]; then
    #
    # maybe we are in Funambol/tool/bin
    #
    FUNAMBOL_HOME=$FUNAMBOL_HOME/..
fi

DS_HOME=$FUNAMBOL_HOME/ds-server
CURRENT_DIR=$pwd

# Setting the JAVA_HOME to the bundle one if not set or if not correctly set
if [ -z "$JAVA_HOME" ] ; then
    JAVA_HOME="$FUNAMBOL_HOME/tools/jre-1.6.0/jre"
else
    if [ ! -f "$JAVA_HOME/bin/java" ] ; then
        JAVA_HOME="$FUNAMBOL_HOME/tools/jre-1.6.0/jre"
    fi
fi

if [ ! -f "$JAVA_HOME/bin/java" ]
then
    echo ". =================================================="
    echo "."
    echo ". Please, set JAVA_HOME to the path of a valid jdk."
    echo "."
    echo ". =================================================="
    exit;
fi

if [ "$1" = "" ] || [ "$2" = "" ] ; then

  echo "Usage: $APPNAME old-file new-file"
  echo "example: $APPNAME core-framework somewhere/core-framework-8.8.2.jar"
  echo ""
  exit 1
  
fi
  

# just in case...
chmod +x $DS_HOME/ant/bin/*
unset ANT_HOME

$DS_HOME/ant/bin/ant --noconfig -buildfile $FUNAMBOL_HOME/bin/patch.xml -Dbasedir=$CURRENT_DIR -Ddir=$FUNAMBOL_HOME -Dold-file=$1 -Dnew-file=$2 $3 $4 -q
