summaryrefslogtreecommitdiff
path: root/configs/profile/root-image/root/.session/session-common.sh.inc
blob: fc665f17ace012a5f497099caeed6b672b67c914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
readonly SESSION_DIR="$(cd $(dirname ${BASH_SOURCE[0]}) ; pwd)"
readonly WIZARD_DIR="${SESSION_DIR}"/install
readonly SESSION_INCLUDE_FILENAME=session-common.sh.inc                    # this file
readonly SESSION_STATE_FILENAME=.session_state
readonly TRANSLATIONS_FILENAME=translations.sh.inc
readonly CHROOT_ENVIRONMENT_FILENAME=chroot-environment.sh
readonly CHROOT_LOGIN_FILENAME=chroot-login.sh
readonly CHROOT_SERVICES_FILENAME=chroot-services.sh
readonly SESSION_INCLUDE_FILE="${SESSION_DIR}"/${SESSION_INCLUDE_FILENAME} # this file
readonly SESSION_STATE_FILE="${SESSION_DIR}"/${SESSION_STATE_FILENAME}
readonly TRANSLATIONS_FILE="${SESSION_DIR}"/${TRANSLATIONS_FILENAME}
readonly CHROOT_ENVIRONMENT_FILE="${WIZARD_DIR}"/${CHROOT_ENVIRONMENT_FILENAME}
readonly CHROOT_LOGIN_FILE="${WIZARD_DIR}"/${CHROOT_LOGIN_FILENAME}
readonly CHROOT_SERVICES_FILE="${WIZARD_DIR}"/${CHROOT_SERVICES_FILENAME}


## sanity checks ##

! which dialog &> /dev/null && echo "can not find the \`dialog\` program"       && exit
(( $EUID ))                 && echo "this program requires superuser privilege" && exit


## state helpers ##

SetStateVar() # (var_name value*)
{
  local var_name=$1 ; shift ;
  local value=$*

  sed -i "/^${var_name}=.*/d"    "${SESSION_STATE_FILE}"
  echo "${var_name}=${value}" >> "${SESSION_STATE_FILE}"
}

GetStateVar() # (var_name [def_value])
{
  local var_name=$1
  local def_value=$2
  local stored_value=$(grep "${var_name}=" "${SESSION_STATE_FILE}" | cut -d '=' -f 2)

  if   [[ -n "${stored_value}" ]] && [[ "${stored_value}" != '_UNDEFINED_' ]]
  then echo ${stored_value}
  else echo ${def_value}
  fi
}


## dialog prompt helpers ##

InitDlg() # (title prompt default_option options*)
{
  declare -l title="$1"
  declare -l prompt="$2"
  declare -l default_option="$3"
  declare -l options=("${@:4}")

  dialog --stdout --sleep 1 --timeout 30 --no-tags --no-cancel   \
         --backtitle    "$title"                                 \
         --default-item "${default_option}"                      \
         --menu         "$prompt" $H $W $N_ITEMS "${options[@]}"
}

WizardDlg() # (title dialog_args*)
{
  declare -l title="$1"
  declare -l options=("${@:2}")

  # when the "Cancel" button or <ESC> key is pressed, in a '--msgbox',
  #    dialog exits with status 1; but nothing is printed to STDOUT
  # install.sh runs under `set -e`; so we can not propogate the non-zero status,
  #   otherwise, callers would need to supress it
  # callers should instead, exit upon the empty result, when a selection is mandatory
  #
  # the exception is '--yesno' dialogs, which exits with status 1 for the 'No' button
  # the non-zero status is propogated for '--yesno' dialogs; and callers must handle it
  # ASSERT: '--yesno' dialogs should pass '--yesno' as positional parameter #2 (options[0])
  dialog --stdout --sleep 1                                  \
         --backtitle "${TR[wizard-${TR_KEY}]}"               \
         --title     "${title}" "${options[@]}"           || \
  ( (( $? == 1 )) && [[ "${options[0]}" != '--yesno' ]] )
  clear >&2
}


## error logging helpers ##

LogError() # (source_file func_name line_n)
{
  local SOURCE_FILE="$1"
  local FUNC_NAME="$( [[ -n "$2" ]] && echo "$2" || echo "${SOURCE_FILE}" )"
  local LINE_N=$3
  local N_CONTEXT_LINES=3
  local N_LINES=$((      1 + (2 * N_CONTEXT_LINES) ))
  local BEGIN_LINE_N=$(( LINE_N - N_CONTEXT_LINES  ))
  local END_LINE_N=$((   LINE_N + N_CONTEXT_LINES  ))
  local marker line

  echo "ERROR: in ${FUNC_NAME}::${LINE_N}" >&2
  sed 's|\\$||' "${SOURCE_FILE}" | pr -tn                      | \
  tail -n +${BEGIN_LINE_N} | head -n ${N_LINES} | tr '\n' '\n' | \
  while read line
  do    line_n=$(sed -E 's|([0-9]+).*|\1|' <<<${line})
        (( line_n == LINE_N )) && marker='==>' || marker='   '
        printf "%s %s\n" "${marker}" "${line}" >&2
  done
}


## translations for user-facing strings ##

readonly TRANSLATIONS=" en es gl pt " # pending translations 'eo' 'fr' 'it' 'pl'
readonly TR_KEY=$(GetStateVar 'TR_KEY' 'en')
source "${TRANSLATIONS_FILE}"