#!/bin/bash
# 💩 / ☠️ Cross-platform Installer with full aliases
set -e

# ----------- FUNCTIONS -----------

detect_os() {
    OS_NAME="$(uname -s)"
    case "$OS_NAME" in
        Darwin) OS="mac";;
        Linux) OS="linux";;
        MINGW*|MSYS*|CYGWIN*) OS="windows";;
        *) echo "❌ Unsupported OS: $OS_NAME"; exit 1;;
    esac
    echo "Detected OS: $OS"
}

force_symlink() {
    local target="$1"
    local linkname="$2"
    if [ -e "$linkname" ] || [ -L "$linkname" ]; then
        rm -f "$linkname"
        echo "🧹 Removed existing $linkname"
    fi
    ln -s "$target" "$linkname" 2>/dev/null || echo "⚠️ Could not create symlink $linkname (may require admin privileges)"
    echo "🔗 Linked $linkname → $target"
}

ensure_raku() {
    if ! command -v raku &>/dev/null; then
        echo "📦 Raku not found. Installing..."
        if [ "$OS" = "mac" ]; then
            if ! command -v brew &>/dev/null; then
                echo "❌ Homebrew not found. Please install it first: https://brew.sh/"
                exit 1
            fi
            brew install rakudo-star
        elif [ "$OS" = "linux" ]; then
            if command -v apt &>/dev/null; then
                sudo apt update && sudo apt install -y rakudo
            elif command -v yum &>/dev/null; then
                sudo yum install -y rakudo
            else
                echo "❌ Unsupported Linux package manager. Install Raku manually."
                exit 1
            fi
        elif [ "$OS" = "windows" ]; then
            echo "❌ Windows detected. Please install Raku manually from https://rakudo.org/downloads/"
            exit 1
        fi
        echo "✅ Raku installed."
    else
        echo "✅ Raku already installed."
    fi
}

# ----------- MAIN -----------

detect_os
ensure_raku

echo "Installing Raku modules..."
echo "Installing JSON::Fast..."
zef install JSON::Fast --force

echo "🔧 Installing 💩 and ☠️ scripts..."

DEST="/usr/local/bin"
if [ "$OS" = "windows" ]; then
    DEST="$HOME/bin"
    mkdir -p "$DEST"
fi

echo "🔧 Copying main scripts and aliases to $DEST..."

# List of files to copy
FILES=(☠️ ☠️.raku skull dead rekt die rip 💩 💩.raku poop poo shit caca crap dung feces turd "doo-doo" 💩💩 💩💩💩 dump)

for file in "${FILES[@]}"; do
    if [[ -f "$file" ]]; then
        sudo cp "$file" "$DEST/$file"
        sudo chmod +x "$DEST/$file"
        echo "✅ Copied $file → $DEST/$file"
    else
        echo "⚠️ File $file not found, skipping..."
    fi
done

echo "✅ All listed files copied and made executable."

# List installed files
ls -l "$DEST/💩" "$DEST/☠️" "$DEST/💩.raku" "$DEST/☠️.raku"

# ----------- CREATE SYMLINKS -----------

# Full runtime aliases
RUNTIME_ALIASES=(poop poo shit caca crap dung feces turd "doo-doo" 💩💩 💩💩💩 dump)
for alias in "${RUNTIME_ALIASES[@]}"; do
    force_symlink "$DEST/💩" "$DEST/$alias"
done

# Compiler aliases
COMPILER_ALIASES=(skull dead rekt die rip)
for alias in "${COMPILER_ALIASES[@]}"; do
    force_symlink "$DEST/☠️" "$DEST/$alias"
done

echo "✅ Installation complete! All aliases installed in $DEST."
