install.sh 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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. # ─────────────────────────────────────────────────────────────────────────────
  373. # SpoolBuddy Installation
  374. # ─────────────────────────────────────────────────────────────────────────────
  375. create_spoolbuddy_user() {
  376. if id "$SPOOLBUDDY_SERVICE_USER" &>/dev/null; then
  377. info "User '$SPOOLBUDDY_SERVICE_USER' already exists"
  378. # Ensure existing installs get a real shell for SSH access
  379. usermod --shell /bin/bash "$SPOOLBUDDY_SERVICE_USER" 2>/dev/null || true
  380. else
  381. info "Creating service user '$SPOOLBUDDY_SERVICE_USER'..."
  382. useradd --system --shell /bin/bash --home-dir "$INSTALL_PATH" "$SPOOLBUDDY_SERVICE_USER"
  383. success "Service user created"
  384. fi
  385. # Add to hardware access groups (gpio, spi, i2c, video for backlight)
  386. for group in gpio spi i2c video; do
  387. if getent group "$group" &>/dev/null; then
  388. usermod -aG "$group" "$SPOOLBUDDY_SERVICE_USER" 2>/dev/null || true
  389. fi
  390. done
  391. success "User added to gpio, spi, i2c, video groups"
  392. # Allow passwordless restart of daemon + kiosk (needed for SSH-based updates from Bambuddy)
  393. cat > /etc/sudoers.d/spoolbuddy << 'SUDOERS'
  394. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/systemctl restart spoolbuddy.service
  395. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/systemctl restart getty@tty1.service
  396. spoolbuddy ALL=(root) NOPASSWD: /usr/bin/find /home -maxdepth 5 *
  397. SUDOERS
  398. chmod 440 /etc/sudoers.d/spoolbuddy
  399. success "Sudoers entries created for service and kiosk restart"
  400. }
  401. download_spoolbuddy() {
  402. if [[ -d "$INSTALL_PATH/.git" ]]; then
  403. info "Existing installation found, updating..."
  404. git config --global --add safe.directory "$INSTALL_PATH" 2>/dev/null || true
  405. cd "$INSTALL_PATH"
  406. git remote set-url origin "$INSTALL_REPO" 2>/dev/null || true
  407. run_with_progress "Fetching updates" git fetch origin
  408. resolve_install_ref "$INSTALL_REF"
  409. else
  410. mkdir -p "$INSTALL_PATH"
  411. run_with_progress "Cloning repository" git clone "$INSTALL_REPO" "$INSTALL_PATH"
  412. cd "$INSTALL_PATH"
  413. resolve_install_ref "$INSTALL_REF"
  414. fi
  415. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$INSTALL_PATH"
  416. }
  417. setup_spoolbuddy_venv() {
  418. cd "$INSTALL_PATH/spoolbuddy"
  419. run_with_progress "Creating SpoolBuddy venv" $PYTHON_CMD -m venv --system-site-packages venv
  420. run_with_progress "Upgrading pip" "$INSTALL_PATH/spoolbuddy/venv/bin/pip" install --upgrade pip
  421. run_with_progress "Installing SpoolBuddy packages" "$INSTALL_PATH/spoolbuddy/venv/bin/pip" install $SPOOLBUDDY_PIP_PACKAGES
  422. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$INSTALL_PATH/spoolbuddy/venv"
  423. }
  424. create_spoolbuddy_env() {
  425. info "Creating SpoolBuddy configuration..."
  426. local env_file="$INSTALL_PATH/spoolbuddy/.env"
  427. cat > "$env_file" << EOF
  428. # SpoolBuddy Configuration
  429. # Generated by install.sh on $(date)
  430. # Bambuddy backend URL
  431. SPOOLBUDDY_BACKEND_URL=$BAMBUDDY_URL
  432. # API key (create one in Bambuddy Settings -> API Keys)
  433. SPOOLBUDDY_API_KEY=$API_KEY
  434. # NAU7802 scale bus (RPi GPIO2/GPIO3)
  435. SPOOLBUDDY_I2C_BUS=1
  436. EOF
  437. chown "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$env_file"
  438. # Keep secrets owner-writable while allowing kiosk user (in spoolbuddy group)
  439. # to read backend URL/API key for dynamic launcher URL resolution.
  440. chgrp "$SPOOLBUDDY_SERVICE_USER" "$env_file"
  441. chmod 640 "$env_file"
  442. success "Configuration saved to $env_file"
  443. }
  444. ensure_kiosk_env_access() {
  445. local env_file="$INSTALL_PATH/spoolbuddy/.env"
  446. if [[ ! -f "$env_file" ]]; then
  447. warn "SpoolBuddy env file not found at $env_file"
  448. return
  449. fi
  450. # Ensure kiosk user is known even when this function is called outside setup_kiosk.
  451. if [[ -z "$KIOSK_USER" ]]; then
  452. KIOSK_USER="${SUDO_USER:-$(logname 2>/dev/null || echo pi)}"
  453. fi
  454. if id "$KIOSK_USER" &>/dev/null; then
  455. usermod -aG "$SPOOLBUDDY_SERVICE_USER" "$KIOSK_USER" 2>/dev/null || true
  456. fi
  457. chgrp "$SPOOLBUDDY_SERVICE_USER" "$env_file"
  458. chmod 640 "$env_file"
  459. if ! su -s /bin/sh -c "test -r '$env_file'" "$KIOSK_USER"; then
  460. error "Kiosk user '$KIOSK_USER' cannot read $env_file (required for dynamic kiosk URL). Check groups/permissions."
  461. fi
  462. success "Verified kiosk user '$KIOSK_USER' can read SpoolBuddy env"
  463. }
  464. setup_ssh_key() {
  465. info "Setting up SSH access for Bambuddy remote updates..."
  466. local ssh_dir="$INSTALL_PATH/.ssh"
  467. local auth_keys="$ssh_dir/authorized_keys"
  468. mkdir -p "$ssh_dir"
  469. chmod 700 "$ssh_dir"
  470. if [[ -n "$SSH_PUBKEY" ]]; then
  471. # Manual key provided via --ssh-pubkey flag
  472. if [[ -f "$auth_keys" ]] && grep -qF "$SSH_PUBKEY" "$auth_keys" 2>/dev/null; then
  473. info "SSH key already present in authorized_keys"
  474. else
  475. echo "$SSH_PUBKEY" >> "$auth_keys"
  476. success "SSH public key added"
  477. fi
  478. else
  479. # No manual key — the daemon will auto-deploy it on first registration
  480. info "SSH key will be deployed automatically when the daemon connects to Bambuddy"
  481. touch "$auth_keys"
  482. fi
  483. chmod 600 "$auth_keys"
  484. chown -R "$SPOOLBUDDY_SERVICE_USER:$SPOOLBUDDY_SERVICE_USER" "$ssh_dir"
  485. }
  486. create_spoolbuddy_service() {
  487. info "Creating SpoolBuddy systemd service..."
  488. local after_line="After=network-online.target"
  489. if [[ "$INSTALL_MODE" == "full" ]]; then
  490. after_line="After=network-online.target bambuddy.service"
  491. fi
  492. cat > /etc/systemd/system/spoolbuddy.service << EOF
  493. [Unit]
  494. Description=SpoolBuddy - NFC Spool Management Daemon
  495. Documentation=https://github.com/maziggy/bambuddy
  496. $after_line
  497. Wants=network-online.target
  498. [Service]
  499. Type=simple
  500. User=$SPOOLBUDDY_SERVICE_USER
  501. WorkingDirectory=$INSTALL_PATH/spoolbuddy
  502. EnvironmentFile=$INSTALL_PATH/spoolbuddy/.env
  503. ExecStart=$INSTALL_PATH/spoolbuddy/venv/bin/python -m daemon.main
  504. Restart=always
  505. RestartSec=5
  506. StandardOutput=journal
  507. StandardError=journal
  508. [Install]
  509. WantedBy=multi-user.target
  510. EOF
  511. systemctl daemon-reload
  512. systemctl enable spoolbuddy.service
  513. success "SpoolBuddy service created and enabled"
  514. }
  515. # ─────────────────────────────────────────────────────────────────────────────
  516. # Bambuddy Installation (full mode only)
  517. # ─────────────────────────────────────────────────────────────────────────────
  518. create_bambuddy_user() {
  519. if id "$BAMBUDDY_SERVICE_USER" &>/dev/null; then
  520. info "User '$BAMBUDDY_SERVICE_USER' already exists"
  521. return
  522. fi
  523. info "Creating service user '$BAMBUDDY_SERVICE_USER'..."
  524. useradd --system --shell /usr/sbin/nologin --home-dir "$INSTALL_PATH" "$BAMBUDDY_SERVICE_USER"
  525. success "Service user created"
  526. }
  527. setup_bambuddy_venv() {
  528. cd "$INSTALL_PATH"
  529. run_with_progress "Creating Bambuddy venv" $PYTHON_CMD -m venv venv
  530. run_with_progress "Upgrading pip" "$INSTALL_PATH/venv/bin/pip" install --upgrade pip
  531. run_with_progress "Installing Bambuddy dependencies" "$INSTALL_PATH/venv/bin/pip" install -r requirements.txt
  532. chown -R "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$INSTALL_PATH/venv"
  533. }
  534. install_nodejs() {
  535. if command -v node &>/dev/null; then
  536. local version
  537. version=$(node --version 2>/dev/null | sed 's/^v//')
  538. local major
  539. major=$(echo "$version" | cut -d'.' -f1)
  540. if [[ "$major" -ge 20 ]]; then
  541. success "Found Node.js v$version"
  542. return
  543. fi
  544. fi
  545. apt-get remove -y nodejs npm > /dev/null 2>&1 || true
  546. run_with_progress "Setting up Node.js repository" bash -c "curl -fsSL https://deb.nodesource.com/setup_22.x | bash -"
  547. run_with_progress "Installing Node.js" apt-get install -y nodejs
  548. hash -r 2>/dev/null || true
  549. success "Node.js installed: $(node --version)"
  550. }
  551. build_frontend() {
  552. cd "$INSTALL_PATH/frontend"
  553. run_with_progress "Installing frontend dependencies" npm ci
  554. run_with_progress "Building frontend" npm run build
  555. }
  556. create_bambuddy_env() {
  557. info "Creating Bambuddy configuration..."
  558. local env_file="$INSTALL_PATH/.env"
  559. cat > "$env_file" << EOF
  560. # Bambuddy Configuration
  561. # Generated by install.sh on $(date)
  562. DEBUG=false
  563. LOG_LEVEL=INFO
  564. LOG_TO_FILE=true
  565. EOF
  566. chown "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$env_file"
  567. chmod 600 "$env_file"
  568. success "Configuration saved to $env_file"
  569. }
  570. create_bambuddy_directories() {
  571. mkdir -p "$INSTALL_PATH/data" "$INSTALL_PATH/logs"
  572. chown -R "$BAMBUDDY_SERVICE_USER:$BAMBUDDY_SERVICE_USER" "$INSTALL_PATH/data" "$INSTALL_PATH/logs"
  573. success "Data directories created"
  574. }
  575. create_bambuddy_service() {
  576. info "Creating Bambuddy systemd service..."
  577. cat > /etc/systemd/system/bambuddy.service << EOF
  578. [Unit]
  579. Description=Bambuddy - Bambu Lab Print Management
  580. Documentation=https://github.com/maziggy/bambuddy
  581. After=network.target
  582. [Service]
  583. Type=simple
  584. User=$BAMBUDDY_SERVICE_USER
  585. Group=$BAMBUDDY_SERVICE_USER
  586. WorkingDirectory=$INSTALL_PATH
  587. EnvironmentFile=$INSTALL_PATH/.env
  588. Environment="DATA_DIR=$INSTALL_PATH/data"
  589. Environment="LOG_DIR=$INSTALL_PATH/logs"
  590. ExecStart=$INSTALL_PATH/venv/bin/uvicorn backend.app.main:app --host 0.0.0.0 --port $BAMBUDDY_PORT
  591. Restart=on-failure
  592. RestartSec=5
  593. StandardOutput=journal
  594. StandardError=journal
  595. NoNewPrivileges=true
  596. PrivateTmp=true
  597. ProtectSystem=strict
  598. ProtectHome=true
  599. ReadWritePaths=$INSTALL_PATH/data $INSTALL_PATH/logs $INSTALL_PATH
  600. [Install]
  601. WantedBy=multi-user.target
  602. EOF
  603. systemctl daemon-reload
  604. systemctl enable bambuddy.service
  605. success "Bambuddy service created and enabled"
  606. }
  607. # ─────────────────────────────────────────────────────────────────────────────
  608. # System Strip-Down (dedicated appliance — remove unnecessary services/packages)
  609. # ─────────────────────────────────────────────────────────────────────────────
  610. strip_services() {
  611. info "Disabling unnecessary services..."
  612. local services=(
  613. bluetooth.service
  614. lightdm.service
  615. cloud-init-local.service
  616. cloud-init.service
  617. cloud-init-network.service
  618. cloud-config.service
  619. cloud-final.service
  620. cloud-init-hotplugd.socket
  621. avahi-daemon.service
  622. avahi-daemon.socket
  623. ModemManager.service
  624. udisks2.service
  625. apparmor.service
  626. man-db.timer
  627. e2scrub_all.timer
  628. e2scrub_reap.service
  629. )
  630. local disabled=0
  631. for svc in "${services[@]}"; do
  632. if systemctl is-enabled "$svc" &>/dev/null; then
  633. systemctl disable "$svc" 2>/dev/null || true
  634. (( ++disabled ))
  635. fi
  636. done
  637. if (( disabled > 0 )); then
  638. success "Disabled $disabled unnecessary services"
  639. else
  640. success "No unnecessary services to disable"
  641. fi
  642. }
  643. strip_packages() {
  644. info "Removing unnecessary packages..."
  645. local packages=(
  646. mkvtoolnix
  647. firmware-atheros
  648. firmware-mediatek
  649. cloud-init
  650. rpi-cloud-init-mods
  651. rpi-connect-lite
  652. avahi-daemon
  653. modemmanager
  654. udisks2
  655. )
  656. local to_remove=()
  657. for pkg in "${packages[@]}"; do
  658. if dpkg -l "$pkg" &>/dev/null 2>&1; then
  659. to_remove+=("$pkg")
  660. fi
  661. done
  662. if (( ${#to_remove[@]} > 0 )); then
  663. run_with_progress "Removing ${#to_remove[@]} packages" apt-get remove --purge -y "${to_remove[@]}"
  664. run_with_progress "Cleaning up dependencies" apt-get autoremove --purge -y
  665. else
  666. success "No unnecessary packages to remove"
  667. fi
  668. }
  669. # ─────────────────────────────────────────────────────────────────────────────
  670. # Kiosk Setup (labwc + Chromium + Plymouth splash)
  671. # ─────────────────────────────────────────────────────────────────────────────
  672. setup_kiosk() {
  673. info "Setting up touchscreen kiosk..."
  674. # Detect kiosk user (the human user who ran sudo)
  675. KIOSK_USER="${SUDO_USER:-$(logname 2>/dev/null || echo pi)}"
  676. KIOSK_URL="${BAMBUDDY_URL}/spoolbuddy?token=${API_KEY}"
  677. local KIOSK_HOME
  678. KIOSK_HOME=$(eval echo "~$KIOSK_USER")
  679. info "Kiosk user: $KIOSK_USER (home: $KIOSK_HOME)"
  680. info "Kiosk URL: $KIOSK_URL"
  681. # Allow kiosk user to read SpoolBuddy env so launcher can resolve backend URL
  682. # and API key dynamically instead of using stale install-time fallback values.
  683. local spoolbuddy_env="$INSTALL_PATH/spoolbuddy/.env"
  684. if [[ -f "$spoolbuddy_env" ]]; then
  685. usermod -aG "$SPOOLBUDDY_SERVICE_USER" "$KIOSK_USER" 2>/dev/null || true
  686. chgrp "$SPOOLBUDDY_SERVICE_USER" "$spoolbuddy_env" 2>/dev/null || true
  687. chmod 640 "$spoolbuddy_env" 2>/dev/null || true
  688. fi
  689. # ── Install kiosk packages ────────────────────────────────────────────
  690. run_with_progress "Installing kiosk packages" apt-get install -y labwc chromium plymouth wlr-randr
  691. # ── config.txt tweaks ─────────────────────────────────────────────────
  692. local boot_config="/boot/firmware/config.txt"
  693. if [[ ! -f "$boot_config" ]]; then
  694. boot_config="/boot/config.txt"
  695. fi
  696. if [[ -f "$boot_config" ]]; then
  697. info "Configuring $boot_config for kiosk..."
  698. # Disable audio (change existing on→off)
  699. sed -i 's/^dtparam=audio=on/dtparam=audio=off/' "$boot_config"
  700. # Disable camera auto-detect (change existing 1→0)
  701. sed -i 's/^camera_auto_detect=1/camera_auto_detect=0/' "$boot_config"
  702. # Append if missing: gpu_mem=32
  703. if ! grep -q "^gpu_mem=" "$boot_config"; then
  704. echo "" >> "$boot_config"
  705. echo "# Kiosk: Minimal GPU firmware memory (KMS uses CMA from system RAM)" >> "$boot_config"
  706. echo "gpu_mem=32" >> "$boot_config"
  707. fi
  708. # Append if missing: dtoverlay=disable-bt
  709. if ! grep -q "^dtoverlay=disable-bt" "$boot_config"; then
  710. echo "" >> "$boot_config"
  711. echo "# Kiosk: Disable Bluetooth hardware" >> "$boot_config"
  712. echo "dtoverlay=disable-bt" >> "$boot_config"
  713. fi
  714. # Append if missing: disable_splash=1
  715. if ! grep -q "^disable_splash=" "$boot_config"; then
  716. echo "" >> "$boot_config"
  717. echo "# Kiosk: Disable Raspberry Pi firmware splash, use custom splash.png" >> "$boot_config"
  718. echo "disable_splash=1" >> "$boot_config"
  719. fi
  720. success "Boot config updated"
  721. fi
  722. # ── cmdline.txt tweaks ────────────────────────────────────────────────
  723. local cmdline="/boot/firmware/cmdline.txt"
  724. if [[ ! -f "$cmdline" ]]; then
  725. cmdline="/boot/cmdline.txt"
  726. fi
  727. if [[ -f "$cmdline" ]]; then
  728. info "Configuring $cmdline for kiosk..."
  729. # Remove serial console (Plymouth needs tty-only console)
  730. sed -i 's/console=serial0,[0-9]* //' "$cmdline"
  731. # Add splash quiet loglevel=3 logo.nologo if missing
  732. grep -q "splash" "$cmdline" || sed -i 's/$/ splash quiet loglevel=3 logo.nologo/' "$cmdline"
  733. # Add video mode if missing
  734. grep -q "video=HDMI-A-1" "$cmdline" || sed -i 's/$/ video=HDMI-A-1:1024x600@60/' "$cmdline"
  735. success "Kernel cmdline updated"
  736. fi
  737. # ── Plymouth splash theme ─────────────────────────────────────────────
  738. info "Installing Plymouth boot splash..."
  739. local theme_dir="/usr/share/plymouth/themes/spoolbuddy"
  740. mkdir -p "$theme_dir"
  741. # Copy bundled splash image from the install directory
  742. local script_dir
  743. script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  744. if [[ -f "$script_dir/splash.png" ]]; then
  745. cp "$script_dir/splash.png" "$theme_dir/splash.png"
  746. elif [[ -f "$INSTALL_PATH/spoolbuddy/install/splash.png" ]]; then
  747. cp "$INSTALL_PATH/spoolbuddy/install/splash.png" "$theme_dir/splash.png"
  748. else
  749. warn "splash.png not found — Plymouth splash will not display an image"
  750. fi
  751. # Write .plymouth theme file
  752. cat > "$theme_dir/spoolbuddy.plymouth" << 'EOF'
  753. [Plymouth Theme]
  754. Name=SpoolBuddy
  755. Description=SpoolBuddy boot splash
  756. ModuleName=script
  757. [script]
  758. ImageDir=/usr/share/plymouth/themes/spoolbuddy
  759. ScriptFile=/usr/share/plymouth/themes/spoolbuddy/spoolbuddy.script
  760. EOF
  761. # Write .script theme file
  762. cat > "$theme_dir/spoolbuddy.script" << 'EOF'
  763. wallpaper_image = Image("splash.png");
  764. screen_width = Window.GetWidth();
  765. screen_height = Window.GetHeight();
  766. resized_wallpaper_image = wallpaper_image.Scale(screen_width, screen_height);
  767. wallpaper_sprite = Sprite(resized_wallpaper_image);
  768. wallpaper_sprite.SetZ(-100);
  769. EOF
  770. plymouth-set-default-theme spoolbuddy
  771. run_with_progress "Updating initramfs" update-initramfs -u
  772. success "Plymouth splash installed"
  773. # ── Auto-login on tty1 ────────────────────────────────────────────────
  774. info "Configuring auto-login for $KIOSK_USER..."
  775. mkdir -p /etc/systemd/system/getty@tty1.service.d
  776. cat > /etc/systemd/system/getty@tty1.service.d/autologin.conf << EOF
  777. [Unit]
  778. After=network-online.target
  779. Wants=network-online.target
  780. [Service]
  781. ExecStart=
  782. ExecStart=-/sbin/agetty --autologin $KIOSK_USER --noclear %I \$TERM
  783. EOF
  784. success "Auto-login configured"
  785. # ── labwc rc.xml (no decorations, no keybinds) ────────────────────────
  786. info "Configuring labwc window manager..."
  787. local labwc_dir="$KIOSK_HOME/.config/labwc"
  788. mkdir -p "$labwc_dir"
  789. cat > "$labwc_dir/rc.xml" << 'EOF'
  790. <?xml version="1.0"?>
  791. <labwc_config>
  792. <theme>
  793. <name></name>
  794. <cornerRadius>0</cornerRadius>
  795. </theme>
  796. <!-- Disable all keybindings - kiosk lockdown -->
  797. <keyboard>
  798. </keyboard>
  799. <!-- Disable right-click menu -->
  800. <mouse>
  801. <default />
  802. </mouse>
  803. <!-- Remove window decorations, maximize Chromium, prevent unfullscreen -->
  804. <windowRules>
  805. <windowRule identifier="*">
  806. <serverDecoration>no</serverDecoration>
  807. </windowRule>
  808. <windowRule identifier="chromium">
  809. <skipTaskbar>yes</skipTaskbar>
  810. <fixedPosition>yes</fixedPosition>
  811. </windowRule>
  812. </windowRules>
  813. </labwc_config>
  814. EOF
  815. # ── kiosk launcher (dynamic URL from spoolbuddy/.env) ─────────────────
  816. local kiosk_launcher="/usr/local/bin/spoolbuddy-kiosk-launch"
  817. cat > "$kiosk_launcher" << EOF
  818. #!/usr/bin/env bash
  819. set -euo pipefail
  820. ENV_FILE="$INSTALL_PATH/spoolbuddy/.env"
  821. FALLBACK_URL="$KIOSK_URL"
  822. backend_url=""
  823. api_key=""
  824. if [[ -r "\$ENV_FILE" ]]; then
  825. backend_url="\$(sed -n 's/^SPOOLBUDDY_BACKEND_URL=//p' "\$ENV_FILE" | tail -n1 | tr -d '\r')"
  826. api_key="\$(sed -n 's/^SPOOLBUDDY_API_KEY=//p' "\$ENV_FILE" | tail -n1 | tr -d '\r')"
  827. backend_url="\${backend_url%\"}"
  828. backend_url="\${backend_url#\"}"
  829. api_key="\${api_key%\"}"
  830. api_key="\${api_key#\"}"
  831. elif [[ -f "\$ENV_FILE" ]]; then
  832. echo "spoolbuddy-kiosk-launch: ERROR: \$ENV_FILE exists but is not readable" >&2
  833. echo "spoolbuddy-kiosk-launch: Fix permissions (group-readable by kiosk user) and restart kiosk" >&2
  834. exit 1
  835. fi
  836. if [[ -n "\$backend_url" && -n "\$api_key" ]]; then
  837. backend_url="\${backend_url%/}"
  838. kiosk_url="\${backend_url}/spoolbuddy?token=\${api_key}"
  839. else
  840. kiosk_url="\$FALLBACK_URL"
  841. fi
  842. exec chromium --kiosk --no-first-run --disable-infobars \
  843. --disable-session-crashed-bubble --disable-features=TranslateUI \
  844. --noerrdialogs --disable-component-update \
  845. --overscroll-history-navigation=0 \
  846. --ozone-platform=wayland \
  847. "\$kiosk_url"
  848. EOF
  849. chmod 755 "$kiosk_launcher"
  850. # Tiny self-check: ensure sed command substitutions were not expanded
  851. # while generating the launcher script.
  852. if ! grep -Fq 'backend_url="$(sed -n' "$kiosk_launcher" || ! grep -Fq 'api_key="$(sed -n' "$kiosk_launcher"; then
  853. error "Kiosk launcher generation failed: dynamic env parsing commands were expanded unexpectedly"
  854. fi
  855. # ── labwc autostart ───────────────────────────────────────────────────
  856. cat > "$labwc_dir/autostart" << EOF
  857. # Force 1024x600 (panel doesn't advertise this natively)
  858. wlr-randr --output HDMI-A-1 --custom-mode 1024x600@60 &
  859. # Launch Chromium via helper that resolves URL from spoolbuddy/.env
  860. $kiosk_launcher &
  861. EOF
  862. chown -R "$KIOSK_USER:$KIOSK_USER" "$labwc_dir"
  863. # ── .bash_profile (source .bashrc, exec labwc on tty1) ────────────────
  864. cat > "$KIOSK_HOME/.bash_profile" << 'EOF'
  865. # Source .bashrc if it exists
  866. if [ -f ~/.bashrc ]; then
  867. . ~/.bashrc
  868. fi
  869. # Auto-start kiosk on tty1
  870. if [ "$(tty)" = "/dev/tty1" ]; then
  871. exec labwc
  872. fi
  873. EOF
  874. chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.bash_profile"
  875. REBOOT_NEEDED="true"
  876. success "Kiosk setup complete"
  877. }
  878. # ─────────────────────────────────────────────────────────────────────────────
  879. # User Prompts
  880. # ─────────────────────────────────────────────────────────────────────────────
  881. parse_args() {
  882. while [[ $# -gt 0 ]]; do
  883. case "$1" in
  884. --mode)
  885. INSTALL_MODE="$2"
  886. shift 2
  887. ;;
  888. --repo)
  889. INSTALL_REPO="$(normalize_github_repo_url "$2")"
  890. shift 2
  891. ;;
  892. --ref)
  893. INSTALL_REF="$2"
  894. shift 2
  895. ;;
  896. --bambuddy-url)
  897. BAMBUDDY_URL="$2"
  898. shift 2
  899. ;;
  900. --api-key)
  901. API_KEY="$2"
  902. shift 2
  903. ;;
  904. --path)
  905. INSTALL_PATH="$2"
  906. shift 2
  907. ;;
  908. --port)
  909. BAMBUDDY_PORT="$2"
  910. shift 2
  911. ;;
  912. --ssh-pubkey)
  913. SSH_PUBKEY="$2"
  914. shift 2
  915. ;;
  916. --yes|-y)
  917. NON_INTERACTIVE="true"
  918. shift
  919. ;;
  920. --help|-h)
  921. show_help
  922. ;;
  923. *)
  924. error "Unknown option: $1 (use --help for usage)"
  925. ;;
  926. esac
  927. done
  928. }
  929. ask_install_mode() {
  930. if [[ -n "$INSTALL_MODE" ]]; then
  931. return
  932. fi
  933. echo ""
  934. echo -e "${BOLD}How would you like to set up SpoolBuddy?${NC}"
  935. echo ""
  936. echo -e " ${CYAN}1)${NC} SpoolBuddy only"
  937. echo " NFC reader + scale on this RPi, Bambuddy runs on another device"
  938. echo ""
  939. echo -e " ${CYAN}2)${NC} SpoolBuddy + Bambuddy"
  940. echo " Both running natively on this Raspberry Pi"
  941. echo ""
  942. while true; do
  943. echo -en "${BOLD}Choose${NC} [${CYAN}1${NC}/${CYAN}2${NC}]: "
  944. read -r choice
  945. case "$choice" in
  946. 1) INSTALL_MODE="spoolbuddy"; return;;
  947. 2) INSTALL_MODE="full"; return;;
  948. *) echo "Please enter 1 or 2.";;
  949. esac
  950. done
  951. }
  952. gather_config() {
  953. echo ""
  954. echo -e "${BOLD}Configuration${NC}"
  955. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  956. echo ""
  957. # Set default install path based on mode
  958. if [[ -z "$INSTALL_PATH" ]]; then
  959. if [[ "$INSTALL_MODE" == "full" ]]; then
  960. INSTALL_PATH="/opt/bambuddy"
  961. else
  962. INSTALL_PATH="/opt/bambuddy"
  963. fi
  964. fi
  965. prompt "Installation directory" "$INSTALL_PATH" INSTALL_PATH
  966. if [[ -z "$INSTALL_REPO" ]]; then
  967. INSTALL_REPO="$GITHUB_REPO"
  968. fi
  969. prompt "Git repository URL" "$INSTALL_REPO" INSTALL_REPO
  970. INSTALL_REPO="$(normalize_github_repo_url "$INSTALL_REPO")"
  971. if [[ -z "$INSTALL_REF" ]]; then
  972. INSTALL_REF="main"
  973. fi
  974. if [[ "$NON_INTERACTIVE" != "true" && -n "$DETECTED_INSTALLER_REF" && "$DETECTED_INSTALLER_REF" != "HEAD" ]]; then
  975. echo ""
  976. echo -e "${BOLD}Install Source Ref${NC}"
  977. echo "1) main"
  978. echo "2) $DETECTED_INSTALLER_REF (detected from installer context)"
  979. echo "3) custom"
  980. while true; do
  981. echo -en "${BOLD}Choose${NC} [1/2/3]: "
  982. read -r ref_choice
  983. case "$ref_choice" in
  984. ""|1)
  985. INSTALL_REF="main"
  986. break
  987. ;;
  988. 2)
  989. INSTALL_REF="$DETECTED_INSTALLER_REF"
  990. break
  991. ;;
  992. 3)
  993. prompt "Git ref (branch/tag/commit)" "$INSTALL_REF" INSTALL_REF
  994. break
  995. ;;
  996. *)
  997. echo "Please enter 1, 2, or 3."
  998. ;;
  999. esac
  1000. done
  1001. else
  1002. prompt "Git ref (branch/tag/commit)" "$INSTALL_REF" INSTALL_REF
  1003. fi
  1004. if [[ "$INSTALL_MODE" == "spoolbuddy" ]]; then
  1005. # Need remote Bambuddy URL and API key
  1006. echo ""
  1007. info "SpoolBuddy needs to connect to your Bambuddy server."
  1008. info "You can find/create an API key in Bambuddy under Settings -> API Keys."
  1009. echo ""
  1010. while [[ -z "$BAMBUDDY_URL" ]]; do
  1011. prompt "Bambuddy server URL (e.g. http://192.168.1.100:8000)" "" BAMBUDDY_URL
  1012. if [[ -z "$BAMBUDDY_URL" ]]; then
  1013. warn "Bambuddy URL is required"
  1014. fi
  1015. done
  1016. while [[ -z "$API_KEY" ]]; do
  1017. prompt "Bambuddy API key" "" API_KEY
  1018. if [[ -z "$API_KEY" ]]; then
  1019. warn "API key is required"
  1020. fi
  1021. done
  1022. else
  1023. # Full mode — Bambuddy runs locally
  1024. prompt "Bambuddy port" "$BAMBUDDY_PORT" BAMBUDDY_PORT
  1025. BAMBUDDY_URL="http://localhost:$BAMBUDDY_PORT"
  1026. echo ""
  1027. info "After installation, create an API key in Bambuddy (Settings -> API Keys)"
  1028. info "and update it in: $INSTALL_PATH/spoolbuddy/.env"
  1029. API_KEY="CHANGE_ME_AFTER_SETUP"
  1030. fi
  1031. # Summary
  1032. echo ""
  1033. echo -e "${BOLD}Installation Summary${NC}"
  1034. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  1035. echo -e " Mode: ${GREEN}$([ "$INSTALL_MODE" == "full" ] && echo "Bambuddy + SpoolBuddy" || echo "SpoolBuddy only")${NC}"
  1036. echo -e " Install path: ${GREEN}$INSTALL_PATH${NC}"
  1037. echo -e " Git repo: ${GREEN}$INSTALL_REPO${NC}"
  1038. echo -e " Git ref: ${GREEN}$INSTALL_REF${NC}"
  1039. if [[ "$INSTALL_MODE" == "full" ]]; then
  1040. echo -e " Bambuddy port: ${GREEN}$BAMBUDDY_PORT${NC}"
  1041. echo -e " Bambuddy URL: ${GREEN}$BAMBUDDY_URL${NC}"
  1042. else
  1043. echo -e " Bambuddy URL: ${GREEN}$BAMBUDDY_URL${NC}"
  1044. fi
  1045. echo ""
  1046. if ! prompt_yes_no "Proceed with installation?" "y"; then
  1047. echo "Installation cancelled."
  1048. exit 0
  1049. fi
  1050. }
  1051. # ─────────────────────────────────────────────────────────────────────────────
  1052. # Main
  1053. # ─────────────────────────────────────────────────────────────────────────────
  1054. main() {
  1055. parse_args "$@"
  1056. detect_installer_source_context
  1057. echo ""
  1058. echo -e "${CYAN}╔══════════════════════════════════════════════════════════╗${NC}"
  1059. echo -e "${CYAN}║ ║${NC}"
  1060. echo -e "${CYAN}║ ____ _ ____ _ _ ║${NC}"
  1061. echo -e "${CYAN}║ / ___| _ __ ___ ___ | | __ ) _ _ __| | __| |_ _ ║${NC}"
  1062. echo -e "${CYAN}║ \\___ \\| '_ \\ / _ \\ / _ \\| | _ \\| | | |/ _\` |/ _\` | | | |║${NC}"
  1063. echo -e "${CYAN}║ ___) | |_) | (_) | (_) | | |_) | |_| | (_| | (_| | |_| |║${NC}"
  1064. echo -e "${CYAN}║ |____/| .__/ \\___/ \\___/|_|____/ \\__,_|\\__,_|\\__,_|\\__, |║${NC}"
  1065. echo -e "${CYAN}║ |_| |___/ ║${NC}"
  1066. echo -e "${CYAN}║ ║${NC}"
  1067. echo -e "${CYAN}║ NFC Spool Management for Bambuddy ║${NC}"
  1068. echo -e "${CYAN}║ ║${NC}"
  1069. echo -e "${CYAN}╚══════════════════════════════════════════════════════════╝${NC}"
  1070. echo ""
  1071. # Check if running via pipe without -y
  1072. if [[ ! -t 0 ]] && [[ "$NON_INTERACTIVE" != "true" ]]; then
  1073. error "Interactive mode requires a terminal. Use -y for unattended install, or download and run directly."
  1074. fi
  1075. # Pre-flight checks
  1076. check_root
  1077. check_raspberry_pi
  1078. check_raspberry_pi_os
  1079. if ! detect_python; then
  1080. info "Python 3.10+ not found, will install..."
  1081. fi
  1082. # Gather user preferences
  1083. ask_install_mode
  1084. gather_config
  1085. # Validate mode
  1086. if [[ "$INSTALL_MODE" != "spoolbuddy" && "$INSTALL_MODE" != "full" ]]; then
  1087. error "Invalid mode: $INSTALL_MODE (must be 'spoolbuddy' or 'full')"
  1088. fi
  1089. echo ""
  1090. echo -e "${BOLD}Starting Installation${NC}"
  1091. echo -e "${CYAN}─────────────────────────────────────────${NC}"
  1092. echo ""
  1093. # ── Step 1: Raspberry Pi hardware config ──────────────────────────────
  1094. info "Configuring Raspberry Pi hardware..."
  1095. enable_spi
  1096. enable_i2c
  1097. configure_boot_config
  1098. echo ""
  1099. # ── Step 2: System packages ───────────────────────────────────────────
  1100. install_system_packages
  1101. detect_python || error "Failed to install Python 3.10+"
  1102. echo ""
  1103. # ── Step 2b: Strip unnecessary services & packages ────────────────────
  1104. strip_services
  1105. strip_packages
  1106. echo ""
  1107. # ── Step 3: Download source code ──────────────────────────────────────
  1108. create_spoolbuddy_user
  1109. download_spoolbuddy
  1110. echo ""
  1111. # ── Step 3b: Kiosk setup (labwc + Chromium + squeekboard + Plymouth) ──
  1112. setup_kiosk
  1113. echo ""
  1114. # ── Step 4: SpoolBuddy setup ──────────────────────────────────────────
  1115. info "Setting up SpoolBuddy..."
  1116. setup_spoolbuddy_venv
  1117. create_spoolbuddy_env
  1118. # Kiosk env access: only needed if actual kiosk hardware is available
  1119. if [[ -f /boot/firmware/config.txt ]] || [[ -f /boot/config.txt ]]; then
  1120. ensure_kiosk_env_access
  1121. fi
  1122. setup_ssh_key
  1123. create_spoolbuddy_service
  1124. echo ""
  1125. # ── Step 5: Bambuddy setup (full mode only) ───────────────────────────
  1126. if [[ "$INSTALL_MODE" == "full" ]]; then
  1127. info "Setting up Bambuddy..."
  1128. create_bambuddy_user
  1129. setup_bambuddy_venv
  1130. install_nodejs
  1131. build_frontend
  1132. create_bambuddy_directories
  1133. create_bambuddy_env
  1134. create_bambuddy_service
  1135. echo ""
  1136. fi
  1137. # ── Done ──────────────────────────────────────────────────────────────
  1138. echo ""
  1139. echo -e "${GREEN}╔══════════════════════════════════════════════════════════╗${NC}"
  1140. echo -e "${GREEN}║ ║${NC}"
  1141. echo -e "${GREEN}║ Installation Complete! ║${NC}"
  1142. echo -e "${GREEN}║ ║${NC}"
  1143. echo -e "${GREEN}╚══════════════════════════════════════════════════════════╝${NC}"
  1144. echo ""
  1145. local ip_addr
  1146. ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}') || ip_addr="<your-ip>"
  1147. if [[ "$INSTALL_MODE" == "full" ]]; then
  1148. echo -e " ${BOLD}Bambuddy:${NC} ${CYAN}http://$ip_addr:$BAMBUDDY_PORT${NC}"
  1149. else
  1150. echo -e " ${BOLD}SpoolBuddy:${NC} Connecting to ${CYAN}$BAMBUDDY_URL${NC}"
  1151. fi
  1152. echo -e " ${BOLD}Kiosk URL:${NC} ${CYAN}$KIOSK_URL${NC}"
  1153. echo -e " ${BOLD}Kiosk user:${NC} ${CYAN}$KIOSK_USER${NC}"
  1154. echo ""
  1155. if [[ "$INSTALL_MODE" == "full" ]]; then
  1156. echo -e " ${BOLD}Next steps:${NC}"
  1157. echo -e " 1. Reboot (required for kiosk, Plymouth splash, and hardware changes)"
  1158. echo -e " 2. The touchscreen kiosk will start automatically after reboot"
  1159. echo -e " 3. On another device, open ${CYAN}http://$ip_addr:$BAMBUDDY_PORT${NC}"
  1160. echo -e " 4. Go to Settings -> API Keys and create an API key"
  1161. echo -e " 5. Update the API key in: ${CYAN}$INSTALL_PATH/spoolbuddy/.env${NC}"
  1162. echo -e " 6. Restart SpoolBuddy: ${CYAN}sudo systemctl restart spoolbuddy${NC}"
  1163. fi
  1164. echo ""
  1165. echo -e " ${BOLD}Manage services:${NC}"
  1166. echo -e " SpoolBuddy status: ${CYAN}sudo systemctl status spoolbuddy${NC}"
  1167. echo -e " SpoolBuddy logs: ${CYAN}sudo journalctl -u spoolbuddy -f${NC}"
  1168. if [[ "$INSTALL_MODE" == "full" ]]; then
  1169. echo -e " Bambuddy status: ${CYAN}sudo systemctl status bambuddy${NC}"
  1170. echo -e " Bambuddy logs: ${CYAN}sudo journalctl -u bambuddy -f${NC}"
  1171. fi
  1172. echo ""
  1173. echo -e " ${BOLD}Configuration:${NC} ${CYAN}$INSTALL_PATH/spoolbuddy/.env${NC}"
  1174. echo -e " ${BOLD}Hardware wiring:${NC} ${CYAN}$INSTALL_PATH/spoolbuddy/README.md${NC}"
  1175. echo -e " ${BOLD}Diagnostics:${NC} ${CYAN}sudo $INSTALL_PATH/spoolbuddy/venv/bin/python $INSTALL_PATH/spoolbuddy/pn5180_diag.py${NC}"
  1176. echo ""
  1177. echo -e " ${YELLOW}A reboot is required to apply all changes (kiosk, Plymouth splash, hardware).${NC}"
  1178. echo ""
  1179. if prompt_yes_no "Reboot now?" "y"; then
  1180. reboot
  1181. else
  1182. echo -e " Run ${CYAN}sudo reboot${NC} when ready."
  1183. fi
  1184. echo ""
  1185. }
  1186. main "$@"