beepzone-helper.sh 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Simple TUI-based BeepZone setup helper using `dialog`.
  4. # Targets macOS + Debian-ish Linux (podman, rustup mysql-client already installed).
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. WORK_DIR="${SCRIPT_DIR}"
  7. : "${DIALOG:=dialog}"
  8. if ! command -v "$DIALOG" >/dev/null 2>&1; then
  9. echo "\n[ERROR] 'dialog' is not installed or not in PATH." >&2
  10. echo "On macOS: brew install dialog" >&2
  11. echo "On Debian: sudo apt install dialog" >&2
  12. exit 1
  13. fi
  14. # Determine whether the dialog implementation supports custom OK label
  15. # 'dialog' uses --ok-label, 'whiptail' uses --ok-button
  16. declare -a DIALOG_OK_FLAG=()
  17. if "$DIALOG" --help 2>&1 | grep -q -- "--ok-label"; then
  18. DIALOG_OK_FLAG=(--ok-label "Continue")
  19. elif "$DIALOG" --help 2>&1 | grep -q -- "--ok-button"; then
  20. DIALOG_OK_FLAG=(--ok-button "Continue")
  21. fi
  22. HAS_PODMAN=true
  23. if ! command -v podman >/dev/null 2>&1; then
  24. HAS_PODMAN=false
  25. fi
  26. IS_DEBIAN_NATIVE=false
  27. if [[ -f /etc/debian_version ]]; then
  28. if grep -qE "^(12|13)" /etc/debian_version; then
  29. IS_DEBIAN_NATIVE=true
  30. fi
  31. fi
  32. # Check for MariaDB/MySQL client - try common brew locations on macOS
  33. MYSQL_CLIENT=""
  34. if command -v mariadb >/dev/null 2>&1; then
  35. MYSQL_CLIENT="mariadb"
  36. elif command -v mysql >/dev/null 2>&1; then
  37. MYSQL_CLIENT="mysql"
  38. elif [[ -x /opt/homebrew/opt/mysql-client/bin/mysql ]]; then
  39. MYSQL_CLIENT="/opt/homebrew/opt/mysql-client/bin/mysql"
  40. export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
  41. elif [[ -x /opt/homebrew/opt/mariadb/bin/mariadb ]]; then
  42. MYSQL_CLIENT="/opt/homebrew/opt/mariadb/bin/mariadb"
  43. export PATH="/opt/homebrew/opt/mariadb/bin:$PATH"
  44. elif [[ -x /opt/homebrew/bin/mariadb ]]; then
  45. MYSQL_CLIENT="/opt/homebrew/bin/mariadb"
  46. export PATH="/opt/homebrew/bin:$PATH"
  47. elif [[ -x /opt/homebrew/bin/mysql ]]; then
  48. MYSQL_CLIENT="/opt/homebrew/bin/mysql"
  49. export PATH="/opt/homebrew/bin:$PATH"
  50. elif [[ -x /usr/local/opt/mysql-client/bin/mysql ]]; then
  51. MYSQL_CLIENT="/usr/local/opt/mysql-client/bin/mysql"
  52. export PATH="/usr/local/opt/mysql-client/bin:$PATH"
  53. elif [[ -x /usr/local/bin/mariadb ]]; then
  54. MYSQL_CLIENT="/usr/local/bin/mariadb"
  55. export PATH="/usr/local/bin:$PATH"
  56. elif [[ -x /usr/local/bin/mysql ]]; then
  57. MYSQL_CLIENT="/usr/local/bin/mysql"
  58. export PATH="/usr/local/bin:$PATH"
  59. fi
  60. # Cargo / Git detection (hide menu options if missing)
  61. HAS_CARGO=false
  62. if command -v cargo >/dev/null 2>&1; then
  63. HAS_CARGO=true
  64. fi
  65. HAS_GIT=false
  66. if command -v git >/dev/null 2>&1; then
  67. HAS_GIT=true
  68. fi
  69. TMP_FILE="$(mktemp)"
  70. CHOICE_FILE="$(mktemp)"
  71. trap 'rm -f "$TMP_FILE" "$CHOICE_FILE"' EXIT
  72. LOG_FILE="/tmp/beepzone-helper.log"
  73. > "$LOG_FILE"
  74. ENV_FILE="$SCRIPT_DIR/.env"
  75. # Load existing settings from .env if present
  76. if [[ -f "$ENV_FILE" ]]; then
  77. source "$ENV_FILE"
  78. fi
  79. # Set defaults if not loaded from .env
  80. : "${BEEPZONE_DB_CONTAINER_NAME:=beepzone-mariadb}"
  81. : "${BEEPZONE_DB_IMAGE:=mariadb:12}"
  82. : "${DB_HOST:=127.0.0.1}"
  83. : "${DB_PORT:=3306}"
  84. : "${DB_NAME:=beepzone}"
  85. : "${DB_USER:=beepzone}"
  86. : "${DB_PASS:=beepzone}"
  87. : "${DB_ROOT_PASSWORD:=root}"
  88. : "${SECKELAPI_REPO:=https://git.teleco.ch/crt/seckelapi.git}"
  89. : "${CLIENT_REPO:=https://git.teleco.ch/crt/beepzone-client-egui-emo.git}"
  90. : "${DEPLOYMENT_TYPE:=clean}"
  91. save_env() {
  92. cat > "$ENV_FILE" << EOF
  93. # BeepZone Setup Configuration
  94. # Mostly autogenerated by beepzone-helper
  95. BEEPZONE_DB_CONTAINER_NAME="$BEEPZONE_DB_CONTAINER_NAME"
  96. BEEPZONE_DB_IMAGE="$BEEPZONE_DB_IMAGE"
  97. DB_HOST="$DB_HOST"
  98. DB_PORT="$DB_PORT"
  99. DB_NAME="$DB_NAME"
  100. DB_USER="$DB_USER"
  101. DB_PASS="$DB_PASS"
  102. DB_ROOT_PASSWORD="$DB_ROOT_PASSWORD"
  103. SECKELAPI_REPO="$SECKELAPI_REPO"
  104. CLIENT_REPO="$CLIENT_REPO"
  105. DEPLOYMENT_TYPE="$DEPLOYMENT_TYPE"
  106. EOF
  107. }
  108. show_capabilities_screen() {
  109. local cf
  110. cf="$(mktemp)"
  111. {
  112. echo "Dependency Overview"
  113. echo ""
  114. echo "What you can do :"
  115. if $HAS_PODMAN; then
  116. echo " [X] Configure and run MariaDB using podman"
  117. else
  118. echo " [ ] Configure and run MariaDB using podman - requires podman in PATH"
  119. fi
  120. if $IS_DEBIAN_NATIVE; then
  121. echo " [X] Configure and run MariaDB natively on your system"
  122. else
  123. echo " [ ] Configure and run MariaDB natively on your system - requires Debian 12/13"
  124. fi
  125. if [[ -n "$MYSQL_CLIENT" ]]; then
  126. echo " [X] Import DB schema or seeded demo DB"
  127. echo " [X] Manage users and roles"
  128. else
  129. echo " [ ] Import DB schema or seeded demo DB - requires MariaDB/MySQL client"
  130. echo " [ ] Manage users and roles - requires MariaDB/MySQL client"
  131. fi
  132. if $HAS_PODMAN; then
  133. echo " [X] Configure and setup SeckelAPI using podman"
  134. else
  135. echo " [ ] Configure and setup SeckelAPI using podman - requires podman in PATH"
  136. fi
  137. if $IS_DEBIAN_NATIVE && $HAS_CARGO; then
  138. echo " [X] Setup SeckelAPI natively on your system"
  139. else
  140. echo " [ ] Setup SeckelAPI natively on your system - requires Debian 12/13 and cargo"
  141. fi
  142. if $HAS_CARGO; then
  143. echo " [X] Build desktop client"
  144. else
  145. echo " [ ] Build desktop client - requires cargo (Rust toolchain)"
  146. fi
  147. if $HAS_GIT; then
  148. echo " [X] Update from git masters"
  149. else
  150. echo " [ ] Update from git masters - requires git"
  151. fi
  152. echo " [X] Clean sources"
  153. echo " [X] Quit"
  154. echo "Options that depend on missing tools are hidden in the main menu."
  155. } > "$cf"
  156. "$DIALOG" --title "BeepZone Helper" "${DIALOG_OK_FLAG[@]}" --textbox "$cf" 22 78 || true
  157. rm -f "$cf"
  158. }
  159. ask_main_menu() {
  160. while true; do
  161. local options=()
  162. if $HAS_PODMAN; then
  163. options+=(1 "Configure and run MariaDB (podman)")
  164. fi
  165. if $IS_DEBIAN_NATIVE; then
  166. options+=(9 "Configure and run MariaDB (native Debian)")
  167. if $HAS_CARGO; then
  168. options+=(10 "Setup SeckelAPI (Native Debian)")
  169. fi
  170. fi
  171. if [[ -n "$MYSQL_CLIENT" ]]; then
  172. options+=(
  173. 2 "Import DB schema and data"
  174. 3 "Manage users and roles"
  175. )
  176. fi
  177. if $HAS_PODMAN; then
  178. options+=(4 "Configure and setup SeckelAPI (podman)")
  179. fi
  180. if $HAS_CARGO; then
  181. options+=(5 "Build desktop client")
  182. fi
  183. options+=(6 "Update from git masters")
  184. options+=(7 "Clean sources")
  185. options+=(8 "Quit")
  186. $DIALOG --clear \
  187. --title "BeepZone Setup" \
  188. --menu "Choose an action" 17 76 7 \
  189. "${options[@]}" 2>"$CHOICE_FILE"
  190. choice=$(<"$CHOICE_FILE")
  191. case $choice in
  192. 1) configure_and_run_db ;;
  193. 9) configure_and_run_native_db ;;
  194. 10) setup_seckelapi_native ;;
  195. 2) import_schema_and_seed ;;
  196. 3) manage_users_and_roles ;;
  197. 4) setup_seckelapi ;;
  198. 5) build_desktop_client ;;
  199. 6) update_from_git_masters ;;
  200. 7) clean_sources ;;
  201. 8) exit 0 ;;
  202. esac
  203. done
  204. }
  205. configure_and_run_db() {
  206. $DIALOG --form "MariaDB container configuration" 18 70 8 \
  207. "DB Host" 1 1 "$DB_HOST" 1 18 30 30 \
  208. "DB Port" 2 1 "$DB_PORT" 2 18 30 30 \
  209. "DB Name" 3 1 "$DB_NAME" 3 18 30 30 \
  210. "DB User" 4 1 "$DB_USER" 4 18 30 30 \
  211. "DB Password" 5 1 "$DB_PASS" 5 18 30 30 \
  212. "Root Password" 6 1 "$DB_ROOT_PASSWORD" 6 18 30 30 \
  213. "Container Name" 7 1 "$BEEPZONE_DB_CONTAINER_NAME" 7 18 30 30 \
  214. "Image" 8 1 "$BEEPZONE_DB_IMAGE" 8 18 30 30 2>"$TMP_FILE" || return
  215. # Parse dialog output (8 lines, one per field)
  216. {
  217. read -r DB_HOST
  218. read -r DB_PORT
  219. read -r DB_NAME
  220. read -r DB_USER
  221. read -r DB_PASS
  222. read -r DB_ROOT_PASSWORD
  223. read -r BEEPZONE_DB_CONTAINER_NAME
  224. read -r BEEPZONE_DB_IMAGE
  225. } < "$TMP_FILE"
  226. save_env
  227. if podman ps -a --format '{{.Names}}' | grep -q "^${BEEPZONE_DB_CONTAINER_NAME}$"; then
  228. $DIALOG --yesno "Container '$BEEPZONE_DB_CONTAINER_NAME' already exists.\n\nStart (or restart) it now?" 10 60
  229. if [[ $? -eq 0 ]]; then
  230. podman start "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || podman restart "$BEEPZONE_DB_CONTAINER_NAME" >/dev/null 2>&1 || true
  231. $DIALOG --msgbox "Container '$BEEPZONE_DB_CONTAINER_NAME' started (or already running)." 7 60
  232. fi
  233. return
  234. fi
  235. run_cmd=(
  236. podman run -d
  237. --name "${BEEPZONE_DB_CONTAINER_NAME}"
  238. -e "MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD}"
  239. -e "MARIADB_DATABASE=${DB_NAME}"
  240. -e "MARIADB_USER=${DB_USER}"
  241. -e "MARIADB_PASSWORD=${DB_PASS}"
  242. -p "${DB_PORT}:3306"
  243. "${BEEPZONE_DB_IMAGE}"
  244. )
  245. pretty_cmd="podman run -d \\
  246. --name ${BEEPZONE_DB_CONTAINER_NAME} \\
  247. -e MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} \\
  248. -e MARIADB_DATABASE=${DB_NAME} \\
  249. -e MARIADB_USER=${DB_USER} \\
  250. -e MARIADB_PASSWORD=${DB_PASS} \\
  251. -p ${DB_PORT}:3306 ${BEEPZONE_DB_IMAGE}"
  252. if $DIALOG --yesno "About to run:\n\n${pretty_cmd}\n\nProceed?" 17 76; then
  253. if "${run_cmd[@]}" >>"$LOG_FILE" 2>&1; then
  254. $DIALOG --msgbox "MariaDB container '${BEEPZONE_DB_CONTAINER_NAME}' started successfully." 8 70
  255. else
  256. error_log=$(tail -20 "$LOG_FILE")
  257. $DIALOG --title "Error" --msgbox "Failed to start MariaDB container.\n\nError:\n${error_log}" 20 76
  258. fi
  259. fi
  260. }
  261. configure_and_run_native_db() {
  262. "$DIALOG" --msgbox "You'll need this for the Native MariaDB Setup:\n\n1. Debian 12/13\n2. Sudo privileges\n3. The following packages installed:\n - mariadb-server\n - mariadb-client\n - sudo\n\nIf you are unsure or know you're missing one hit CTRL-C now" 14 60
  263. # Ask if root password is set
  264. if ! "$DIALOG" --yesno "Have you already set up a root password for MariaDB?" 10 60; then
  265. # User said No (exit code 1)
  266. clear
  267. echo "Running mysql_secure_installation..."
  268. sudo mysql_secure_installation
  269. fi
  270. $DIALOG --form "MariaDB configuration" 16 70 6 \
  271. "DB Host" 1 1 "$DB_HOST" 1 18 30 30 \
  272. "DB Port" 2 1 "$DB_PORT" 2 18 30 30 \
  273. "DB Name" 3 1 "$DB_NAME" 3 18 30 30 \
  274. "DB User" 4 1 "$DB_USER" 4 18 30 30 \
  275. "DB Password" 5 1 "$DB_PASS" 5 18 30 30 \
  276. "Root Password" 6 1 "$DB_ROOT_PASSWORD" 6 18 30 30 2>"$TMP_FILE" || return
  277. # Parse dialog output (6 lines)
  278. {
  279. read -r DB_HOST
  280. read -r DB_PORT
  281. read -r DB_NAME
  282. read -r DB_USER
  283. read -r DB_PASS
  284. read -r DB_ROOT_PASSWORD
  285. } < "$TMP_FILE"
  286. save_env
  287. # Check if mariadb is even running
  288. if ! systemctl is-active --quiet mariadb; then
  289. if $DIALOG --yesno "MariaDB service aint running. Try and start it?" 10 60; then
  290. sudo systemctl start mariadb || {
  291. $DIALOG --msgbox "Crap, Failed to start MariaDB service" 8 60
  292. return
  293. }
  294. else
  295. return
  296. fi
  297. fi
  298. local create_sql="
  299. CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\`;
  300. CREATE USER IF NOT EXISTS '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASS}';
  301. GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'%';
  302. FLUSH PRIVILEGES;
  303. "
  304. if "$DIALOG" --yesno "Script will now do the following:\n\nCreate DB: $DB_NAME\nCreate User: $DB_USER\n\nThis alright?" 12 70; then
  305. if echo "$create_sql" | mysql -u root -p"$DB_ROOT_PASSWORD" 2>>"$LOG_FILE"; then
  306. "$DIALOG" --msgbox "Database '$DB_NAME' and user '$DB_USER' created successfully." 8 70
  307. else
  308. "$DIALOG" --msgbox "Fuck something went wrong Check $LOG_FILE for details." 8 70
  309. fi
  310. fi
  311. }
  312. import_schema_and_seed() {
  313. local import_type schema_file full_dump_file
  314. schema_file="$WORK_DIR/backend/database/schema/beepzone-schema-dump.sql"
  315. full_dump_file="$WORK_DIR/backend/database/dev/beepzone-full-dump.sql"
  316. # Ask what type of import
  317. $DIALOG --clear \
  318. --title "Database Import" \
  319. --menu "Select import type" 12 70 2 \
  320. 1 "Full dump (schema + data in one file, not recommended unless you know what you're doing)" \
  321. 2 "Clean schema only (no data, recommended but make sure to read docs on how to get started)" 2>"$CHOICE_FILE" || return
  322. import_type=$(<"$CHOICE_FILE")
  323. if [[ "$import_type" == "1" ]]; then
  324. # Full dump import
  325. if [[ ! -f "$full_dump_file" ]]; then
  326. $DIALOG --msgbox "full dump file not found at:\n$full_dump_file" 10 70
  327. return
  328. fi
  329. $DIALOG --yesno "import full database dump from:\n$full_dump_file\n\nThis will DROP and recreate the database.\n\nYou Sure?" 12 70 || return
  330. {
  331. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  332. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  333. echo "USE \`$DB_NAME\`;"
  334. echo "SET FOREIGN_KEY_CHECKS=0;"
  335. cat "$full_dump_file"
  336. echo "SET FOREIGN_KEY_CHECKS=1;"
  337. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  338. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  339. error_log=$(tail -30 "$LOG_FILE")
  340. $DIALOG --title "Fuck" --msgbox "full dump import failed.\n\nError:\n${error_log}" 22 76
  341. return
  342. }
  343. DEPLOYMENT_TYPE="dev"
  344. save_env
  345. $DIALOG --msgbox "full database dump imported successfully!" 8 70
  346. else
  347. # Clean schema import
  348. if [[ ! -f "$schema_file" ]]; then
  349. $DIALOG --msgbox "schema file not found at:\n$schema_file" 10 60
  350. return
  351. fi
  352. $DIALOG --yesno "import clean schema from:\n$schema_file\n\nThis will DROP and recreate the database.\n\nProceed?" 12 70 || return
  353. {
  354. echo "DROP DATABASE IF EXISTS \`$DB_NAME\`;"
  355. echo "CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_uca1400_ai_ci;"
  356. echo "USE \`$DB_NAME\`;"
  357. echo "SET FOREIGN_KEY_CHECKS=0;"
  358. cat "$schema_file"
  359. echo "SET FOREIGN_KEY_CHECKS=1;"
  360. } | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  361. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" >>"$LOG_FILE" 2>&1 || {
  362. error_log=$(tail -30 "$LOG_FILE")
  363. $DIALOG --title "Error" --msgbox "schema import failed.\n\nError:\n${error_log}" 22 76
  364. return
  365. }
  366. DEPLOYMENT_TYPE="clean"
  367. save_env
  368. $DIALOG --msgbox "schema imported successfully!\n\nI recommend you make an Admin role with power 100 and and Admin user as the next step." 8 70
  369. fi
  370. }
  371. manage_users_and_roles() {
  372. while true; do
  373. $DIALOG --clear \
  374. --title "Manage Users & Roles" \
  375. --menu "Choose an action:" 17 60 6 \
  376. 1 "Create a role" \
  377. 2 "Create a user" \
  378. 3 "Delete a role" \
  379. 4 "Delete a user" \
  380. 5 "Back to main menu" 2>"$CHOICE_FILE"
  381. [[ ! -s "$CHOICE_FILE" ]] && return 0
  382. choice=$(<"$CHOICE_FILE")
  383. case $choice in
  384. 1) create_role ;;
  385. 2) create_user ;;
  386. 3) delete_role ;;
  387. 4) delete_user ;;
  388. 5) return 0 ;;
  389. esac
  390. done
  391. }
  392. create_role() {
  393. $DIALOG --title "Create Role" \
  394. --form "Enter role details:" 12 60 2 \
  395. "Role name:" 1 1 "" 1 20 30 0 \
  396. "Power (1-100):" 2 1 "" 2 20 10 0 \
  397. 2>"$TMP_FILE"
  398. [[ ! -s "$TMP_FILE" ]] && return 1
  399. local name power
  400. {
  401. read -r name
  402. read -r power
  403. } < "$TMP_FILE"
  404. [[ -z "$name" || -z "$power" ]] && {
  405. $DIALOG --msgbox "Role name and power are required." 8 50
  406. return 1
  407. }
  408. # Validate power is a number between 1-100
  409. if ! [[ "$power" =~ ^[0-9]+$ ]] || [[ "$power" -lt 1 || "$power" -gt 100 ]]; then
  410. $DIALOG --msgbox "Power must be a number between 1 and 100." 8 50
  411. return 1
  412. fi
  413. local sql="INSERT INTO roles (name, power) VALUES ('$name', $power);"
  414. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  415. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  416. error_log=$(tail -20 "$LOG_FILE")
  417. $DIALOG --title "Error" --msgbox "Failed to create role.\n\nError:\n${error_log}" 20 76
  418. return 1
  419. }
  420. $DIALOG --msgbox "Role '$name' created successfully!" 8 50
  421. }
  422. delete_role() {
  423. # Fetch roles from the database
  424. local roles_list
  425. roles_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  426. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  427. -e "SELECT id, name, power FROM roles ORDER BY power DESC;" -s -N 2>>"$LOG_FILE") || {
  428. $DIALOG --msgbox "Failed to fetch roles from database." 10 60
  429. return
  430. }
  431. # Build role selection menu
  432. local role_options=()
  433. while IFS=$'\t' read -r role_id role_name role_power; do
  434. role_options+=("$role_id" "$role_name (power: $role_power)")
  435. done <<< "$roles_list"
  436. if [[ ${#role_options[@]} -eq 0 ]]; then
  437. $DIALOG --msgbox "No roles found in database." 10 60
  438. return
  439. fi
  440. # Select role to delete
  441. $DIALOG --menu "Select role to delete:" 20 70 10 "${role_options[@]}" 2>"$TMP_FILE" || return
  442. local selected_role_id
  443. selected_role_id=$(<"$TMP_FILE")
  444. # Get role name for confirmation
  445. local selected_role_name
  446. selected_role_name=$(echo "$roles_list" | awk -v id="$selected_role_id" -F'\t' '$1 == id {print $2}')
  447. # Check if any users are using this role
  448. local user_count
  449. user_count=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  450. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  451. -e "SELECT COUNT(*) FROM users WHERE role_id = $selected_role_id;" -s -N 2>>"$LOG_FILE")
  452. if [[ "$user_count" -gt 0 ]]; then
  453. $DIALOG --msgbox "Cannot delete role '$selected_role_name'.\n$user_count user(s) are currently assigned this role." 10 60
  454. return
  455. fi
  456. # Confirm deletion
  457. $DIALOG --yesno "Are you sure you want to delete role '$selected_role_name' (ID: $selected_role_id)?\n\nThis action cannot be undone." 10 60 || return
  458. # Delete role
  459. local sql="DELETE FROM roles WHERE id = $selected_role_id;"
  460. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  461. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  462. error_log=$(tail -20 "$LOG_FILE")
  463. $DIALOG --title "Error" --msgbox "Failed to delete role.\n\nError:\n${error_log}" 20 76
  464. return
  465. }
  466. $DIALOG --msgbox "Role '$selected_role_name' deleted successfully!" 8 60
  467. }
  468. create_user() {
  469. # Get available roles
  470. local roles_list
  471. roles_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  472. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  473. -e "SELECT id, name, power FROM roles ORDER BY power DESC;" -s -N 2>>"$LOG_FILE") || {
  474. $DIALOG --msgbox "Failed to fetch roles from database.\nEnsure schema is imported and roles exist." 10 60
  475. return
  476. }
  477. # Build role selection menu
  478. local role_options=()
  479. while IFS=$'\t' read -r role_id role_name role_power; do
  480. role_options+=("$role_id" "$role_name (power: $role_power)")
  481. done <<< "$roles_list"
  482. if [[ ${#role_options[@]} -eq 0 ]]; then
  483. $DIALOG --msgbox "No roles found in database.\nPlease create a role first." 10 60
  484. return
  485. fi
  486. # Select role
  487. $DIALOG --menu "Select user role" 15 60 5 "${role_options[@]}" 2>"$TMP_FILE" || return
  488. local selected_role_id
  489. selected_role_id=$(<"$TMP_FILE")
  490. # Get user details
  491. $DIALOG --form "Create BeepZone user" 14 70 5 \
  492. "Name (full name)" 1 1 "" 1 20 40 40 \
  493. "Username" 2 1 "" 2 20 40 40 \
  494. "Password" 3 1 "" 3 20 40 40 \
  495. "Email" 4 1 "" 4 20 40 40 \
  496. "Phone" 5 1 "" 5 20 40 40 2>"$TMP_FILE" || return
  497. local name username password email phone
  498. {
  499. read -r name
  500. read -r username
  501. read -r password
  502. read -r email
  503. read -r phone
  504. } < "$TMP_FILE"
  505. if [[ -z "$name" || -z "$username" || -z "$password" ]]; then
  506. $DIALOG --msgbox "Name, username, and password are required." 8 60
  507. return
  508. fi
  509. # Hash password with bcrypt (cost 12)
  510. local password_hash
  511. # Try htpasswd first (native Unix tool)
  512. if command -v htpasswd >/dev/null 2>&1; then
  513. password_hash=$(htpasswd -nbB -C 12 "$username" "$password" 2>>"$LOG_FILE" | cut -d: -f2)
  514. # Fall back to Python bcrypt
  515. elif command -v python3 >/dev/null 2>&1; then
  516. password_hash=$(python3 -c "import bcrypt; print(bcrypt.hashpw('$password'.encode('utf-8'), bcrypt.gensalt(12)).decode('utf-8'))" 2>>"$LOG_FILE")
  517. fi
  518. if [[ -z "$password_hash" ]]; then
  519. $DIALOG --msgbox "Error: Failed to hash password. No bcrypt tool found.\n\nInstall options:\n- htpasswd: brew install httpd (macOS) or apt install apache2-utils (Linux)\n- Python bcrypt: pip3 install bcrypt" 12 70
  520. return
  521. fi
  522. # Insert user with hashed password
  523. local sql
  524. sql="INSERT INTO users (name, username, password, role_id, email, phone, active) VALUES
  525. ('$name', '$username', '$password_hash', $selected_role_id, "
  526. [[ -n "$email" ]] && sql+="'$email'" || sql+="NULL"
  527. sql+=", "
  528. [[ -n "$phone" ]] && sql+="'$phone'" || sql+="NULL"
  529. sql+=", 1);"
  530. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  531. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  532. error_log=$(tail -20 "$LOG_FILE")
  533. $DIALOG --title "Error" --msgbox "Failed to create user.\n\nError:\n${error_log}" 20 76
  534. return
  535. }
  536. $DIALOG --msgbox "User '$username' created successfully!" 8 60
  537. }
  538. delete_user() {
  539. # Fetch users from the database
  540. local users_list
  541. users_list=$(podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  542. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" \
  543. -e "SELECT id, username, name FROM users ORDER BY id;" -s -N 2>>"$LOG_FILE") || {
  544. $DIALOG --msgbox "Failed to fetch users from database." 10 60
  545. return
  546. }
  547. # Build user selection menu
  548. local user_options=()
  549. while IFS=$'\t' read -r user_id username name; do
  550. user_options+=("$user_id" "$username - $name")
  551. done <<< "$users_list"
  552. if [[ ${#user_options[@]} -eq 0 ]]; then
  553. $DIALOG --msgbox "No users found in database." 10 60
  554. return
  555. fi
  556. # Select user to delete
  557. $DIALOG --menu "Select user to delete:" 20 70 10 "${user_options[@]}" 2>"$TMP_FILE" || return
  558. local selected_user_id
  559. selected_user_id=$(<"$TMP_FILE")
  560. # Get username for confirmation
  561. local selected_username
  562. selected_username=$(echo "$users_list" | awk -v id="$selected_user_id" -F'\t' '$1 == id {print $2}')
  563. # Confirm deletion
  564. $DIALOG --yesno "Are you sure you want to delete user '$selected_username' (ID: $selected_user_id)?\n\nThis action cannot be undone." 10 60 || return
  565. # Delete user
  566. local sql="DELETE FROM users WHERE id = $selected_user_id;"
  567. echo "$sql" | podman exec -i "$BEEPZONE_DB_CONTAINER_NAME" \
  568. mariadb -h"$DB_HOST" -P"$DB_PORT" -uroot -p"$DB_ROOT_PASSWORD" "$DB_NAME" >>"$LOG_FILE" 2>&1 || {
  569. error_log=$(tail -20 "$LOG_FILE")
  570. $DIALOG --title "Error" --msgbox "Failed to delete user.\n\nError:\n${error_log}" 20 76
  571. return
  572. }
  573. $DIALOG --msgbox "User '$selected_username' deleted successfully!" 8 60
  574. }
  575. clone_if_missing() {
  576. local repo_url="$1" dest_dir="$2"
  577. if [[ -d "$dest_dir/.git" ]]; then
  578. return
  579. fi
  580. git clone "$repo_url" "$dest_dir" >>"$LOG_FILE" 2>&1 || {
  581. error_log=$(tail -20 "$LOG_FILE")
  582. $DIALOG --title "Error" --msgbox "Failed to clone $repo_url.\n\nError:\n${error_log}" 20 76
  583. return 1
  584. }
  585. }
  586. setup_seckelapi() {
  587. local sources_dir="$WORK_DIR/backend/seckelapi/sources"
  588. local config_dir="$WORK_DIR/backend/seckelapi/config"
  589. local sources_basics_config="$sources_dir/config/basics.toml"
  590. # Check if sources are already cloned
  591. if [[ ! -d "$sources_dir/.git" ]]; then
  592. mkdir -p "$sources_dir"
  593. clone_if_missing "$SECKELAPI_REPO" "$sources_dir" || return
  594. fi
  595. # Ask about config update
  596. $DIALOG --clear \
  597. --title "SeckelAPI Configuration" \
  598. --menu "How do you want to handle the config?" 12 70 2 \
  599. 1 "Auto-update from database settings" \
  600. 2 "Manually edit config file" 2>"$CHOICE_FILE"
  601. [[ ! -s "$CHOICE_FILE" ]] && return 0
  602. config_choice=$(<"$CHOICE_FILE")
  603. # Ensure sources config directory exists and copy all template configs
  604. mkdir -p "$sources_dir/config"
  605. # Always copy all template configs from config/ to sources/config/
  606. for conf_file in "$config_dir"/*.toml; do
  607. conf_name=$(basename "$conf_file")
  608. cp "$conf_file" "$sources_dir/config/$conf_name" 2>>"$LOG_FILE"
  609. done
  610. if [[ "$config_choice" == "1" ]]; then
  611. # Auto-update basics.toml in sources with database settings (only [database] section)
  612. if [[ -f "$sources_basics_config" ]]; then
  613. # Determine database host - use host.containers.internal for container deployments
  614. local db_host_value="$DB_HOST"
  615. sed -i.bak \
  616. -e '/^\[database\]/,/^\[/ {
  617. /^host = /s|= .*|= "'"$db_host_value"'"|
  618. /^port = /s|= .*|= '"$DB_PORT"'|
  619. /^database = /s|= .*|= "'"$DB_NAME"'"|
  620. /^username = /s|= .*|= "'"$DB_USER"'"|
  621. /^password = /s|= .*|= "'"$DB_PASS"'"|
  622. }' \
  623. "$sources_basics_config"
  624. $DIALOG --msgbox "Config updated with database settings from .env" 8 60
  625. else
  626. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  627. return 1
  628. fi
  629. else
  630. # Open config file for manual editing (in sources/config/)
  631. if [[ -f "$sources_basics_config" ]]; then
  632. ${EDITOR:-nano} "$sources_basics_config"
  633. else
  634. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  635. return 1
  636. fi
  637. fi
  638. # Ask about build and deployment
  639. $DIALOG --clear \
  640. --title "Build & Deploy SeckelAPI" \
  641. --menu "Choose an action:" 12 70 2 \
  642. 1 "Build and deploy to podman container" \
  643. $( $HAS_CARGO && echo "2 \"Build natively on host\"" ) 2>"$CHOICE_FILE"
  644. [[ ! -s "$CHOICE_FILE" ]] && return 0
  645. build_choice=$(<"$CHOICE_FILE")
  646. if [[ "$build_choice" == "2" ]]; then
  647. # Native host build with live output
  648. clear
  649. echo "Building SeckelAPI..."
  650. echo "===================="
  651. echo ""
  652. if (cd "$sources_dir" && cargo build --release); then
  653. # Copy config files to the build output directory
  654. local target_dir="$sources_dir/target/release"
  655. mkdir -p "$target_dir/config"
  656. cp "$sources_dir/config"/*.toml "$target_dir/config/" 2>>"$LOG_FILE"
  657. echo ""
  658. echo "Build completed successfully!"
  659. echo "Binary location: $target_dir/seckelapi"
  660. echo "Config location: $target_dir/config/"
  661. read -p "Press Enter to continue..."
  662. else
  663. echo ""
  664. echo "Build failed! Check the output above for errors."
  665. read -p "Press Enter to continue..."
  666. return 1
  667. fi
  668. elif [[ "$build_choice" == "1" ]]; then
  669. # Build and deploy to podman container
  670. clear
  671. echo "Building SeckelAPI container..."
  672. echo "================================"
  673. echo ""
  674. # Get the host gateway IP for container to access host services
  675. # For Podman, we'll use the gateway IP from the default bridge network
  676. local host_gateway="host.containers.internal"
  677. # Update database host for container networking
  678. if [[ -f "$sources_basics_config" ]]; then
  679. echo "Updating database host to: $host_gateway"
  680. sed -i.container-bak \
  681. -e '/^\[database\]/,/^\[/ {
  682. /^host = /s|= .*|= "'"$host_gateway"'"|
  683. }' \
  684. "$sources_basics_config"
  685. fi
  686. local container_name="beepzone-seckelapi"
  687. local image_name="beepzone-seckelapi:latest"
  688. local containerfile="$WORK_DIR/backend/seckelapi/Containerfile"
  689. # Stop and remove existing container if running
  690. if podman ps -a --format "{{.Names}}" | grep -q "^${container_name}$"; then
  691. echo "Stopping and removing existing container..."
  692. podman stop "$container_name" 2>/dev/null || true
  693. podman rm "$container_name" 2>/dev/null || true
  694. fi
  695. # Build container image
  696. echo "Building container image..."
  697. if podman build -t "$image_name" -f "$containerfile" "$WORK_DIR/backend/seckelapi"; then
  698. echo ""
  699. echo "Container image built successfully!"
  700. echo ""
  701. # Ask to run the container
  702. if $DIALOG --yesno "Start the SeckelAPI container now?" 8 50; then
  703. echo "Starting container..."
  704. # Run container with port mapping and host gateway
  705. if podman run -d \
  706. --name "$container_name" \
  707. --add-host host.containers.internal:host-gateway \
  708. -p 5777:5777 \
  709. "$image_name"; then
  710. echo ""
  711. echo "Container started successfully!"
  712. echo "Container name: $container_name"
  713. echo "API listening on: http://0.0.0.0:5777"
  714. echo ""
  715. echo "Useful commands:"
  716. echo " podman logs $container_name - View logs"
  717. echo " podman stop $container_name - Stop container"
  718. echo " podman start $container_name - Start container"
  719. echo " podman restart $container_name - Restart container"
  720. read -p "Press Enter to continue..."
  721. else
  722. echo ""
  723. echo "Failed to start container!"
  724. read -p "Press Enter to continue..."
  725. return 1
  726. fi
  727. fi
  728. else
  729. echo ""
  730. echo "Container build failed! Check the output above for errors."
  731. read -p "Press Enter to continue..."
  732. return 1
  733. fi
  734. fi
  735. }
  736. setup_seckelapi_native() {
  737. if ! $HAS_CARGO; then
  738. "$DIALOG" --msgbox "Cargo not found in PATH.\n\nInstall Rust toolchain (rustup) to use native SeckelAPI setup." 10 70
  739. return
  740. fi
  741. # 1. Check Pre-requisites
  742. $DIALOG --msgbox "Pre-requisites for Native SeckelAPI Setup:\n\n1. Rust toolchain (rustup, cargo)\n2. build-essential (gcc, etc.)\n3. pkg-config\n4. libssl-dev\n5. Sudo privileges (for systemd service)\n\nEnsure these are met before proceeding." 14 60
  743. local sources_dir="$WORK_DIR/backend/seckelapi/sources"
  744. local config_dir="$WORK_DIR/backend/seckelapi/config"
  745. local sources_basics_config="$sources_dir/config/basics.toml"
  746. # 2. Clone sources if missing
  747. if [[ ! -d "$sources_dir/.git" ]]; then
  748. mkdir -p "$sources_dir"
  749. clone_if_missing "$SECKELAPI_REPO" "$sources_dir" || return
  750. fi
  751. # 3. Configure basics.toml
  752. # Ensure sources config directory exists and copy all template configs
  753. mkdir -p "$sources_dir/config"
  754. for conf_file in "$config_dir"/*.toml; do
  755. conf_name=$(basename "$conf_file")
  756. cp "$conf_file" "$sources_dir/config/$conf_name" 2>>"$LOG_FILE"
  757. done
  758. if [[ -f "$sources_basics_config" ]]; then
  759. # Update with DB settings from .env (localhost since it's native)
  760. sed -i.bak \
  761. -e '/^\[database\]/,/^\[/ {
  762. /^host = /s|= .*|= "'"$DB_HOST"'"|
  763. /^port = /s|= .*|= '"$DB_PORT"'|
  764. /^database = /s|= .*|= "'"$DB_NAME"'"|
  765. /^username = /s|= .*|= "'"$DB_USER"'"|
  766. /^password = /s|= .*|= "'"$DB_PASS"'"|
  767. }' \
  768. "$sources_basics_config"
  769. else
  770. $DIALOG --msgbox "Error: basics.toml not found at $sources_basics_config" 8 60
  771. return 1
  772. fi
  773. # 4. Build Release
  774. clear
  775. echo "Building SeckelAPI (Native)..."
  776. echo "============================"
  777. echo ""
  778. if (cd "$sources_dir" && cargo build --release); then
  779. # Copy config files to the build output directory
  780. local target_dir="$sources_dir/target/release"
  781. mkdir -p "$target_dir/config"
  782. cp "$sources_dir/config"/*.toml "$target_dir/config/" 2>>"$LOG_FILE"
  783. echo ""
  784. echo "Build completed successfully!"
  785. else
  786. echo ""
  787. echo "Build failed! Check the output above for errors."
  788. read -p "Press Enter to continue..."
  789. return 1
  790. fi
  791. # 5. Install Systemd Service
  792. if $DIALOG --yesno "Do you want to install SeckelAPI as a systemd service?" 8 60; then
  793. local service_name="beepzone-seckelapi"
  794. local service_file="/tmp/${service_name}.service"
  795. local current_user
  796. current_user=$(whoami)
  797. # Working directory for the binary
  798. local working_dir="$sources_dir/target/release"
  799. local exec_path="$working_dir/seckelapi"
  800. cat > "$service_file" <<EOF
  801. [Unit]
  802. Description=BeepZone SeckelAPI Service
  803. After=network.target mariadb.service
  804. Requires=mariadb.service
  805. [Service]
  806. Type=simple
  807. User=${current_user}
  808. WorkingDirectory=${working_dir}
  809. ExecStart=${exec_path}
  810. Restart=always
  811. RestartSec=10
  812. Environment=RUST_LOG=info
  813. [Install]
  814. WantedBy=multi-user.target
  815. EOF
  816. echo "Installing systemd service..."
  817. sudo mv "$service_file" "/etc/systemd/system/${service_name}.service"
  818. sudo systemctl daemon-reload
  819. sudo systemctl enable "${service_name}"
  820. if sudo systemctl start "${service_name}"; then
  821. $DIALOG --msgbox "Service '${service_name}' installed and started successfully!" 8 60
  822. else
  823. $DIALOG --msgbox "Failed to start service '${service_name}'. Check 'sudo systemctl status ${service_name}'" 10 70
  824. fi
  825. fi
  826. }
  827. update_from_git_masters() {
  828. local seckelapi_sources="$WORK_DIR/backend/seckelapi/sources"
  829. local client_sources="$WORK_DIR/frontend/desktop-client/sources"
  830. if $DIALOG --yesno "This will run 'git pull' in both backend and frontend source directories.\n\nContinue?" 10 60; then
  831. clear
  832. echo "Updating sources from git masters..."
  833. echo "===================================="
  834. echo ""
  835. # Update SeckelAPI
  836. if [[ -d "$seckelapi_sources/.git" ]]; then
  837. echo "Updating SeckelAPI..."
  838. if (cd "$seckelapi_sources" && git pull); then
  839. echo "SeckelAPI updated successfully."
  840. else
  841. echo "Failed to update SeckelAPI."
  842. fi
  843. else
  844. echo "SeckelAPI sources not found or not a git repository. Skipping."
  845. fi
  846. echo ""
  847. # Update Desktop Client
  848. if [[ -d "$client_sources/.git" ]]; then
  849. echo "Updating Desktop Client..."
  850. if (cd "$client_sources" && git pull); then
  851. echo "Desktop Client updated successfully."
  852. else
  853. echo "Failed to update Desktop Client."
  854. fi
  855. else
  856. echo "Desktop Client sources not found or not a git repository. Skipping."
  857. fi
  858. echo ""
  859. read -p "Press Enter to continue..."
  860. fi
  861. }
  862. clean_sources() {
  863. $DIALOG --yesno "This will delete all sources directories including hidden files:\n\n- backend/seckelapi/sources\n- frontend/desktop-client/sources\n\nAre you sure?" 12 70 || return
  864. local seckelapi_sources="$WORK_DIR/backend/seckelapi/sources"
  865. local client_sources="$WORK_DIR/frontend/desktop-client/sources"
  866. # Clean SeckelAPI sources
  867. if [[ -d "$seckelapi_sources" ]]; then
  868. find "$seckelapi_sources" -mindepth 1 -delete 2>>"$LOG_FILE"
  869. if [[ $? -eq 0 ]]; then
  870. echo "SeckelAPI sources contents removed" >>"$LOG_FILE"
  871. fi
  872. fi
  873. # Clean desktop client sources
  874. if [[ -d "$client_sources" ]]; then
  875. find "$client_sources" -mindepth 1 -delete 2>>"$LOG_FILE"
  876. if [[ $? -eq 0 ]]; then
  877. echo "Desktop client sources contents removed" >>"$LOG_FILE"
  878. fi
  879. fi
  880. $DIALOG --msgbox "All sources cleaned!" 7 50
  881. # Ask to re-clone
  882. if $DIALOG --yesno "Do you want to pull fresh sources now?" 7 50; then
  883. # Clone SeckelAPI
  884. mkdir -p "$(dirname "$seckelapi_sources")"
  885. if clone_if_missing "$SECKELAPI_REPO" "$seckelapi_sources"; then
  886. $DIALOG --msgbox "SeckelAPI sources cloned successfully!" 7 50
  887. else
  888. $DIALOG --msgbox "Failed to clone SeckelAPI sources. Check log." 7 50
  889. return 1
  890. fi
  891. # Clone desktop client
  892. mkdir -p "$(dirname "$client_sources")"
  893. if clone_if_missing "$CLIENT_REPO" "$client_sources"; then
  894. $DIALOG --msgbox "Desktop client sources cloned successfully!" 7 50
  895. else
  896. $DIALOG --msgbox "Failed to clone desktop client sources. Check log." 7 50
  897. return 1
  898. fi
  899. $DIALOG --msgbox "All sources pulled fresh!" 7 50
  900. fi
  901. }
  902. build_desktop_client() {
  903. local sources_dir="$WORK_DIR/frontend/desktop-client/sources"
  904. if ! $HAS_CARGO; then
  905. "$DIALOG" --msgbox "Cargo not found in PATH.\n\nInstall Rust toolchain (rustup) to build the desktop client." 10 70
  906. return
  907. fi
  908. # Check if sources are already cloned
  909. if [[ ! -d "$sources_dir/.git" ]]; then
  910. mkdir -p "$sources_dir"
  911. clone_if_missing "$CLIENT_REPO" "$sources_dir" || return
  912. fi
  913. if $DIALOG --yesno "Build BeepZone desktop client in release mode now?" 8 70; then
  914. echo "Building BeepZone Desktop Client..."
  915. echo "===================================="
  916. echo ""
  917. if (cd "$sources_dir" && cargo build --release); then
  918. echo ""
  919. echo "Build completed successfully!"
  920. echo "Binary location: $sources_dir/target/release/"
  921. read -p "Press Enter to continue..."
  922. else
  923. echo ""
  924. echo "Build failed! Check the output above for errors."
  925. read -p "Press Enter to continue..."
  926. return 1
  927. fi
  928. fi
  929. }
  930. show_capabilities_screen
  931. ask_main_menu