Files
mapper/scripts/setup.sh

215 lines
6.9 KiB
Bash

#!/bin/bash
# MPVJ Headless Setup Script (Hardened & Pre-compiled OF)
# 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)"
# 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)
[ "$FREE_SPACE_KB" -lt 4194304 ] && { whiptail --msgbox "Error: 4GB free space required." 8 45 < /dev/tty; exit 1; }
# 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
elif command -v pacman > /dev/null; then
pacman -S --noconfirm git hostapd dnsmasq avahi curl ffmpeg base-devel zeromq wget
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..."
# 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 / 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
# 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 Backend Install
cd "$REAL_HOME/mpvj/backend"
rm -rf node_modules package-lock.json
sudo -u "$REAL_USER" npm install --omit=optional --package-lock=false
# 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
echo "Installing Pre-compiled OpenFrameworks (FAST)..."
cd "$REAL_HOME"
if [ ! -d "openFrameworks" ]; then
# Download pre-compiled release for arm64 (v0.12.1)
sudo -u "$REAL_USER" wget -q https://github.com/openframeworks/openFrameworks/releases/download/0.12.1/of_v0.12.1_linuxarm64_release.tar.gz
sudo -u "$REAL_USER" tar -xzf of_v0.12.1_linuxarm64_release.tar.gz
sudo -u "$REAL_USER" mv of_v0.12.1_linuxarm64_release openFrameworks
sudo -u "$REAL_USER" rm of_v0.12.1_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
# Apply the highlight patch to the addon before building
cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper"
if [ -f src/Osc/OscControl.cpp ]; then
echo "Applying highlight surface OSC listener modification..."
# Only add if not already present
if ! grep -q "ofxPiMapper/surface/highlight" src/Osc/OscControl.cpp; then
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
fi
# Build Example (Explicitly force ARM64 platform variables)
cd "$REAL_HOME/openFrameworks/addons/ofxPiMapper/example_basic"
echo "Starting compilation (forcing ARM64 platform)..."
sudo -u "$REAL_USER" make -j1 PLATFORM_OS=Linux PLATFORM_ARCH=arm64 PLATFORM_VARIANT=raspberrypi
cp bin/example_basic /usr/local/bin/ofxPiMapper
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