2021-04-20 20:18:55 +00:00
#!/bin/bash
2021-04-21 12:23:55 +00:00
set -o pipefail
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
2021-04-20 20:18:55 +00:00
################################################
2021-04-21 13:04:56 +00:00
# Initialize script environment
# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895)
function get_source_dir() {
local source="${BASH_SOURCE[0]}"
while [[ -h $source ]]
do
local tmp="$(cd -P "$(dirname "${source}")" && pwd)"
source="$(readlink "${source}")"
[[ $source != /* ]] && source="${tmp}/${source}"
done
echo -n "$(realpath "$(dirname "${source}")")"
}
GENTOO_INSTALL_REPO_DIR="$(get_source_dir)"
cd "$GENTOO_INSTALL_REPO_DIR" || { echo "Could not change into directory '$GENTOO_INSTALL_REPO_DIR'"; exit 1; }
2021-04-20 20:18:55 +00:00
# TODO check install dialog
echo "Please install dialog on your system to use the configurator"
################################################
# Configuration storage
2021-04-21 13:04:56 +00:00
SAVE_AS_FILENAME="${1-"gentoo.conf"}"
if [[ -e "$SAVE_AS_FILENAME" ]]; then
# Load settings
UNSAVED_CHANGES=false
source "$SAVE_AS_FILENAME"
else
# Default settings
UNSAVED_CHANGES=true
2021-04-21 12:23:55 +00:00
2021-04-21 13:04:56 +00:00
HOSTNAME="gentoo"
# TODO get from current system
TIMEZONE="Europe/London"
KEYMAP="us"
KEYMAP_INITRAMFS="$KEYMAP"
LOCALES=""
LOCALE="C.utf8"
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
GENTOO_MIRROR="https://mirror.eu.oneandone.net/linux/distributions/gentoo/gentoo"
GENTOO_ARCH="amd64"
STAGE3_BASENAME="stage3-$GENTOO_ARCH-systemd"
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
SELECT_MIRRORS=true
SELECT_MIRRORS_LARGE_FILE=false
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
INIT_SYSTEM=systemd
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
ADDITIONAL_PACKAGES=("app-editors/neovim")
INSTALL_SSHD=true
2021-04-20 20:18:55 +00:00
2021-04-21 13:04:56 +00:00
ROOT_SSH_AUTHORIZED_KEYS=""
fi
2021-04-20 20:18:55 +00:00
################################################
2021-04-21 12:23:55 +00:00
# Menu definition
2021-04-20 20:18:55 +00:00
2021-04-21 12:23:55 +00:00
MENU_ITEMS=(
"HOSTNAME"
"TIMEZONE"
"KEYMAP"
"LOCALE"
"INIT_SYSTEM"
"KEYFILE"
)
2021-04-20 20:18:55 +00:00
2021-04-21 12:23:55 +00:00
function HOSTNAME_tag() { echo "Hostname"; }
function HOSTNAME_label() { echo "($HOSTNAME)"; }
2021-04-21 13:04:56 +00:00
function HOSTNAME_help() { echo "Enter the desired system hostname here, be aware that when creating mdadm raid arrays, this value will be recorded in metadata block. If you change it later, you should also update the metadata."; }
2021-04-21 12:23:55 +00:00
function HOSTNAME_menu() {
local sel
sel="$(dialog --clear \
--help-button --help-label "Menu" \
--ok-label "Next" --cancel-label "Exit" \
--extra-button --extra-label "Back" \
--title "Select hostname" \
--inputbox "Enter the hostname for your new system." \
8 72 "$HOSTNAME" 3>&2 2>&1 1>&3 3>&-)"
UNSAVED_CHANGES=true
}
function TIMEZONE_tag() { echo "Timezone"; }
function TIMEZONE_label() { echo "($TIMEZONE)"; }
2021-04-21 13:04:56 +00:00
function TIMEZONE_help() { echo "The timezone for the new system."; }
function TIMEZONE_menu() {
true
}
2021-04-21 12:23:55 +00:00
function KEYMAP_tag() { echo "Keymap"; }
function KEYMAP_label() { echo "($KEYMAP)"; }
2021-04-21 13:04:56 +00:00
function KEYMAP_help() { echo "The default vconsole keymap for the system."; }
2021-04-21 12:23:55 +00:00
function KEYMAP_menu() {
2021-04-20 20:18:55 +00:00
local items=()
local map
for map in $(find /usr/share/keymaps/ /usr/share/kbd/keymaps/ -type f -iname '*.map.gz' -printf "%f\n" 2>/dev/null | sort -u); do
map="${map%%.map.gz}"
if [[ $map == $KEYMAP ]]; then
items+=("${map}" "off")
else
items+=("${map}" "off")
fi
done
2021-04-21 12:23:55 +00:00
local sel
sel="$(dialog --clear \
--help-button --help-label "Menu" \
--ok-label "Next" --cancel-label "Exit" \
--extra-button --extra-label "Back" \
--noitem \
--title "Select keymap" \
--radiolist "Select which keymap to use in the vconsole." \
16 72 8 "${items[@]}" 3>&2 2>&1 1>&3 3>&-)"
2021-04-20 20:18:55 +00:00
}
2021-04-21 12:23:55 +00:00
function LOCALE_tag() { echo "Locale"; }
function LOCALE_label() { echo "($LOCALE)"; }
2021-04-21 13:04:56 +00:00
function LOCALE_help() { echo "The locale to set for the new system. Be careful, the available options deviate from the corresponding names in the list of locales which is used by locale-gen. For example the locale 'en_US.utf8' is called 'en_US.UTF-8' in /etc/locale.gen). Use the name as shown in `eselect locale` here."; }
function LOCALE_menu() {
true
}
2021-04-20 20:18:55 +00:00
2021-04-21 12:23:55 +00:00
function INIT_SYSTEM_tag() { echo "Init system"; }
function INIT_SYSTEM_label() { echo "($INIT_SYSTEM)"; }
2021-04-21 13:04:56 +00:00
function INIT_SYSTEM_help() { echo ""; }
function INIT_SYSTEM_menu() {
true
}
2021-04-20 20:18:55 +00:00
2021-04-21 12:23:55 +00:00
function KEYFILE_tag() { echo "Key file"; }
function KEYFILE_label() { echo "($KEYFILE)"; }
2021-04-21 13:04:56 +00:00
function KEYFILE_help() { echo ""; }
function KEYFILE_menu() {
true
}
2021-04-20 20:18:55 +00:00
2021-04-21 12:23:55 +00:00
################################################
# Menu functions
# $1: filename
function save() {
echo save to "$1"
2021-04-21 13:04:56 +00:00
cat > "$1" <<EOF
# vim: set ft=sh ts=4 sw=4 sts=-1 noet:
# This file will be interpreted by /bin/bash.
################################################
# Disk configuration
create_btrfs_raid_layout swap=8GiB luks=true /dev/sdX
luks_getkeyfile() {
case "\$1" in
#'my_luks_partition') echo -n '/path/to/my_luks_partition_keyfile' ;;
*) echo -n "/path/to/luks-keyfile" ;;
esac
}
################################################
# System configuration
HOSTNAME=${HOSTNAME@Q}
TIMEZONE=${TIMEZONE@Q}
KEYMAP=${KEYMAP@Q}
KEYMAP_INITRAMFS=${KEYMAP_INITRAMFS@Q}
LOCALES=${LOCALES@Q}
LOCALE=${LOCALE@Q}
################################################
# Gentoo configuration
GENTOO_MIRROR="https://mirror.eu.oneandone.net/linux/distributions/gentoo/gentoo"
GENTOO_ARCH="amd64"
STAGE3_BASENAME="stage3-$GENTOO_ARCH-systemd"
SELECT_MIRRORS=true
SELECT_MIRRORS_LARGE_FILE=false
SYSTEMD=true
################################################
# Additional (optional) configuration
ADDITIONAL_PACKAGES=("app-editors/neovim")
INSTALL_SSHD=true
ROOT_SSH_AUTHORIZED_KEYS=""
################################################
# Prove that you have read the config
I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=true
EOF
2021-04-21 12:23:55 +00:00
}
function msgbox_help() {
dialog --clear \
--msgbox "$1" \
8 66 3>&2 2>&1 1>&3 3>&-
}
function menu_exit() {
if [[ $UNSAVED_CHANGES == "true" ]]; then
local sel
sel="$(dialog --clear \
--help-button --help-label "Back" \
--yes-label "Save" --no-label "Discard" \
--yesno "Do you want to save your configuration?\n(Press <ESC><ESC>, or choose <Back> to continue gentoo configuration)." \
8 66 3>&2 2>&1 1>&3 3>&-)"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
save "gentoo.conf"
exit 0
elif [[ $diag_exit == 1 ]]; then
# <Discard>
exit 0
else
# Back to menu (<ESC><ESC>, <Back>)
true
fi
else
# Nothing was changed. Exit immediately.
exit 0
fi
}
function menu_save_as() {
local sel
sel="$(dialog --clear \
--ok-label "Save" \
--inputbox "Enter a filename to which this configuration should be saved.\n(Press <ESC><ESC>, or choose <Cancel> to abort)." \
8 66 "$SAVE_AS_FILENAME" 3>&2 2>&1 1>&3 3>&-)"
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Save>
SAVE_AS_FILENAME="$sel"
save "$SAVE_AS_FILENAME"
UNSAVED_CHANGES=false
else
# Back to menu (<ESC><ESC>, <Cancel>)
true
fi
}
function menu() {
2021-04-20 20:18:55 +00:00
local item
2021-04-21 12:23:55 +00:00
local item_tag
2021-04-20 20:18:55 +00:00
local tag_item_list=()
2021-04-21 12:23:55 +00:00
declare -A reverse_lookup
# Create menu list
2021-04-20 20:18:55 +00:00
for item in "${MENU_ITEMS[@]}"; do
2021-04-21 12:23:55 +00:00
item_tag="$("${item}_tag")"
tag_item_list+=("$item_tag" "$("${item}_label")")
reverse_lookup["$item_tag"]="$item"
2021-04-20 20:18:55 +00:00
done
2021-04-21 12:23:55 +00:00
local sel
sel="$(dialog --clear \
--title "Gentoo configuration" \
--extra-button --extra-label "Exit" \
--help-button \
--ok-label "Select" --cancel-label "Save" \
2021-04-21 13:04:56 +00:00
--menu "This is the gentoo configuration menu. Read and adjust all options below carefully. Save your desired configuration and run ./install afterwards. Use the help if you want further information for any item." \
20 72 12 "${tag_item_list[@]}" 3>&2 2>&1 1>&3 3>&-)"
2021-04-21 12:23:55 +00:00
local diag_exit="$?"
if [[ $diag_exit == 0 ]]; then
# <Select>
"${reverse_lookup[$sel]}_menu"
elif [[ $diag_exit == 1 ]]; then
# <Save>
menu_save_as
elif [[ $diag_exit == 2 ]]; then
# <Help>
msgbox_help "$("${reverse_lookup[${sel#HELP }]}_help")"
else
# Exit (<ESC><ESC>, <Exit>)
menu_exit
true
fi
}
2021-04-21 13:04:56 +00:00
# Begin menu loop. Exit will be called to end this loop where it is appropriate.
2021-04-21 12:23:55 +00:00
while true; do
menu
done