summaryrefslogtreecommitdiff
path: root/install.sh
blob: aeacab7d8d06f2a6f85bdba0f300a902d238d803 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/bin/bash

# Parabola Install Wizard
#
# this is the main script of the Parabola Install Wizard
#
# this script is normally called from the .bash_profile of live ISO,
#   when the user has selected the 'wizard' session (per session-init.sh)
#
# this script does not depend on any existing state though;
#   so it can be launched manually, or stand-alone on other systems
#
# this script will call session-init.sh if necessary,
#   to setup the language and keyboard
#
#
# Copyright (C) 2015      Jorge Lopez <jorginho@riseup.net>
# Copyright (C) 2015-2016 André Fabian Silva Delgado <emulatorman@hyperbola.info>
# Copyright (C) 2020,2022 bill-auger <bill-auger@programmer.net>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Parabola Install Wizard.
#
# Parabola Install Wizard is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Parabola Install Wizard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Parabola Install Wizard. If not, see <http://www.gnu.org/licenses/>.


source $(cd $(dirname ${BASH_SOURCE[0]}) ; pwd)/session-common.sh.inc # sets: $Lang


NoticeIntro()
{
  # checks to skip this notice per preset config
  # NOTE: The 'READY' key is the flag for unattended install.
  #       This program will not set the 'READY' state var.
  #       Preset configs must specify it explicitly.
  AreConfiguredStateVars 'READY' ||
  WizardDlg "${TR[dlg_intro-${Lang}]}" --msgbox "${TR[notice_intro-${Lang}]}" 0 0
}

Init()
{
# MOCK_INIT ; return 0 ;

  # check for and validate a complete unattended-install config state
  if   AreConfiguredStateVars 'READY'
  then AreConfiguredStateVars ${STATEVARS_PRESET[*]}  &&
       [[ "$(GetStateVar 'PART_METHOD')" == 'auto' ]] ||
       ExitFail "${TR[insane_preset-${Lang}]}\n${STATEVARS_PRESET[@]/#/\\n\\t* }"

  # check for prior state-vars, and offer to reset them or to resume instead
  elif grep -vE '(LANG|TR_KEY|XKBMAP|SESSION)' ./.session_state &> /dev/null &&
       ! (( $( WizardDlg "${TR[dlg_preconfig-${Lang}]}"            \
                         --yesno "${TR[preconfig-${Lang}]}" 0 0 ) ))
  then truncate --size=0 ./.session_state
  fi

  # sanity checks
  while ! AreConfiguredStateVars 'LANG' 'TR_KEY' 'XKBMAP'
  do    SetStateVar 'SESSION' 'wizard' ; ./session-init.sh ;
  done
  SetLang $(GetStateVar 'TR_KEY')

  # detect isorepo and internet
  local has_isorepo=$( [[ -d /isorepo && -f /etc/pacman-offline.conf ]] && echo 1 || echo 0 )
  local is_online
  ( # display fake progress-bar while curl is fetching
    set -o pipefail
    [[ "$( ${CURL_CMD[@]} )" == 200 ]]                              |
    ( while (( t < 99 )) && sleep 0.1 ; do echo $((++t)) ; done ; ) |
    WizardDlg "" --gauge "${TR[online-${Lang}]} ...." 0 70
  ) && is_online=$(( ! $? )) || is_online=$(( ! $? ))

  # setup state to prompt for online or offline install, as appropriate;
  # or quit if install can not continue
  if   (( has_isorepo & is_online )) ; then SetStateVar 'INSTALL' 'prompt'  ;
  elif (( has_isorepo             )) ; then SetStateVar 'INSTALL' 'offline' ;
  elif (( is_online               )) ; then SetStateVar 'INSTALL' 'online'  ;
  else ExitFail "${TR[offline-${Lang}]}"
  fi
}

InitKeyring()
{
  # install latest keyring, if possible
  pacman -Sy --noconfirm --needed ${KEYRINGS[$(uname -m)]}
}

