#!/bin/sh

#  Gant -- A Groovy way of scripting Ant tasks.
#
#  Copyright © 2006-9 Russel Winder
#
#  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
#  compliance with the License. You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software distributed under the License is
#  distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
#  implied. See the License for the specific language governing permissions and limitations under the
#  License.
#
#  Author : Russel Winder <russel.winder@concertant.com>

#  Gant initiation script for Linux and UNIX -- requires Groovy version.

#  Solaris 10 does not have readlink as standard -- though some people will have the version from
#  Sunfreeware which is the same as the version on Ubuntu, Cygwin, MSYS, etc.  Mac OS X has a version of
#  readlink that is very different to that on Ubuntu, Solaris/Sunfreeware, Cygwin, MSYS, etc.
#
#  This function attempts to just do the right thing.

doReadLink ( ) {
    case `uname` in
        Darwin )
            gantLocation=`dirname $1`
            gantPath=`readlink "$1"`
            if [ "$gantPath" = "" ]
            then
                readlinkValue="$1"
            else
                linkDir=`dirname $gantPath`
                currentDirectory=`pwd`
                cd $gantLocation
                cd $linkDir
                gantLocation=`pwd`
                gantPath=`basename $gantPath`
                readlinkValue="$gantLocation/$gantPath"
                cd $currentDirectory
            fi
            ;;
        SunOS )
            readlinkPath=`which readlink`
            if [ `expr "$readlinkPath" : '\([^ ]*\)'` = "no" ]
            then
                echo "No readlink command available, please set $2."
                exit 1
            else
                readlinkValue=`readlink -f $1`
            fi
            ;;
        * )
            readlinkValue=`readlink -f $1`
            ;;
    esac
}

#  If GANT_HOME is not set, deduce a path.  Assume the executable is in $GANT_HOME/bin.

if [ -z "$GANT_HOME" ]
then
    if [ -h $0 ]
    then
        doReadLink $0 GANT_HOME
        GANT_HOME=$readlinkValue
        GANT_HOME=`dirname $GANT_HOME`
    else
        GANT_HOME=`dirname $0`
    fi
    GANT_HOME=`dirname $GANT_HOME`
fi

#  If GROOVY_HOME is not set, deduce a path -- this is needed in order to discover the location of the
#  startGroovy script.  Assume the executable is in $GROOVY_HOME/bin.

if [ -z "$GROOVY_HOME" ]
then
    GROOVY_HOME=`which groovy`
    if [ -h $GROOVY_HOME ]
    then
        doReadLink $GROOVY_HOME GROOVY_HOME
        GROOVY_HOME=$readlinkValue
    fi
    GROOVY_HOME=`dirname "$GROOVY_HOME"`
    GROOVY_HOME=`dirname "$GROOVY_HOME"`
fi

#  If ANT_HOME is not set, deduce a path -- this is needed in order to discover the location of the jars
#  associated with the Ant installation.  Assume the executable is in $ANT_HOME/bin.

if [ -z "$ANT_HOME" ]
then
    ANT_HOME=`which ant`
    if [ -h $ANT_HOME ]
    then
        doReadLink $ANT_HOME ANT_HOME
        ANT_HOME=$readlinkValue
    fi
    ANT_HOME=`dirname "$ANT_HOME"`
    ANT_HOME=`dirname "$ANT_HOME"`
fi

GROOVY_APP_NAME=Gant
GROOVY_CONF="$GANT_HOME/conf/gant-starter.conf"

. "$GROOVY_HOME/bin/startGroovy"

if $cygwin
then
    GANT_HOME=`cygpath --mixed "$GANT_HOME"`
    ANT_HOME=`cygpath --mixed "$ANT_HOME"`
fi
JAVA_OPTS="$JAVA_OPTS -Dgant.home=$GANT_HOME -Dant.home=$ANT_HOME"

startGroovy gant.Gant "$@"
