#!/bin/bash

# Configuration
LINK_PATH="$HOME/.espressif"
TARGET="/eecs/dept/www/www.eecs.yorku.ca/course_archive/2025-26/W/2021/esp/.espressif"

# 1. Validation Logic
if [ -e "$LINK_PATH" ] || [ -L "$LINK_PATH" ]; then
    # Resolve the current link to its absolute canonical path
    # 'readlink -m' handles the path even if the target is missing
    CURRENT_POINT=$(readlink -m "$LINK_PATH")

    if [[ "$CURRENT_POINT" == "$TARGET" ]]; then
        echo "✅ Valid symlink to course directory detected."
    else
        echo "❌ Link mismatch!"
        echo "Current: $CURRENT_POINT"
        echo "Target:  $TARGET"
        echo "Instruction: Run 'rm -rf $LINK_PATH' and try again."
        exit 1
    fi
else
    # Create the link if it doesn't exist
    echo "✨ Link missing. Creating symlink to course archive..."
    ln -s "$TARGET" "$LINK_PATH"
fi

# 2. Direct Launch
exec /eecs/dept/www/www.eecs.yorku.ca/course_archive/2025-26/W/2021/esp/Espressif-IDE/espressif-ide

