#!/usr/bin/env bash
# install.sh — Build Oxide Engine and install to the system.
#
# Installs:
#   /usr/local/bin/oxide-editor        — editor binary
#   /usr/local/share/oxide/assets/     — shared assets (icons, shaders, etc.)
#
# Usage:
#   ./install.sh               # installs to /usr/local (may need sudo)
#   PREFIX=/opt/oxide ./install.sh

set -euo pipefail

PREFIX="${PREFIX:-/usr/local}"
BIN_DIR="$PREFIX/bin"
SHARE_DIR="$PREFIX/share/oxide"

# ── Colour helpers ────────────────────────────────────────────────────────────
GREEN='\033[0;32m'
CYAN='\033[0;36m'
RED='\033[0;31m'
NC='\033[0m'

info()  { echo -e "${CYAN}[oxide]${NC} $*"; }
ok()    { echo -e "${GREEN}[oxide]${NC} $*"; }
err()   { echo -e "${RED}[oxide] ERROR:${NC} $*" >&2; exit 1; }

# ── Pre-flight ────────────────────────────────────────────────────────────────
command -v cargo &>/dev/null || err "cargo not found — install Rust via https://rustup.rs"

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# ── Build ─────────────────────────────────────────────────────────────────────
info "Building in release mode…"
cargo build --release --workspace

# ── Install binary ────────────────────────────────────────────────────────────
info "Installing oxide-editor → $BIN_DIR/oxide-editor"
install -Dm755 target/release/oxide-editor "$BIN_DIR/oxide-editor"

# ── Install assets ────────────────────────────────────────────────────────────
if [[ -d assets ]]; then
    info "Installing assets → $SHARE_DIR/assets/"
    install -d "$SHARE_DIR/assets"
    cp -r assets/. "$SHARE_DIR/assets/"
fi

# ── Done ──────────────────────────────────────────────────────────────────────
ok "Installation complete."
echo
echo "  Run the editor with:  oxide-editor"
echo "  Uninstall with:       sudo rm $BIN_DIR/oxide-editor && sudo rm -rf $SHARE_DIR"
