#!/usr/bin/env bash

# TODO Build the jar if it does not exist
# TODO Check if Java is installed.

# Determine the operating system
OS="$(uname)"

# Set the path to the JAR file
JAR_PATH='build/libs/2025LogProg-project-GhentProlog-0.1-SNAPSHOT.jar'

# Check if Java is installed
if ! command -v java &> /dev/null; then
    printf 'Error: Java is not installed. Please install Java to run this program.\n'
    printf '       Visit https://www.java.com/en/download/ for more information.\n'
    exit 1
fi

# Check if the JAR file exists
if [ ! -f "${JAR_PATH}" ]; then
    printf 'Info: JAR file not found at "%s"\n' "${JAR_PATH}"
    printf 'Info: Building the project...\n'
    ./gradlew fatJar
    if [ "${?}" -ne 0 ]; then
        printf 'Error: Build failed\n'
        exit 1
    fi
fi

# Run the Java command based on the operating system
if [[ "${OS}" == 'Linux' || "${OS}" == 'Darwin' ]]; then
    # Unix-based systems (Linux, macOS)
    java -jar "${JAR_PATH}" "$@"
elif [[ "${OS}" == *"NT"* ]]; then
    # Windows
    java -jar "${JAR_PATH}" "$@"
else
    printf 'Error: Unsupported operating system: "%s"\n' "${OS}"
    exit 1
fi

