#!/bin/bash

# Remote Installer for 3D Virtual CD Drive
# This script fetches files from your server and installs the tool.

BASE_URL="http://projectfiles.aaronbroughton.com/vcd_package"
INSTALL_PATH="/usr/local/bin/vcd"

echo "------------------------------------------"
echo "   3D Virtual CD Drive - Remote Installer "
echo "------------------------------------------"

# 1. Check for Xcode Command Line Tools
if ! xcode-select -p &>/dev/null; then
    echo "[!] Error: Xcode Command Line Tools not found."
    echo "[*] Please run: xcode-select --install"
    exit 1
fi

# 2. Create temporary workspace
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR"
echo "[*] Fetching files from $BASE_URL..."

# 3. Download the files
curl -s -O "$BASE_URL/main_3d.mm"
curl -s -O "$BASE_URL/Makefile"

if [ ! -f "main_3d.mm" ] || [ ! -f "Makefile" ]; then
    echo "[!] Error: Could not fetch source files from the server."
    echo "[*] Check if the URL is accessible: $BASE_URL"
    exit 1
fi

# 4. Compile
echo "[*] Compiling native 3D app (SceneKit/C++)..."
# The Makefile name might need to be 'Makefile' (case sensitive)
if clang++ -Wall -O2 -std=c++11 -framework AppKit -framework Foundation -framework SceneKit main_3d.mm -o vcd3d; then
    echo "[*] Installing to /usr/local/bin (requires sudo)..."
    sudo cp vcd3d "$INSTALL_PATH"
    sudo chmod +x "$INSTALL_PATH"
    echo ""
    echo "------------------------------------------"
    echo "   DONE! 3D Virtual CD Drive Installed    "
    echo "   Type 'vcd' to begin.                   "
    echo "------------------------------------------"
else
    echo "[!] Error: Compilation failed."
    exit 1
fi

# 5. Cleanup
rm -rf "$TEMP_DIR"
