#!/usr/bin/env bash
function dialog_if_terminal_set_vars() {
declare -g DIALOG_RESULT=""
declare -g DIALOG_EXIT_CODE=0
[[ ! -t 0 ]] && exit_with_error "stdin is not a terminal. can't use dialog." "dialog_if_terminal_set_vars ${*}" "err"
[[ ! -t 1 ]] && exit_with_error "stdout is not a terminal. can't use dialog." "dialog_if_terminal_set_vars ${*}" "err"
[[ ! -t 2 ]] && exit_with_error "stderr is not a terminal. can't use dialog." "dialog_if_terminal_set_vars ${*}" "err"
[[ "${CI}" == "true" ]] && exit_with_error "CI=true. can't use dialog." "dialog_if_terminal_set_vars ${*}" "err"
[[ ! -f /usr/bin/dialog ]] && exit_with_error "Dialog is not installed at /usr/bin/dialog" "dialog_if_terminal_set_vars ${*}" "err"
set +e
set +o errtrace
set +o errexit
exec 3>&1
DIALOG_RESULT=$(NCURSES_NO_UTF8_ACS=1 dialog "$@" 2>&1 1>&3)
DIALOG_EXIT_CODE=$?
exec 3>&-
set -e
set -o errtrace
set -o errexit
clear
return 0
}
dialog_menu() {
declare -g DIALOG_MENU_RESULT=""
provided_title=$1
provided_backtitle=$2
provided_menuname=$3
dialog_if_terminal_set_vars --title "$provided_title" --backtitle "${provided_backtitle}" --menu "$provided_menuname" $TTY_Y $TTY_X $((TTY_Y - 8)) "${@:4}"
DIALOG_MENU_RESULT="${DIALOG_RESULT}"
return $DIALOG_EXIT_CODE
}
dialog_checklist() {
declare -g DIALOG_CHECKLIST_RESULT=""
provided_title=$1
provided_backtitle=$2
provided_menuname=$3
dialog_if_terminal_set_vars --title "${provided_title}" --backtitle "${provided_backtitle}" --checklist "${provided_menuname}" $TTY_Y $TTY_X $((TTY_Y - 8)) "${@:4}"
DIALOG_CHECKLIST_RESULT="${DIALOG_RESULT}"
return $DIALOG_EXIT_CODE
}
show_developer_warning() {
local temp_rc
temp_rc=$(mktemp)
cat <<- 'EOF' > "${temp_rc}"
screen_color = (WHITE,RED,ON)
EOF
local warn_text="You are switching to the \Z1EXPERT MODE\Zn
This allows building experimental configurations that are provided
\Z1AS IS\Zn to developers and expert users,
\Z1WITHOUT ANY RESPONSIBILITIES\Zn from the Armbian team:
- You are using these configurations \Z1AT YOUR OWN RISK\Zn
- Bug reports related to the dev kernel, CSC, WIP and EOS boards
\Z1will be closed without a discussion\Zn
- Forum posts related to dev kernel, CSC, WIP and EOS boards
should be created in the \Z2\"Community forums\"\Zn section
"
DIALOGRC=$temp_rc dialog_if_terminal_set_vars --title "Expert mode warning" --backtitle "${backtitle}" --colors --defaultno --no-label "I do not agree" --yes-label "I understand and agree" --yesno "$warn_text" "${TTY_Y}" "${TTY_X}"
[[ ${DIALOG_EXIT_CODE} -ne 0 ]] && exit_with_error "Error switching to the expert mode"
SHOW_WARNING=no
}
function distro_menu() {
local distrib_dir="${1}"
if [[ -d "${distrib_dir}" && -f "${distrib_dir}/support" ]]; then
local support_level
support_level="$(cat "${distrib_dir}/support")"
if [[ "${support_level}" != "supported" && $EXPERT != "yes" ]]; then
:
else
local distro_codename distro_fullname
distro_codename="$(basename "${distrib_dir}")"
distro_fullname="$(cat "${distrib_dir}/name")"
local expert_infos=""
[[ $EXPERT == "yes" ]] && expert_infos="(${support_level})"
options+=("${distro_codename}" "${distro_fullname} ${expert_infos}")
fi
fi
}
function distros_options() {
for distrib_dir in "config/distributions/"*; do
distro_menu "${distrib_dir}"
done
}