#!/bin/bash
# CamCast Relay Agent — macOS Installer
# Double-click this file to install

clear
echo ""
echo "  ========================================"
echo "    CamCast Relay Agent — Installer"
echo "    camcast.live"
echo "  ========================================"
echo ""
echo "  This will install everything needed to"
echo "  stream your cameras to CamCast.live."
echo ""
read -p "  Press Enter to begin... "

INSTALL_DIR="$HOME/CamCast"
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

# ─── Check for Homebrew ───
echo ""
echo "  [1/4] Checking for Homebrew..."
if ! command -v brew &>/dev/null; then
    echo "  Homebrew not found. Installing..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    # Add to PATH for Apple Silicon Macs
    if [ -f "/opt/homebrew/bin/brew" ]; then
        eval "$(/opt/homebrew/bin/brew shellenv)"
        echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> "$HOME/.zprofile"
    fi
fi
echo "  Homebrew ready"

# ─── Check for Node.js ───
echo ""
echo "  [2/4] Checking for Node.js..."
if ! command -v node &>/dev/null; then
    echo "  Installing Node.js..."
    brew install node
fi
echo "  Found Node.js $(node --version)"

# ─── Check for FFmpeg ───
echo ""
echo "  [3/4] Checking for FFmpeg..."
if ! command -v ffmpeg &>/dev/null; then
    echo "  Installing FFmpeg..."
    brew install ffmpeg
fi
echo "  Found FFmpeg"

# ─── Download CamCast Relay ───
echo ""
echo "  [4/4] Downloading CamCast Relay Agent..."

RELAY_DIR="$INSTALL_DIR/relay"
mkdir -p "$RELAY_DIR"

# Try downloading from server
curl -sL "https://camcast.live/downloads/camcast-relay.zip" -o "$INSTALL_DIR/camcast-relay.zip" 2>/dev/null
if [ -f "$INSTALL_DIR/camcast-relay.zip" ] && [ -s "$INSTALL_DIR/camcast-relay.zip" ]; then
    echo "  Extracting..."
    unzip -qo "$INSTALL_DIR/camcast-relay.zip" -d "$RELAY_DIR"
    rm -f "$INSTALL_DIR/camcast-relay.zip"
else
    echo "  Download not available yet. Using local copy if present..."
    rm -f "$INSTALL_DIR/camcast-relay.zip"
fi

cd "$RELAY_DIR"

# Install npm dependencies
if [ -f "package.json" ]; then
    echo "  Installing dependencies..."
    npm install --production --silent 2>/dev/null
fi

# ─── Install as background service ───
echo ""
echo "  ========================================"
echo "    Setting up CamCast Relay..."
echo "  ========================================"
echo ""

# Install PM2
npm install -g pm2 2>/dev/null

cd "$RELAY_DIR"
pm2 delete camcast-relay 2>/dev/null
pm2 delete camcast-manager 2>/dev/null
pm2 start web-ui.js --name camcast-manager
pm2 start relay.js --name camcast-relay
pm2 save

# Create startup script
pm2 startup 2>/dev/null | grep "sudo" | bash 2>/dev/null

echo ""
echo "  ========================================"
echo "    Installation Complete!"
echo "  ========================================"
echo ""
echo "  Opening CamCast Relay Manager in your"
echo "  browser. Use it to add cameras and"
echo "  check their health."
echo ""
echo "  Manager URL: http://localhost:3100"
echo ""

# Open in browser
open http://localhost:3100 2>/dev/null || xdg-open http://localhost:3100 2>/dev/null

echo "  CamCast Relay is running and will"
echo "  auto-start when this computer boots."
echo ""
echo "  To open the manager again, visit:"
echo "    http://localhost:3100"
echo ""
read -p "  Press Enter to exit... "
