#!/bin/sh

# Configuration variables

# Remove all class files in the interactivate directory to avoid versioning problems
# IF THE SCRIPT IS RUNNING TOO SLOW AND YOU ARE SURE YOU DON'T HAVE
# ANY JAVA 1.5+ CLASS FILES IN YOUR INTERACTIVATE DIRECTORY (FROM ECLIPSE
# OR ANOTHER JDK, THIS COMMANDE MAY BE COMMENTED OUT.  DO NOT COMMIT
# WITH THIS LINE COMMENTED OUT.
find . -name '*.class' -type f -print | xargs rm

# If Swing, gui14.  Else gui11
GUI=gui11

#  Interactivate directory of the builder's stable branch in Interactivate.
#  will assume that this directory exists in the user home directory
INTERACTIVATEROOT=$HOME/public_html/interactivate

# JDK Location - Use Java 1.4 compiler - lowest that Shodor supports
JAVAROOT=/usr/java/j2sdk1.4.2_16
TOOLFILE=tools.jar

# Temporary directory for .java files. Converter.java will be used to
# remove "package" references on these .java files.
TEMP_BUILD_DIR=$INTERACTIVATEROOT/tempBuildDir

# Update the stable branch to working copy
cd $INTERACTIVATEROOT

# Set svn:ignore properties on any new applets
svn propset -R svn:ignore -F ./svnignore.txt .

# Comment out for repetitive builds for efficiency if no need
# to build for every iteration.
svn update

# Make the orgclasses directory
mkdir $INTERACTIVATEROOT/orgclasses

# Compile the org folder to the orgclasses directory
$JAVAROOT/bin/javac -deprecation -d $INTERACTIVATEROOT/orgclasses -classpath $JAVAROOT/lib/$TOOLFILE:$INTERACTIVATEROOT org/shodor/events/*.java org/shodor/listeners/*.java org/shodor/layout/*.java org/shodor/math11/*.java org/shodor/data/*.java org/shodor/$GUI/*.java org/shodor/util11/*.java org/shodor/io/*.java

# Delete the previous org folder in applet directory
rm -rf $INTERACTIVATEROOT/$1/org

# Copy the org classes into the applet directory
# - switches - /s recursive
#              /e copy empty directories
#              /Q quiet mode
#              /Y replace files without prompting 
#xcopy %INTERACTIVATEROOT%\orgclasses %1 /s /e /Q /Y

# Copy org classes to the master folder.
cp -R $INTERACTIVATEROOT/orgclasses/org $INTERACTIVATEROOT/$1

# create temporary directory for .jar files
mkdir $TEMP_BUILD_DIR

# copy .java files to temp directory
cp -R $INTERACTIVATEROOT/$1 $TEMP_BUILD_DIR

# If we are building a slave applet, copy the slave's parameters.txt
# to the $TEMP_BUILD_DIR directory then pack parameters.txt in the jar.

# JASON HAD
# if [[ $2 != "" ]]; then
#   cp $INTERACTIVATEROOT/$2/parameters.txt $INTERACTIVATEROOT/$1
# fi  

# MDV - I THINK JASON MEANT
if [[ $2 != "" ]]; then
  cp $INTERACTIVATEROOT/$2/parameters.txt $TEMP_BUILD_DIR/$1
fi  

# Compile Converter.java to create executable to omit "package" instances
$JAVAROOT/bin/javac Converter.java

# Remove the "package" references as the applet will be self-contained.
# Converter is a Java application that does this.  (Author: Matt DesVoigne)
$JAVAROOT/bin/java Converter $TEMP_BUILD_DIR/$1

# Remove Converter class files
rm -f Converter.class
rm -f Converter$1.class

# Compile the applet against the local org directory
$JAVAROOT/bin/javac $TEMP_BUILD_DIR/$1/*.java

# Create the jar file against local class files, txt files, images, sound 
# filees, and local org files
cd $TEMP_BUILD_DIR/$1 
IMAGESTOJAR="images/*.gif images/*.jpg images/*.jpeg images/*.png";
if [[ $1 == "firealt" ]]; then
  IMAGESTOJAR="images/*.gif images/TreeImages10/*.gif
  images/TreeImages12/*.gif images/TreeImages15/*.gif";
fi
if [[ $1 == "fireestimguess" ]]; then
  IMAGESTOJAR="images/*.gif images/smallImages/*.gif
  images/mediumImages/*.gif images/largeImages/*.gif";
fi
$JAVAROOT/bin/jar -cmf $TEMP_BUILD_DIR/$1/META-INF/MANIFEST.MF $TEMP_BUILD_DIR/$1.jar *.class *.txt *.au *.wav $IMAGESTOJAR org 
cd ..

# if NOT building a slave applet, move the jar file to the applet directory
if [[ $2 == "" ]]; then
  mv $1.jar $INTERACTIVATEROOT/$1
fi

# if building a slave applet, move the jar file to the slave applet directory
if [[ $2 != "" ]]; then
  mv $1.jar $INTERACTIVATEROOT/$2
fi

# cleanup local directory
rm -rf $INTERACTIVATEROOT/$1/*.bat 
rm -rf $INTERACTIVATEROOT/$1/*.sh 
rm -rf $INTERACTIVATEROOT/$1/*.class 
# rm -rf $INTERACTIVATEROOT/$1/*.jar 
rm -rf $INTERACTIVATEROOT/$1/org 
rm -rf $INTERACTIVATEROOT/orgclasses
rm -rf $TEMP_BUILD_DIR

