select-board.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash
  2. # Pick the board type every other script builds and installs for. The choice is
  3. # kept in .board; each script prints it first. BOARD=<id> in the environment
  4. # overrides it for a single command.
  5. #
  6. # Usage: ./select-board.sh menu
  7. # ./select-board.sh <id> select directly (scripts, ssh without tty)
  8. # ./select-board.sh -l list board types, mark the selected one
  9. set -euo pipefail
  10. CONFIG_NO_BOARD=1 . "$(dirname "$0")/config.sh"
  11. current="$(cat "$BOARD_SELECTION" 2>/dev/null || true)"
  12. mapfile -t ids < <(list_boards | cut -f1)
  13. mapfile -t names < <(list_boards | cut -f2)
  14. [ ${#ids[@]} -gt 0 ] || die "no board files in $BOARDS_DIR"
  15. describe() { # describe ID
  16. local i
  17. for i in "${!ids[@]}"; do
  18. [ "${ids[$i]}" != "$1" ] || { echo "${names[$i]} [$1]"; return; }
  19. done
  20. echo "$1"
  21. }
  22. set_board() {
  23. local id="$1" i
  24. for i in "${ids[@]}"; do
  25. if [ "$i" = "$id" ]; then
  26. echo "$id" > "$BOARD_SELECTION"
  27. info "board type: $(describe "$id")"
  28. info "build output goes to out/$id/, configuration from boards/$id.sh and files/$id/"
  29. return 0
  30. fi
  31. done
  32. die "unknown board type '$id' (known: ${ids[*]})"
  33. }
  34. case "${1:-}" in
  35. -l|--list)
  36. for i in "${!ids[@]}"; do
  37. mark=" "; [ "${ids[$i]}" != "$current" ] || mark="*"
  38. printf '%s %-8s %s\n' "$mark" "${ids[$i]}" "${names[$i]}"
  39. done
  40. exit 0 ;;
  41. -h|--help)
  42. sed -n '2,10s/^# \{0,1\}//p' "$0"; exit 0 ;;
  43. "") ;;
  44. *) set_board "$1"; exit 0 ;;
  45. esac
  46. [ -t 0 ] || die "no terminal for the menu; give the board id: $0 <$(IFS='|'; echo "${ids[*]}")>"
  47. cur_desc=none; [ -z "$current" ] || cur_desc="$(describe "$current")"
  48. echo "Current board type: $cur_desc"
  49. echo "Select the board type:"
  50. labels=()
  51. for i in "${!ids[@]}"; do
  52. mark=""; [ "${ids[$i]}" != "$current" ] || mark=" (current)"
  53. labels+=("${names[$i]} [${ids[$i]}]$mark")
  54. done
  55. PS3="Number (Ctrl-D to keep the current one): "
  56. select _ in "${labels[@]}"; do
  57. if [ -n "${REPLY:-}" ] && [ "$REPLY" -ge 1 ] 2>/dev/null && [ "$REPLY" -le ${#ids[@]} ]; then
  58. set_board "${ids[$((REPLY - 1))]}"
  59. exit 0
  60. fi
  61. echo "Pick 1-${#ids[@]}."
  62. done
  63. echo
  64. info "unchanged: $cur_desc"