#!/bin/sh
set -e

# This script builds Psych java extension.
# I just copied the commands emitted by Rake.
# Should be useful when you are trying to build this package for the
# first time in a new archiecture. Not ideal, but for subsequent package
# builds since ruby-psych build depends on itself indirectly through jruby,
# the script will not be needed and the psych jar will be pulled from
# the previous release available in the archive.

BOOTSTRAP_PATH=tmp/bootstrap/java/psych
JRE=/usr/lib/jvm/default-java/jre
CLASSPATH=
MAVEN_REPO=/usr/share/maven-repo
EXT_JAVA_SRC=ext/java
EXT_JAVA_CLASSES=org/jruby/ext/psych

for jar in resources rt sunrsasign jsse jce charsets jfr; do
    CLASSPATH="${CLASSPATH}:${JRE}/lib/${jar}.jar"
done

CLASSPATH="${CLASSPATH}:${JRE}/classes"
CLASSPATH="${CLASSPATH}:/usr/share/jruby/lib/jruby.jar"
CLASSPATH="${CLASSPATH}:${MAVEN_REPO}/org/yaml/snakeyaml/1.x/snakeyaml-1.x.jar"

mkdir -p "${BOOTSTRAP_PATH}"

javac -extdirs "${JRE}/lib/ext:/usr/java/packages/lib/ext" \
    -target 1.7                          \
    -source 1.7                          \
    -Xlint:unchecked                     \
    -cp "${CLASSPATH}"                   \
    -d "${BOOTSTRAP_PATH}"               \
    "${EXT_JAVA_SRC}/PsychEmitter.java"  \
    "${EXT_JAVA_SRC}/PsychLibrary.java"  \
    "${EXT_JAVA_SRC}/PsychParser.java"   \
    "${EXT_JAVA_SRC}/PsychToRuby.java"   \
    "${EXT_JAVA_SRC}/PsychYamlTree.java"

jar cf "${BOOTSTRAP_PATH}/psych.jar" \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychEmitter\$1.class"            \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychEmitter.class"               \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychLibrary\$1.class"            \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychLibrary\$YAMLEncoding.class" \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychLibrary.class"               \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychParser\$1.class"             \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychParser.class"                \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychToRuby\$ClassLoader.class"   \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychToRuby\$ToRuby.class"        \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychToRuby.class"                \
    -C "${BOOTSTRAP_PATH}" "${EXT_JAVA_CLASSES}/PsychYamlTree.class"

install -c "${BOOTSTRAP_PATH}/psych.jar" lib/psych.jar
