#!/bin/bash

set -e # Exit immediately if any command fails

MAIN_CLASS="dothack/DotHack.java"
PROJECT_NAME="dh_musicfix"
SOURCE_PATH="src"
PATH_LIBRARIES="lib"
PATH_PROGUARD="/home/sillysagiri/Documents/dothack/proguard.jar"
CLASS_VERSION="1.4"

###

rm -rf "build"
mkdir -p "build/class"

###

CLASSPATH=$(find "$PATH_LIBRARIES" -type f | tr '\n' ':' | sed 's/:$//')

# compile class
javac -source $CLASS_VERSION -target $CLASS_VERSION -cp $CLASSPATH -encoding "utf-8" -d "build/class" -sourcepath $SOURCE_PATH -Xlint:-options -g "${SOURCE_PATH}/${MAIN_CLASS}"

# pack .jar
jar cvfm "build/${PROJECT_NAME}_not_preverified.jar" "${SOURCE_PATH}/META-INF/MANIFEST.MF" -C "build/class" . -C "assets" .

# preverify
java -jar $PATH_PROGUARD -microedition -injars "build/${PROJECT_NAME}_not_preverified.jar" -outjars "build/${PROJECT_NAME}.jar" -libraryjars $CLASSPATH -dontshrink -dontoptimize -dontobfuscate -target $CLASS_VERSION

# release build
# java -jar $PATH_PROGUARD -microedition -injars "build/${PROJECT_NAME}_not_preverified.jar" -outjars "build/${PROJECT_NAME}_release.jar" -libraryjars $CLASSPATH -target $CLASS_VERSION -keep "class * extends javax.microedition.midlet.MIDlet" -optimizationpasses 4 -repackageclasses "" -allowaccessmodification