fix: harden setup script with robust root check and portable shell syntax

This commit is contained in:
Timothy Hofland
2026-03-10 22:54:12 +01:00
parent 90dcd7cbfe
commit a5d1e88fd5

View File

@ -12,9 +12,10 @@ if [ -z "$REPO_URL" ]; then
exit 1 exit 1
fi fi
# Check for root # 0. ROOT CHECK (Robust)
if [ "$EUID" -ne 0 ]; then if [ "$(id -u)" -ne 0 ]; then
echo "Please run as root (use sudo)" echo "ERROR: This script must be run as root."
echo "Please use: sudo bash $0 $@"
exit 1 exit 1
fi fi
@ -33,7 +34,8 @@ WIFI_SSID=${WIFI_SSID:-MPVJ-AP}
while true; do 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, 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:]') WIFI_COUNTRY=$(echo "$WIFI_COUNTRY" | tr '[:lower:]' '[:upper:]')
if [[ ! "$WIFI_COUNTRY" =~ ^[A-Z]{2}$ ]]; then # Portable regex check using grep
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 exactly 2 letters (e.g., US)." 8 45 < /dev/tty
else else
break break
@ -96,18 +98,15 @@ fi
# 2.6 Graphics Driver Configuration (KMS/DRM) # 2.6 Graphics Driver Configuration (KMS/DRM)
echo "Configuring graphics drivers for headless operation..." echo "Configuring graphics drivers for headless operation..."
if [ -f /boot/config.txt ]; then if [ -f /boot/config.txt ]; then
# Increase GPU memory (needed for mapping)
if grep -q "gpu_mem=" /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 sed -i "s/gpu_mem=.*/gpu_mem=256/g" /boot/config.txt
else else
echo "gpu_mem=256" >> /boot/config.txt echo "gpu_mem=256" >> /boot/config.txt
fi fi
# Enable KMS driver if not present
if ! grep -q "dtoverlay=vc4-kms-v3d" /boot/config.txt; then if ! grep -q "dtoverlay=vc4-kms-v3d" /boot/config.txt; then
echo "dtoverlay=vc4-kms-v3d" >> /boot/config.txt echo "dtoverlay=vc4-kms-v3d" >> /boot/config.txt
fi fi
fi fi
# Disable boot to desktop (force console auto-login)
if command -v raspi-config > /dev/null; then if command -v raspi-config > /dev/null; then
raspi-config nonint do_boot_behaviour B2 > /dev/null 2>&1 raspi-config nonint do_boot_behaviour B2 > /dev/null 2>&1
fi fi
@ -133,7 +132,7 @@ EOF
sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd sed -i 's/#DAEMON_CONF=""/DAEMON_CONF="\/etc\/hostapd\/hostapd.conf"/g' /etc/default/hostapd
# 3.2 dnsmasq # 3.2 dnsmasq
mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak 2>/dev/null || true [ -f /etc/dnsmasq.conf ] && mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak
cat <<EOF > /etc/dnsmasq.conf cat <<EOF > /etc/dnsmasq.conf
interface=wlan0 interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h
@ -223,7 +222,6 @@ if [ ! -d "openFrameworks" ]; then
cd openFrameworks/scripts/linux/debian cd openFrameworks/scripts/linux/debian
./install_dependencies.sh -y > /dev/null 2>&1 ./install_dependencies.sh -y > /dev/null 2>&1
cd ../../../ cd ../../../
# Compile OF (this takes a while)
cd scripts/linux/debian cd scripts/linux/debian
./install_codecs.sh > /dev/null 2>&1 ./install_codecs.sh > /dev/null 2>&1
cd ../../../ cd ../../../
@ -233,14 +231,13 @@ fi
cd /home/pi/openFrameworks/addons cd /home/pi/openFrameworks/addons
if [ ! -d "ofxPiMapper" ]; then if [ ! -d "ofxPiMapper" ]; then
sudo -u pi git clone https://github.com/kr15h/ofxPiMapper.git sudo -u pi git clone https://github.com/kr15h/ofxPiMapper.git
sudo -u pi git clone https://github.com/vanderlin/ofxOfelia.git # dependency often needed sudo -u pi git clone https://github.com/vanderlin/ofxOfelia.git
fi fi
# Build Example # Build Example with Highlight Patch
cd /home/pi/openFrameworks/addons/ofxPiMapper cd /home/pi/openFrameworks/addons/ofxPiMapper
if [ -f src/Osc/OscControl.cpp ]; then if [ -f src/Osc/OscControl.cpp ]; then
echo "Applying highlight surface OSC listener modification..." echo "Applying highlight surface OSC listener modification..."
# Add highlight listener that selects the surface for 1 second (mock highlight)
sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/i \ sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/i \
if (m.getAddress() == "/ofxPiMapper/surface/highlight"){ \ if (m.getAddress() == "/ofxPiMapper/surface/highlight"){ \
int surfaceIndex = m.getArgAsInt32(0); \ int surfaceIndex = m.getArgAsInt32(0); \
@ -249,7 +246,6 @@ if [ -f src/Osc/OscControl.cpp ]; then
}' src/Osc/OscControl.cpp }' src/Osc/OscControl.cpp
fi fi
# Build Example
cd /home/pi/openFrameworks/addons/ofxPiMapper/example-basic cd /home/pi/openFrameworks/addons/ofxPiMapper/example-basic
sudo -u pi make -j1 > /dev/null 2>&1 sudo -u pi make -j1 > /dev/null 2>&1
cp bin/example-basic /usr/local/bin/ofxPiMapper cp bin/example-basic /usr/local/bin/ofxPiMapper