NoticeCustomize()
{
  # checks to skip this notice per preset or partial config
  AreConfiguredStateVars ${STATEVARS_WMDE[*]}   && # SelectDefaults()
  AreConfiguredStateVars ${STATEVARS_ENV[*]}    && # SelectDefaults()
  AreConfiguredStateVars ${STATEVARS_LOGINS[*]} && # SelectLogins()
  AreConfiguredStateVars ${STATEVARS_BASE[*]}   || # SelectDefaults(), SelectBase()
  WizardDlg "${TR[dlg_customize-${Lang}]}" --msgbox "${TR[notice_customize-${Lang}]}" 0 0
}

SelectDefaults()
{
# MOCK_SELECTDEFAULTS ; return 0 ;    # bypass SelectBase, SelectWmde, SelectEnv
# MOCK_SELECTDEFAULTS_NO ; return 0 ; # exercise SelectBase, SelectWmde, SelectEnv

  # checks to skip this stage per preset or partial config
  ! AreConfiguredStateVars ${STATEVARS_DEFAUILTS[*]} || return 0

  # prompt to select the default installation configuration
  # (bypasses many other prompts)
  if   (( $( WizardDlg "${TR[dlg_defaults-${Lang}]}"            \
                       --yesno "${TR[defaults-${Lang}]}" 0 0 ) ))
  then [[ "$(GetStateVar 'INSTALL')" == 'prompt' ]] && \
       SetStateVar 'INSTALL'  ${DEF_INSTALL}
       SetStateVar 'BASE'     ${DEF_PKG_SET}
       SetStateVar 'INIT'     ${DEF_INIT}
       SetStateVar 'WMDE'     ${DEF_WMDE}
       SetStateVar 'HOSTNAME' ${DEF_HOSTNAME}
       SetStateVar 'TIMEZONE' ${DEF_TIMEZONE}
       SetStateVar 'KEYMAP'   $(GetStateVar 'XKBMAP')
       SetStateVar 'LOCALES'  $(GetStateVar 'LANG'  )
  fi
#
#   local base=$( DIALOGOPTS='--default-item desktop'                     \
#                 WizardDlg "${TR[dlg_defaults-${Lang}]}"                 \
#                           --menu "${TR[defaults-${Lang}]}" 20 70 50     \
#                           'standard' "${TR[defaults_standard-${Lang}]}" \
#                           'desktop'  "${TR[defaults_desktop-${Lang}]}"  \
#                           'custom'   "${TR[defaults_custom-${Lang}]}"   )
#   [[ -n "${base}" ]] || exit
#
#   if   [[ "${base}" != 'custom' ]]
#   then local wmde=$([[ "${base}" == 'standard' ]] && echo 'cli' || echo 'lxde')
}

_SetPass() # ("dlg_title" "prompt1" "prompt2") - assumes: $pass , sets: $pass
{
  local dlg_title="$1"
  local prompt1="$2"
  local prompt2="$3"
  local pass2='in valid' ; pass='contains space' ;
  local err_msg=''

  until [[ "${pass}" == "${pass2}" ]]
  do    pass='in valid' pass2='contains space'
        until ValidateId "${pass}"
        do    pass=$( WizardDlg "${dlg_title}"  --passwordbox "${err_msg}${prompt1}" 8 56 )
              [[ -n "${pass}"  ]] || exit
        done
        until ValidateId "${pass2}"
        do    pass2=$( WizardDlg "${dlg_title}" --passwordbox "${prompt2}" 8 56 )
              [[ -n "${pass2}" ]] || exit
        done
        err_msg="\Z1${TR[pass_err-${Lang}]}\Zn\n"
  done
}

