#!/bin/sh
glilang=en


# This script must be run from within the directory in which it lives
thisdir=`pwd`
if [ ! -f "${thisdir}/makejar.sh" ]; then
    if [ "$glilang" = "es" ]; then
        echo "Este guión deberá ejecutarse desde el directorio en el que reside."
    elif [ "$glilang" = "fr" ]; then
	echo "Ce script doit être exécuté à partir du répertoire dans lequel il se trouve."
    elif [ "$glilang" = "ru" ]; then
	echo "üÔÏÔ ÓËÒÉÐÔ ÄÏÌÖÅÎ ÂÙÔØ ×ÚÑÔ ÉÚ ÄÉÒÅËÔÏÒÉÉ, × ËÏÔÏÒÏÊ ÏÎ ÒÁÓÐÏÌÏÖÅÎ"
    else
	echo "This script must be run from the directory in which it resides."
    fi
    exit 1
fi

## ---- Check that the GLI has been compiled ----
if [ ! -f "classes/org/greenstone/gatherer/GathererProg.class" ]; then
    echo
    if [ "$glilang" = "es" ]; then
	echo "Usted necesita compilar la Interfaz de la Biblioteca Digital Greenstone"
	echo "(por medio de makegli.sh) antes de ejecutar este guión."
    elif [ "$glilang" = "fr" ]; then
	echo "Vous devez compiler le Greenstone Interface (en utilisant makegli.sh)"
	echo "avant d'exécuter ce script."
    elif [ "$glilang" = "ru" ]; then
	echo "÷Ù ÄÏÌÖÎÙ ËÏÍÐÉÌÉÒÏ×ÁÔØ ÂÉÂÌÉÏÔÅÞÎÙÊ ÉÎÔÅÒÆÅÊÓ Greenstone"
	echo "(ÉÓÐÏÌØÚÕÑ makegli.sh) ÐÅÒÅÄ ××ÏÄÏÍ ÜÔÏÇÏ ÓËÒÉÐÔÁ"
    else
	echo "You need to compile the Greenstone Librarian Interface (using makegli.sh)"
	echo "before running this script."
    fi
    exit 1
fi

echo "Assuming that Java code is freshly compiled..."

# All the GLI class files and supporting libraries are put into the "jar" directory
if [ ! -d jar ]; then
  mkdir jar

  cd lib
  jar xf apache.jar com org
  jar xf qfslib.jar de

  mv com de org ../jar/.
  cd ..
fi

# Copy the latest version of the GLI classes into the jar directory
rm -rf jar/org/greenstone
cp -r classes/org/greenstone jar/org

# Some of the things to go into the JAR file are optional, and may not exist
optional=
if [ -f collect.zip ]; then
    optional="$optional collect.zip"
fi

# Recreate the metadata.zip file (contains the GLI metadata directory)
rm -f metadata.zip
zip -r metadata.zip metadata >/dev/null

# Jar everything up
jar cf GLI.jar .java.policy metadata.zip $optional help \
    -C classes dictionary.properties -C classes dictionary_es.properties \
    -C classes dictionary_fr.properties -C classes dictionary_ru.properties \
    -C classes feedback.properties -C classes images -C classes xml \
    -C jar com -C jar de -C jar org

# Generate the GLIServer.jar file for remote building
jar cf GLIServer.jar -C classes org/greenstone/gatherer/remote

# ---- Make signed JAR file for the applet, if desired ----
if [ "$1" = "-sign" ]; then
    rm -f SignedGatherer.jar
    echo "greenstone" | jarsigner -keystore .greenstonestore -signedjar SignedGatherer.jar GLI.jar privateKey 2> /dev/null
    echo
    echo "Installing SignedGatherer in ../bin/java"
    mv SignedGatherer.jar ../bin/java/.
fi
