#!/bin/bash

OS_ALLCAPS=`uname | tr 'a-z' 'A-Z'`

selfcon_ext=selfcontained-ant
installed_dir=${selfcon_ext}/apache-ant

if [ ! -d selfcontained-jdk ] ; then
    echo ""
    echo "Checking for java ..."
    command -v java 

    if [ $? != 0 ] ; then
	echo ""
	echo "Failed to find 'java'"
	echo "After 5 seconds, initiating: "
	echo ""
	echo "    ./get-selfcontained-jdk.sh"
	echo "[Press ^C to stop]"
	sleep 5
	./get-selfcontained-jdk.sh
    else
	echo ""
	echo "Detected 'java' but it is not the Greenstone3 supplied 'selfcontained-jdk'"
	echo "=> Recommend running:"
	echo ""
	echo "    ./get-selfcontained-jdk.sh"
	echo ""
	echo "After 5 seconds, continuing with getting ant"
	echo "[Press ^C to stop]"
	sleep 5
    fi
fi

echo ""
if [ ! -d $selfcon_ext ] ; then
    echo "Checking out from svn: Greenstone3's $selfcon_ext extension"
    svn co https://svn.greenstone.org/gs3-extensions/$selfcon_ext/trunk $selfcon_ext

    if [ $? != 0 ] ; then
	echo "Error encountered checking out: " 1>&2
	echo "    svn co https://svn.greenstone.org/gs3-extensions/$selfcon_ext/trunk $selfcon_ext" 1>&2
	exit 1
    fi
else
    echo "Detected directory '$selfcon_ext'"
    echo "=> Taken to mean that the svn check-out command has already been run"
fi

echo ""
if [ ! -d $installed_dir ] ; then
    echo "Runing the $selfcon_ext/PREPARE-UNIX.sh"

    cd $selfcon_ext && ./PREPARE-UNIX.sh

    if [ $? != 0 ] ; then
	echo "Error encountered running: " 1>&2
	echo "        cd $selfcon_ext && ./PREPARE-UNIX.sh" 1>&2
	exit 1
    fi
	
else
    echo "Detected directory '$installed_dir'"
    echo "=> Taken to mean that the $selfcon_ext/PREPARE-UNIX.sh command has already been run"    
fi


if [ "x$ANT_HOME" != "x$PWD/$installed_dir" ] ; then
    echo ""
    echo "To use this installed version of Apache Ant, in the top-level Greenstone3 directory run:"
    echo "    source ./gs3-setup-cli.sh"
    echo ""
else
    echo ""
    echo "Detected ANT_HOME set to \$PWD/$installed_dir"
    echo "=> Taken to mean that the $selfcon_ext SETUP.sh file has been sourced"
fi

echo ""