SelectLogins()
{
# MOCK_SELECTLOGINS ; return 0 ;

  # checks to skip this stage per preset or partial config
  ! AreConfiguredStateVars ${STATEVARS_LOGINS[*]} || return 0

  # prompt for root user password
  local pass
  _SetPass "${TR[dlg_root-${Lang}]}" "${TR[root_pass-${Lang}]}" "${TR[root_pass2-${Lang}]}"
  SetStateVar 'ROOT_PASS' $(openssl passwd ${pass})

# MOCK_SELECTLOGINS_USER ; return 0 ;

  # prompt to create an unprivileged user login
  local dlg_title="${TR[dlg_user-${Lang}]}"
  if ! (( $( WizardDlg "${dlg_title}" --yesno "${TR[user-${Lang}]}" 8 56 ) ))
  then SetStateVar 'USER_LOGIN' 'NONE' # flag no user creation
       SetStateVar 'USER_PASS'  'NONE' # flag no user creation
       return 0
  fi

  # prompt for unprivileged user login
  local login=''
  dlg_title="${dlg_title/*) /}" # trim "(Optional) "
  until ValidateId "${login}"
  do    login=$( WizardDlg "${dlg_title}"                                           \
                           --inputbox "${TR[user_login-${Lang}]}" 8 56 ${login/ */} )
        [[ -n "${login}" ]] || exit
  done

  # prompt for unprivileged user password
  _SetPass "${dlg_title}" "${TR[user_pass-${Lang}]}" "${TR[user_pass2-${Lang}]}"
  SetStateVar 'USER_LOGIN' ${login}
  SetStateVar 'USER_PASS'  $(openssl passwd ${pass})
}

SelectBase()
{
# MOCK_SELECTBASE ; return 0 ;

  # checks to skip this stage per preset config, partial config, or SelectDefaults()
  ! AreConfiguredStateVars ${STATEVARS_BASE[*]} || return 0

  # prompt for local vs remote package repos, if possible
  local install=$( [[ "$(GetStateVar 'INSTALL')" != 'prompt' ]]          && \
                   echo $(GetStateVar 'INSTALL')                         || \
                   WizardDlg "${TR[dlg_base-${Lang}]}"                      \
                             --menu    "${TR[install-${Lang}]}" 20 70 50    \
                             'offline' "${TR[install_offline-${Lang}]}"     \
                             'online'  "${TR[install_online-${Lang}]}"      )
  [[ -n "${install}" ]] && SetStateVar 'INSTALL' ${install} || exit

  # prompt for base package set
  local base=$( DIALOGOPTS='--default-item standard'                \
                WizardDlg "${TR[dlg_base-${Lang}]}"                 \
                          --menu     "${TR[base-${Lang}]}" 20 70 50 \
                          'base'     "${TR[base_mini-${Lang}]}"     \
                          'standard' "${TR[base_posix-${Lang}]}"    )
  [[ -n "${base}" ]] && SetStateVar 'BASE' ${base} || exit

  # prompt for init-system
  local init=$( DIALOGOPTS='--default-item systemd'                \
                WizardDlg "${TR[dlg_base-${Lang}]}"                \
                          --menu    "${TR[init-${Lang}]}" 20 70 50 \
                          'openrc'  "${TR[init_openrc-${Lang}]}"   \
                          'systemd' "${TR[init_systemd-${Lang}]}"  )
  [[ -n "${init}" ]] && SetStateVar 'INIT' ${init} || exit
}

NoticeOptional()
{
  # checks to skip this notice per preset or partial config
  AreConfiguredStateVars ${STATEVARS_BOOT[*]} && # SelectBoot()
  AreConfiguredStateVars ${STATEVARS_WMDE[*]} && # SelectWmde()
  AreConfiguredStateVars ${STATEVARS_ENV[*]}  || # SelectEnv()
  WizardDlg "${TR[dlg_optional-${Lang}]}" --msgbox "${TR[notice_optional-${Lang}]}" 0 0
}

SelectBoot()
{
# MOCK_SELECTBOOT ; return 0 ;

  # checks to skip this stage per preset or partial config
  ! AreConfiguredStateVars ${STATEVARS_BOOT[*]} || return 0

  # prompt to install GRUB
  (( $( WizardDlg "${TR[dlg_boot-${Lang}]}"                  \
                  --yesno "${TR[grub-${Lang}]}" 8 56 ) )) && \
  SetStateVar 'BOOT' 'grub'                               || \
  SetStateVar 'BOOT' 'none'
}

