Files
mapper/scripts/setup.sh

271 lines
10 KiB
Bash

#!/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 <REPO_URL>
set -e
REPO_URL=$1
if [ -z "$REPO_URL" ]; then
echo "Usage: sudo bash $0 <REPO_URL>"
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 <<EOF > /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 <<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
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 frontend exists
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 dist folder."
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
# Media directory
mkdir -p "$REAL_HOME/media"
chown -R "$REAL_USER:$REAL_USER" "$REAL_HOME/mpvj" "$REAL_HOME/media"
# ── 4.5 BUILD ofxPiMapper NATIVELY ───────────────────────────────────────────
echo "Building ofxPiMapper natively (this takes a while)..."
OF_VERSION="0.12.1"
OF_FILE="of_v${OF_VERSION}_linuxaarch64_release.tar.gz"
OF_URL="https://github.com/openframeworks/openFrameworks/releases/download/${OF_VERSION}/${OF_FILE}"
# Install build deps
DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential libzmq3-dev \
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
# Temporary swap so the Pi doesn't OOM during compilation
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
cd "$REAL_HOME"
if [ ! -d "openFrameworks" ]; then
sudo -u "$REAL_USER" wget "$OF_URL" || sudo -u "$REAL_USER" curl -L -O "$OF_URL"
sudo -u "$REAL_USER" tar -xzf "$OF_FILE"
sudo -u "$REAL_USER" mv "of_v${OF_VERSION}_linux"*"_release" openFrameworks
sudo -u "$REAL_USER" rm -f "$OF_FILE"
fi
ADDON_DIR="$REAL_HOME/openFrameworks/addons/ofxPiMapper"
rm -rf "$ADDON_DIR"
sudo -u "$REAL_USER" git clone --depth 1 https://github.com/kr15h/ofxPiMapper.git "$ADDON_DIR"
cd "$ADDON_DIR"
# Patches
OSC_FILE=$(find src -name "OscControl.cpp" | head -n 1)
if [ -n "$OSC_FILE" ]; then
sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/r /dev/stdin' "$OSC_FILE" <<'EOF_PATCH'
if (m.getAddress() == "/ofxPiMapper/surface/highlight"){
int surfaceIndex = m.getArgAsInt32(0);
mapper->getSurfaceManager()->selectSurface(surfaceIndex);
return;
}
EOF_PATCH
fi
cat <<'EOF_COMPAT' > src/cpp11_compat.h
#ifndef OFXPIMAPPER_CPP11_COMPAT_H
#define OFXPIMAPPER_CPP11_COMPAT_H
#include <memory>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#if __cplusplus < 201402L
namespace std {
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
#endif
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
#endif
EOF_COMPAT
sed -i '1i #include "cpp11_compat.h"' src/ofxPiMapper.h
find src -type f \( -name "*.h" -o -name "*.cpp" \) \
-exec sed -i 's/\([^:a-zA-Z0-9]\)unique_ptr\b/\1std::unique_ptr/g' {} + \
-exec sed -i 's/\([^:a-zA-Z0-9]\)make_unique\b/\1std::make_unique/g' {} +
for f in $(grep -l "unique_ptr" src/*.h 2>/dev/null || true); do
grep -q "<memory>" "$f" || sed -i '1i #include <memory>' "$f"
done
cd "$ADDON_DIR/example_basic"
GLOBAL_FLAGS="-Wno-error -Wno-reorder -Wno-sign-compare -Wno-delete-non-virtual-dtor -std=c++11 -O0 -fno-tree-vrp"
sudo -u "$REAL_USER" make clean 2>/dev/null || true
sudo -u "$REAL_USER" make -j1 \
PLATFORM_CFLAGS="$GLOBAL_FLAGS -I$ADDON_DIR/src" \
USER_CFLAGS="$GLOBAL_FLAGS" \
USER_CPPFLAGS="-std=c++11" \
OF_PROJECT_OPTIMIZATION_FLAGS="-O0"
install -m 755 bin/example_basic /usr/local/bin/ofxPiMapper
# Remove swap
swapoff /swapfile_mpvj 2>/dev/null || true
rm -f /swapfile_mpvj
# ── 5. SYSTEMD SERVICE ───────────────────────────────────────────────────────
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
# Write .env
cat <<EOF > "$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