#!/bin/bash

# MPVJ Headless Setup Script (Final Polish & Robustness)
# Usage: curl -sSL <URL>/setup.sh | sudo bash -s -- <REPO_URL>

set -e

REPO_URL=$1

if [ -z "$REPO_URL" ]; then
    echo "Usage: $0 <REPO_URL>"
    exit 1
fi

# 0. BASH & ROOT CHECK
if [ -z "$BASH_VERSION" ]; then
  echo "ERROR: This script must be run with BASH, not SH."
  echo "Please use: sudo bash setup.sh $REPO_URL"
  exit 1
fi

if [ "$(id -u)" -ne 0 ]; then
  echo "ERROR: This script must be run as root (use sudo)."
  exit 1
fi

# Detect user
REAL_USER=${SUDO_USER:-$(logname)}
REAL_HOME=$(eval echo "~$REAL_USER")
echo "Target User: $REAL_USER ($REAL_HOME)"

# 0.5 SCRUB & SANITIZE PHASE
echo "Scrubbing system for a clean installation..."
systemctl stop mpvj-backend.service 2>/dev/null || true
pkill -9 ofxPiMapper 2>/dev/null || true
pkill -9 node 2>/dev/null || true

# Remove old setup artifacts
swapoff /swapfile_mpvj 2>/dev/null || true
rm -f /swapfile_mpvj 2>/dev/null || true

# Clear any stale apt locks
rm -f /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock 2>/dev/null || true

# Ask to wipe previous data for a fresh start
if [ -d "$REAL_HOME/mpvj" ] || [ -d "$REAL_HOME/openFrameworks" ]; then
  if whiptail --title "Cleanup" --yesno "Found existing MPVJ or OpenFrameworks data. Wipe them for a clean install?" 10 60 < /dev/tty; then
    echo "Wiping old directories..."
    rm -rf "$REAL_HOME/mpvj" "$REAL_HOME/openFrameworks"
  fi
fi

# 1. INTERROGATION PHASE
echo "Interrogating user for configuration..."

# 1.1 Hostname
HOSTNAME=$(whiptail --inputbox "Enter Hostname (e.g. mpvj)" 8 45 "mpvj" --title "Hostname Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
HOSTNAME=${HOSTNAME:-mpvj}

# 1.2 SSID
WIFI_SSID=$(whiptail --inputbox "Enter WiFi SSID (e.g. MPVJ-AP)" 8 45 "MPVJ-AP" --title "WiFi SSID Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
WIFI_SSID=${WIFI_SSID:-MPVJ-AP}

# 1.3 Country Code & Password
while true; do
  WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB)" 8 45 "US" --title "WiFi Country Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
  WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]')
  if ! echo "$WIFI_COUNTRY" | grep -qE "^[A-Z]{2}$"; then
    whiptail --msgbox "Error: Country Code must be 2 letters." 8 45 < /dev/tty
  else
    break
  fi
done

