install.sh 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. #!/usr/bin/env bash
  2. #
  3. # SpoolBuddy Installation Script for Raspberry Pi
  4. #
  5. # Supports two scenarios:
  6. # 1) SpoolBuddy only — NFC/scale companion connecting to a remote Bambuddy instance
  7. # 2) SpoolBuddy + Bambuddy — both running natively on this Raspberry Pi
  8. #
  9. # Usage:
  10. # Interactive: curl -fsSL https://raw.githubusercontent.com/maziggy/bambuddy/main/spoolbuddy/install.sh -o install.sh && chmod +x install.sh && sudo ./install.sh
  11. # Unattended: sudo ./install.sh --mode spoolbuddy --bambuddy-url http://192.168.1.100:8000 --api-key bb_xxx --yes
  12. #
  13. # Options:
  14. # --mode MODE Installation mode: "spoolbuddy" (companion only) or "full" (both)
  15. # --repo URL Git repository URL to install from (default: upstream repo)
  16. # --ref REF Git ref to install (branch/tag/commit, default: main)
  17. # --bambuddy-url URL Bambuddy server URL (required for spoolbuddy mode)
  18. # --api-key KEY Bambuddy API key (required for spoolbuddy mode)
  19. # --path PATH Installation directory (default: /opt/spoolbuddy or /opt/bambuddy)
  20. # --port PORT Bambuddy port (full mode only, default: 8000)
  21. # --ssh-pubkey KEY Bambuddy SSH public key for remote updates
  22. # --yes, -y Non-interactive mode, accept defaults
  23. # --help, -h Show this help message
  24. #
  25. set -e
  26. # ─────────────────────────────────────────────────────────────────────────────
  27. # Constants
  28. # ─────────────────────────────────────────────────────────────────────────────
  29. RED='\033[0;31m'
  30. GREEN='\033[0;32m'
  31. YELLOW='\033[1;33m'
  32. CYAN='\033[0;36m'
  33. BOLD='\033[1m'
  34. NC='\033[0m'
  35. GITHUB_REPO="https://github.com/maziggy/bambuddy.git"
  36. SPOOLBUDDY_SERVICE_USER="spoolbuddy"
  37. BAMBUDDY_SERVICE_USER="bambuddy"
  38. # Packages needed for SpoolBuddy hardware (NFC reader + scale)
  39. SYSTEM_PACKAGES="python3 python3-pip python3-venv python3-dev python3-spidev python3-libgpiod gpiod libgpiod-dev i2c-tools git"
  40. # Python packages for SpoolBuddy daemon
  41. SPOOLBUDDY_PIP_PACKAGES="spidev gpiod smbus2 httpx"
  42. # ─────────────────────────────────────────────────────────────────────────────
  43. # Variables (set by args or prompts)
  44. # ─────────────────────────────────────────────────────────────────────────────
  45. INSTALL_MODE="" # "spoolbuddy" or "full"
  46. INSTALL_PATH=""
  47. INSTALL_REPO=""
  48. INSTALL_REF=""
  49. DETECTED_INSTALLER_REPO=""
  50. DETECTED_INSTALLER_REF=""
  51. BAMBUDDY_URL=""
  52. API_KEY=""
  53. BAMBUDDY_PORT="8000"
  54. NON_INTERACTIVE="false"
  55. REBOOT_NEEDED="false"
  56. KIOSK_USER="" # auto-detected from $SUDO_USER
  57. KIOSK_URL="" # derived from $BAMBUDDY_URL/spoolbuddy?token=$API_KEY
  58. SSH_PUBKEY="" # Bambuddy's SSH public key for remote updates
  59. # ─────────────────────────────────────────────────────────────────────────────
  60. # Helpers
  61. # ─────────────────────────────────────────────────────────────────────────────
  62. info() { echo -e "${CYAN}[INFO]${NC} $1"; }
  63. success() { echo -e "${GREEN}[OK]${NC} $1"; }
  64. warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
  65. error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
  66. # Run a long-running command with a spinner + live progress output.
  67. # Usage: run_with_progress "description" command [args...]
  68. run_with_progress() {
  69. local desc="$1"
  70. shift
  71. local log_file
  72. log_file=$(mktemp /tmp/spoolbuddy-install.XXXXXX)
  73. local start_time=$SECONDS
  74. # Run command in background, capture stdout+stderr
  75. "$@" > "$log_file" 2>&1 &
  76. local pid=$!
  77. # Spinner frames (braille pattern)
  78. local -a spin=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏")
  79. local i=0
  80. while kill -0 "$pid" 2>/dev/null; do
  81. local elapsed=$(( SECONDS - start_time ))
  82. local time_str
  83. if (( elapsed >= 60 )); then
  84. time_str="$(( elapsed / 60 ))m$(printf '%02d' $(( elapsed % 60 )))s"
  85. else
  86. time_str="${elapsed}s"
  87. fi
  88. # Last chunk of output (handles \r progress lines and regular \n lines)
  89. local last_line=""
  90. last_line=$(tail -c 4096 "$log_file" 2>/dev/null | tr '\r' '\n' | sed 's/\x1b\[[0-9;]*[mGKHJ]//g' | sed '/^[[:space:]]*$/d' | tail -1 | sed 's/^[[:space:]]*//' | cut -c1-50) || true
  91. printf "\r ${spin[$((i % 10))]} %-36s ${CYAN}%6s${NC} %s\033[K" "$desc" "$time_str" "$last_line"
  92. i=$(( i + 1 ))
  93. sleep 0.15
  94. done
  95. local exit_code=0
  96. wait "$pid" || exit_code=$?
  97. # Clear spinner line
  98. printf "\r\033[K"
  99. # Format elapsed time for summary
  100. local elapsed=$(( SECONDS - start_time ))
  101. local time_suffix=""
  102. if (( elapsed >= 60 )); then
  103. time_suffix=" ($(( elapsed / 60 ))m $(( elapsed % 60 ))s)"
  104. elif (( elapsed >= 5 )); then
  105. time_suffix=" (${elapsed}s)"
  106. fi
  107. if [[ $exit_code -eq 0 ]]; then
  108. success "${desc}${time_suffix}"
  109. rm -f "$log_file"
  110. else
  111. echo -e "${RED}[FAIL]${NC} ${desc}${time_suffix}"
  112. echo ""
  113. echo -e " ${YELLOW}Last 20 lines:${NC}"
  114. tail -20 "$log_file" 2>/dev/null | sed 's/^/ /'
  115. echo ""
  116. echo -e " Full log: ${CYAN}$log_file${NC}"
  117. exit 1
  118. fi
  119. }
  120. prompt() {
  121. local prompt_text="$1"
  122. local default_value="$2"
  123. local var_name="$3"
  124. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  125. eval "$var_name=\"$default_value\""
  126. return
  127. fi
  128. if [[ -n "$default_value" ]]; then
  129. echo -en "${BOLD}$prompt_text${NC} [${CYAN}$default_value${NC}]: "
  130. else
  131. echo -en "${BOLD}$prompt_text${NC}: "
  132. fi
  133. read -r input
  134. if [[ -z "$input" ]]; then
  135. eval "$var_name=\"$default_value\""
  136. else
  137. eval "$var_name=\"$input\""
  138. fi
  139. }
  140. prompt_yes_no() {
  141. local prompt_text="$1"
  142. local default="$2"
  143. if [[ "$NON_INTERACTIVE" == "true" ]]; then
  144. [[ "$default" == "y" ]] && return 0 || return 1
  145. fi
  146. local yn_hint="[y/n]"
  147. [[ "$default" == "y" ]] && yn_hint="[Y/n]"
  148. [[ "$default" == "n" ]] && yn_hint="[y/N]"
  149. while true; do
  150. echo -en "${BOLD}$prompt_text${NC} $yn_hint: "
  151. read -r yn
  152. [[ -z "$yn" ]] && yn="$default"
  153. case "$yn" in
  154. [Yy]* ) return 0;;
  155. [Nn]* ) return 1;;
  156. * ) echo "Please answer yes or no.";;
  157. esac
  158. done
  159. }
  160. show_help() {
  161. echo "SpoolBuddy Installation Script for Raspberry Pi"
  162. echo ""
  163. echo "Usage: sudo $0 [OPTIONS]"
  164. echo ""
  165. echo "Options:"
  166. echo " --mode MODE \"spoolbuddy\" (companion only) or \"full\" (Bambuddy + SpoolBuddy)"
  167. echo " --repo URL Git repository URL to install from"
  168. echo " --ref REF Git ref to install (branch/tag/commit)"
  169. echo " --bambuddy-url URL Bambuddy server URL (required for spoolbuddy mode)"
  170. echo " --api-key KEY Bambuddy API key (required for spoolbuddy mode)"
  171. echo " --path PATH Installation directory (default: /opt/spoolbuddy or /opt/bambuddy)"
  172. echo " --port PORT Bambuddy port (full mode only, default: 8000)"
  173. echo " --ssh-pubkey KEY Bambuddy SSH public key for remote updates"
  174. echo " --yes, -y Non-interactive mode, accept defaults"
  175. echo " --help, -h Show this help message"
  176. echo ""
  177. echo "Examples:"
  178. echo " Interactive:"
  179. echo " sudo ./install.sh"
  180. echo ""
  181. echo " SpoolBuddy companion (unattended):"
  182. echo " sudo ./install.sh --mode spoolbuddy --bambuddy-url http://192.168.1.100:8000 --api-key bb_xxx -y"
  183. echo ""
  184. echo " Full install (unattended):"
  185. echo " sudo ./install.sh --mode full --port 8000 -y"
  186. exit 0
  187. }
  188. normalize_github_repo_url() {
  189. local url="$1"
  190. if [[ -z "$url" ]]; then
  191. echo ""
  192. return
  193. fi
  194. # Convert git@github.com:owner/repo(.git) to https://github.com/owner/repo.git
  195. if [[ "$url" =~ ^git@github.com:(.+)$ ]]; then
  196. url="https://github.com/${BASH_REMATCH[1]}"
  197. fi
  198. # Keep remote URL style consistent.
  199. url="${url%.git}"
  200. echo "${url}.git"
  201. }
  202. detect_installer_source_context() {
  203. local script_dir
  204. script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  205. if git -C "$script_dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  206. DETECTED_INSTALLER_REF="$(git -C "$script_dir" rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
  207. local origin_url
  208. origin_url="$(git -C "$script_dir" remote get-url origin 2>/dev/null || true)"
  209. DETECTED_INSTALLER_REPO="$(normalize_github_repo_url "$origin_url")"
  210. fi
  211. # Optional environment overrides for raw-download installs.
  212. if [[ -n "${SPOOLBUDDY_INSTALL_REPO:-}" ]]; then
  213. DETECTED_INSTALLER_REPO="$(normalize_github_repo_url "$SPOOLBUDDY_INSTALL_REPO")"
  214. fi
  215. if [[ -n "${SPOOLBUDDY_INSTALL_REF:-}" ]]; then
  216. DETECTED_INSTALLER_REF="$SPOOLBUDDY_INSTALL_REF"
  217. fi
  218. if [[ -z "$INSTALL_REPO" ]]; then
  219. if [[ -n "$DETECTED_INSTALLER_REPO" ]]; then
  220. INSTALL_REPO="$DETECTED_INSTALLER_REPO"
  221. else
  222. INSTALL_REPO="$GITHUB_REPO"
  223. fi
  224. fi
  225. if [[ -z "$INSTALL_REF" ]]; then
  226. if [[ -n "$DETECTED_INSTALLER_REF" && "$DETECTED_INSTALLER_REF" != "HEAD" ]]; then
  227. INSTALL_REF="$DETECTED_INSTALLER_REF"
  228. else
  229. INSTALL_REF="main"
  230. fi
  231. fi
  232. }
  233. resolve_install_ref() {
  234. local ref="$1"
  235. # If ref exists on origin as a branch, track/reset it. Otherwise treat it as tag/commit.
  236. if git ls-remote --exit-code --heads origin "$ref" >/dev/null 2>&1; then
  237. git checkout -B "$ref" "origin/$ref" > /dev/null 2>&1
  238. git reset --hard "origin/$ref" > /dev/null 2>&1
  239. else
  240. git checkout "$ref" > /dev/null 2>&1
  241. fi
  242. }
  243. # ─────────────────────────────────────────────────────────────────────────────
  244. # Pre-flight Checks
  245. # ─────────────────────────────────────────────────────────────────────────────
  246. check_root() {
  247. if [[ $EUID -ne 0 ]]; then
  248. error "This script must be run as root (use sudo)"
  249. fi
  250. }
  251. check_raspberry_pi() {
  252. if ! grep -q "Raspberry Pi\|BCM2" /proc/cpuinfo 2>/dev/null; then
  253. error "This script is designed for Raspberry Pi only"
  254. fi
  255. # Detect Pi model for hardware recommendations
  256. local model
  257. model=$(tr -d '\0' < /proc/device-tree/model 2>/dev/null) || model="Unknown"
  258. success "Detected: $model"
  259. }
  260. check_raspberry_pi_os() {
  261. if [[ ! -f /etc/os-release ]]; then
  262. error "Cannot detect operating system"
  263. fi
  264. . /etc/os-release
  265. if [[ "$ID" != "raspbian" && "$ID" != "debian" ]]; then
  266. warn "Expected Raspberry Pi OS (Debian-based), found: $ID"
  267. if ! prompt_yes_no "Continue anyway?" "n"; then
  268. exit 0
  269. fi
  270. fi
  271. success "OS: $PRETTY_NAME"
  272. }
  273. detect_python() {
  274. local cmd=""
  275. if command -v python3 &>/dev/null; then
  276. cmd="python3"
  277. elif command -v python &>/dev/null; then
  278. local ver
  279. ver=$(python --version 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)
  280. if [[ "$ver" -ge 3 ]]; then
  281. cmd="python"
  282. fi
  283. fi
  284. if [[ -z "$cmd" ]]; then
  285. return 1
  286. fi
  287. local version
  288. version=$($cmd -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
  289. local major minor
  290. major=$(echo "$version" | cut -d'.' -f1)
  291. minor=$(echo "$version" | cut -d'.' -f2)
  292. if [[ "$major" -lt 3 ]] || { [[ "$major" -eq 3 ]] && [[ "$minor" -lt 10 ]]; }; then
  293. warn "Python $version found, but 3.10+ is required"
  294. return 1
  295. fi
  296. PYTHON_CMD="$cmd"
  297. success "Found Python $version"
  298. return 0
  299. }
  300. # ─────────────────────────────────────────────────────────────────────────────
  301. # Raspberry Pi Hardware Configuration
  302. # ─────────────────────────────────────────────────────────────────────────────
  303. enable_spi() {
  304. if raspi-config nonint get_spi 2>/dev/null | grep -q "1"; then
  305. info "Enabling SPI..."
  306. raspi-config nonint do_spi 0
  307. REBOOT_NEEDED="true"
  308. success "SPI enabled"
  309. else
  310. success "SPI already enabled"
  311. fi
  312. }
  313. enable_i2c() {
  314. if raspi-config nonint get_i2c 2>/dev/null | grep -q "1"; then
  315. info "Enabling I2C..."
  316. raspi-config nonint do_i2c 0
  317. REBOOT_NEEDED="true"
  318. success "I2C enabled"
  319. else
  320. success "I2C already enabled"
  321. fi
  322. }
  323. configure_boot_config() {
  324. # Find the boot config file (Bookworm+ uses /boot/firmware/config.txt)
  325. local boot_config="/boot/firmware/config.txt"
  326. if [[ ! -f "$boot_config" ]]; then
  327. boot_config="/boot/config.txt"
  328. fi
  329. if [[ ! -f "$boot_config" ]]; then
  330. warn "Boot config not found at /boot/firmware/config.txt or /boot/config.txt"
  331. warn "You may need to manually add: dtparam=i2c_arm=on and dtoverlay=spi0-0cs"
  332. return
  333. fi
  334. info "Configuring $boot_config..."
  335. # Migrate legacy SpoolBuddy setting (bus 0 / i2c_vc) to bus 1 / i2c_arm.
  336. if grep -q "^dtparam=i2c_vc=on" "$boot_config"; then
  337. sed -i "s/^dtparam=i2c_vc=on$/# dtparam=i2c_vc=on (disabled by SpoolBuddy installer; use i2c_arm bus 1)/" "$boot_config"
  338. REBOOT_NEEDED="true"
  339. success "Disabled legacy dtparam=i2c_vc=on"
  340. fi
  341. if grep -q "^# SpoolBuddy: I2C bus 0 for NAU7802 scale (GPIO0/GPIO1)" "$boot_config"; then
  342. sed -i "s/^# SpoolBuddy: I2C bus 0 for NAU7802 scale (GPIO0\/GPIO1)$/# SpoolBuddy: I2C bus 1 for NAU7802 scale (GPIO2\/GPIO3)/" "$boot_config"
  343. fi
  344. # Ensure I2C bus 1 (GPIO2/GPIO3) is enabled for NAU7802 scale
  345. if ! grep -q "^dtparam=i2c_arm=on" "$boot_config"; then
  346. echo "" >> "$boot_config"
  347. echo "# SpoolBuddy: I2C bus 1 for NAU7802 scale (GPIO2/GPIO3)" >> "$boot_config"
  348. echo "dtparam=i2c_arm=on" >> "$boot_config"
  349. REBOOT_NEEDED="true"
  350. success "Added dtparam=i2c_arm=on"
  351. else
  352. success "dtparam=i2c_arm=on already set"
  353. fi
  354. # Disable SPI auto chip-select (manual CS on GPIO23 for PN5180)
  355. if ! grep -q "^dtoverlay=spi0-0cs" "$boot_config"; then
  356. echo "" >> "$boot_config"
  357. echo "# SpoolBuddy: Disable SPI auto CS (manual CS on GPIO23 for PN5180)" >> "$boot_config"
  358. echo "dtoverlay=spi0-0cs" >> "$boot_config"
  359. REBOOT_NEEDED="true"
  360. success "Added dtoverlay=spi0-0cs"
  361. else
  362. success "dtoverlay=spi0-0cs already set"
  363. fi
  364. }
  365. # ─────────────────────────────────────────────────────────────────────────────
  366. # Package Installation
  367. # ─────────────────────────────────────────────────────────────────────────────
  368. install_system_packages() {
  369. run_with_progress "Updating package lists" apt-get update
  370. run_with_progress "Installing system packages" apt-get install -y $SYSTEM_PACKAGES
  371. }
  372. install_wifi_safeguard() {
  373. # Protect WiFi credentials from being wiped by apt upgrades.
  374. # Raspberry Pi OS Bookworm migrated from wpa_supplicant/dhcpcd to
  375. # NetworkManager, but certain package upgrades (raspberrypi-sys-mods,
  376. # raspi-config, NetworkManager itself) can delete saved connections
  377. # from /etc/NetworkManager/system-connections/. This hook backs them
  378. # up before dpkg runs and restores them if they vanish.
  379. local hook_file="/etc/apt/apt.conf.d/80-preserve-wifi"
  380. if [[ -f "$hook_file" ]]; then
  381. success "WiFi safeguard already installed"
  382. return
  383. fi
  384. # Only install if NetworkManager is the active network manager
  385. if ! systemctl is-active --quiet NetworkManager 2>/dev/null; then
  386. return
  387. fi
  388. # Write a helper script (avoids quote escaping issues in APT config)
  389. local helper="/usr/local/sbin/preserve-wifi"
  390. cat > "$helper" << 'HELPEREOF'
  391. #!/bin/sh
  392. # Called by APT hooks to preserve NetworkManager WiFi connections.
  393. NM_DIR="/etc/NetworkManager/system-connections"
  394. BAK_DIR="/etc/NetworkManager/system-connections.bak"
  395. case "$1" in
  396. backup)
  397. if [ -d "$NM_DIR" ] && [ -n "$(ls -A "$NM_DIR" 2>/dev/null)" ]; then
  398. cp -a "$NM_DIR/" "$BAK_DIR/"
  399. fi
  400. ;;
  401. restore)
  402. if [ -d "$BAK_DIR" ] && [ -z "$(ls -A "$NM_DIR" 2>/dev/null)" ]; then
  403. cp -a "$BAK_DIR"/* "$NM_DIR"/
  404. nmcli general reload 2>/dev/null
  405. fi
  406. rm -rf "$BAK_DIR" 2>/dev/null
  407. ;;
  408. esac
  409. HELPEREOF
  410. chmod +x "$helper"
  411. cat > "$hook_file" << 'APTEOF'
  412. // Preserve NetworkManager WiFi connections across apt upgrades.
  413. // Installed by SpoolBuddy.
  414. DPkg::Pre-Invoke {"/usr/local/sbin/preserve-wifi backup";};
  415. DPkg::Post-Invoke {"/usr/local/sbin/preserve-wifi restore";};
  416. APTEOF
  417. success "WiFi safeguard installed (${hook_file})"
  418. }
  419. upgrade_system_packages() {
  420. run_with_progress "Upgrading system packages" apt-get upgrade -y
  421. }
  422. # ─────────────────────────────────────────────────────────────────────────────
  423. # SpoolBuddy Installation
  424. # ─────────────────────────────────────────────────────────────────────────────
  425. create_spoolbuddy_user() {
  426. if id "$SPOOLBUDDY_SERVICE_USER" &>/dev/null; then
  427. info "User '$SPOOLBUDDY_SERVICE_USER' already exists"
  428. # Ensure existing installs get a real shell for SSH access
  429. usermod --shell /bin/bash "$SPOOLBUDDY_SERVICE_USER" 2>/dev/null || true
  430. else
  431. info "Creating service user '$SPOOLBUDDY_SERVICE_USER'..."
  432. useradd --system --shell /bin/bash --home-dir "$INSTALL_PATH" "$SPOOLBUDDY_SERVICE_USER"
  433. success "Service user created"
  434. fi
  435. # Add to hardware access groups (gpio, spi, i2c, video for backlight)
  436. for group in gpio spi i2c video; do
  437. if getent group "$group" &>/dev/null; then
  438. usermod -aG "$group" "$SPOOLBUDDY_SERVICE_USER" 2>/dev/null || true
  439. fi
  440. done
  441. success "User added to gpio, spi, i2c, video groups"
  442. # Allow passwordless restart of daemon + kiosk (needed for SSH-based updates from Bambuddy)
  443. cat > /etc/sudoers.d/spoolbuddy << 'SUDOERS'
  444. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/systemctl restart spoolbuddy.service
  445. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/systemctl restart getty@tty1.service
  446. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/find /home -maxdepth 5 *
  447. spoolbuddy ALL=(root) NOPASSWD: /sbin/reboot
  448. spoolbuddy ALL=(root) NOPASSWD: /sbin/shutdown -h now
  449. SUDOERS
  450. chmod 440 /etc/sudoers.d/spoolbuddy
  451. success "Sudoers entries created for service, kiosk restart, reboot and shutdown"
  452. }
  453. download_spoolbuddy() {
  454. if [[ -d "$INSTALL_PATH/.git" ]]; then
  455. info "Existing installation found, updating..."
  456. git config --global --add safe.directory "$INSTALL_PATH" 2>/dev/null || true
  457. cd "$INSTALL_PATH"
  458. git remote set-url origin "$INSTALL_REPO" 2>/dev/null || true
  459. run_with_progress "Fetching updates" git fetch origin
  460. resolve_install_ref "$INSTALL_REF"
  461. else
  462. mkdir -p "$INSTALL_PATH"
  463. run_with_progress "Cloning repository" git clone "$INSTALL_REPO" "$INSTALL_PATH"
  464. cd "$INSTALL_PATH"
  465. resolve_install_ref "$INSTALL_REF"
  466. fi
  467. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$INSTALL_PATH"
  468. }
  469. setup_spoolbuddy_venv() {
  470. cd "$INSTALL_PATH/spoolbuddy"
  471. run_with_progress "Creating SpoolBuddy venv" $PYTHON_CMD -m venv --system-site-packages venv
  472. run_with_progress "Upgrading pip" "$INSTALL_PATH/spoolbuddy/venv/bin/pip" install --upgrade pip
  473. run_with_progress "Installing SpoolBuddy packages" "$INSTALL_PATH/spoolbuddy/venv/bin/pip" install $SPOOLBUDDY_PIP_PACKAGES
  474. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$INSTALL_PATH/spoolbuddy/venv"
  475. }
  476. create_spoolbuddy_env() {
  477. info "Creating SpoolBuddy configuration..."
  478. local env_file="$INSTALL_PATH/spoolbuddy/.env"
  479. cat > "$env_file" << EOF
  480. # SpoolBuddy Configuration
  481. # Generated by install.sh on $(date)
  482. # Bambuddy backend URL
  483. SPOOLBUDDY_BACKEND_URL=$BAMBUDDY_URL
  484. # API key (create one in Bambuddy Settings -> API Keys)
  485. SPOOLBUDDY_API_KEY=$API_KEY
  486. # NAU7802 scale bus (RPi GPIO2/GPIO3)
  487. SPOOLBUDDY_I2C_BUS=1
  488. EOF
  489. chown "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$env_file"
  490. # Keep secrets owner-writable while allowing kiosk user (in spoolbuddy group)
  491. # to read backend URL/API key for dynamic launcher URL resolution.
  492. chgrp "$SPOOLBUDDY_SERVICE_USER" "$env_file"
  493. chmod 640 "$env_file"
  494. success "Configuration saved to $env_file"
  495. }
  496. ensure_kiosk_env_access() {
  497. local env_file="$INSTALL_PATH/spoolbuddy/.env"
  498. if [[ ! -f "$env_file" ]]; then
  499. warn "SpoolBuddy env file not found at $env_file"
  500. return
  501. fi
  502. # Ensure kiosk user is known even when this function is called outside setup_kiosk.
  503. if [[ -z "$KIOSK_USER" ]]; then
  504. KIOSK_USER="${SUDO_USER:-$(logname 2>/dev/null || echo pi)}"
  505. fi
  506. if id "$KIOSK_USER" &>/dev/null; then
  507. usermod -aG "$SPOOLBUDDY_SERVICE_USER" "$KIOSK_USER" 2>/dev/null || true
  508. fi
  509. chgrp "$SPOOLBUDDY_SERVICE_USER" "$env_file"
  510. chmod 640 "$env_file"
  511. if ! su -s /bin/sh -c "test -r '$env_file'" "$KIOSK_USER"; then
  512. error "Kiosk user '$KIOSK_USER' cannot read $env_file (required for dynamic kiosk URL). Check groups/permissions."
  513. fi
  514. success "Verified kiosk user '$KIOSK_USER' can read SpoolBuddy env"
  515. }
  516. setup_ssh_key() {
  517. info "Setting up SSH access for Bambuddy remote updates..."
  518. local ssh_dir="$INSTALL_PATH/.ssh"
  519. local auth_keys="$ssh_dir/authorized_keys"
  520. mkdir -p "$ssh_dir"
  521. chmod 700 "$ssh_dir"
  522. if [[ -n "$SSH_PUBKEY" ]]; then
  523. # Manual key provided via --ssh-pubkey flag
  524. if [[ -f "$auth_keys" ]] && grep -qF "$SSH_PUBKEY" "$auth_keys" 2>/dev/null; then
  525. info "SSH key already present in authorized_keys"
  526. else
  527. echo "$SSH_PUBKEY" >> "$auth_keys"
  528. success "SSH public key added"
  529. fi
  530. else
  531. # No manual key — the daemon will auto-deploy it on first registration
  532. info "SSH key will be deployed automatically when the daemon connects to Bambuddy"
  533. touch "$auth_keys"
  534. fi
  535. chmod 600 "$auth_keys"
  536. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$ssh_dir"
  537. }
  538. create_spoolbuddy_service() {
  539. info "Creating SpoolBuddy systemd service..."
  540. local after_line="After=network-online.target"
  541. if [[ "$INSTALL_MODE" == "full" ]]; then
  542. after_line="After=network-online.target bambuddy.service"
  543. fi
  544. cat > /etc/systemd/system/spoolbuddy.service << EOF
  545. [Unit]
  546. Description=SpoolBuddy - NFC Spool Management Daemon
  547. Documentation=https://github.com/maziggy/bambuddy
  548. $after_line
  549. Wants=network-online.target
  550. [Service]
  551. Type=simple
  552. User=$SPOOLBUDDY_SERVICE_USER
  553. WorkingDirectory=$INSTALL_PATH/spoolbuddy
  554. EnvironmentFile=$INSTALL_PATH/spoolbuddy/.env
  555. ExecStart=$INSTALL_PATH/spoolbuddy/venv/bin/python -m daemon.main
  556. Restart=always
  557. RestartSec=5
  558. StandardOutput=journal
  559. StandardError=journal
  560. [Install]
  561. WantedBy=multi-user.target
  562. EOF
  563. systemctl daemon-reload
  564. systemctl enable spoolbuddy.service
  565. success "SpoolBuddy service created and enabled"
  566. }
  567. # ─────────────────────────────────────────────────────────────────────────────
  568. # Bambuddy Installation (full mode only)
  569. # ─────────────────────────────────────────────────────────────────────────────
  570. create_bambuddy_user() {
  571. if id "$BAMBUDDY_SERVICE_USER" &>/dev/null; then
  572. info "User '$BAMBUDDY_SERVICE_USER' already exists"
  573. return
  574. fi
  575. info "Creating service user '$BAMBUDDY_SERVICE_USER'..."
  576. useradd --system --shell /usr/sbin/nologin --home-dir "$INSTALL_PATH" "$BAMBUDDY_SERVICE_USER"
  577. success "Service user created"
  578. }
  579. setup_bambuddy_venv() {
  580. cd "$INSTALL_PATH"
  581. run_with_progress "Creating Bambuddy venv" $PYTHON_CMD -m venv venv
  582. run_with_progress "Upgrading pip" "$INSTALL_PATH/venv/bin/pip" install --upgrade pip
  583. run_with_progress "Installing Bambuddy dependencies" "$INSTALL_PATH/venv/bin/pip" install -r requirements.txt
  584. chown -R "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$INSTALL_PATH/venv"
  585. }
  586. install_nodejs() {
  587. if command -v node &>/dev/null; then
  588. local version
  589. version=$(node --version 2>/dev/null | sed 's/^v//')
  590. local major
  591. major=$(echo "$version" | cut -d'.' -f1)
  592. if [[ "$major" -ge 20 ]]; then
  593. success "Found Node.js v$version"
  594. return
  595. fi
  596. fi
  597. apt-get remove -y nodejs npm > /dev/null 2>&1 || true
  598. run_with_progress "Setting up Node.js repository" bash -c "curl -fsSL https://deb.nodesource.com/setup_22.x | bash -"
  599. run_with_progress "Installing Node.js" apt-get install -y nodejs
  600. hash -r 2>/dev/null || true
  601. success "Node.js installed: $(node --version)"
  602. }
  603. build_frontend() {
  604. cd "$INSTALL_PATH/frontend"
  605. run_with_progress "Installing frontend dependencies" npm ci
  606. run_with_progress "Building frontend" npm run build
  607. }
  608. create_bambuddy_env() {
  609. info "Creating Bambuddy configuration..."
  610. local env_file="$INSTALL_PATH/.env"
  611. cat > "$env_file" << EOF
  612. # Bambuddy Configuration
  613. # Generated by install.sh on $(date)
  614. DEBUG=false
  615. LOG_LEVEL=INFO
  616. LOG_TO_FILE=true
  617. EOF
  618. chown "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$env_file"
  619. chmod 600 "$env_file"
  620. success "Configuration saved to $env_file"
  621. }
  622. create_bambuddy_directories() {
  623. mkdir -p "$INSTALL_PATH/data" "$INSTALL_PATH/logs"
  624. chown -R "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$INSTALL_PATH/data" "$INSTALL_PATH/logs"
  625. success "Data directories created"
  626. }
  627. create_bambuddy_service() {
  628. info "Creating Bambuddy systemd service..."
  629. cat > /etc/systemd/system/bambuddy.service << EOF
  630. [Unit]
  631. Description=Bambuddy - Bambu Lab Print Management
  632. Documentation=https://github.com/maziggy/bambuddy
  633. After=network.target
  634. [Service]
  635. Type=simple
  636. User=$BAMBUDDY_SERVICE_USER
  637. Group=$BAMBUDDY_SERVICE_USER
  638. WorkingDirectory=$INSTALL_PATH
  639. EnvironmentFile=$INSTALL_PATH/.env
  640. Environment="DATA_DIR=$INSTALL_PATH/data"
  641. Environment="LOG_DIR=$INSTALL_PATH/logs"
  642. ExecStart=$INSTALL_PATH/venv/bin/uvicorn backend.app.main:app --host 0.0.0.0 --port $BAMBUDDY_PORT
  643. Restart=on-failure
  644. RestartSec=5
  645. StandardOutput=journal
  646. StandardError=journal
  647. NoNewPrivileges=true
  648. PrivateTmp=true
  649. ProtectSystem=strict
  650. ProtectHome=true
  651. ReadWritePaths=$INSTALL_PATH/data $INSTALL_PATH/logs $INSTALL_PATH
  652. [Install]
  653. WantedBy=multi-user.target
  654. EOF
  655. systemctl daemon-reload
  656. systemctl enable bambuddy.service
  657. success "Bambuddy service created and enabled"
  658. }
  659. bootstrap_spoolbuddy_kiosk_key() {
  660. # Provision an API key for the local SpoolBuddy kiosk and write it into
  661. # spoolbuddy/.env. Runs against the Bambuddy DB directly (via the CLI),
  662. # so the bambuddy service does not need to be running yet.
  663. info "Provisioning SpoolBuddy kiosk API key..."
  664. local env_file="$INSTALL_PATH/spoolbuddy/.env"
  665. if [[ ! -f "$env_file" ]]; then
  666. warn "SpoolBuddy env file not found at $env_file — skipping kiosk key bootstrap"
  667. return
  668. fi
  669. # CWD must be $INSTALL_PATH so `python -m backend.app.cli` finds the backend
  670. # package on sys.path (matches the systemd unit's WorkingDirectory).
  671. local kiosk_key
  672. if ! kiosk_key="$(cd "$INSTALL_PATH" && sudo -u "$BAMBUDDY_SERVICE_USER" \
  673. env DATA_DIR="$INSTALL_PATH/data" LOG_DIR="$INSTALL_PATH/logs" \
  674. "$INSTALL_PATH/venv/bin/python" -m backend.app.cli kiosk-bootstrap --force)"; then
  675. error "Failed to bootstrap SpoolBuddy kiosk API key"
  676. fi
  677. if [[ -z "$kiosk_key" || "$kiosk_key" != bb_* ]]; then
  678. error "CLI returned an invalid API key (got: ${kiosk_key:0:8}...)"
  679. fi
  680. if ! grep -q '^SPOOLBUDDY_API_KEY=' "$env_file"; then
  681. error "Sentinel 'SPOOLBUDDY_API_KEY=' line missing in $env_file"
  682. fi
  683. # Escape for sed replacement (the key is base64url-safe, no slashes, but be defensive)
  684. local escaped_key
  685. escaped_key=$(printf '%s\n' "$kiosk_key" | sed -e 's/[\/&]/\\&/g')
  686. sed -i "s/^SPOOLBUDDY_API_KEY=.*/SPOOLBUDDY_API_KEY=${escaped_key}/" "$env_file"
  687. success "SpoolBuddy kiosk API key provisioned"
  688. }
  689. # ─────────────────────────────────────────────────────────────────────────────
  690. # System Strip-Down (dedicated appliance — remove unnecessary services/packages)
  691. # ─────────────────────────────────────────────────────────────────────────────
  692. strip_services() {
  693. info "Disabling unnecessary services..."
  694. local services=(
  695. bluetooth.service
  696. lightdm.service
  697. cloud-init-local.service
  698. cloud-init.service
  699. cloud-init-network.service
  700. cloud-config.service
  701. cloud-final.service
  702. cloud-init-hotplugd.socket
  703. avahi-daemon.service
  704. avahi-daemon.socket
  705. ModemManager.service
  706. udisks2.service
  707. apparmor.service
  708. man-db.timer
  709. e2scrub_all.timer
  710. e2scrub_reap.service
  711. # Audio stack (no speakers on a spool reader)
  712. pipewire.service
  713. pipewire.socket
  714. pipewire-pulse.service
  715. pipewire-pulse.socket
  716. wireplumber.service
  717. # Printing
  718. cups.service
  719. cups.socket
  720. cups-browsed.service
  721. # Desktop services
  722. accounts-daemon.service
  723. upower.service
  724. polkit.service
  725. # Flatpak portals (not using Flatpak)
  726. xdg-desktop-portal.service
  727. xdg-desktop-portal-gtk.service
  728. xdg-document-portal.service
  729. xdg-permission-store.service
  730. # NFS/RPC (unnecessary + security surface)
  731. rpcbind.service
  732. rpcbind.socket
  733. # Bluetooth media proxy
  734. mpris-proxy.service
  735. )
  736. local disabled=0
  737. for svc in "${services[@]}"; do
  738. if systemctl is-enabled "$svc" &>/dev/null; then
  739. systemctl disable "$svc" 2>/dev/null || true
  740. systemctl mask "$svc" 2>/dev/null || true
  741. (( ++disabled ))
  742. fi
  743. done
  744. if (( disabled > 0 )); then
  745. success "Disabled $disabled unnecessary services"
  746. else
  747. success "No unnecessary services to disable"
  748. fi
  749. # Mask user-level services globally via /etc/systemd/user/ overrides.
  750. # The su-based approach doesn't reliably reach the user's systemd instance
  751. # when run from sudo, so we create global masks that apply before login.
  752. local user_services=(
  753. pipewire.service
  754. pipewire.socket
  755. pipewire-pulse.service
  756. pipewire-pulse.socket
  757. wireplumber.service
  758. xdg-desktop-portal.service
  759. xdg-desktop-portal-gtk.service
  760. xdg-document-portal.service
  761. xdg-permission-store.service
  762. mpris-proxy.service
  763. )
  764. mkdir -p /etc/systemd/user
  765. local user_masked=0
  766. for svc in "${user_services[@]}"; do
  767. if [[ ! -L "/etc/systemd/user/$svc" ]] || [[ "$(readlink /etc/systemd/user/$svc)" != "/dev/null" ]]; then
  768. ln -sf /dev/null "/etc/systemd/user/$svc"
  769. (( ++user_masked ))
  770. fi
  771. done
  772. if (( user_masked > 0 )); then
  773. success "Masked $user_masked unnecessary user services globally"
  774. fi
  775. }
  776. strip_packages() {
  777. info "Removing unnecessary packages..."
  778. local packages=(
  779. mkvtoolnix
  780. firmware-atheros
  781. firmware-mediatek
  782. cloud-init
  783. rpi-cloud-init-mods
  784. rpi-connect-lite
  785. avahi-daemon
  786. modemmanager
  787. udisks2
  788. pipewire
  789. pipewire-pulse
  790. wireplumber
  791. cups
  792. cups-browsed
  793. cups-common
  794. cups-client
  795. rpcbind
  796. )
  797. local to_remove=()
  798. for pkg in "${packages[@]}"; do
  799. if dpkg -l "$pkg" &>/dev/null 2>&1; then
  800. to_remove+=("$pkg")
  801. fi
  802. done
  803. if (( ${#to_remove[@]} > 0 )); then
  804. run_with_progress "Removing ${#to_remove[@]} packages" apt-get remove --purge -y "${to_remove[@]}"
  805. run_with_progress "Cleaning up dependencies" apt-get autoremove --purge -y
  806. else
  807. success "No unnecessary packages to remove"
  808. fi
  809. }
  810. # ─────────────────────────────────────────────────────────────────────────────
  811. # Kiosk Setup (labwc + Chromium + Plymouth splash)
  812. # ─────────────────────────────────────────────────────────────────────────────
  813. setup_kiosk() {
  814. info "Setting up touchscreen kiosk..."
  815. # Detect kiosk user (the human user who ran sudo)
  816. KIOSK_USER="${SUDO_USER:-$(logname 2>/dev/null || echo pi)}"
  817. KIOSK_URL="${BAMBUDDY_URL}/spoolbuddy?token=${API_KEY}"
  818. local KIOSK_HOME
  819. KIOSK_HOME=$(eval echo "~$KIOSK_USER")
  820. info "Kiosk user: $KIOSK_USER (home: $KIOSK_HOME)"
  821. info "Kiosk URL: $KIOSK_URL"
  822. # Allow kiosk user to read SpoolBuddy env so launcher can resolve backend URL
  823. # and API key dynamically instead of using stale install-time fallback values.
  824. local spoolbuddy_env="$INSTALL_PATH/spoolbuddy/.env"
  825. if [[ -f "$spoolbuddy_env" ]]; then
  826. usermod -aG "$SPOOLBUDDY_SERVICE_USER" "$KIOSK_USER" 2>/dev/null || true
  827. chgrp "$SPOOLBUDDY_SERVICE_USER" "$spoolbuddy_env" 2>/dev/null || true
  828. chmod 640 "$spoolbuddy_env" 2>/dev/null || true
  829. fi
  830. # ── Install kiosk packages ────────────────────────────────────────────
  831. # Temporarily block initramfs rebuilds during package install — we rebuild
  832. # once at the end after the Plymouth theme is configured, saving ~4 runs
  833. # (one per installed kernel per hook trigger).
  834. if [[ -x /usr/sbin/update-initramfs ]]; then
  835. dpkg-divert --local --rename --add /usr/sbin/update-initramfs >/dev/null 2>&1 || true
  836. ln -sf /bin/true /usr/sbin/update-initramfs
  837. fi
  838. run_with_progress "Installing kiosk packages" apt-get install -y labwc chromium plymouth wlr-randr swayidle wlopm jq curl
  839. # Restore real update-initramfs
  840. if dpkg-divert --list /usr/sbin/update-initramfs 2>/dev/null | grep -q local; then
  841. rm -f /usr/sbin/update-initramfs
  842. dpkg-divert --local --rename --remove /usr/sbin/update-initramfs >/dev/null 2>&1 || true
  843. fi
  844. # ── config.txt tweaks ─────────────────────────────────────────────────
  845. local boot_config="/boot/firmware/config.txt"
  846. if [[ ! -f "$boot_config" ]]; then
  847. boot_config="/boot/config.txt"
  848. fi
  849. if [[ -f "$boot_config" ]]; then
  850. info "Configuring $boot_config for kiosk..."
  851. # Disable audio (change existing on→off)
  852. sed -i 's/^dtparam=audio=on/dtparam=audio=off/' "$boot_config"
  853. # Disable camera auto-detect (change existing 1→0)
  854. sed -i 's/^camera_auto_detect=1/camera_auto_detect=0/' "$boot_config"
  855. # Append if missing: gpu_mem=32
  856. if ! grep -q "^gpu_mem=" "$boot_config"; then
  857. echo "" >> "$boot_config"
  858. echo "# Kiosk: Minimal GPU firmware memory (KMS uses CMA from system RAM)" >> "$boot_config"
  859. echo "gpu_mem=32" >> "$boot_config"
  860. fi
  861. # Append if missing: dtoverlay=disable-bt
  862. if ! grep -q "^dtoverlay=disable-bt" "$boot_config"; then
  863. echo "" >> "$boot_config"
  864. echo "# Kiosk: Disable Bluetooth hardware" >> "$boot_config"
  865. echo "dtoverlay=disable-bt" >> "$boot_config"
  866. fi
  867. # Append if missing: disable_splash=1
  868. if ! grep -q "^disable_splash=" "$boot_config"; then
  869. echo "" >> "$boot_config"
  870. echo "# Kiosk: Disable Raspberry Pi firmware splash, use custom splash.png" >> "$boot_config"
  871. echo "disable_splash=1" >> "$boot_config"
  872. fi
  873. success "Boot config updated"
  874. fi
  875. # ── cmdline.txt tweaks ────────────────────────────────────────────────
  876. local cmdline="/boot/firmware/cmdline.txt"
  877. if [[ ! -f "$cmdline" ]]; then
  878. cmdline="/boot/cmdline.txt"
  879. fi
  880. if [[ -f "$cmdline" ]]; then
  881. info "Configuring $cmdline for kiosk..."
  882. # Remove serial console (Plymouth needs tty-only console)
  883. sed -i 's/console=serial0,[0-9]* //' "$cmdline"
  884. # Disable console blanking (kernel default is 600s, can blank during boot transition)
  885. grep -q "consoleblank=" "$cmdline" || sed -i 's/$/ consoleblank=0/' "$cmdline"
  886. # Add splash quiet loglevel=3 logo.nologo if missing
  887. grep -q "splash" "$cmdline" || sed -i 's/$/ splash quiet loglevel=3 logo.nologo/' "$cmdline"
  888. # Add video mode if missing
  889. grep -q "video=HDMI-A-1" "$cmdline" || sed -i 's/$/ video=HDMI-A-1:1024x600@60/' "$cmdline"
  890. success "Kernel cmdline updated"
  891. fi
  892. # ── Plymouth splash theme ─────────────────────────────────────────────
  893. info "Installing Plymouth boot splash..."
  894. local theme_dir="/usr/share/plymouth/themes/spoolbuddy"
  895. mkdir -p "$theme_dir"
  896. # Copy bundled splash image from the install directory
  897. local script_dir
  898. script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  899. if [[ -f "$script_dir/splash.png" ]]; then
  900. cp "$script_dir/splash.png" "$theme_dir/splash.png"
  901. elif [[ -f "$INSTALL_PATH/spoolbuddy/install/splash.png" ]]; then
  902. cp "$INSTALL_PATH/spoolbuddy/install/splash.png" "$theme_dir/splash.png"
  903. else
  904. warn "splash.png not found — Plymouth splash will not display an image"
  905. fi
  906. # Write .plymouth theme file
  907. cat > "$theme_dir/spoolbuddy.plymouth" << 'EOF'
  908. [Plymouth Theme]
  909. Name=SpoolBuddy
  910. Description=SpoolBuddy boot splash
  911. ModuleName=script
  912. [script]
  913. ImageDir=/usr/share/plymouth/themes/spoolbuddy
  914. ScriptFile=/usr/share/plymouth/themes/spoolbuddy/spoolbuddy.script
  915. EOF
  916. # Write .script theme file
  917. cat > "$theme_dir/spoolbuddy.script" << 'EOF'
  918. wallpaper_image = Image("splash.png");
  919. screen_width = Window.GetWidth();
  920. screen_height = Window.GetHeight();
  921. resized_wallpaper_image = wallpaper_image.Scale(screen_width, screen_height);
  922. wallpaper_sprite = Sprite(resized_wallpaper_image);
  923. wallpaper_sprite.SetZ(-100);
  924. EOF
  925. plymouth-set-default-theme spoolbuddy
  926. run_with_progress "Updating initramfs" update-initramfs -u
  927. success "Plymouth splash installed"
  928. # ── Auto-login on tty1 ────────────────────────────────────────────────
  929. info "Configuring auto-login for $KIOSK_USER..."
  930. mkdir -p /etc/systemd/system/getty@tty1.service.d
  931. cat > /etc/systemd/system/getty@tty1.service.d/autologin.conf << EOF
  932. [Unit]
  933. After=network-online.target
  934. Wants=network-online.target
  935. [Service]
  936. ExecStart=
  937. ExecStart=-/sbin/agetty --autologin $KIOSK_USER --noclear %I \$TERM
  938. EOF
  939. success "Auto-login configured"
  940. # ── labwc rc.xml (no decorations, no keybinds) ────────────────────────
  941. info "Configuring labwc window manager..."
  942. local labwc_dir="$KIOSK_HOME/.config/labwc"
  943. mkdir -p "$labwc_dir"
  944. cat > "$labwc_dir/rc.xml" << 'EOF'
  945. <?xml version="1.0"?>
  946. <labwc_config>
  947. <!-- Disable screen blanking — kiosk must stay on -->
  948. <core>
  949. <screenBlankTimeout>0</screenBlankTimeout>
  950. </core>
  951. <theme>
  952. <name></name>
  953. <cornerRadius>0</cornerRadius>
  954. </theme>
  955. <!-- Disable all keybindings - kiosk lockdown -->
  956. <keyboard>
  957. </keyboard>
  958. <!-- Disable right-click menu -->
  959. <mouse>
  960. <default />
  961. </mouse>
  962. <!-- Remove window decorations, maximize Chromium, prevent unfullscreen -->
  963. <windowRules>
  964. <windowRule identifier="*">
  965. <serverDecoration>no</serverDecoration>
  966. </windowRule>
  967. <windowRule identifier="chromium">
  968. <skipTaskbar>yes</skipTaskbar>
  969. <fixedPosition>yes</fixedPosition>
  970. </windowRule>
  971. </windowRules>
  972. </labwc_config>
  973. EOF
  974. # ── Override Debian/RPi Chromium defaults for kiosk performance ──────
  975. cat > /etc/chromium.d/spoolbuddy-kiosk << 'CHROMIUM_EOF'
  976. # SpoolBuddy kiosk: add kiosk-specific flags on top of Pi defaults.
  977. # Preserves Pi GPU settings (gpu-rasterization, ANGLE/GLES) for stability.
  978. CHROMIUM_FLAGS="$CHROMIUM_FLAGS --disable-smooth-scrolling"
  979. CHROMIUM_FLAGS="$CHROMIUM_FLAGS --disable-extensions"
  980. CHROMIUM_FLAGS="$CHROMIUM_FLAGS --disable-background-timer-throttling"
  981. CHROMIUM_FLAGS="$CHROMIUM_FLAGS --disable-renderer-backgrounding"
  982. CHROMIUM_FLAGS="$CHROMIUM_FLAGS --disable-crash-reporter"
  983. CHROMIUM_EOF
  984. success "Chromium kiosk performance flags installed"
  985. # ── kiosk launcher (dynamic URL from spoolbuddy/.env) ─────────────────
  986. local kiosk_launcher="/usr/local/bin/spoolbuddy-kiosk-launch"
  987. cat > "$kiosk_launcher" << EOF
  988. #!/usr/bin/env bash
  989. set -euo pipefail
  990. ENV_FILE="$INSTALL_PATH/spoolbuddy/.env"
  991. FALLBACK_URL="$KIOSK_URL"
  992. backend_url=""
  993. api_key=""
  994. if [[ -r "\$ENV_FILE" ]]; then
  995. backend_url="\$(sed -n 's/^SPOOLBUDDY_BACKEND_URL=//p' "\$ENV_FILE" | tail -n1 | tr -d '\r')"
  996. api_key="\$(sed -n 's/^SPOOLBUDDY_API_KEY=//p' "\$ENV_FILE" | tail -n1 | tr -d '\r')"
  997. backend_url="\${backend_url%\"}"
  998. backend_url="\${backend_url#\"}"
  999. api_key="\${api_key%\"}"
  1000. api_key="\${api_key#\"}"
  1001. elif [[ -f "\$ENV_FILE" ]]; then
  1002. echo "spoolbuddy-kiosk-launch: ERROR: \$ENV_FILE exists but is not readable" >&2
  1003. echo "spoolbuddy-kiosk-launch: Fix permissions (group-readable by kiosk user) and restart kiosk" >&2
  1004. exit 1
  1005. fi
  1006. if [[ -n "\$backend_url" && -n "\$api_key" ]]; then
  1007. backend_url="\${backend_url%/}"
  1008. kiosk_url="\${backend_url}/spoolbuddy?token=\${api_key}"
  1009. else
  1010. kiosk_url="\$FALLBACK_URL"
  1011. fi
  1012. # Wait for the Bambuddy backend to be reachable before launching Chromium.
  1013. # Without this the browser opens before uvicorn has bound to the port on a
  1014. # cold boot and the user sees an ERR_CONNECTION_REFUSED splash until they
  1015. # manually reload. Probe /health (no auth, no body) with a short timeout.
  1016. probe_url="\${backend_url:-http://localhost}/health"
  1017. for _i in \$(seq 1 60); do
  1018. if curl -sf --max-time 2 "\$probe_url" >/dev/null 2>&1; then
  1019. break
  1020. fi
  1021. sleep 1
  1022. done
  1023. # Ephemeral user-data-dir under /tmp + wipe on every launch.
  1024. #
  1025. # The kiosk has no per-user state worth persisting (the auth token is in
  1026. # the URL query, not a stored cookie), but the default profile at
  1027. # ~/.config/chromium was accumulating two specific kinds of state across
  1028. # reboots that broke deploys badly:
  1029. #
  1030. # 1. HTTP disk cache holding old index.html across browser restarts.
  1031. # Chromium's heuristic-cache freshness window kept the old HTML
  1032. # "fresh" for days, which referenced an old content-hashed bundle,
  1033. # so newly deployed code never reached the running tab even after
  1034. # pkill+relaunch. Reproduced in the wild during the #1133 rollout
  1035. # — the kiosk kept showing the pre-fix picker for hours after every
  1036. # cache-clear attempt because the persistent profile would re-seed
  1037. # the cache from disk on next start.
  1038. # 2. A stuck Service Worker registration, which intercepted requests
  1039. # with its own cache layer (CacheStorage), independent of the HTTP
  1040. # cache. Even after \`rm -rf Default/Cache/*\` the SW could replay
  1041. # stale responses from CacheStorage until explicitly unregistered.
  1042. #
  1043. # Wiping the user-data-dir on every launch is the simplest, most
  1044. # bulletproof escape hatch — every kiosk restart is now functionally
  1045. # equivalent to a private-window first-load. Future deploys propagate
  1046. # automatically: the next chromium launch picks up the latest bundle
  1047. # without any extra tooling. Trade-off is a slightly slower first paint
  1048. # (no warm cache) and zero offline support, neither of which matter for
  1049. # a single-purpose kiosk facing a backend on the same LAN.
  1050. USER_DATA_DIR="/tmp/spoolbuddy-kiosk-userdata"
  1051. rm -rf "\$USER_DATA_DIR"
  1052. exec chromium --kiosk --no-first-run --disable-infobars \
  1053. --disable-session-crashed-bubble --disable-features=TranslateUI \
  1054. --noerrdialogs --disable-component-update \
  1055. --overscroll-history-navigation=0 \
  1056. --ozone-platform=wayland \
  1057. --disable-crash-reporter --disable-breakpad \
  1058. --user-data-dir="\$USER_DATA_DIR" \
  1059. "\$kiosk_url"
  1060. EOF
  1061. chmod 755 "$kiosk_launcher"
  1062. # Tiny self-check: ensure sed command substitutions were not expanded
  1063. # while generating the launcher script.
  1064. if ! grep -Fq 'backend_url="$(sed -n' "$kiosk_launcher" || ! grep -Fq 'api_key="$(sed -n' "$kiosk_launcher"; then
  1065. error "Kiosk launcher generation failed: dynamic env parsing commands were expanded unexpectedly"
  1066. fi
  1067. # ── labwc autostart ───────────────────────────────────────────────────
  1068. cat > "$labwc_dir/autostart" << EOF
  1069. # Force 1024x600 (panel doesn't advertise this natively)
  1070. wlr-randr --output HDMI-A-1 --custom-mode 1024x600@60 &
  1071. # Idle watchdog: powers off HDMI via wlopm after the configured inactivity
  1072. # timeout (SpoolBuddy Settings → Display → Screen blank timeout). Reads the
  1073. # current value from the backend on startup; UI changes take effect on the
  1074. # next reboot / kiosk restart.
  1075. $INSTALL_PATH/spoolbuddy/install/spoolbuddy-idle.sh &
  1076. # Launch Chromium via helper that resolves URL from spoolbuddy/.env
  1077. $kiosk_launcher &
  1078. EOF
  1079. chown -R "$KIOSK_USER:$KIOSK_USER" "$labwc_dir"
  1080. # ── .bash_profile (source .bashrc, exec labwc on tty1) ────────────────
  1081. cat > "$KIOSK_HOME/.bash_profile" << 'EOF'
  1082. # Source .bashrc if it exists
  1083. if [ -f ~/.bashrc ]; then
  1084. . ~/.bashrc
  1085. fi
  1086. # Auto-start kiosk on tty1
  1087. if [ "$(tty)" = "/dev/tty1" ]; then
  1088. exec labwc
  1089. fi
  1090. EOF
  1091. chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.bash_profile"
  1092. REBOOT_NEEDED="true"
  1093. success "Kiosk setup complete"
  1094. }
  1095. # ─────────────────────────────────────────────────────────────────────────────
  1096. # User Prompts
  1097. # ─────────────────────────────────────────────────────────────────────────────
  1098. parse_args() {
  1099. while [[ $# -gt 0 ]]; do
  1100. case "$1" in
  1101. --mode)
  1102. INSTALL_MODE="$2"
  1103. shift 2
  1104. ;;
  1105. --repo)
  1106. INSTALL_REPO="$(normalize_github_repo_url "$2")"
  1107. shift 2
  1108. ;;
  1109. --ref)
  1110. INSTALL_REF="$2"
  1111. shift 2
  1112. ;;
  1113. --bambuddy-url)
  1114. BAMBUDDY_URL="$2"
  1115. shift 2
  1116. ;;
  1117. --api-key)
  1118. API_KEY="$2"
  1119. shift 2
  1120. ;;
  1121. --path)
  1122. INSTALL_PATH="$2"
  1123. shift 2
  1124. ;;
  1125. --port)
  1126. BAMBUDDY_PORT="$2"
  1127. shift 2
  1128. ;;
  1129. --ssh-pubkey)
  1130. SSH_PUBKEY="$2"
  1131. shift 2
  1132. ;;
  1133. --yes|-y)
  1134. NON_INTERACTIVE="true"
  1135. shift
  1136. ;;
  1137. --help|-h)
  1138. show_help
  1139. ;;
  1140. *)
  1141. error "Unknown option: $1 (use --help for usage)"
  1142. ;;
  1143. esac
  1144. done
  1145. }
  1146. ask_install_mode() {
  1147. if [[ -n "$INSTALL_MODE" ]]; then
  1148. return
  1149. fi
  1150. echo ""
  1151. echo -e "${BOLD}How would you like to set up SpoolBuddy?${NC}"
  1152. echo ""
  1153. echo -e " ${CYAN}1)${NC} SpoolBuddy only"
  1154. echo " NFC reader + scale on this RPi, Bambuddy runs on another device"
  1155. echo ""
  1156. echo -e " ${CYAN}2)${NC} SpoolBuddy + Bambuddy"
  1157. echo " Both running natively on this Raspberry Pi"
  1158. echo ""
  1159. while true; do
  1160. echo -en "${BOLD}Choose${NC} [${CYAN}1${NC}/${CYAN}2${NC}]: "
  1161. read -r choice
  1162. case "$choice" in
  1163. 1) INSTALL_MODE="spoolbuddy"; return;;
  1164. 2) INSTALL_MODE="full"; return;;
  1165. *) echo "Please enter 1 or 2.";;
  1166. esac
  1167. done
  1168. }
  1169. gather_config() {
  1170. echo ""
  1171. echo -e "${BOLD}Configuration${NC}"
  1172. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  1173. echo ""
  1174. # Set default install path based on mode
  1175. if [[ -z "$INSTALL_PATH" ]]; then
  1176. if [[ "$INSTALL_MODE" == "full" ]]; then
  1177. INSTALL_PATH="/opt/bambuddy"
  1178. else
  1179. INSTALL_PATH="/opt/bambuddy"
  1180. fi
  1181. fi
  1182. prompt "Installation directory" "$INSTALL_PATH" INSTALL_PATH
  1183. if [[ -z "$INSTALL_REPO" ]]; then
  1184. INSTALL_REPO="$GITHUB_REPO"
  1185. fi
  1186. prompt "Git repository URL" "$INSTALL_REPO" INSTALL_REPO
  1187. INSTALL_REPO="$(normalize_github_repo_url "$INSTALL_REPO")"
  1188. if [[ -z "$INSTALL_REF" ]]; then
  1189. INSTALL_REF="main"
  1190. fi
  1191. if [[ "$NON_INTERACTIVE" != "true" && -n "$DETECTED_INSTALLER_REF" && "$DETECTED_INSTALLER_REF" != "HEAD" ]]; then
  1192. echo ""
  1193. echo -e "${BOLD}Install Source Ref${NC}"
  1194. echo "1) main"
  1195. echo "2) $DETECTED_INSTALLER_REF (detected from installer context)"
  1196. echo "3) custom"
  1197. while true; do
  1198. echo -en "${BOLD}Choose${NC} [1/2/3]: "
  1199. read -r ref_choice
  1200. case "$ref_choice" in
  1201. ""|1)
  1202. INSTALL_REF="main"
  1203. break
  1204. ;;
  1205. 2)
  1206. INSTALL_REF="$DETECTED_INSTALLER_REF"
  1207. break
  1208. ;;
  1209. 3)
  1210. prompt "Git ref (branch/tag/commit)" "$INSTALL_REF" INSTALL_REF
  1211. break
  1212. ;;
  1213. *)
  1214. echo "Please enter 1, 2, or 3."
  1215. ;;
  1216. esac
  1217. done
  1218. else
  1219. prompt "Git ref (branch/tag/commit)" "$INSTALL_REF" INSTALL_REF
  1220. fi
  1221. if [[ "$INSTALL_MODE" == "spoolbuddy" ]]; then
  1222. # Need remote Bambuddy URL and API key
  1223. echo ""
  1224. info "SpoolBuddy needs to connect to your Bambuddy server."
  1225. info "You can find/create an API key in Bambuddy under Settings -> API Keys."
  1226. echo ""
  1227. while [[ -z "$BAMBUDDY_URL" ]]; do
  1228. prompt "Bambuddy server URL (e.g. http://192.168.1.100:8000)" "" BAMBUDDY_URL
  1229. if [[ -z "$BAMBUDDY_URL" ]]; then
  1230. warn "Bambuddy URL is required"
  1231. fi
  1232. done
  1233. while [[ -z "$API_KEY" ]]; do
  1234. prompt "Bambuddy API key" "" API_KEY
  1235. if [[ -z "$API_KEY" ]]; then
  1236. warn "API key is required"
  1237. fi
  1238. done
  1239. else
  1240. # Full mode — Bambuddy runs locally
  1241. prompt "Bambuddy port" "$BAMBUDDY_PORT" BAMBUDDY_PORT
  1242. BAMBUDDY_URL="http://localhost:$BAMBUDDY_PORT"
  1243. echo ""
  1244. info "After installation, create an API key in Bambuddy (Settings -> API Keys)"
  1245. info "and update it in: $INSTALL_PATH/spoolbuddy/.env"
  1246. API_KEY="CHANGE_ME_AFTER_SETUP"
  1247. fi
  1248. # Summary
  1249. echo ""
  1250. echo -e "${BOLD}Installation Summary${NC}"
  1251. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  1252. echo -e " Mode: ${GREEN}$([ "$INSTALL_MODE" == "full" ] && echo "Bambuddy + SpoolBuddy" || echo "SpoolBuddy only")${NC}"
  1253. echo -e " Install path: ${GREEN}$INSTALL_PATH${NC}"
  1254. echo -e " Git repo: ${GREEN}$INSTALL_REPO${NC}"
  1255. echo -e " Git ref: ${GREEN}$INSTALL_REF${NC}"
  1256. if [[ "$INSTALL_MODE" == "full" ]]; then
  1257. echo -e " Bambuddy port: ${GREEN}$BAMBUDDY_PORT${NC}"
  1258. echo -e " Bambuddy URL: ${GREEN}$BAMBUDDY_URL${NC}"
  1259. else
  1260. echo -e " Bambuddy URL: ${GREEN}$BAMBUDDY_URL${NC}"
  1261. fi
  1262. echo ""
  1263. if ! prompt_yes_no "Proceed with installation?" "y"; then
  1264. echo "Installation cancelled."
  1265. exit 0
  1266. fi
  1267. }
  1268. # ─────────────────────────────────────────────────────────────────────────────
  1269. # Main
  1270. # ─────────────────────────────────────────────────────────────────────────────
  1271. main() {
  1272. parse_args "$@"
  1273. detect_installer_source_context
  1274. echo ""
  1275. echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
  1276. echo -e "${CYAN}║ ║${NC}"
  1277. echo -e "${CYAN}║ ____ _ ____ _ _ ║${NC}"
  1278. echo -e "${CYAN}║ / ___| _ __ ___ ___ | | __ ) _ _ __| | __| |_ _ ║${NC}"
  1279. echo -e "${CYAN}║ \\___ \\| '_ \\ / _ \\ / _ \\| | _ \\| | | |/ _\` |/ _\` | | | |║${NC}"
  1280. echo -e "${CYAN}║ ___) | |_) | (_) | (_) | | |_) | |_| | (_| | (_| | |_| |║${NC}"
  1281. echo -e "${CYAN}║ |____/| .__/ \\___/ \\___/|_|____/ \\__,_|\\__,_|\\__,_|\\__, |║${NC}"
  1282. echo -e "${CYAN}║ |_| |___/ ║${NC}"
  1283. echo -e "${CYAN}║ ║${NC}"
  1284. echo -e "${CYAN}║ NFC Spool Management for Bambuddy ║${NC}"
  1285. echo -e "${CYAN}║ ║${NC}"
  1286. echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
  1287. echo ""
  1288. # Check if running via pipe without -y
  1289. if [[ ! -t 0 ]] && [[ "$NON_INTERACTIVE" != "true" ]]; then
  1290. error "Interactive mode requires a terminal. Use -y for unattended install, or download and run directly."
  1291. fi
  1292. # Pre-flight checks
  1293. check_root
  1294. check_raspberry_pi
  1295. check_raspberry_pi_os
  1296. if ! detect_python; then
  1297. info "Python 3.10+ not found, will install..."
  1298. fi
  1299. # Gather user preferences
  1300. ask_install_mode
  1301. gather_config
  1302. # Validate mode
  1303. if [[ "$INSTALL_MODE" != "spoolbuddy" && "$INSTALL_MODE" != "full" ]]; then
  1304. error "Invalid mode: $INSTALL_MODE (must be 'spoolbuddy' or 'full')"
  1305. fi
  1306. echo ""
  1307. echo -e "${BOLD}Starting Installation${NC}"
  1308. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  1309. echo ""
  1310. # ── Step 1: Raspberry Pi hardware config ──────────────────────────────
  1311. info "Configuring Raspberry Pi hardware..."
  1312. enable_spi
  1313. enable_i2c
  1314. configure_boot_config
  1315. echo ""
  1316. # ── Step 2: System packages ───────────────────────────────────────────
  1317. install_system_packages
  1318. install_wifi_safeguard
  1319. upgrade_system_packages
  1320. detect_python || error "Failed to install Python 3.10+"
  1321. echo ""
  1322. # ── Step 2b: Strip unnecessary services & packages ────────────────────
  1323. strip_services
  1324. strip_packages
  1325. echo ""
  1326. # ── Step 3: Download source code ──────────────────────────────────────
  1327. create_spoolbuddy_user
  1328. download_spoolbuddy
  1329. echo ""
  1330. # ── Step 3b: Kiosk setup (labwc + Chromium + squeekboard + Plymouth) ──
  1331. setup_kiosk
  1332. echo ""
  1333. # ── Step 4: SpoolBuddy setup ──────────────────────────────────────────
  1334. info "Setting up SpoolBuddy..."
  1335. setup_spoolbuddy_venv
  1336. create_spoolbuddy_env
  1337. # Kiosk env access: only needed if actual kiosk hardware is available
  1338. if [[ -f /boot/firmware/config.txt ]] || [[ -f /boot/config.txt ]]; then
  1339. ensure_kiosk_env_access
  1340. fi
  1341. setup_ssh_key
  1342. create_spoolbuddy_service
  1343. echo ""
  1344. # ── Step 5: Bambuddy setup (full mode only) ───────────────────────────
  1345. if [[ "$INSTALL_MODE" == "full" ]]; then
  1346. info "Setting up Bambuddy..."
  1347. create_bambuddy_user
  1348. setup_bambuddy_venv
  1349. install_nodejs
  1350. build_frontend
  1351. create_bambuddy_directories
  1352. create_bambuddy_env
  1353. create_bambuddy_service
  1354. bootstrap_spoolbuddy_kiosk_key
  1355. echo ""
  1356. fi
  1357. # ── Done ──────────────────────────────────────────────────────────────
  1358. echo ""
  1359. echo -e "${GREEN}╔══════════════════════════════════════════════════════════╗${NC}"
  1360. echo -e "${GREEN}║ ║${NC}"
  1361. echo -e "${GREEN}║ Installation Complete! ║${NC}"
  1362. echo -e "${GREEN}║ ║${NC}"
  1363. echo -e "${GREEN}╚══════════════════════════════════════════════════════════╝${NC}"
  1364. echo ""
  1365. local ip_addr
  1366. ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}') || ip_addr="<your-ip>"
  1367. if [[ "$INSTALL_MODE" == "full" ]]; then
  1368. echo -e " ${BOLD}Bambuddy:${NC} ${CYAN}http://$ip_addr:$BAMBUDDY_PORT${NC}"
  1369. else
  1370. echo -e " ${BOLD}SpoolBuddy:${NC} Connecting to ${CYAN}$BAMBUDDY_URL${NC}"
  1371. fi
  1372. echo -e " ${BOLD}Kiosk URL:${NC} ${CYAN}$KIOSK_URL${NC}"
  1373. echo -e " ${BOLD}Kiosk user:${NC} ${CYAN}$KIOSK_USER${NC}"
  1374. echo ""
  1375. if [[ "$INSTALL_MODE" == "full" ]]; then
  1376. echo -e " ${BOLD}Next steps:${NC}"
  1377. echo -e " 1. Reboot (required for kiosk, Plymouth splash, and hardware changes)"
  1378. echo -e " 2. The touchscreen kiosk will start automatically after reboot"
  1379. echo -e " 3. On another device, open ${CYAN}http://$ip_addr:$BAMBUDDY_PORT${NC} to complete first-run admin setup"
  1380. fi
  1381. echo ""
  1382. echo -e " ${BOLD}Manage services:${NC}"
  1383. echo -e " SpoolBuddy status: ${CYAN}sudo systemctl status spoolbuddy${NC}"
  1384. echo -e " SpoolBuddy logs: ${CYAN}sudo journalctl -u spoolbuddy -f${NC}"
  1385. if [[ "$INSTALL_MODE" == "full" ]]; then
  1386. echo -e " Bambuddy status: ${CYAN}sudo systemctl status bambuddy${NC}"
  1387. echo -e " Bambuddy logs: ${CYAN}sudo journalctl -u bambuddy -f${NC}"
  1388. fi
  1389. echo ""
  1390. echo -e " ${BOLD}Configuration:${NC} ${CYAN}$INSTALL_PATH/spoolbuddy/.env${NC}"
  1391. echo -e " ${BOLD}Hardware wiring:${NC} ${CYAN}$INSTALL_PATH/spoolbuddy/README.md${NC}"
  1392. echo -e " ${BOLD}Diagnostics:${NC} ${CYAN}sudo $INSTALL_PATH/spoolbuddy/venv/bin/python $INSTALL_PATH/spoolbuddy/pn5180_diag.py${NC}"
  1393. echo ""
  1394. echo -e " ${YELLOW}A reboot is required to apply all changes (kiosk, Plymouth splash, hardware).${NC}"
  1395. echo ""
  1396. if prompt_yes_no "Reboot now?" "y"; then
  1397. reboot
  1398. else
  1399. echo -e " Run ${CYAN}sudo reboot${NC} when ready."
  1400. fi
  1401. echo ""
  1402. }
  1403. main "$@"