#!/bin/sh
# melbabuild for Linux

#############################################
### Variables
#############################################

### here you have to set JAVA_HOME_BIN with the last character "\"
### example: JAVA_HOME_BIN=/usr/local/j2sdk/bin/

JAVA_HOME_BIN=???

JAVA_VM="$JAVA_HOME_BIN"java
JAVA_JAR="$JAVA_HOME_BIN"jar
JAVA_COMPIL="$JAVA_HOME_BIN"javac

### if you haven't the JDK 1.4 , you need an XML parser for Java (as CRIMSON from Apache)
### example : XMLPARSER=/usr/share/Apache/crimson/crimson.jar
### if you have the JDK 1.4, keep this variable empty

XMLPARSER=

### here you can set JAVAX_JAR to the complete path of the javax library
### It's required to make servlets. If you don't need to make servlets, keep
### the variable with nothing.
### example : JAVAX_JAR=/share/Apache/Tomcat/common/lib/servlet.jar

JAVAX_JAR=

### internal name of the melba package (not corresponding to the version of melba)

MELBA_PACKAGE=melba_a0

#############################################
### check JAVA_HOME_BIN
#############################################

if [ "$JAVA_HOME_BIN" = "???" ]
then
  echo .
  echo Please edit this file and set the JAVA_HOME_BIN variable.
  exit 1
fi

#############################################
### check parameters
#############################################

if [ "$1" = "" ]
then
  echo .
  echo Usage: melbabuild input_filename
  echo     or melbabuild input_filename output_filename
  echo .
  echo input_filename is the sashipa instance file, stored in '<MELBALABO-HOME>/src_xml'
  echo output_filename is the result file, that will be stored in '<MELBALABO-HOME>/result'
  echo The file extensions ".xml" and ".jar" must be not specified !
  echo .
  exit 1
fi

if [ "$2" = "" ]
then
  INPUT_XML=$1.xml
  OUTPUT_JAR=$1.jar
else
  INPUT_XML=$1.xml
  OUTPUT_JAR=$2.jar
fi

#############################################
### go to ./work/src_java and clean-up
#############################################

cd ./work/src_java
if ! [ $? -eq 0 ] ; then exit 1 ; fi

rm * >/dev/null 2>/dev/null

#############################################
### execute Sashipa2Melba on INPUT_XML then put result into ./work/src_java
#############################################

echo - Build Java source file\(s\) with Sashipa2Melba, from "$INPUT_XML"

"$JAVA_VM" -classpath ../../melbakit.jar:"$XMLPARSER" sashipamelba.Sashipa2Melba "../../src_xml/$INPUT_XML" $3 $4 $5 $6 $7 $8 $9
if ! [ $? -eq 0 ]
then
  if [ $? -eq 2 ]
  then
    @echo .
    @echo -- Perhap\'s you have found a bug in Sashipa2Melba.
    @echo -- Please send an email to bugreport@sashipamelba.com with the XML file.
  fi
  cd ../../
  exit 1
fi

#############################################
### restore melba compiled package to ./work/classes
#############################################

echo - Restore the compiled Melba package from melbakit.jar

cd ../classes/
if ! [ $? -eq 0 ] ; then exit 1 ; fi

rm -r * >/dev/null 2>/dev/null

"$JAVA_JAR" xf ../../melbakit.jar melba_classes.jar
if ! [ $? -eq 0 ] ; then exit 1 ; fi
"$JAVA_JAR" xf melba_classes.jar "$MELBA_PACKAGE"
if ! [ $? -eq 0 ] ; then exit 1 ; fi

#############################################
### compile to ./work/classes
#############################################

echo - Compile the Java source file\(s\) created by Sashipa2Melba

"$JAVA_COMPIL" -g:none -O -d . -classpath .:"$JAVAX_JAR" ../src_java/*.java
if ! [ $? -eq 0 ]
then
  echo .
  echo -- If you have this message, you have found a bug in Sashipa2Melba.
  echo -- Please send an email to bugreport@sashipamelba.com with the XML file.
  echo -- You can see manually problems in 'work/src_java/*.java' ...
  echo -- But you can correct the problem in your XML file. This problem is
  echo -- probably that you should have an error message when executing
  echo -- Sashipa2Melba.
  cd ../../
  exit 1
fi

#############################################
### delete previous archive in ./result/
#############################################

cd ../../result/
if ! [ $? -eq 0 ] ; then exit 1 ; fi
rm "$OUTPUT_JAR" >/dev/null 2>/dev/null

#############################################
### make archive into ./result/
#############################################

echo - Make archive "$OUTPUT_JAR" with the compiled files + Melba + the XML source file

cd ../work/classes/
if ! [ $? -eq 0 ] ; then exit 1 ; fi

cp "../../src_xml/$INPUT_XML" ./ >/dev/null
if ! [ $? -eq 0 ] ; then exit 1 ; fi

cp ../src_java/GuiManifest.mf ./ >/dev/null
if ! [ $? -eq 0 ]
then
  echo ERROR ! File GuiManifest.mf is missing...
  cd ../../
  exit 1
fi

"$JAVA_JAR" cfm "$OUTPUT_JAR" GuiManifest.mf "$MELBA_PACKAGE" *.class "$INPUT_XML"
if ! [ $? -eq 0 ] ; then exit 1 ; fi

cp "$OUTPUT_JAR" ../../result/ >/dev/null
if ! [ $? -eq 0 ] ; then exit 1 ; fi

cd ../../
if ! [ $? -eq 0 ] ; then exit 1 ; fi

#############################################
### end
#############################################

echo Build succeeded...
