#!/bin/bash
# Bash/Zsh Java Environment Switch - {{env_name}}
# Generated by fnva

# Remove existing Java paths from PATH
NEW_PATH=""
IFS=':' read -ra ADDR <<< "$PATH"
for i in "${ADDR[@]}"; do
    if [[ ! "$i" =~ java && ! "$i" =~ jdk ]]; then
        if [[ -z "$NEW_PATH" ]]; then
            NEW_PATH="$i"
        else
            NEW_PATH="$NEW_PATH:$i"
        fi
    fi
done

# Set new JAVA_HOME and update PATH
export JAVA_HOME="{{java_home}}"
export PATH="{{java_bin}}:$NEW_PATH"

# Set fnva environment tracking
export FNVA_CURRENT_JAVA="{{env_name}}"
export FNVA_ENV_TYPE="Java"

# Verify the switch
echo "✅ Switched to Java environment: {{env_name}}"
echo "📁 JAVA_HOME: $JAVA_HOME"
echo "🔍 Java Version:"
if [ -x "{{java_bin}}/java" ]; then
    "{{java_bin}}/java" -version 2>&1 | head -n 1 | sed 's/^/   /'
else
    echo "   Failed to get Java version"
fi

# Add to shell history
echo "fnva java use {{env_name}}" >> ~/.fnva/history 2>/dev/null || true