while true; do
  WIFI_PASS=$(whiptail --passwordbox "Enter WiFi Password (min 8 chars)" 8 45 --title "WiFi Password Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1
  if [ ${#WIFI_PASS} -lt 8 ]; then
    whiptail --msgbox "Error: Minimum 8 characters required." 8 45 < /dev/tty
  else
    break
  fi
done

# 2. SYSTEM STAGING PHASE
echo "System Staging Phase..."

# 2.1 Pre-Flight Disk Check
FREE_SPACE_KB=$(df / --output=avail | tail -n1)
if [ "$FREE_SPACE_KB" -lt 4194304 ]; then
  whiptail --msgbox "Error: 4GB free space required." 8 45 < /dev/tty
  exit 1
fi

# 2.2 System Updates
if command -v apt-get > /dev/null; then
  apt-get update -y && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
elif command -v pacman > /dev/null; then
  pacman -Syu --noconfirm
fi

# 2.3 Install Dependencies
echo "Installing Core Dependencies..."
if command -v apt-get > /dev/null; then
  DEBIAN_FRONTEND=noninteractive apt-get install -y git hostapd dnsmasq avahi-daemon curl ffmpeg build-essential libzmq3-dev libavahi-compat-libdnssd-dev wget unzip \
    libmpg123-dev libsndfile1-dev libopenal-dev libassimp-dev \
    libglew-dev libglfw3-dev liburiparser-dev \
    libcurl4-openssl-dev libpugixml-dev libasound2-dev \
    libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
    gstreamer1.0-plugins-good gstreamer1.0-plugins-bad \
    gstreamer1.0-plugins-ugly gstreamer1.0-libav \
    libgtk-3-dev libboost-filesystem-dev \
    libfontconfig1-dev libfreetype-dev libx11-dev \
    libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev \
    libpulse-dev libudev-dev libfreeimage-dev librtaudio-dev \
    freeglut3-dev libxxf86vm-dev
elif command -v pacman > /dev/null; then
  pacman -S --noconfirm git hostapd dnsmasq avahi curl ffmpeg base-devel zeromq wget unzip
fi

# Install Node.js if missing
if ! command -v node > /dev/null; then
  curl -fsSL https://deb.nodesource.com/setup_20.x | bash - || true
  command -v apt-get > /dev/null && apt-get install -y nodejs
fi

# 2.4 Hostname & WiFi Country
hostnamectl set-hostname "$HOSTNAME"
sed -i "s/127.0.1.1.*/127.0.1.1\t$HOSTNAME/g" /etc/hosts || true

# 2.5 OS-Agnostic Swap Increase (2GB)
echo "Ensuring 2GB Swap for build..."
if [ ! -f /swapfile_mpvj ]; then
  fallocate -l 2G /swapfile_mpvj || dd if=/dev/zero of=/swapfile_mpvj bs=1M count=2048
  chmod 600 /swapfile_mpvj
  mkswap /swapfile_mpvj
  swapon /swapfile_mpvj
fi

# 3. NETWORKING
echo "Configuring Networking..."
rfkill unblock wlan || true

if command -v nmcli > /dev/null; then
  echo "NetworkManager detected. Configuring stable hotspot..."
  # Clean up existing profiles
  nmcli con delete MPVJ-AP 2>/dev/null || true
  
  # Ensure wlan0 is not connected to anything else (prevents supplicant-timeout)
  nmcli device set wlan0 autoconnect no
  nmcli device disconnect wlan0 2>/dev/null || true
  
  # Create the Access Point with specific 2.4GHz settings for Pi 3B stability
  nmcli con add type wifi ifname wlan0 mode ap con-name MPVJ-AP autoconnect yes ssid "$WIFI_SSID"
  nmcli con modify MPVJ-AP 802-11-wireless.mode ap 802-11-wireless.band bg 
  nmcli con modify MPVJ-AP ipv4.method shared ipv4.addresses 192.168.4.1/24
  nmcli con modify MPVJ-AP wifi-sec.key-mgmt wpa-psk
  nmcli con modify MPVJ-AP wifi-sec.psk "$WIFI_PASS"
  nmcli con modify MPVJ-AP connection.autoconnect-priority 100
  
  # Ensure legacy services are out of the way
  systemctl stop hostapd dnsmasq 2>/dev/null || true
  systemctl mask hostapd dnsmasq 2>/dev/null || true
  
  echo "Stable NetworkManager Hotspot configured."
else
  echo "Using legacy hostapd/dnsmasq logic..."
  # hostapd
  cat <<EOF > /etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=$WIFI_SSID
hw_mode=g
channel=7
wpa=2
wpa_passphrase=$WIFI_PASS
wpa_key_mgmt=WPA-PSK
EOF
  [ -f /etc/default/hostapd ] && sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd

  # dnsmasq
  [ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak || true
  cat <<EOF > /etc/dnsmasq.conf
interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
address=/$HOSTNAME.local/192.168.4.1
EOF

  # dhcpcd
  if [ -f /etc/dhcpcd.conf ]; then
    if ! grep -q "interface wlan0" /etc/dhcpcd.conf; then
      echo -e "\ninterface wlan0\n    static ip_address=192.168.4.1/24\n    nohook wpa_supplicant" >> /etc/dhcpcd.conf
    fi
  fi

  systemctl unmask hostapd dnsmasq 2>/dev/null || true
  systemctl enable hostapd dnsmasq
fi

systemctl enable avahi-daemon

# 4. DEPLOYMENT
echo "Deploying Application..."
[ -d "$REAL_HOME/mpvj" ] && mv "$REAL_HOME/mpvj" "$REAL_HOME/mpvj.old.$(date +%s)"
sudo -u "$REAL_USER" git clone "$REPO_URL" "$REAL_HOME/mpvj"
mkdir -p "$REAL_HOME/media"
chown -R "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj" "$REAL_HOME/media"

# 4.2 Build
export NODE_OPTIONS="--max-old-space-size=512"
cd "$REAL_HOME/mpvj/backend"
echo "Installing backend dependencies..."
rm -rf node_modules package-lock.json
sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false

cd "$REAL_HOME/mpvj/frontend"
if [ ! -d "dist" ]; then
  echo "Installing frontend dependencies and building..."
  rm -rf node_modules package-lock.json
  sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false
  sudo -u "$REAL_USER" npm run build
else
  echo "Pre-built frontend detected. Skipping frontend build."
fi

# 4.3 Setup Systemd
cat <<EOF > /etc/systemd/system/mpvj-backend.service
[Unit]
Description=MPVJ Backend
After=network.target
[Service]
Type=simple
User=$REAL_USER
WorkingDirectory=$REAL_HOME/mpvj/backend
ExecStart=/usr/bin/node index.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable mpvj-backend.service

# 4.4 .env
cat <<EOF > "$REAL_HOME/mpvj/backend/.env"
PORT=80
MPVJ_HOSTNAME=$HOSTNAME
MPVJ_SSID=$WIFI_SSID
MPVJ_MEDIA_DIR=$REAL_HOME/media
EOF

# 4.5 THE PRE-BUILT ENGINE SOLUTION (FASTEST)
echo "Installing Pre-compiled ofxPiMapper Binary..."
BINARY_URL="https://gitea.superwcpot.nl/timothy/mapper/raw/branch/main/bin/ofxPiMapper"
if ! curl -sSL -o /usr/local/bin/ofxPiMapper "$BINARY_URL"; then
  wget -O /usr/local/bin/ofxPiMapper "$BINARY_URL"
fi
chmod +x /usr/local/bin/ofxPiMapper

# 5. FINALIZATION
echo "Cleaning up..."
swapoff /swapfile_mpvj 2>/dev/null || true
rm /swapfile_mpvj 2>/dev/null || true

whiptail --title "Success" --msgbox "INSTALL COMPLETE!\n\nRebooting into AP Mode.\nSSID: $WIFI_SSID\nIP: 192.168.4.1" 12 45 < /dev/tty
reboot
