<commit_before># NOTE: THIS SCRIPT IS SUPPOSED TO RUN IN A POSIX SHELL

cd "$(dirname "$0")"
TIGHTDB_JAVA_HOME="$(pwd)"

MODE="$1"
[ $# -gt 0 ] && shift


DEP_JARS="commons-io.jar commons-lang.jar freemarker.jar"

JAR_DIR="$TIGHTDB_JAVA_HOME/lib"


word_list_append()
{
    local list_name new_word list
    list_name="$1"
    new_word="$2"
    list="$(eval "printf \"%s\\n\" \"\${$list_name}\"")" || return 1
    if [ "$list" ]; then
        eval "$list_name=\"\$list \$new_word\""
    else
        eval "$list_name=\"\$new_word\""
    fi
    return 0
}

word_list_prepend()
{
    local list_name new_word list
    list_name="$1"
    new_word="$2"
    list="$(eval "printf \"%s\\n\" \"\${$list_name}\"")" || return 1
    if [ "$list" ]; then
        eval "$list_name=\"\$new_word \$list\""
    else
        eval "$list_name=\"\$new_word\""
    fi
    return 0
}

remove_suffix()
{
    local string suffix match_x
    string="$1"
    suffix="$2"
    match_x="$(printf "%s" "$string" | tail -c "${#suffix}" && echo x)" || return 1
    if [ "$match_x" != "${suffix}x" ]; then
        printf "%s\n" "$string"
        return 0
    fi
    printf "%s" "$string" | sed "s/.\{${#suffix}\}\$//" || return 1
    echo
    return 0
}



# Setup OS specific stuff
OS="$(uname)" || exit 1
ARCH="$(uname -m)" || exit 1
LIB_SUFFIX_SHARED=".so"
STAT_FORMAT_SWITCH="-c"
NUM_PROCESSORS=""
ABSORB_DEP_JARS=""
if [ "$OS" = "Darwin" ]; then
    LIB_SUFFIX_SHARED=".dylib"
    STAT_FORMAT_SWITCH="-f"
    NUM_PROCESSORS="$(sysctl -n hw.ncpu)" || exit 1
    word_list_prepend MAKEFLAGS "-w" || exit 1
    # Absorbing standard JAR files into tightdb-devkit.jar is a fantasticly bad idea, we must back out of this approach as soon as possible!!!
    ABSORB_DEP_JARS="1"
else
    if [ -r /proc/cpuinfo ]; then
        NUM_PROCESSORS="$(cat /proc/cpuinfo | grep -E 'processor[[:space:]]*:' | wc -l)" || exit 1
    fi
fi
if [ "$NUM_PROCESSORS" ]; then
    word_list_prepend MAKEFLAGS "-j$NUM_PROCESSORS" || exit 1
fi
export MAKEFLAGS
USE_LIB64=""
IS_REDHAT_DERIVATIVE=""
if [ -e /etc/redhat-release ] || grep -q "Amazon" /etc/system-release 2>/dev/null; then
    IS_REDHAT_DERIVATIVE="1"
fi
if [ "$IS_REDHAT_DERIVATIVE" -o -e /etc/SuSE-release ]; then
    if [ "$ARCH" = "x86_64" -o "$ARCH" = "ia64" ]; then
        USE_LIB64="1"
    fi
fi
JNI_SUFFIX="$LIB_SUFFIX_SHARED"
JNI_LIBDIR="lib" # Absolute, or relative to installation prefix
JAVA_INC="include"
JAVA_BIN="bin"
if [ "$OS" = "Darwin" ]; then
    JNI_LIBDIR="/Library/Java/Extensions"
    JNI_SUFFIX=".jnilib"
    JAVA_INC="Headers"
    JAVA_BIN="Commands"
elif [ "$USE_LIB64" ]; then
    JNI_LIBDIR="/usr/lib64"
else
    JNI_LIBDIR="/usr/lib"
fi



readlink_f()
{
    local LINK TARGET
    LINK="$1"
    if ! TARGET="$(readlink "$LINK")"; then
        printf "%s\n" "$LINK"
        return 0
    fi
    readlink_f "$TARGET"
}

same_path_target()
{
    local A B A_ID B_ID
    A="$1"
    B="$2"
    if [ -e "$A" -a -e "$B" ]; then
        A_ID="$(stat -L "$STAT_FORMAT_SWITCH%d:%i" "$A")" || exit 1
        B_ID="$(stat -L "$STAT_FORMAT_SWITCH%d:%i" "$B")" || exit 1
        if [ "$A_ID" = "$B_ID" ]; then
            return 0
        fi
    fi
    return 1
}

# Find 'jni.h', 'java' and 'javac'
find_java()
{
    if [ -z "$JAVA_HOME" -o \! -e "$JAVA_HOME/$JAVA_BIN/javac" ]; then
        if ! JAVAC="$(which javac)"; then
            echo "No JAVA_HOME and no Java compiler in PATH" 1>&2
            return 1
        fi
        JAVAC="$(readlink_f "$JAVAC")" || return 1
        JAVA_HOME="$(remove_suffix "$JAVAC" "/$JAVA_BIN/javac")" || return 1
        if [ "$JAVA_HOME" = "$JAVAC" ]; then
            echo "Could not determine JAVA_HOME from path of 'javac' command" 1>&2
            return 1
        fi
    fi
    for x in "$JAVA_INC/jni.h" "$JAVA_BIN/java" "$JAVA_BIN/javac"; do
        if ! [ -f "$JAVA_HOME/$x" ]; then
            echo "No '$x' in '$JAVA_HOME'" 1>&2
            return 1
        fi
    done
    JAVA="$JAVA_HOME/$JAVA_BIN/java"
    JAVAC="$JAVA_HOME/$JAVA_BIN/javac"
    JAVA_VERSION="$(java -version 2>&1 | grep '^java version' | sed 's/.*"\(.*\).*"/\1/')"
    JAVA_MAJOR="$(printf "%s\n" "$JAVA_VERSION" | cut -d. -f1)" || return 1
    JAVA_MINOR="$(printf "%s\n" "$JAVA_VERSION" | cut -d. -f2)" || return 1
    if ! [ "$JAVA_MAJOR" -gt 1 -o "$JAVA_MAJOR" -eq 1 -a "$JAVA_MINOR" -ge 6 ] 2>/dev/null; then
        echo "Need Java version 1.6 or newer (is '$JAVA_VERSION')" 1>&2
        return 1
    fi
}



DEP_JAR_PATHS=""
for x in $DEP_JARS; do
    if [ "$ABSORB_DEP_JARS" -a -d "dep_jars" ]; then
        word_list_append DEP_JAR_PATHS "$TIGHTDB_JAVA_HOME/dep_jars/$x"
    else
        word_list_append DEP_JAR_PATHS "/usr/share/java/$x"
    fi
done



case "$MODE" in

    "clean")
        cd "$TIGHTDB_JAVA_HOME/tightdb_jni/src" || exit 1
        make clean || exit 1
        if [ "$JNI_SUFFIX" != "$LIB_SUFFIX_SHARED" ]; then
            rm -f "libtightdb-jni$JNI_SUFFIX" || exit 1
        fi
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-core" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-generator" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-test" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        rm -f "$JAR_DIR/tightdb.jar" || exit 1
        rm -f "$JAR_DIR/tightdb-devkit.jar" || exit 1
        exit 0
        ;;

    "build")
        find_java || exit 1

        # Build libtightdb-jni.so
        cd "$TIGHTDB_JAVA_HOME/tightdb_jni/src" || exit 1
        TIGHTDB_ENABLE_FAT_BINARIES="1" make EXTRA_CFLAGS="-I$JAVA_HOME/$JAVA_INC -I$JAVA_HOME/$JAVA_INC/linux" || exit 1
        if [ "$JNI_SUFFIX" != "$LIB_SUFFIX_SHARED" ]; then
            ln -f "libtightdb-jni$LIB_SUFFIX_SHARED" "libtightdb-jni$JNI_SUFFIX" || exit 1
        fi

        # Build tightdb.jar
        mkdir -p "$JAR_DIR" || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-core/src/main" || exit 1
        (cd java && $JAVAC                        com/tightdb/*.java com/tightdb/internal/*.java  com/tightdb/lib/*.java)  || exit 1
        (cd java && jar cf "$JAR_DIR/tightdb.jar" com/tightdb/*.class com/tightdb/internal/*.class com/tightdb/lib/*.class) || exit 1
        jar i "$JAR_DIR/tightdb.jar" || exit 1

        # Build tightdb-devkit.jar
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-generator/src/main" || exit 1
        CLASSPATH="$(printf "%s\n" "$DEP_JAR_PATHS" | sed 's/  */:/g'):$JAR_DIR/tightdb.jar" || exit 1
        export CLASSPATH
        # FIXME: Must run ResourceGenerator to produce java/com/tightdb/generator/Templates.java
        TEMP_DIR="$(mktemp -d /tmp/tightdb.java.build.XXXX)" || exit 1
        MANIFEST="$TEMP_DIR/MANIFEST.MF"
        touch "$MANIFEST" || exit 1
        if [ "$ABSORB_DEP_JARS" ]; then<commit_msg>Adjusted the build script to the new package name "typed".<commit_after># NOTE: THIS SCRIPT IS SUPPOSED TO RUN IN A POSIX SHELL

cd "$(dirname "$0")"
TIGHTDB_JAVA_HOME="$(pwd)"

MODE="$1"
[ $# -gt 0 ] && shift


DEP_JARS="commons-io.jar commons-lang.jar freemarker.jar"

JAR_DIR="$TIGHTDB_JAVA_HOME/lib"


word_list_append()
{
    local list_name new_word list
    list_name="$1"
    new_word="$2"
    list="$(eval "printf \"%s\\n\" \"\${$list_name}\"")" || return 1
    if [ "$list" ]; then
        eval "$list_name=\"\$list \$new_word\""
    else
        eval "$list_name=\"\$new_word\""
    fi
    return 0
}

word_list_prepend()
{
    local list_name new_word list
    list_name="$1"
    new_word="$2"
    list="$(eval "printf \"%s\\n\" \"\${$list_name}\"")" || return 1
    if [ "$list" ]; then
        eval "$list_name=\"\$new_word \$list\""
    else
        eval "$list_name=\"\$new_word\""
    fi
    return 0
}

