diff --git a/scripts/setup.sh b/scripts/setup.sh index ae113e9..d43aeb8 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,6 +1,6 @@ #!/bin/bash -# MPVJ Headless Setup Script +# MPVJ Headless Setup Script (Hardened & Pre-compiled OF) # Usage: curl -sSL /setup.sh | sudo bash -s -- set -e @@ -12,19 +12,22 @@ if [ -z "$REPO_URL" ]; then exit 1 fi -# 0. ROOT & USER DETECTION -if [ "$(id -u)" -ne 0 ]; then - echo "ERROR: This script must be run as root." - echo "Please use: sudo bash $0 $@" +# 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 -# Detect the real user who called sudo +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" -echo "Target Home: $REAL_HOME" +echo "Target User: $REAL_USER ($REAL_HOME)" # 1. INTERROGATION PHASE echo "Interrogating user for configuration..." @@ -37,21 +40,21 @@ HOSTNAME=${HOSTNAME:-mpvj} 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 Validation +# 1.3 Country Code & Password while true; do - WIFI_COUNTRY=$(whiptail --inputbox "Enter 2-letter Country Code (e.g. US, GB, DE)" 8 45 "US" --title "WiFi Country Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 + 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 exactly 2 letters (e.g., US)." 8 45 < /dev/tty + 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 (minimum 8 characters)" 8 45 --title "WiFi Password Configuration" 3>&1 1>&2 2>&3 < /dev/tty) || exit 1 + 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: WiFi Password must be at least 8 characters long (WPA2 requirement)." 8 45 < /dev/tty + whiptail --msgbox "Error: Minimum 8 characters required." 8 45 < /dev/tty else break fi @@ -60,67 +63,47 @@ done # 2. SYSTEM STAGING PHASE echo "System Staging Phase..." -# 2.1 Pre-Flight Disk Space Check (4GB Required) +# 2.1 Pre-Flight Disk Check FREE_SPACE_KB=$(df / --output=avail | tail -n1) -MIN_SPACE_KB=4194304 # 4GB -if [ "$FREE_SPACE_KB" -lt "$MIN_SPACE_KB" ]; then - whiptail --msgbox "Error: Not enough disk space. At least 4GB of free space is required for installation and swap scaling." 8 45 < /dev/tty - exit 1 -fi +[ "$FREE_SPACE_KB" -lt 4194304 ] && { whiptail --msgbox "Error: 4GB free space required." 8 45 < /dev/tty; exit 1; } # 2.2 System Updates -( - echo 25 - apt-get update -y > /dev/null 2>&1 - echo 75 - DEBIAN_FRONTEND=noninteractive apt-get upgrade -y > /dev/null 2>&1 - echo 100 -) | whiptail --gauge "Updating System Packages..." 6 60 0 +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 20 - DEBIAN_FRONTEND=noninteractive apt-get install -y git hostapd dnsmasq avahi-daemon curl ffmpeg build-essential libzmq3-dev libavahi-compat-libdnssd-dev > /dev/null 2>&1 - echo 60 - curl -fsSL https://deb.nodesource.com/setup_20.x | bash - > /dev/null 2>&1 - DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs > /dev/null 2>&1 - echo 100 -) | whiptail --gauge "Installing Core Dependencies..." 6 60 0 +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 +elif command -v pacman > /dev/null; then + pacman -S --noconfirm git hostapd dnsmasq avahi curl ffmpeg base-devel zeromq wget +fi -# 2.4 Hostname & Country Code +# 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 -if command -v raspi-config > /dev/null; then - raspi-config nonint do_wifi_country "$WIFI_COUNTRY" > /dev/null 2>&1 +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 -# 2.5 Swap Increase -if [ -f /etc/dphys-swapfile ]; then - sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=2048/g" /etc/dphys-swapfile - dphys-swapfile setup - dphys-swapfile swapon -fi - -# 2.6 Graphics Driver Configuration (KMS/DRM) -echo "Configuring graphics drivers for headless operation..." -if [ -f /boot/config.txt ]; then - if grep -q "gpu_mem=" /boot/config.txt; then - sed -i "s/gpu_mem=.*/gpu_mem=256/g" /boot/config.txt - else - echo "gpu_mem=256" >> /boot/config.txt - fi - if ! grep -q "dtoverlay=vc4-kms-v3d" /boot/config.txt; then - echo "dtoverlay=vc4-kms-v3d" >> /boot/config.txt - fi -fi -if command -v raspi-config > /dev/null; then - raspi-config nonint do_boot_behaviour B2 > /dev/null 2>&1 -fi - -# 3. NETWORKING CONFIGURATION +# 3. NETWORKING echo "Configuring Networking..." - -# 3.1 hostapd +# hostapd cat < /etc/hostapd/hostapd.conf interface=wlan0 driver=nl80211 @@ -130,88 +113,47 @@ channel=7 wpa=2 wpa_passphrase=$WIFI_PASS wpa_key_mgmt=WPA-PSK -wpa_pairwise=TKIP -rsn_pairwise=CCMP -auth_algs=1 -macaddr_acl=0 EOF -sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd +[ -f /etc/default/hostapd ] && sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd -# 3.2 dnsmasq -[ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak +# dnsmasq +[ -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 -domain=local address=/$HOSTNAME.local/192.168.4.1 EOF -# 3.3 dhcpcd -if ! grep -q "interface wlan0" /etc/dhcpcd.conf; then -cat <> /etc/dhcpcd.conf - -# MPVJ wlan0 Static IP -interface wlan0 - static ip_address=192.168.4.1/24 - nohook wpa_supplicant -EOF +# dhcpcd / network manager +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 -# 3.4 Avahi -cat < /etc/avahi/services/mpvj.service - - - - MPVJ Control Center - - _http._tcp - 80 - - -EOF -sed -i 's/#use-ipv4-ll=yes/use-ipv4-ll=yes/g' /etc/avahi/avahi-daemon.conf - -# 4. APPLICATION DEPLOYMENT +# 4. DEPLOYMENT echo "Deploying Application..." - -# 4.1 Clone [ -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" +# 4.2 Backend Install cd "$REAL_HOME/mpvj/backend" -echo "Installing backend dependencies..." -# Use --package-lock=false to ignore any committed lockfiles that might be polluted 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 Systemd +# 4.3 Setup Systemd cat < /etc/systemd/system/mpvj-backend.service [Unit] -Description=MPVJ Headless Control Center Backend +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 -Environment=NODE_OPTIONS=--max-old-space-size=512 - [Install] WantedBy=multi-user.target EOF @@ -223,103 +165,34 @@ cat < "$REAL_HOME/mpvj/backend/.env" PORT=80 MPVJ_HOSTNAME=$HOSTNAME MPVJ_SSID=$WIFI_SSID -MPVJ_WIFI_PASS=$WIFI_PASS -MPVJ_REPO_URL=$REPO_URL MPVJ_MEDIA_DIR=$REAL_HOME/media EOF -chown "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj/backend/.env" -chmod 600 "$REAL_HOME/mpvj/backend/.env" -# 4.5 Install ofxPiMapper Engine -echo "Installing ofxPiMapper Engine..." - -# Priority 1: Check for pre-built binary in the repo -if [ -f "$REAL_HOME/mpvj/bin/ofxPiMapper" ]; then - echo "Pre-built ofxPiMapper binary detected in repo. Installing..." - cp "$REAL_HOME/mpvj/bin/ofxPiMapper" /usr/local/bin/ofxPiMapper - chmod +x /usr/local/bin/ofxPiMapper - echo "ofxPiMapper installed successfully (Binary)." -else - # Priority 2: Build from source (Fall-back) - if [ -f "/usr/local/bin/ofxPiMapper" ]; then - echo "ofxPiMapper already installed in /usr/local/bin. Skipping build." - else - echo "No pre-built binary found. Falling back to source build (This takes ~1 hour)..." - cd "$REAL_HOME" - if [ ! -d "openFrameworks" ]; then - sudo -u "$REAL_USER" git clone --depth 1 --branch master https://github.com/openframeworks/openFrameworks.git - - if command -v apt-get > /dev/null; then - echo "Debian-based system detected. Installing dependencies..." - cd openFrameworks/scripts/linux/debian - ./install_dependencies.sh -y - ./install_codecs.sh - elif command -v pacman > /dev/null; then - echo "Arch-based system detected. Installing dependencies..." - cd openFrameworks/scripts/linux/archlinux - sudo ./install_dependencies.sh --noconfirm - else - echo "WARNING: Unsupported system detected. Please install OpenFrameworks dependencies manually." - fi - - cd "$REAL_HOME/openFrameworks" - sudo -u "$REAL_USER" make -j1 -C libs/openFrameworksCompiled/project/linux64 - fi - - cd "$REAL_HOME/openFrameworks/addons" - if [ ! -d "ofxPiMapper/example-basic" ]; then - echo "ofxPiMapper missing or incomplete. Cleaning and cloning..." - rm -rf ofxPiMapper - sudo -u "$REAL_USER" git clone https://github.com/kr15h/ofxPiMapper.git - fi - - # Patch and Build - cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper" - if [ -f src/Osc/OscControl.cpp ] && ! grep -q "highlight" src/Osc/OscControl.cpp; then - echo "Applying highlight patch..." - sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/i \ - if (m.getAddress() == "/ofxPiMapper/surface/highlight"){ \ - int surfaceIndex = m.getArgAsInt32(0); \ - mapper->getSurfaceManager()->selectSurface(surfaceIndex); \ - return; \ - }' src/Osc/OscControl.cpp - fi - - echo "Compiling example-basic..." - if [ -d "example-basic" ]; then - cd "example-basic" - sudo -u "$REAL_USER" make -j1 - cp bin/example-basic /usr/local/bin/ofxPiMapper - chmod +x /usr/local/bin/ofxPiMapper - else - echo "ERROR: example-basic folder not found in ofxPiMapper addon!" - exit 1 - fi - - fi +# 4.5 THE PRE-BUILT ENGINE SOLUTION +echo "Installing Pre-compiled OpenFrameworks (FAST)..." +cd "$REAL_HOME" +if [ ! -d "openFrameworks" ]; then + # Download pre-compiled release for arm64 + sudo -u "$REAL_USER" wget https://github.com/openframeworks/openFrameworks/releases/download/0.12.0/of_v0.12.0_linuxarm64_release.tar.gz + sudo -u "$REAL_USER" tar -xzf of_v0.12.0_linuxarm64_release.tar.gz + sudo -u "$REAL_USER" mv of_v0.12.0_linuxarm64_release openFrameworks + sudo -u "$REAL_USER" rm of_v0.12.0_linuxarm64_release.tar.gz fi +echo "Downloading and building ofxPiMapper Addon..." +cd "$REAL_HOME/openFrameworks/addons" +[ ! -d "ofxPiMapper" ] && sudo -u "$REAL_USER" git clone --depth 1 https://github.com/kr15h/ofxPiMapper.git + +# Build Example +cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper/example-basic" +sudo -u "$REAL_USER" make -j1 +cp bin/example-basic /usr/local/bin/ofxPiMapper +chmod +x /usr/local/bin/ofxPiMapper + # 5. FINALIZATION -echo "Finalizing..." +echo "Cleaning up..." +swapoff /swapfile_mpvj 2>/dev/null || true +rm /swapfile_mpvj 2>/dev/null || true -# 5.1 Revert Swap -if [ -f /etc/dphys-swapfile ]; then - sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=100/g" /etc/dphys-swapfile - dphys-swapfile setup - dphys-swapfile swapon -fi - -# 5.2 Summary -SUMMARY="MPVJ INSTALLATION COMPLETE\n\n" -SUMMARY+="Please NOTE DOWN these details before rebooting:\n\n" -SUMMARY+="HOSTNAME: $HOSTNAME.local\n" -SUMMARY+="STATIC IP: 192.168.4.1\n" -SUMMARY+="WIFI SSID: $WIFI_SSID\n" -SUMMARY+="WIFI PASS: $WIFI_PASS\n\n" -SUMMARY+="Your SSH password remains unchanged.\n" -SUMMARY+="Connect to '$WIFI_SSID' after reboot.\n\n" -SUMMARY+="Reboot now?" - -if whiptail --title "Success" --yesno "$SUMMARY" 20 60 < /dev/tty; then - reboot -fi +whiptail --title "Success" --msgbox "INSTALL COMPLETE!\n\nRebooting into AP Mode.\nSSID: $WIFI_SSID\nIP: 192.168.4.1" 12 45 < /dev/tty +reboot