1
0

beepzone-helper.sh 40 KB

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