#!/usr/bin/env bash

set -e

: "${CALCITE_BUILD_DIR:=./calcite-build}"
: "${CALCITE_CURRENT:=1.41.0}"

# Load environment overrides from calcite_version.env file, if present
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="${SCRIPT_DIR}/calcite_version.env"
if [ -f "${ENV_FILE}" ]; then
    echo "Loading environment from ${ENV_FILE}"
    source "${ENV_FILE}"
fi

usage() {
    echo "This script builds the sql-to-dbsp compiler"
    echo "known options: -n -c"
    echo "-c use the current released (${CALCITE_CURRENT}) version of Calcite"
    echo "-n use the next (unreleased, ${CALCITE_NEXT}) version of Calcite"
    exit 1
}

update_pom() {
    VERSION=$1
    sed -i -E "s|<calcite.version>(.*)</calcite.version>|<calcite.version>${VERSION}</calcite.version>|" ./SQL-compiler/pom.xml
}

if [ "${CALCITE_BUILD_NEXT}" = "y" ]; then
    update_pom "${CALCITE_NEXT}"

    if [ ! -d "${CALCITE_BUILD_DIR}" ]; then
        echo "Cloning Calcite into ${CALCITE_BUILD_DIR}"
        git clone --depth 1 --quiet --single-branch --branch "${CALCITE_BRANCH}" ${GIT_ARGS} "${CALCITE_REPO}" "${CALCITE_BUILD_DIR}"
    else
        # Check if there is a valid checkout of Calcite
        REPO=`git -C ${CALCITE_BUILD_DIR} rev-parse --show-toplevel`
        REPO_PATH=$(realpath -- "${REPO}")
        CALCITE_PATH=$(realpath -- "${CALCITE_BUILD_DIR}")
        if [ "${REPO_PATH}" = "${CALCITE_PATH}" ]; then
            # This looks like a valid checkout of Calcite (* checks that REPO is a suffix of CALCITE_BUILD_DIR
            echo "Using existing Calcite source in ${CALCITE_BUILD_DIR}"
        else
            # No, clone it again
            rm -rf "${CALCITE_BUILD_DIR}"
            echo "Cloning Calcite into ${CALCITE_BUILD_DIR}"
            git clone --depth 1 --quiet --single-branch --branch "${CALCITE_BRANCH}" ${GIT_ARGS} "${CALCITE_REPO}" "${CALCITE_BUILD_DIR}"
        fi
    fi

    if [[ -n "${CALCITE_NEXT_COMMIT}" ]]; then
        pushd "${CALCITE_BUILD_DIR}" >/dev/null
        git fetch --prune "${CALCITE_REPO}" "${CALCITE_NEXT_COMMIT}"
        echo "Checking out commit ${CALCITE_NEXT_COMMIT}"
        git checkout --force "${CALCITE_NEXT_COMMIT}"
        popd >/dev/null
    fi

    pushd "${CALCITE_BUILD_DIR}" >/dev/null
    jvm_version=$(./gradlew --version | sed -n 's/^JVM: *//p')
    case $jvm_version in
        19* | 20* | 21*) ;;
        2[5-9]*)
            echo >&2 "*** ERROR *** Java version 25+ does not work with Calcite"
            exit 1
            ;;
        *)
            echo "*** WARNING *** Only Java versions 19, 20, and 21 are known to work with Calcite but you have $jvm_version"
            ;;
    esac
    ./gradlew build -x test -x checkStyleMain -x autoStyleJavaCheck build --console=plain -Dorg.gradle.logging.level=quiet

    for DIR in core linq4j; do
        ARTIFACT=calcite-${DIR}
        mvn install:install-file \
            -Dfile="${DIR}/build/libs/${ARTIFACT}-${CALCITE_NEXT}-SNAPSHOT.jar" \
            -DgroupId=org.apache.calcite \
            -DartifactId="${ARTIFACT}" \
            -Dversion="${CALCITE_NEXT}" \
            -Dpackaging=jar \
            -DgeneratePom=true -q -B
    done
    popd >/dev/null
else
    update_pom "${CALCITE_CURRENT}"
fi

mvn package -DskipTests --no-transfer-progress -DargLine="-ea" -q -B "$@"
