#!/bin/bash # MPVJ Headless Setup Script # Usage: curl -sSL /setup.sh | sudo bash -s -- set -e REPO_URL=$1 if [ -z "$REPO_URL" ]; then echo "Usage: $0 " 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 $@" exit 1 fi # Detect the real user who called sudo REAL_USER=${SUDO_USER:-$(logname)} REAL_HOME=$(eval echo "~$REAL_USER") echo "Target User: $REAL_USER" echo "Target Home: $REAL_HOME" # 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 Validation 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=$(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 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 if [ ${#WIFI_PASS} -lt 8 ]; then whiptail --msgbox "Error: WiFi Password must be at least 8 characters long (WPA2 requirement)." 8 45 < /dev/tty else break fi done # 2. SYSTEM STAGING PHASE echo "System Staging Phase..." # 2.1 Pre-Flight Disk Space Check (4GB Required) 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 # 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 # 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 # 2.4 Hostname & Country Code 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 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 echo "Configuring Networking..." # 3.1 hostapd cat < /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 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 # 3.2 dnsmasq [ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak 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 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 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" cd "$REAL_HOME/mpvj/backend" # Clean up any ghost dependencies from git or previous runs rm -rf node_modules package-lock.json sudo -u "$REAL_USER" npm install --jobs 1 --omit=optional cd "$REAL_HOME/mpvj/frontend" if [ ! -d "dist" ]; then rm -rf node_modules package-lock.json sudo -u "$REAL_USER" npm install --jobs 1 --omit=optional sudo -u "$REAL_USER" npm run build fi # 4.3 Systemd cat < /etc/systemd/system/mpvj-backend.service [Unit] Description=MPVJ Headless Control Center 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 systemctl daemon-reload systemctl enable mpvj-backend.service # 4.4 .env 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 OpenFrameworks and ofxPiMapper (This will take ~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 cd openFrameworks/scripts/linux/debian ./install_dependencies.sh -y > /dev/null 2>&1 cd ../../../ cd scripts/linux/debian ./install_codecs.sh > /dev/null 2>&1 cd ../../../ sudo -u "$REAL_USER" make -j1 -C libs/openFrameworksCompiled/project/linux64 > /dev/null 2>&1 fi cd "$REAL_HOME/openFrameworks/addons" if [ ! -d "ofxPiMapper" ]; then sudo -u "$REAL_USER" git clone https://github.com/kr15h/ofxPiMapper.git sudo -u "$REAL_USER" git clone https://github.com/vanderlin/ofxOfelia.git fi # Build Example with Highlight Patch cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper" if [ -f src/Osc/OscControl.cpp ]; then echo "Applying highlight surface OSC listener modification..." 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 cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper/example-basic" sudo -u "$REAL_USER" make -j1 > /dev/null 2>&1 cp bin/example-basic /usr/local/bin/ofxPiMapper chmod +x /usr/local/bin/ofxPiMapper # 5. FINALIZATION echo "Finalizing..." # 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