#!/bin/sh

exec >$0.log 2>&1

APPLECOMMANDER_FILES=$(cd com; \
	find . -name "*.java" -not -name "*Test.java" -not -name "ImageIoImage.java" \
		-not -name "SunJpegImage.java" -print |
	sed 's#^\./#com/#')

OPTS="-static -O3 --no-bounds-checking -funroll-loops -finline-functions \
	-fkeep-inline-functions -malign-double"

echo "**********************************************"
echo "**                  COMPILING               **"
echo "**********************************************"
echo

for i in $APPLECOMMANDER_FILES
do
	OBJ_FILE=$(echo $i | sed 's/\//_/g' | sed 's/\.java$/\.o/')
	echo Compiling $i to $OBJ_FILE
	gcj --classpath="AppleCommander.jar" $OPTS -g0 -c -o $OBJ_FILE $i
done

echo "**********************************************"
echo "**                 RESOURCES                **"
echo "**********************************************"
echo
for i in $(ls com/webcodepro/applecommander/ui/images/*.gif \
	   com/webcodepro/applecommander/storage/*.dump \
	   com/webcodepro/applecommander/storage/os/prodos/*.properties)
do
	FILENAME=$(basename $i)
	OBJ=$FILENAME.o
	echo Compiling resource $i
	gcj -c --resource=$i -o $OBJ $i
done

echo "**********************************************"
echo "**                 BUILD EXE                **"
echo "**********************************************"
echo
gcj -s -fjni -mwindows --classpath="AppleCommander.jar" $OPTS \
	--main=com.webcodepro.applecommander.ui.AppleCommander \
	-o AppleCommander *.o

echo "**********************************************"
echo "**                   CLEAN                  **"
echo "**********************************************"
echo
rm *.o

echo "**********************************************"
echo "**             STRIPPING EXE                **"
echo "**********************************************"
echo
strip -x AppleCommander.exe -o AppleCommander-strip.exe

# Only run UPX if it is on the command path:
UPX=$(type -p upx)
if [ "$UPX" ] && [ -x $UPX ]
then
    echo "**********************************************"
    echo "**           PACKING EXE WITH UPX           **"
    echo "** (will need to choose the smallest exe..) **"
    echo "**********************************************"
    echo
    upx --best --crp-ms=999999 --nrv2b -o AppleCommander-strip-nrv2b.exe AppleCommander-strip.exe
    upx --best --crp-ms=999999 --nrv2d -o AppleCommander-strip-nrv2d.exe AppleCommander-strip.exe
    upx --best --crp-ms=999999 -o AppleCommander-strip-upxbest.exe AppleCommander-strip.exe
    upx -o AppleCommander-strip-upxdefault.exe AppleCommander-strip.exe
fi

echo "**********************************************"
echo "**                   DONE!!                 **"
echo "**********************************************"
