#!/bin/bash # Pick the board type every other script builds and installs for. The choice is # kept in .board; each script prints it first. BOARD= in the environment # overrides it for a single command. # # Usage: ./select-board.sh menu # ./select-board.sh select directly (scripts, ssh without tty) # ./select-board.sh -l list board types, mark the selected one set -euo pipefail CONFIG_NO_BOARD=1 . "$(dirname "$0")/config.sh" current="$(cat "$BOARD_SELECTION" 2>/dev/null || true)" mapfile -t ids < <(list_boards | cut -f1) mapfile -t names < <(list_boards | cut -f2) [ ${#ids[@]} -gt 0 ] || die "no board files in $BOARDS_DIR" describe() { # describe ID local i for i in "${!ids[@]}"; do [ "${ids[$i]}" != "$1" ] || { echo "${names[$i]} [$1]"; return; } done echo "$1" } set_board() { local id="$1" i for i in "${ids[@]}"; do if [ "$i" = "$id" ]; then echo "$id" > "$BOARD_SELECTION" info "board type: $(describe "$id")" info "build output goes to out/$id/, configuration from boards/$id.sh and files/$id/" return 0 fi done die "unknown board type '$id' (known: ${ids[*]})" } case "${1:-}" in -l|--list) for i in "${!ids[@]}"; do mark=" "; [ "${ids[$i]}" != "$current" ] || mark="*" printf '%s %-8s %s\n' "$mark" "${ids[$i]}" "${names[$i]}" done exit 0 ;; -h|--help) sed -n '2,10s/^# \{0,1\}//p' "$0"; exit 0 ;; "") ;; *) set_board "$1"; exit 0 ;; esac [ -t 0 ] || die "no terminal for the menu; give the board id: $0 <$(IFS='|'; echo "${ids[*]}")>" cur_desc=none; [ -z "$current" ] || cur_desc="$(describe "$current")" echo "Current board type: $cur_desc" echo "Select the board type:" labels=() for i in "${!ids[@]}"; do mark=""; [ "${ids[$i]}" != "$current" ] || mark=" (current)" labels+=("${names[$i]} [${ids[$i]}]$mark") done PS3="Number (Ctrl-D to keep the current one): " select _ in "${labels[@]}"; do if [ -n "${REPLY:-}" ] && [ "$REPLY" -ge 1 ] 2>/dev/null && [ "$REPLY" -le ${#ids[@]} ]; then set_board "${ids[$((REPLY - 1))]}" exit 0 fi echo "Pick 1-${#ids[@]}." done echo info "unchanged: $cur_desc"