#!/bin/bash

package=TDBJava
version=

progname=$0

source ../cascade-make/lib/cascade-lib.bash GEXTTDBEDIT ../.. $*

STARTTS=`date +"%s.%3N"`
echo
echo "========= Compiling TDBJava  ========="
# Check for existing JAR... skip if it's already there!
JARPATH="${GEXTTDBEDIT_INSTALLED}/lib/TDBJava.jar"
if [ -e "${JARPATH}" ]; then
    echo " * TDBJava library already compiled. To recompile, remove: ${JARPATH}"
else
    # Ensure Greenstone3 home is set
    if [ -z "${GSDL3HOME}" ]; then
	echo "Error! GSDL3HOME environment variable not set... have you run gs3-setup.sh in installed GSDL3 directory."
	exit 1
    fi
    echo " Found GSDL3HOME: ${GSDL3HOME}"
    WEBLIB="${GSDL3HOME}/WEB-INF/lib"
    echo " Found GSDL3 web library: ${WEBLIB}"
    # Ensure we have access to JAVAC
    if [ -z "${JAVA_HOME}" ]; then
	echo "Error! JAVA_HOME environment variable not set. Verify Java is installed and export JAVA_HOME to point to location."
	exit 1
    fi
    echo " Found JAVA_HOME: ${JAVA_HOME}"
    # And also that the path to the installed files for TDBEdit has been set
    if [ -z "${GEXTTDBEDIT}" ]; then
	echo "Error! GEXTTDBEDIT not set"
	exit 1
    fi
    echo " Found GEXTTDBEDIT: ${GEXTTDBEDIT}"
    echo " Found GEXTTDBEDIT_INSTALLED: ${GEXTTDBEDIT_INSTALLED}"
    # Ensure the we have a class version of FlatDatabaseWrapper
    if [ ! -f "${WEBLIB}/gsdl3.jar" ]; then
	echo "Error! Greenstone3 JAR not available. Change to '${GSDL3HOME}' and run 'ant'"
	exit 1
    fi
    echo " Found GSDL3 library: ${WEBLIB}/gsdl3.jar"
    # And that we have a log4j jar
    pushd "${WEBLIB}"
    log4jjar=`find * -name "log4j-?.?.?.jar" 2>&1`
    popd
    if [ "${log4jjar}x" == "x" ]; then
	echo "Error! No Log4J JAR available in: ${WEBLIB}'"
	exit 1
    fi
    echo " Found Log4J library: ${WEBLIB}/${log4jjar}"
    CLASSPATH=`mktemp -d 2>/dev/null || mktemp -d -t 'tdbjava'`
    echo " * Created temporary directory for classes: ${CLASSPATH}"
    # Compiling the TDBJava code itself is straightforward
    # The TDBWrapper, unfortunately, needs a bit more work as it needs a class path
    # to Apache and GSDL3 class files
    SOURCEPATH="${GEXTTDBEDIT}/src/java/org/greenstone/tdbjava"
    CWD=`pwd`
    cd "${SOURCEPATH}"
    `javac -cp "${WEBLIB}/${log4jjar}:${WEBLIB}/gsdl3.jar" -d ${CLASSPATH} *.java`
    cd "${CWD}"
    echo " * Compiled source files to classes"
    `jar -cvf "${JARPATH}" -C ${CLASSPATH}/ . 2>&1 > /dev/null`
    echo " * Gathered class files into JAR archive: ${JARPATH}"
    `rm ${CLASSPATH}/org/greenstone/tdbjava/*.class`
    `rmdir ${CLASSPATH}/org/greenstone/tdbjava`
    `rm ${CLASSPATH}/org/greenstone/gsdl3/util/*.class`
    `rmdir ${CLASSPATH}/org/greenstone/gsdl3/util`
    `rmdir ${CLASSPATH}/org/greenstone/gsdl3`
    `rmdir ${CLASSPATH}/org/greenstone`
    `rmdir ${CLASSPATH}/org`
    `rmdir ${CLASSPATH}`
    echo " * Removed temporary directory"
fi
# Print out a nice duration message
ENDTS=`date +"%s.%3N"`
# HACK - ensure that BC prints the answer with a leading 0 before the point
ELAPSED=`echo "scale=3;x=${ENDTS}-${STARTTS};if (x<1) print 0;x" | bc`
echo "===== Complete in ${ELAPSED} seconds! ====="
echo
exit 0