remove_suffix()
{
    local string suffix match_x
    string="$1"
    suffix="$2"
    match_x="$(printf "%s" "$string" | tail -c "${#suffix}" && echo x)" || return 1
    if [ "$match_x" != "${suffix}x" ]; then
        printf "%s\n" "$string"
        return 0
    fi
    printf "%s" "$string" | sed "s/.\{${#suffix}\}\$//" || return 1
    echo
    return 0
}



# Setup OS specific stuff
OS="$(uname)" || exit 1
ARCH="$(uname -m)" || exit 1
LIB_SUFFIX_SHARED=".so"
STAT_FORMAT_SWITCH="-c"
NUM_PROCESSORS=""
ABSORB_DEP_JARS=""
if [ "$OS" = "Darwin" ]; then
    LIB_SUFFIX_SHARED=".dylib"
    STAT_FORMAT_SWITCH="-f"
    NUM_PROCESSORS="$(sysctl -n hw.ncpu)" || exit 1
    word_list_prepend MAKEFLAGS "-w" || exit 1
    # Absorbing standard JAR files into tightdb-devkit.jar is a fantasticly bad idea, we must back out of this approach as soon as possible!!!
    ABSORB_DEP_JARS="1"
else
    if [ -r /proc/cpuinfo ]; then
        NUM_PROCESSORS="$(cat /proc/cpuinfo | grep -E 'processor[[:space:]]*:' | wc -l)" || exit 1
    fi
fi
if [ "$NUM_PROCESSORS" ]; then
    word_list_prepend MAKEFLAGS "-j$NUM_PROCESSORS" || exit 1
fi
export MAKEFLAGS
USE_LIB64=""
IS_REDHAT_DERIVATIVE=""
if [ -e /etc/redhat-release ] || grep -q "Amazon" /etc/system-release 2>/dev/null; then
    IS_REDHAT_DERIVATIVE="1"
fi
if [ "$IS_REDHAT_DERIVATIVE" -o -e /etc/SuSE-release ]; then
    if [ "$ARCH" = "x86_64" -o "$ARCH" = "ia64" ]; then
        USE_LIB64="1"
    fi
fi
JNI_SUFFIX="$LIB_SUFFIX_SHARED"
JNI_LIBDIR="lib" # Absolute, or relative to installation prefix
JAVA_INC="include"
JAVA_BIN="bin"
if [ "$OS" = "Darwin" ]; then
    JNI_LIBDIR="/Library/Java/Extensions"
    JNI_SUFFIX=".jnilib"
    JAVA_INC="Headers"
    JAVA_BIN="Commands"
elif [ "$USE_LIB64" ]; then
    JNI_LIBDIR="/usr/lib64"
else
    JNI_LIBDIR="/usr/lib"
fi



readlink_f()
{
    local LINK TARGET
    LINK="$1"
    if ! TARGET="$(readlink "$LINK")"; then
        printf "%s\n" "$LINK"
        return 0
    fi
    readlink_f "$TARGET"
}

same_path_target()
{
    local A B A_ID B_ID
    A="$1"
    B="$2"
    if [ -e "$A" -a -e "$B" ]; then
        A_ID="$(stat -L "$STAT_FORMAT_SWITCH%d:%i" "$A")" || exit 1
        B_ID="$(stat -L "$STAT_FORMAT_SWITCH%d:%i" "$B")" || exit 1
        if [ "$A_ID" = "$B_ID" ]; then
            return 0
        fi
    fi
    return 1
}

# Find 'jni.h', 'java' and 'javac'
find_java()
{
    if [ -z "$JAVA_HOME" -o \! -e "$JAVA_HOME/$JAVA_BIN/javac" ]; then
        if ! JAVAC="$(which javac)"; then
            echo "No JAVA_HOME and no Java compiler in PATH" 1>&2
            return 1
        fi
        JAVAC="$(readlink_f "$JAVAC")" || return 1
        JAVA_HOME="$(remove_suffix "$JAVAC" "/$JAVA_BIN/javac")" || return 1
        if [ "$JAVA_HOME" = "$JAVAC" ]; then
            echo "Could not determine JAVA_HOME from path of 'javac' command" 1>&2
            return 1
        fi
    fi
    for x in "$JAVA_INC/jni.h" "$JAVA_BIN/java" "$JAVA_BIN/javac"; do
        if ! [ -f "$JAVA_HOME/$x" ]; then
            echo "No '$x' in '$JAVA_HOME'" 1>&2
            return 1
        fi
    done
    JAVA="$JAVA_HOME/$JAVA_BIN/java"
    JAVAC="$JAVA_HOME/$JAVA_BIN/javac"
    JAVA_VERSION="$(java -version 2>&1 | grep '^java version' | sed 's/.*"\(.*\).*"/\1/')"
    JAVA_MAJOR="$(printf "%s\n" "$JAVA_VERSION" | cut -d. -f1)" || return 1
    JAVA_MINOR="$(printf "%s\n" "$JAVA_VERSION" | cut -d. -f2)" || return 1
    if ! [ "$JAVA_MAJOR" -gt 1 -o "$JAVA_MAJOR" -eq 1 -a "$JAVA_MINOR" -ge 6 ] 2>/dev/null; then
        echo "Need Java version 1.6 or newer (is '$JAVA_VERSION')" 1>&2
        return 1
    fi
}



DEP_JAR_PATHS=""
for x in $DEP_JARS; do
    if [ "$ABSORB_DEP_JARS" -a -d "dep_jars" ]; then
        word_list_append DEP_JAR_PATHS "$TIGHTDB_JAVA_HOME/dep_jars/$x"
    else
        word_list_append DEP_JAR_PATHS "/usr/share/java/$x"
    fi
done



case "$MODE" in

    "clean")
        cd "$TIGHTDB_JAVA_HOME/tightdb_jni/src" || exit 1
        make clean || exit 1
        if [ "$JNI_SUFFIX" != "$LIB_SUFFIX_SHARED" ]; then
            rm -f "libtightdb-jni$JNI_SUFFIX" || exit 1
        fi
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-core" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-generator" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-test" || exit 1
        find src/ -type f -name '*.class' -delete || exit 1
        rm -f "$JAR_DIR/tightdb.jar" || exit 1
        rm -f "$JAR_DIR/tightdb-devkit.jar" || exit 1
        exit 0
        ;;

    "build")
        find_java || exit 1

        # Build libtightdb-jni.so
        cd "$TIGHTDB_JAVA_HOME/tightdb_jni/src" || exit 1
        TIGHTDB_ENABLE_FAT_BINARIES="1" make EXTRA_CFLAGS="-I$JAVA_HOME/$JAVA_INC -I$JAVA_HOME/$JAVA_INC/linux" || exit 1
        if [ "$JNI_SUFFIX" != "$LIB_SUFFIX_SHARED" ]; then
            ln -f "libtightdb-jni$LIB_SUFFIX_SHARED" "libtightdb-jni$JNI_SUFFIX" || exit 1
        fi

        # Build tightdb.jar
        mkdir -p "$JAR_DIR" || exit 1
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-core/src/main" || exit 1
        (cd java && $JAVAC                        com/tightdb/*.java com/tightdb/internal/*.java  com/tightdb/typed/*.java)  || exit 1
        (cd java && jar cf "$JAR_DIR/tightdb.jar" com/tightdb/*.class com/tightdb/internal/*.class com/tightdb/typed/*.class) || exit 1
        jar i "$JAR_DIR/tightdb.jar" || exit 1

        # Build tightdb-devkit.jar
        cd "$TIGHTDB_JAVA_HOME/tightdb-java-generator/src/main" || exit 1
        CLASSPATH="$(printf "%s\n" "$DEP_JAR_PATHS" | sed 's/  */:/g'):$JAR_DIR/tightdb.jar" || exit 1
        export CLASSPATH
        # FIXME: Must run ResourceGenerator to produce java/com/tightdb/generator/Templates.java
        TEMP_DIR="$(mktemp -d /tmp/tightdb.java.build.XXXX)" || exit 1
        MANIFEST="$TEMP_DIR/MANIFEST.MF"
        touch "$MANIFEST" || exit 1
        if [ "$ABSORB_DEP_JARS" ]; then
{'fraction_non_alphanumeric': 0.16830386378519974, 'fraction_numerical': 0.014843920541366514, 'mean_word_length': 2.537709137709138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6896890', 'n_tokens_mistral': 5764, 'n_tokens_neox': 5289, 'n_words': 1254}