#!/bin/bash # MPVJ Deployment Script # Runs on the Raspberry Pi. Expects pre-built artifacts in the repo: # frontend/dist/ — built by scripts/build.sh # bin/ofxPiMapper — aarch64 binary built by scripts/build.sh # # Usage: sudo bash setup.sh set -e REPO_URL=$1 if [ -z "$REPO_URL" ]; then echo "Usage: sudo bash $0 " exit 1 fi if [ -z "$BASH_VERSION" ]; then echo "ERROR: This script must be run with bash." exit 1 fi if [ "$(id -u)" -ne 0 ]; then echo "ERROR: Must be run as root (sudo)." exit 1 fi # Detect real user REAL_USER=${SUDO_USER:-$(logname 2>/dev/null || true)} [ -z "$REAL_USER" ] && REAL_USER=root REAL_HOME=$(eval echo "~$REAL_USER" 2>/dev/null || echo "/root") echo "Target User: $REAL_USER ($REAL_HOME)" # Ensure whiptail is available if ! command -v whiptail > /dev/null 2>&1; then apt-get update -y DEBIAN_FRONTEND=noninteractive apt-get install -y whiptail fi # ── 0. SCRUB ───────────────────────────────────────────────────────────────── echo "Stopping any running services..." systemctl stop mpvj-backend.service 2>/dev/null || true pkill -9 ofxPiMapper 2>/dev/null || true pkill -9 node 2>/dev/null || true rm -f /var/lib/dpkg/lock-frontend /var/lib/apt/lists/lock /var/cache/apt/archives/lock 2>/dev/null || true if [ -d "$REAL_HOME/mpvj" ]; then if whiptail --title "Cleanup" --yesno "Found existing MPVJ install. Wipe it for a clean install?" 10 60 < /dev/tty; then rm -rf "$REAL_HOME/mpvj" fi fi # ── 1. CONFIGURATION ───────────────────────────────────────────────────────── HOSTNAME=$(whiptail --inputbox "Enter Hostname" 8 45 "mpvj" --title "Hostname" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 HOSTNAME=${HOSTNAME:-mpvj} WIFI_SSID=$(whiptail --inputbox "Enter WiFi SSID" 8 45 "MPVJ-AP" --title "WiFi SSID" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_SSID=${WIFI_SSID:-MPVJ-AP} while true; do WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB)" 8 45 "US" --title "WiFi Country" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]') echo "$WIFI_COUNTRY" | grep -qE "^[A-Z]{2}$" && break whiptail --msgbox "Country Code must be exactly 2 letters." 8 45 < /dev/tty done while true; do WIFI_PASS=$(whiptail --passwordbox "Enter WiFi Password (min 8 chars)" 8 45 --title "WiFi Password" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 [ ${#WIFI_PASS} -ge 8 ] && break whiptail --msgbox "Minimum 8 characters required." 8 45 < /dev/tty done # ── 2. SYSTEM SETUP ────────────────────────────────────────────────────────── FREE_SPACE_KB=$(df / --output=avail | tail -n1) if [ "$FREE_SPACE_KB" -lt 2097152 ]; then whiptail --msgbox "Error: At least 2GB free space required." 8 45 < /dev/tty exit 1 fi echo "Installing runtime dependencies..." apt-get update -y DEBIAN_FRONTEND=noninteractive apt-get install -y \ git hostapd dnsmasq avahi-daemon curl ffmpeg \ libzmq3-dev libavahi-compat-libdnssd-dev if ! command -v node > /dev/null 2>&1; then curl -fsSL https://deb.nodesource.com/setup_20.x | bash - apt-get install -y nodejs fi # Hostname hostnamectl set-hostname "$HOSTNAME" sed -i "s/127.0.1.1.*/127.0.1.1\t$HOSTNAME/g" /etc/hosts || true # ── 3. NETWORKING ──────────────────────────────────────────────────────────── echo "Configuring networking..." cat < /etc/hostapd/hostapd.conf interface=wlan0 driver=nl80211 ssid=$WIFI_SSID hw_mode=g channel=7 country_code=$WIFI_COUNTRY 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 [ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak || true cat < /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 if [ -f /etc/dhcpcd.conf ] && ! grep -q "interface wlan0" /etc/dhcpcd.conf; then printf '\ninterface wlan0\n static ip_address=192.168.4.1/24\n nohook wpa_supplicant\n' >> /etc/dhcpcd.conf fi # ── 4. DEPLOY APP ──────────────────────────────────────────────────────────── echo "Deploying application from $REPO_URL ..." [ -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" # Verify pre-built artifacts exist if [ ! -d "$REAL_HOME/mpvj/frontend/dist" ]; then echo "ERROR: frontend/dist/ not found in repo." echo "Run scripts/build.sh on your x86_64 machine first and commit the artifacts." exit 1 fi if [ ! -f "$REAL_HOME/mpvj/bin/ofxPiMapper" ]; then echo "ERROR: bin/ofxPiMapper not found in repo." echo "Run scripts/build.sh on your x86_64 machine first and commit the artifacts." exit 1 fi # Install npm deps for backend only (no build needed) cd "$REAL_HOME/mpvj/backend" sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false # Install ofxPiMapper binary install -m 755 "$REAL_HOME/mpvj/bin/ofxPiMapper" /usr/local/bin/ofxPiMapper # Media directory mkdir -p "$REAL_HOME/media" chown -R "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj" "$REAL_HOME/media" # ── 5. SYSTEMD SERVICE ─────────────────────────────────────────────────────── cat < /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 # Write .env cat < "$REAL_HOME/mpvj/backend/.env" PORT=80 MPVJ_HOSTNAME=$HOSTNAME MPVJ_SSID=$WIFI_SSID MPVJ_MEDIA_DIR=$REAL_HOME/media EOF # ── 6. DONE ─────────────────────────────────────────────────────────────────── whiptail --title "Success" --msgbox "INSTALL COMPLETE!\n\nRebooting into AP Mode.\nSSID: $WIFI_SSID\nIP: 192.168.4.1" 12 45 < /dev/tty reboot