#!/bin/bash
# Stellar Conflicts installer
# Downloads and installs the latest release for your platform.
#
# Usage: curl -fsSL https://releases.stellar-conflicts.com/scripts/install.sh | bash
#
# Environment variables:
#   SC_VERSION   - version to install (default: latest)
#   SC_BASE_URL  - download base URL (default: https://releases.stellar-conflicts.com)

set -euo pipefail

VERSION="${SC_VERSION:-latest}"
BASE_URL="${SC_BASE_URL:-https://releases.stellar-conflicts.com}"

detect_platform() {
    local os arch
    os="$(uname -s)"
    arch="$(uname -m)"

    case "$os" in
        Darwin) PLATFORM="macos" ;;
        Linux)  PLATFORM="linux" ;;
        *)      echo "Unsupported OS: $os"; exit 1 ;;
    esac

    case "$arch" in
        x86_64|amd64)  ARCH="x86_64" ;;
        arm64|aarch64) ARCH="aarch64" ;;
        *)             echo "Unsupported arch: $arch"; exit 1 ;;
    esac
}

install_macos() {
    local archive="StellarConflicts-${VERSION}-macos.tar.gz"
    local url="${BASE_URL}/${VERSION}/${archive}"
    local install_dir="$HOME/Applications"

    echo ":: Downloading ${url}..."
    curl -fSL "$url" -o "/tmp/${archive}"

    echo ":: Installing to ${install_dir}..."
    mkdir -p "$install_dir"
    tar xzf "/tmp/${archive}" -C "$install_dir"
    rm "/tmp/${archive}"

    # Remove quarantine flag
    xattr -dr com.apple.quarantine "$install_dir/Stellar Conflicts.app" 2>/dev/null || true

    echo ":: Installed to ${install_dir}/Stellar Conflicts.app"
    echo "   You can move it to /Applications if you prefer."
}

install_linux() {
    local archive="StellarConflicts-${VERSION}-linux.tar.gz"
    local url="${BASE_URL}/${VERSION}/${archive}"
    local install_dir="$HOME/.local/share/stellar-conflicts"
    local bin_dir="$HOME/.local/bin"

    echo ":: Downloading ${url}..."
    curl -fSL "$url" -o "/tmp/${archive}"

    echo ":: Installing to ${install_dir}..."
    mkdir -p "$install_dir" "$bin_dir"
    tar xzf "/tmp/${archive}" -C "$install_dir" --strip-components=1
    rm "/tmp/${archive}"

    # Symlink binary
    ln -sf "$install_dir/stellar-conflicts" "$bin_dir/stellar-conflicts"

    # Generate .desktop file with resolved paths
    local desktop_file="/tmp/stellar-conflicts.desktop"
    cat > "$desktop_file" << EOF
[Desktop Entry]
Name=Stellar Conflicts
Exec=${bin_dir}/stellar-conflicts
Icon=stellar-conflicts
Type=Application
Categories=Game;
Comment=2D multiplayer space combat
StartupWMClass=Stellar Conflicts Reforged
EOF
    xdg-desktop-menu install --novendor "$desktop_file" 2>/dev/null || true
    rm "$desktop_file"

    # Install icon
    if [ -f "$install_dir/stellar-conflicts.png" ]; then
        xdg-icon-resource install --novendor --size 1024 \
            "$install_dir/stellar-conflicts.png" stellar-conflicts 2>/dev/null || true
    fi

    echo ":: Installed. Binary at: ${bin_dir}/stellar-conflicts"
    echo "   Make sure ${bin_dir} is in your PATH."
}

detect_platform
echo "Stellar Conflicts installer (${PLATFORM}/${ARCH})"
echo ""

case "$PLATFORM" in
    macos) install_macos ;;
    linux) install_linux ;;
esac

echo ":: Done!"
