#!/bin/bash 

# Initial defaults
ECLIPSE_PATH=${ECLIPSE:-"eclipse"}
INSTALL_COPILOT=false
IDEMPIERE_SOURCE_FOLDER=${IDEMPIERE_SOURCE_FOLDER:-idempiere}

# Update these URLs as needed
XTEXT_RUNTIME_REPOSITORY=https://download.eclipse.org/modeling/tmf/xtext/updates/releases/2.35.0
TARGETPLATFORM_DSL_REPOSITORY=https://download.eclipse.org/cbi/updates/tpd/nightly/N202403260932
MWE_REPOSITORY=https://download.eclipse.org/modeling/emft/mwe/updates/releases/2.18.0/
EMF_REPOSITORY=https://download.eclipse.org/modeling/emf/emf/builds/release/2.38.0
LSP4_REPOSITORY="https://download.eclipse.org/lsp4e/releases/latest/"
COPILOT_REPOSITORY="https://azuredownloads-g3ahgwb5b8bkbxhd.b01.azurefd.net/github-copilot/"

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
    case $1 in
    --source)
    IDEMPIERE_SOURCE_FOLDER="$2"
    shift # past argument
    shift # past value
    ;;
    --eclipse)
    ECLIPSE_PATH="$2"
    shift # past argument
    shift # past value
    ;;
    --install-copilot) 
    INSTALL_COPILOT=true
    shift
    ;;
    --help)
    echo "Usage: setup-ws.sh [OPTION]"
    echo ""
    echo -e "  --eclipse <eclipse ide folder>"
    echo -e "\tSet eclipse ide folder (default is eclipse)"
    echo -e "  --source <idempiere source folder>"
    echo -e "\tSet idempiere source folder (default is idempiere)"
    echo -e "  --install-copilot"
    echo -e "\tAutomaticaly install copilot plugin (default is N)"
    echo -e "  --help"
    echo -e "\tdisplay this help and exit"
    exit 0
    ;;
    --*)
    echo "Unknown option $1"
    exit 1
    ;;
    *)
    POSITIONAL_ARGS+=("$1") # save positional arg
    shift
    ;;
    esac
done

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [[ $IDEMPIERE_SOURCE_FOLDER == "/*" ]]; then
    :
else
    IDEMPIERE_SOURCE_FOLDER="$DIR/$IDEMPIERE_SOURCE_FOLDER" 
fi

# --- Path Logic for macOS vs Linux/Windows ---
if [[ "$OSTYPE" == "darwin"* ]]; then
    # On Mac, we assume $ECLIPSE_PATH is 'Eclipse.app' or the symlink 'eclipse'
    if [[ -d "$ECLIPSE_PATH/Contents/Eclipse" ]]; then
        DESTINATION="$DIR/$ECLIPSE_PATH/Contents/Eclipse"
        ECLIPSE_EXE="$DIR/$ECLIPSE_PATH/Contents/MacOS/eclipse"
    else
        DESTINATION="$DIR/$ECLIPSE_PATH"
        ECLIPSE_EXE="$DIR/Eclipse.app/Contents/MacOS/eclipse"
    fi
    
    ARCH=$(uname -m)
    JUSTJ_ARCH="x86_64"
    [[ "$ARCH" == "arm64" ]] && JUSTJ_ARCH="aarch64"
    
    # Dynamically find the JustJ bundle to avoid hardcoding the timestamp
    JUSTJ_BUNDLE=$(ls "$DESTINATION/plugins" | grep "org.eclipse.justj.openjdk.hotspot.jre.full.macosx.${JUSTJ_ARCH}" | head -n 1)
    ECLIPSE_JRE="$DESTINATION/plugins/$JUSTJ_BUNDLE/jre"
else
    # Linux / Windows logic
    cd "$ECLIPSE_PATH"
    DESTINATION="$(pwd)"
    ECLIPSE_EXE="./eclipse"
    [[ "$OSTYPE" == "msys" ]] && JUSTJ_OS="win32" || JUSTJ_OS="linux"
    JUSTJ_BUNDLE=$(ls "./plugins" | grep "org.eclipse.justj.openjdk.hotspot.jre.full.${JUSTJ_OS}" | head -n 1)
    ECLIPSE_JRE="$DESTINATION/plugins/$JUSTJ_BUNDLE/jre"
fi

if [ "$JAVA_HOME" = "" ] ; then
  JAVA_HOME="$ECLIPSE_JRE"
fi

echo
echo "-------------------------------------------------------"
echo "Environment Summary:"
echo "  Eclipse Binary: $ECLIPSE_EXE"
echo "  Destination:    $DESTINATION"
echo "  JRE Path:       $ECLIPSE_JRE"
echo "  Source Folder:  $IDEMPIERE_SOURCE_FOLDER"
echo "-------------------------------------------------------"

echo
echo "*** Install XText Runtime ***"
echo
"$ECLIPSE_EXE" -vm "$ECLIPSE_JRE/bin/java" -nosplash -data "$IDEMPIERE_SOURCE_FOLDER" -application org.eclipse.equinox.p2.director \
    -repository $XTEXT_RUNTIME_REPOSITORY,$MWE_REPOSITORY,$EMF_REPOSITORY -destination "$DESTINATION" \
    -installIU "org.eclipse.xtext.runtime.feature.group,org.eclipse.xtext.ui.feature.group,org.eclipse.emf.mwe2.runtime,org.eclipse.emf.codegen.ecore.xtext,org.eclipse.emf.ecore.xcore.feature.group" 

echo
echo "*** Install CBI Target Platform DSL Editor ***"
echo
"$ECLIPSE_EXE" -vm "$ECLIPSE_JRE/bin/java" -nosplash -data "$IDEMPIERE_SOURCE_FOLDER" -application org.eclipse.equinox.p2.director \
    -repository $TARGETPLATFORM_DSL_REPOSITORY -destination "$DESTINATION" \
    -installIU org.eclipse.cbi.targetplatform.feature.feature.group

echo
echo "*** Run Workspace Setup Ant Task ***"
echo
"$ECLIPSE_EXE" -vm "$ECLIPSE_JRE/bin/java" -nosplash -data "$IDEMPIERE_SOURCE_FOLDER" -application org.eclipse.ant.core.antRunner -buildfile "$DIR/setup-ws.xml" -Didempiere="$IDEMPIERE_SOURCE_FOLDER"

echo
echo "*** Load Target Platform ***"
echo
"$ECLIPSE_EXE" -vm "$ECLIPSE_JRE/bin/java" -nosplash -data "$IDEMPIERE_SOURCE_FOLDER" -application org.eclipse.ant.core.antRunner -buildfile "$DIR/loadtargetplatform.xml" -Didempiere="$IDEMPIERE_SOURCE_FOLDER"

if [ "$INSTALL_COPILOT" = true ]; then
    echo
    echo "*** Install GitHub Copilot ***"
    echo
    "$ECLIPSE_EXE" -vm "$ECLIPSE_JRE/bin/java" -nosplash -data "$IDEMPIERE_SOURCE_FOLDER" -application org.eclipse.equinox.p2.director \
    -repository $LSP4_REPOSITORY,$COPILOT_REPOSITORY -destination "$DESTINATION" \
    -installIU "com.microsoft.copilot.eclipse.feature.feature.group" 
fi

cd "$DIR"
echo
echo "Workspace setup complete."