SelectWmde()
{
# MOCK_SELECTWMDE ; return 0 ;

  # checks to skip this stage per preset config, partial config, or SelectDefaults()
  ! AreConfiguredStateVars ${STATEVARS_WMDE[*]} || return 0

  # prompt to install a graphical environment
  if   (( $( WizardDlg "${TR[dlg_wmde-${Lang}]}"            \
                       --yesno "${TR[gui-${Lang}]}" 8 56 ) ))
  then # prompt for WM/DE
       local wmde=$( DIALOGOPTS='--default-item lxde'                \
                     WizardDlg "${TR[dlg_wmde-${Lang}]}"             \
                               --menu "${TR[wmde-${Lang}]}" 20 70 50 \
                               'cli'  "${TR[wmde_cli-${Lang}]}"      \
                               'lxde' "${TR[wmde_lxde-${Lang}]}"     \
                               'mate' "${TR[wmde_mate-${Lang}]}"     )
       [[ -n "${wmde}" ]] || exit
       SetStateVar 'WMDE' ${wmde}
  else SetStateVar 'WMDE' 'cli'
  fi
}

SelectEnv()
{
# MOCK_SELECTENV ; return 0 ;

  # checks to skip this stage per preset config, partial config, or SelectDefaults()
  ! AreConfiguredStateVars ${STATEVARS_ENV[*]} || return 0

  local curr_msg="${TR[option_current-${Lang}]}"
  local host_lang=$(GetStateVar 'LANG')

  # prompt for hostname, if not set per SelectDefaults()
  local hostname=''
  local display_host
  until ValidateId "${hostname}"
  do    display_host=$( [[ -n "${hostname}" ]] && echo ${hostname/ */}  || \
                                                  echo ${DEF_HOSTNAME}     )
        hostname=$( WizardDlg "${TR[dlg_env-${Lang}]}"                  \
                              --inputbox "${TR[hostname-${Lang}]}" 8 56 \
                              ${display_host}                           )
        [[ -n "${hostname}" ]] || exit
  done
  SetStateVar 'HOSTNAME' ${hostname}

  # prompt for timezone, if not set per SelectDefaults()
  declare -A zoneinfos=()
  local region
  local timezones=()
  local timezone=''
  if   [[ -z "$(GetStateVar 'TIMEZONE')" ]]
  then eval $(cd /usr/share/zoneinfo/ &> /dev/null                          ; \
              find * -mindepth 1 -maxdepth 1 -type f                          \
                     -regextype egrep -not -regex '(Etc|posix|right)/.*'      \
                     -printf 'zoneinfos[%h]+=%h/%f, ' | sort --version-sort ; )
       if region=$( DIALOGOPTS='--no-items'                             \
                    WizardDlg "${TR[dlg_env-${Lang}]}"                  \
                              --menu "${TR[timezone-${Lang}]}" 20 70 50 \
                              UTC                                       \
                              ${!zoneinfos[@]}                          )
       then if [[ "${region}" == 'UTC' ]]
            then timezone='UTC'
            else timezones=( ${zoneinfos[${region}]//,/ } )
                 timezones=( $(paste -d ' ' <(printf "%s\n" ${timezones[*]}    | sort) \
                                            <(printf "%s\n" ${timezones[*]#*/} | sort) ) )
                 timezone=$( DIALOGOPTS='--no-tags'                              \
                             WizardDlg "${TR[dlg_env-${Lang}]}"                  \
                                       --menu "${TR[timezone-${Lang}]}" 20 70 50 \
                                       ${timezones[@]}                           )
            fi
       fi
       [[ -n "${timezone}" ]] && SetStateVar 'TIMEZONE' ${timezone} || exit
  fi

  # prompt for console keymap
  local keymap
  local keymaps=()
  for keymap in $(sort -u ./KEYMAPS_VT)
  do  if   [[ ${keymap} =~ ^$(GetStateVar 'XKBMAP' 'us') ]]
      then keymaps=(  "${keymap}" "${curr_msg}" "${keymaps[@]}" )
      else keymaps+=( "${keymap}" ''                            )
      fi
  done
  keymap=$( WizardDlg "${TR[dlg_env-${Lang}]}"                \
                      --menu "${TR[keymap-${Lang}]}" 20 70 50 \
                      "${keymaps[@]}"                         )
  [[ -n "${keymap}" ]] && SetStateVar 'KEYMAP' ${keymap} || exit

  # prompt for locales
  local all_locales=()
  local locale
  local display_locales=()
  local locales
  mapfile -t all_locales < <(sed '1,23d ; s|^#||' /etc/locale.gen)
  for locale in "${all_locales[@]}"
  do  if   [[ ${locale} =~ ^${host_lang} ]]
      then display_locales=(  ${locale} 'on' "${display_locales[@]}" )
      else display_locales+=( ${locale} 'off'                        )
      fi
  done
  locales=$( WizardDlg "${TR[dlg_env-${Lang}]}"                      \
                       --checklist "${TR[locales-${Lang}]}" 20 70 50 \
                       ${display_locales[@]}                         )
  [[ "${locales}" != "${CANCEL_SENTINEL}" ]] && SetStateVar 'LOCALES' "${locales}" || exit

  # prompt for language
  local language
  local languages=()
  for language in ${locales}
  do  if   [[ ${language} == ${host_lang} ]]
      then languages=(  "${language}" "${curr_msg}" "${languages[@]}" )
      else languages+=( "${language}" ''                              )
      fi
  done
  language=$( DIALOGOPTS="--default-item ${host_lang}"            \
              WizardDlg "${TR[dlg_env-${Lang}]}"                  \
                        --menu "${TR[language-${Lang}]}" 20 70 50 \
                        'POSIX' ''                                \
                        "${languages[@]}"                         )
  [[ -n "${language}" ]] && SetStateVar 'LANG' ${language} || exit
}

NoticePartition()
{
  # checks to skip this notice per preset config
  AreConfiguredStateVars ${STATEVARS_PARTITION[*]} 'READY' || # SelectPartition()
  WizardDlg "${TR[dlg_partition-${Lang}]}" --msgbox "${TR[notice_partition-${Lang}]}" 0 0
}

SelectPartition()
{
  # checks to skip this stage per preset config
  # NOTE: The user must select all partitioning options,
  #       if the 'READY' state-var is not set, even upon a resumed run.
  if ! AreConfiguredStateVars ${STATEVARS_PARTITION[*]} 'READY'
  then # reset partitioning state (unnecessary, but pedantic)
       SetStateVar 'DEVICE'      ; SetStateVar 'ROOT_PART' ;
       SetStateVar 'DEVICE_N'    ; SetStateVar 'BOOT_PART' ;
       SetStateVar 'PART_METHOD' ; SetStateVar 'HOME_PART' ;
  else return 0
  fi

  # query disk device information
  PopulateDisksOptions # populates $DlgParams
  (( ${#DlgParams[@]} )) || ExitFail "${TR[device_none-${Lang}]}"

  # prompt for disk device
  local device_n=$( WizardDlg "${TR[dlg_part-${Lang}]}"               \
                              --menu "${TR[device-${Lang}]}" 20 70 50 \
                              "${DlgParams[@]}" )
  local device=$( GetDevice $(( ${device_n} - 1 )) )
  [[ -n "${device_n}" ]] && SetStateVar 'DEVICE_N' ${device_n} || exit
  [[ -n "${device}"   ]] && SetStateVar 'DEVICE'   ${device}   || exit

  # prompt for partitioner
  local part_method=$( WizardDlg "${TR[dlg_part-${Lang}]}"               \
                                 --menu   "${TR[part-${Lang}]}" 20 70 50 \
                                 'auto'   "${TR[part_auto-${Lang}]}"     \
                                 'manual' "${TR[part_man-${Lang}]}"      )
  [[ -n "${part_method}" ]] && SetStateVar 'PART_METHOD' ${part_method} || exit
}

_FormatAndMount()
{
  declare -A partitions=( [Root]=$(GetStateVar 'ROOT_PART') \
                          [Boot]=$(GetStateVar 'BOOT_PART') \
                          [Home]=$(GetStateVar 'HOME_PART') )
  local partition_role
  local partition
  local dlg_msg

  # sanity checks
  AreConfiguredStateVars 'ROOT_PART'

  # format un-formatted partitions or re-format formatted partiions
  for partition_role in ${!partitions[@]}
  do  partition=${partitions[${partition_role}]}
      [[ -n "${partition}" ]] || continue

      # let's be paranoid about this (also, so pipefail is not needed below)
      # GetDisksPartsData() has ensured that /mnt exists and nothing is mounted on it
      blkid ${partition} > /dev/null || ExitFail "${TR[part_error-${Lang}]}"

      # determine whether each target partition is already formatted
      if   blkid ${partition} | grep ' TYPE="' &> /dev/null
      then mount ${partition} /mnt
           if   [[ -n "$(ls --almost-all /mnt)" ]]
           then dlg_msg="${partition_role} partiion: ${partition} ${TR[reformat-${Lang}]}"
           fi
           umount ${partition}
      else dlg_msg="${partition_role} partiion: ${partition} ${TR[format-${Lang}]}"
      fi

      # prompt to re-format formatted partiion or format un-formatted partition
      if   (( $( WizardDlg "${TR[dlg_part-${Lang}]}" --yesno "${dlg_msg}" 20 70 ) ))
      then yes | mkfs.ext4 -q ${partition}
      fi
ls -a /mnt
      # final verification
      blkid ${partition} | grep ' TYPE="' > /dev/null || ExitFail "${TR[part_error-${Lang}]}"
  done

  # mount partitions
  if [[ -n "${partitions[Root]}" ]] ; then mkdir -p                  /mnt      ;
                                           mount ${partitions[Root]} /mnt      ; fi ;
  if [[ -n "${partitions[Boot]}" ]] ; then mkdir -p                  /mnt/boot ;
                                           mount ${partitions[Boot]} /mnt/boot ; fi ;
  if [[ -n "${partitions[Home]}" ]] ; then mkdir -p                  /mnt/home ;
                                           mount ${partitions[Home]} /mnt/home ; fi ;
}

PartitionManual()
{
  # sanity checks
  AreConfiguredStateVars 'DEVICE' 'DEVICE_N'

  # manual partitioning
  local device=$(GetStateVar 'DEVICE')
  if   [[ -n "${DISPLAY}" ]] && which gparted &> /dev/null ; then gparted ${device} ;
  elif                          which cfdisk  &> /dev/null ; then cfdisk  ${device} ;
  fi

  # query partition information
  local device_n=$(GetStateVar 'DEVICE_N')
  PopulatePartsData $(( ${device_n} )) # populates $PartsData
  PopulatePartOptions                  # populates $DlgParams
  (( ${#DlgParams[@]} )) || ExitFail "${TR[part_none-${Lang}]}"

  # prompt for root partition
  local part_n=$( WizardDlg "${TR[dlg_part-${Lang}]}"                   \
                            --menu "${TR[mount_root-${Lang}]}" 20 70 50 \
                            "${DlgParams[@]}"                           )
  if   [[ -n "${part_n}" ]]
  then SetStateVar 'ROOT_PART' ${device}${part_n}
       RemovePartOption ${part_n}
  else exit
  fi

  # prompt to mount additional partitions
  local dlg_opts="--ok-label '${TR[yes-${Lang}]}' --cancel-label '${TR[no-${Lang}]}'"
  if   (( ${#DlgParams[@]} ))
  then part_n=$( DIALOGOPTS="${dlg_opts}"                                      \
                 WizardDlg "${TR[dlg_part-${Lang}]}"                           \
                           --menu         "${TR[mount_boot-${Lang}]}" 20 70 50 \
                           "${DlgParams[@]}"                                   )
       if   [[ -n "${part_n}" ]]
       then SetStateVar 'BOOT_PART' ${device}${part_n}
            RemovePartOption ${part_n}
       fi
  fi
  if   (( ${#DlgParams[@]} ))
  then part_n=$( DIALOGOPTS="${dlg_opts}"                                      \
                 WizardDlg "${TR[dlg_part-${Lang}]}"                           \
                           --menu         "${TR[mount_home-${Lang}]}" 20 70 50 \
                           "${DlgParams[@]}"                                   )
      if   [[ -n "${part_n}" ]]
      then SetStateVar 'HOME_PART' ${device}${part_n}
           RemovePartOption ${part_n}
      fi
  fi

  _FormatAndMount
}

NoticeReady()
{
  # checks to skip this stage per preset config, or if manually partitioning
  ! AreConfiguredStateVars 'READY' || return 0

  local ready_msg="${TR[notice_ready-${Lang}]}"

  if   [[ "$(GetStateVar 'PART_METHOD')" == 'auto' ]]
  then ready_msg+="\n\n\Z1${TR[ready_warn-${Lang}]}\Zn\n\n  * $(GetStateVar 'DEVICE')"
  fi

  (( $( WizardDlg "${TR[dlg_ready-${Lang}]}" --yesno "${ready_msg}" 0 0 ) )) || exit
}

PartitionAuto()
{
  # sanity checks
  AreConfiguredStateVars 'DEVICE'

  # checks to skip this stage per preset config
  # NOTE: this is the only path for preset unattended-install configurations
  #       it currently supports only a single disk (root and swap partitions)
  if   [[ -n "$(GetStateVar 'ROOT_PART')" ]]
  then SetStateVar 'BOOT_PART' ''
       SetStateVar 'HOME_PART' ''
       return 0
  fi

  # automatic partitioning
  local device=$(GetStateVar 'DEVICE')
  local offset_mb=1
  local root_part_n=1
  local swap_part_n=2
  local root_type_n=83
  local swap_type_n=82
  SetStateVar 'ROOT_PART' ${device}${root_part_n}

  # create partition table
  parted -s ${device} -- mklabel msdos

  # configure partitions
  parted -s ${device} -- mkpart primary  ${offset_mb}MiB -${SWAP_MB}MiB # $root_part_n
  parted -s ${device} -- mkpart primary -${SWAP_MB}MiB   -1s            # $swap_part_n
  parted -s ${device} -- set ${root_part_n} boot on

  # format partitions
  format() # (device part_n type_n)
  {
    sleep 0.5 ; partprobe ;
    (echo t ; echo ${part_n} ; echo ${type_n} ; echo w) | fdisk ${device}
  }
  format ${device} ${root_part_n} ${root_type_n} &> /dev/null
  format ${device} ${swap_part_n} ${swap_type_n} &> /dev/null
  yes | mkfs.ext4 ${device}${root_part_n}        &> /dev/null
  mkswap          ${device}${swap_part_n}        &> /dev/null

  _FormatAndMount
}

InstallPkgs()
{
  # sanity checks
  AreConfiguredStateVars ${STATEVARS_BASE[*]} ${STATEVARS_BOOT[*]} ${STATEVARS_WMDE[*]}

  local init=$(      GetStateVar 'INIT'   )
  local base_set=$(  GetStateVar 'BASE'   )
  local bootloader=$(GetStateVar 'BOOT'   )
  local wmde=$(      GetStateVar 'WMDE'   )
  local install=$(   GetStateVar 'INSTALL')
  local conf_regex=$( [[ "${init}" == 'openrc'  ]] && echo "$PACCONF_NONSYSTEMD_REGEX" || \
                                                      echo "$PACCONF_SYSTEMD_REGEX"       )
  local packages=( ${BASE_PACKAGES[${base_set}]}   \
                   ${BOOT_PACKAGES[${bootloader}]} \
                   ${GUI_PACKAGES[${wmde}]}        )

  # prepare pacman.conf for online/offline install, systemd/non-systemd
  if   [[ "${install}" == 'offline'  ]]
  then cp /etc/pacman-offline.conf ./pacman-wizard.conf
  else cp ./pacman.conf            ./pacman-wizard.conf
       sed -i "$conf_regex"        ./pacman-wizard.conf
  fi

  # install packages
  (
    set -o pipefail
    pacstrap -C ./pacman-wizard.conf /mnt ${packages[*]} 2>&1 | \
    WizardDlg "${TR[dlg_pacstrap-${Lang}]}" --progressbox -1 -1
  ) || ! echo -e "\n${TR[pacstrap_fail-${Lang}]}" || exit 1
}

ConfigChroot()
{
  # sanity checks
  AreConfiguredStateVars ${STATEVARS_CHROOTENV[*]}      # chroot-environment.sh
  AreConfiguredStateVars ${STATEVARS_CHROOTLOGIN[*]}    # chroot-login.sh
  AreConfiguredStateVars ${STATEVARS_CHROOTSERVICES[*]} # chroot-services.sh

  # copy helper scripts into the chroot and execute them in-chroot
  install -m100 ./chroot-environment.sh /mnt/root/
  install -m100 ./chroot-login.sh       /mnt/root/
  install -m100 ./chroot-services.sh    /mnt/root/
  install -m400 ./.session_state        /mnt/root/
  install -m400 ./session-common.sh.inc /mnt/root/
  genfstab -p /mnt                   >> /mnt/etc/fstab
  arch-chroot /mnt /root/chroot-environment.sh # configure environment
  arch-chroot /mnt /root/chroot-login.sh       # register login
  arch-chroot /mnt /root/chroot-services.sh    # configure services
}

NoticeDone() { WizardDlg "" --msgbox "${TR[notice_done-${Lang}]}" 8 56 ; }

Cleanup()
{
  # clean-up in-chroot config scripts and unmount partitions
  rm /mnt/root/chroot-environment.sh &> /dev/null || true
  rm /mnt/root/chroot-login.sh       &> /dev/null || true
  rm /mnt/root/chroot-services.sh    &> /dev/null || true
  rm /mnt/root/.session_state        &> /dev/null || true
  rm /mnt/root/session-common.sh.inc &> /dev/null || true
  UnmountAll
}


## error logging ##

set -o errexit -o errtrace
trap 'trap - ERR           ; LogError() { : ; } ; Cleanup ;'                  EXIT INT TERM
trap 'trap - EXIT INT TERM ; LogError "${FUNCNAME[0]}" ${LINENO} ; Cleanup ;' ERR


## main entry ##

## essential non-interactive non-destructive steps ##
NoticeIntro     # Explain that no changes will be made until the final stage
Init            # Check for isorepo existence and internet connection
InitKeyring     # Initialize pacman keyring

## essential interactive non-destructive steps ##
NoticeCustomize # Explain that the next decisions are mandatory, but interchangeable
SelectDefaults  # Choose to install the standard system, or to customize
SelectLogins    # Set root password and decide to register unprivileged login user
SelectBase      # Select base package-set                            (bypassed per SelectDefaults)

## non-essential interactive non-destructive steps ##
NoticeOptional  # Explain that the next decisions are non-essential
SelectBoot      # Choose to install bootloader
SelectWmde      # Choose to install graphical environment            (bypassed per SelectDefaults)
SelectEnv       # Set environment parameters for the target system   (bypassed per SelectDefaults)

## essential interactive destructive steps ##
NoticePartition # Explain that the next decisions are mandatory, and destructive
SelectPartition # Choose to partition manually or defer to automatic
PartitionManual # Partition and mount target disks (if partitioning manually)

## essential non-interactive destructive steps ##
NoticeReady     # Display chosen options and offer last chance to bail
PartitionAuto   # Partition and mount target disks (if partitioning automatically)
InstallPkgs     # Install packages with pacstrap
ConfigChroot    # Generate fstab, register login, configure services (in-chroot)
NoticeDone      # Yay!!!
Cleanup         # Clean-up and un-mount the chroot

exit