#!/usr/bin/bash
#
# Copyright  (C) 2023-2026 Axel Konrad <ak-li@siduction.org>
#                2012-2013 Alf Gaida <agaida@siduction.org>
#                2012      Sebastian Görgen <zeppo@siduction.net>
#                2009-2010 Nikolas Poniros <edhunter@sidux.com>
#
# heavily based on install-gui written by
#	Horst Tritremmel <hjt@users.berlios.de>
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# On Debian GNU/Linux systems, the text of the GPL license can be
# found in /usr/share/common-licenses/GPL.
#
#-----------------------------------------------------------------------

# Short description of the script:
# The script creates the config file ($HOME/.sidconf) needed by the
# fll-installer and then calls the fll-installer to install siduction
# using said config file.
# Exit codes are:
#     0 if successful,
#     1 if user aborts before the config file is written
#     2 if user aborts after config file is written (whenever a user
#       presses OK at an error message it is considered as a user abort
#       even if he has no other choice)

# echo "" >> /root/debug
# echo "$(date +%T) Start cli-installer" >> /root/debug

call_help() {
	echo "Usage: cli-installer -I [interface]"
	echo "Options:"
	echo " -v print program version"
	echo " -h print this help message"
	echo " -I interface"
	echo "		select an interface to use with ssft."
	echo "		options are: text , dialog "
	echo "		Default is dialog if installed"
	exit
}

while getopts vhI: opt; do
	case $opt in
		v)  echo "Version: 0.2.9"; exit;;
		I)  interface="$OPTARG";;
		*)  call_help;;
	esac
done


if [ "$( id -u )" -ne 0 ]; then
	echo >&2 "E: You need to be root to run the script"
	exit 1
fi

# Set up frontend
if [ -f /usr/bin/ssft.sh ]; then
	if [ ! -f /usr/bin/ssft-bak.sh ]; then
		cp /usr/bin/ssft.sh /usr/bin/ssft-bak.sh
		sed -i 's/0 0/24 60/' /usr/bin/ssft.sh
	fi
	. /usr/bin/ssft.sh || exit 1

	[ "$interface" = "text" ] && SSFT_FRONTEND="$interface" || SSFT_FRONTEND="dialog"

	if [ "$SSFT_FRONTEND" = "dialog" ] && [ -z "$(which dialog)" ]; then
		echo "dialog is not installed"
		echo "using text instead"
		SSFT_FRONTEND="text"
	fi
else
	echo "Please install ssft"
	exit 1
fi

# If we use dialog as front-end then redirect all stderr to ERROR_LOG
if [ "$SSFT_FRONTEND" = "dialog" ]; then

	# Log file to write in potential error messages
	ERROR_LOG=$(mktemp /tmp/error_log.XXXXXXXX)

	exec 3>&2

	# Use this so all stderr goes to ERROR_LOG
	exec 2> "$ERROR_LOG"
fi



### DEFINE CONSTANTS ###
#-----------------------------------------------------------------------
HOST_ALLOWED_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# '-' needs to be at the end because of grep
HOST_ALLOWED_CHAR_OTHERS="0123456789.-"
USER_ALLOWED_CHARS="abcdefghijklmnopqrstuvwxyz0123456789.-"
# Dont change the order of the character because it might not work with grep anymore
NAME_NOT_ALLOWED_CHARS="]°\"!§$%&/(){}=?\`´+*~#;:=,><|_^\\[-"

# We use file system UUIDs:
# ESP				c12a7328-f81f-11d2-ba4b-00a0c93ec93b
# XBOOTLDR			bc13c2ff-59e6-4262-a352-b275fd6f7172
# SWAP				0657fd6d-a4ab-43c4-84e5-0933c84b4f4f
# Linux filesystem	0fc63daf-8483-4772-8e79-3d69d8477de4
ESP_UUID="c12a7328-f81f-11d2-ba4b-00a0c93ec93b"
XBOOT_UUID="bc13c2ff-59e6-4262-a352-b275fd6f7172"
# SWAP_UUID="0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" # Not in use


### VARIABLES NEEDED BY THE CLI-INSTALLER
#-----------------------------------------------------------------------
CONFIG_FILE="$HOME/.sidconf"
PARTITION_PROG="cfdisk"
CHECK_IF_HOME_EXISTS="no"
HOME_PARTITION=""
IS_DISK_USB="no"

# Environment for root TTY
# We have to use 'linux', because the output from 'dialog' becomes
# unreadable when using 'xterm'.
term_org="$(echo "$TERM")"
TERM=linux

# Make sure that cancel the cli-installer deletes the config file,
# and resets $TERM and ssft.sh.
trap "rm -f $CONFIG_FILE; TERM=$term_org; mv /usr/bin/ssft-bak.sh /usr/bin/ssft.sh; clear; exit 2" INT HUP QUIT TERM USR1 USR2


if [ -f "$CONFIG_FILE" ]; then
	echo "	ERROR: Config file \"$CONFIG_FILE\" exists. 
	Please remove it and run the script again."
	exit 1
else
	touch "$CONFIG_FILE"
fi

# Create a list of available partitions and mount points
PART_LIST=$(/usr/share/fll-installer-common/disk -p 2>/dev/null \
				| grep -v ",swap," | cut -d "," -f1)
SWAP_PART_LIST=$(blkid -t TYPE=swap -o device | sort)
MNT_POINTS="boot boot/efi efi home opt root tmp usr var "


### VARIABLES NEEDED BY THE CONFIG FILE
#-----------------------------------------------------------------------
FORMAT_HD="yes"
CONNECT="no"
FILESYSTEM=""
ROOT_PARTITION=""
MOUNT_POINT_LIST=""
IGNORE_DISK_SPACE="yes"
AUTO_SWAP="yes"
SWAP_LIST=""
REAL_NAME=""
USERNAME=""
USERPASS=""
ROOTPASS=""
# not using HOSTNAME as its an evn variable
HOST_NAME="siductionbox"
SERVICES_LIST=""
BOOT_LOADER="grub"
BOOT_WHERE=""
AUTOMOUNT_BOOT="no"


### SSFT PURPOSE FUNCTIONS
#-----------------------------------------------------------------------
# Function to call ssft_yesno
# $1 is the title for ssft_yesno
# $2 is the text for ssft_yesno
# returns 0 if yes was selected != 0 otherwise
yesno() {
	ssft_yesno "$1" "$2"
	return $?
}

# Function to display an error message
# Clean up the temp files
# and exit the program
# Be carefull of the exit_code used! Use exit_code = 2 if you dont want
# the config to be deleted
error_msg() {
	title="  ERROR  "
	msg="$1"
	msg="$msg
	
	$ERROR_LOG
	might contain more information"
	exit_code="$2"
	ssft_display_error "$title" "$msg"
	if [ "$exit_code" -ne 2 ]; then
		rm -f "$CONFIG_FILE"
	fi
	mv /usr/bin/ssft-bak.sh /usr/bin/ssft.sh
	clear
	exit "$exit_code"
}

# Function to call ssft_display_message
# $1 is the title for ssft_display_message
# $2 is the text for ssft_display_message
display_msg() {
	ssft_display_message "$1" "$2"
}

# Function to call ssft_select_multiple
# $1 is the title for ssft_select_multiple
# $2 is the text for ssft_select_multiple
# $@ the items list for ssft_select_multiple
# returns the exit code of ssft_select_multiple
select_more() {
	title="$1"
	msg="$2"
	shift 2
	ssft_select_multiple "$title" "$msg" $@
	return "$?"
}

# Function to call ssft_select_single
# $1 is the title for ssft_select_single
# $2 is the text for ssft_select_single
# $@ the items list for ssft_select_single
# returns the exit code of ssft_select_single
select_one() {
	title="$1"
	msg="$2"
	shift 2
	ssft_select_single "$title" "$msg" $@
	return "$?"
}

# Function to request a password with ssft_read_password
# $1 is the message displayed for ssft_read_password
# returns the exit code of ssft_read_password
input_pass() {
	ssft_read_password "  Input Password  " "$1"
	return "$?"
}

# Function to request a text input
# $1 is the title for ssft_read_string
# $2 is the message displayed for ssft_read_string
# returns the exit code of ssft_read_string
input_string() {
	ssft_read_string "$1" "$2"
	return "$?"
}

### GENARAL PURPOSE FUNCTIONS
#-----------------------------------------------------------------------
# Function to call cfdisk_wrapper to format the hard disk
format_disk() {
	title="  Format disk  "
	msg="
	Do you want to partition your hard disk?"
	if yesno "$title" "$msg"; then
		msg="
		Please choose a partitioning program. 
		Available programs are:"
		programs_list="cfdisk fdisk parted"
		SSFT_DEFAULT="$PARTITION_PROG"
		
		if select_one "$title" "$msg" $programs_list; then
			PARTITION_PROG="$SSFT_RESULT"
			msg="
			Please choose a disk to partition.
			Available disks are:"
			partition_list=$(/usr/share/fll-installer-common/disk -d 2>/dev/null)
			
			if select_more "$title" "$msg" $partition_list; then
			check_for_mounted_disks "1"
				for disk in $SSFT_RESULT ; do
					/usr/share/fll-installer-common/cfdisk_wrapper "$PARTITION_PROG" "$disk"
				done
			fi
		fi
	else
		msg="
		You have chosen not to partition your hard disk.
		If you have not at least one partition on the disk the
		installer will not be able to complete the installation
		If you do not have a partition please abort now!

		Press ctrl+C to abort or ENTER to continue"
		display_msg "$title" "$msg"
	fi
}

# Function for timezone selection
set_timezone() {
	initialTZ="$(timedatectl | grep -o "Time zone: .*" | cut -d " " -f 3-)"
	title="  Timezone selection  "
	msg="
	Current timezone is: $initialTZ

	Do you want to change the timezone?"
	if yesno "$title" "$msg"; then
		dpkg-reconfigure tzdata
	else
		msg="
		Keeping current timezone"
		display_msg "$title" "$msg"
	fi
}

# Internet connection test.
check_con() {
	if ping -c 3 siduction.org 2>/dev/null | grep -q "3 received"; then
		CONNECT="yes"
	else
		title="  Internet connection  "
		msg="
		I: No Internet connection!
		
		Would you like to set up a connection?"
		if yesno "$title" "$msg"; then
			clear
			/usr/bin/nmtui
			if ping -c 3 siduction.org 2>/dev/null | grep -q "3 received"; then
			CONNECT="yes"
			fi
		fi
	fi
	if [ "$CONNECT" = "no" ]; then
		msg="
		Continue without Internet connection? "
		if yesno "$title" "$msg"; then
			msg="
			You will then be unable to update the  
			apt source files or install software."
			display_msg "$title" "$msg"
		else
			error_msg "cli-installer canceled in check_con" "1"
		fi
	fi
clear
}

# Function to check the length of a string
# $1 the string
# $2 string type can be "user" "root" "username" "hostname"
# $3 var to check if string is a password
# $4 minimum length of other than password (positive integer)
# $5 the function from where the request comes
length_check() {
	string="$1"
	type="$2"
	is_pass="$3"
	num="$4"
	title="  $5  "
	if [ "$is_pass" = "yes" ]; then
		while [ "${#string}" -lt 6 ]; do
			msg="
			The password is too short!
			It needs to be at least 6 characters long

			Enter a $type password. Will not be echoed"
			if input_pass "$msg"; then
				string="$SSFT_RESULT"
			else
				error_msg "Can not continue without a password" "1"
			fi
		done
	else
		while [ "${#string}" -lt "$num" ]; do
			msg="
			The $type is too short!
			It needs to be at least $num characters long

			Enter $type again."
			if input_string "$title" "$msg"; then
				string="$SSFT_RESULT"
			else
				error_msg "Can not continue without a $type" "1"
			fi
		done
	fi
	case "$type" in
		user)	USERPASS="$string";;
		root)	ROOTPASS="$string";;
		"hostname")	HOST_NAME="$string";;
		username)	USERNAME="$string";;
		*)	REAL_NAME="$string"
	esac
}

# Function to compare 2 passwords
# $1 password 1
# $2 password 2
# $3 password type "user" or "root"
compare_pass() {
	pass1="$1"
	pass2="$2"
	type="$3"
	while [ "$pass1" != "$pass2" ]; do
		msg="
		The passwords do not match
		Enter a $type password. Will not be echoed"
		if input_pass "$msg"; then
			pass1="$SSFT_RESULT"
		else
			error_msg "Can not continue without a $type password" "1"
		fi
		# This line will set ${type}PASS assuming that it passes the length test
		length_check "$pass1" "$type" "yes"

		msg="
		Please enter the password again. Will not be echoed"
		if input_pass "$msg"; then
			pass2="$SSFT_RESULT"
		else
			error_msg "Can not continue without a $type password" "1"
		fi
	done
}

# Function to make sure that all disks are unmounted before
# partition program start and before installation start
# $1 is 1 if config was not written or 2 if config was written
check_for_mounted_disks() {
	/usr/share/fll-installer-common/um_all "check"

	before_config_write="$1"

	exit_status="$?"

	if [ "$exit_status" -eq 1 ]; then
		title="  Unmount Partitions  "
		msg="
		Some partitions are still mounted.
		You'll need to unmount them for
		the installer to continue.
		
		Unmount now?"
		if yesno "$title" "$msg"; then
			/usr/share/fll-installer-common/um_all
			exit_status="$?"
			if [ "$exit_status" -eq 1 ]; then
				if [ "$before_config_write" -eq 1 ]; then
					msg="
					Could not unmount all partitions.
					Aborting.."
				else
					msg="
					Could not unmount all partitions.
					Aborting..

					I: If you choose 'no' the configuration file will
					stay in $CONFIG_FILE!"
				fi
				error_msg "$msg" "$before_config_write"
			else
				display_msg "$title" "Partitions unmounted successfully"
			fi
		else
			if [ "$before_config_write" -eq 1 ]; then
			msg="
			You chose not to unmount the partitions.
			Aborting.."
			else
			msg="
			You chose not to unmount the partitions.
			Aborting..

			I: If you choose 'no' the configuration file will
			stay in $CONFIG_FILE!"
			fi
			error_msg "$msg" "$before_config_write"
		fi
    fi
}

# Functions for determining the properties of hard disks and partitions.
check_gpt() {
	result="no"
	if [ "$(lsblk -n -o PTTYPE "$1")" = "gpt" ] ; then
		result="ok"
	fi
}

check_esp() {
	result="no"
	if [ "$(lsblk -n -o PARTTYPE "$1")" = "$ESP_UUID" ] ; then
		result="ok"
	fi
}

check_vfat() {
	result="no"
	if [ "$(lsblk -n -o FSTYPE -n "$1")" = "vfat" ]; then
		result="ok"
	fi
}

# Function to get the names of disks and partitions from the path.
# E.g.	sda1	from /dev/sda1 (partition)
# or	nvme0n1	from /dev/nvme0n1p3 (disk)
part_name() {
	pattern="$1"
	p_name="${pattern##/*/}"
	d_name="$(sed 's|p\?[0-9]\+$||' <<< "$p_name")"
	
	# d_name and p_name ar identical if a mapper is specified.
	if grep -q "mapper" <<< "$pattern"; then
		d_name="$p_name"
	fi
	
}

# Only for systemd-boot
# Funktion to pick the one partition that
# fits and remove the others from list.
part_pick() {
	if [ "$(wc -l <<< "$found")" -ge 1 ]; then
		for part in $found; do
			part_name "$part"
			if grep -q -v "$d_name" <<< "$ROOT_PARTITION"; then
				found="$(sed /"$d_name"/d <<< "$found")"
			fi
		done
	fi
}

# The function checks whether the selected partition can be used as an ESP.
check_efi() {
	local partition="$1"
	local title_pre="$title"

	if check_gpt "$partition" && [ "$result" = "no" ]; then
		msg_ch_efi_1=" The device does not have a GUID partition table.
		An ESP is not necessary."
		efi_result="1"
		
	elif ! grep -q '[a-z]1$' <<< "$partition"; then
		msg_ch_efi_2=" It is not the first partition on this device."
		efi_result="2"
		
	elif check_esp "$partition" && [ "$result" = "no" ]; then
		msg_ch_efi_3=" The partition type is not set to EFI System."
		efi_result="3"
		
	elif check_vfat "$partition" && [ "$result" = "no" ]; then
		title="  ESP format is incorrect  "
		msg="
		The format for the ESP must be vfat.

		Q: Do you want the program to format $partition?

		VERY IMPORTANT WARNING! 
		This will destroy all data on $partition! "
		if yesno "$title" "$msg"; then
			mkfs.vfat "$partition" &>/dev/null
			efi_result="0"
			title="$title_pre"
		else
			msg_ch_efi_4=" The file system format is not suitable. "
			efi_result="4"
		fi
	else
		efi_result="0"
	fi
}

# The function checks whether the selected partition can be used as an XBOOTLDR.
check_xboot() {
	local partition="$1"

	result="no"
	if [ "$(lsblk -n -o PARTTYPE "$partition")" = "$XBOOT_UUID" ] ; then
		if check_vfat "$partition" && [ "$result" = "no" ]; then
			title="  XBOOTLDR format is incorrect  "
			msg="
			The format for the XBOOTLDR must be vfat. 

			Q: Do you want the program to format $partition? 

			VERY IMPORTANT WARNING! 
			This will destroy all data on $partition! "
			if yesno "$title" "$msg"; then
				mkfs.vfat "$partition" &>/dev/null
				result="ok"
			fi
		else
			result="ok"
		fi
	fi
}

remove_part_point() {
	# Remove the partition name $p_name from the list
	if [ -n "$1" ]; then
		part_name "$1"
		PART_LIST="$(grep -v "$p_name" <<< "$PART_LIST")"
	fi
	
	# Remove the selected mount point from the list of available mount points
	if [ -n "$mnt_point" ]; then
		if grep -q "efi" <<< "$mnt_point"; then
			# The order of the commands is essential
			MNT_POINTS="${MNT_POINTS/boot\/efi /}"
			MNT_POINTS="${MNT_POINTS/efi /}"
		else
			MNT_POINTS="${MNT_POINTS/$mnt_point /}"
		fi
	fi
}

### FUNCTIONS TO GET THE VALUES FOR THE VARIABLES
### THAT WILL BE PASSED LATER TO THE CONFIG FILE
#-----------------------------------------------------------------------

# Function that asks the user whether to enable non-free sources.
non_free_sources() {
	title="  Additional non-free sources  "
	msg="
	Would you also like to use 'non-free' sources?

	Default is 'no'.

I: This allows you to install non-free drivers
	(like nvidia) or proprietary software. It adds
	'contrib' and 'non-free' to your software sources.
	
	This means that you diviate from the debian dfsg
	guidelines."
	options="no yes"
	SSFT_DEFAULT="no"

	select_one "$title" "$msg" $options
	if [ "$SSFT_RESULT" = "yes" ]; then
		dir_pre="$(pwd)"
		cd /etc/apt/sources.list.d/ || exit 1
		
		for sourcefile in debian.sources extra.sources fixes.sources; do
			if grep -q "non-free " "$sourcefile"; then
				continue
			else
				sed -i /Components/'s/main/main contrib non-free/' "$sourcefile"
			fi
		done
		
		if ! grep -q "non-free " dbgsym.sources; then
			sed -i /Components/'s/contrib/contrib non-free/' dbgsym.sources
		fi
		cd "$dir_pre" || exit 1
		
		# Synchronize the package index files from their sources.
		if [ "$CONNECT" = "yes" ]; then
			display_msg "$title" "
			Now the package index files are 
			synchronized from their sources."
			clear
			/usr/bin/apt update
			sleep 2
		else
			display_msg "$title" "
			E: No internet connection. 
			Can't update the package index files."
		fi
	fi
}

# Only for root partition
format_root() {
	title="  Format hard disk  "
	msg="
	Do you want the installer to format
	your root partition?"
	if yesno "$title" "$msg"; then
		msg="
		Select the file system to format your
		root partition with."
		select_fs "$msg"
	else
		FORMAT_HD="no"
	fi
}

# Only for root partition
select_fs() {
	title="  Filesystem selection  "
	msg="$1"
	options="ext4 ext2 btrfs xfs"
	if select_one "$title" "$msg" $options; then
		FILESYSTEM="$SSFT_RESULT"
	else
		error_msg "
		Cannot continue without specifying the
		file system format for the root partition." "1"
	fi

	# If the user selects btrfs we have some work to do.
	# Reduce list of available mount points.
	if [ "$FILESYSTEM" = "btrfs" ]; then
		MNT_POINTS="boot boot/efi efi "
		
		# Install Snapper and siduction-btrfs
		display_msg "$title" "
			Your selection is btrfs. 
			Packages Snapper and siduction-btrfs 
			will now be installed. "
		clear
		local pkg_path="/usr/share/siduction/btrfs-packages"
		if ! /usr/bin/apt install \
			$pkg_path/snapper_* \
			$pkg_path/libsnapper* \
			$pkg_path/siduction-btrfs* \
			$pkg_path/libbtrfs* \
			$pkg_path/libboost-thread*;
		then
			display_msg "$title" "
			E: Can't install Snapper and siduction-btrfs.
			You will need to do this after the system installation."
			return
		fi
		sleep 2
		# Snapper basic configuration
		cp /usr/share/snapper/config-templates/default /etc/snapper/configs/root
		sed -i '/SNAPPER_CONFIGS/s/""/"root"/' /etc/default/snapper
		
		# For the systemd units we need a file with the name
		# of the file system type of the root directory.
		local fstypename="/usr/share/siduction/btrfs"
		echo "DO NOT REMOVE THIS FILE!" > "$fstypename"
		echo "It is required by the siduction_btrfs systemd units." >> "$fstypename"
	fi
}

choose_root() {
	title="  Install Partition  "
	msg="
	Choose a partition to install siduction onto.
	Available partitions are:"
	if select_one "$title" "$msg" $PART_LIST "custom"; then
		ROOT_PARTITION="$SSFT_RESULT"
		if [ "$ROOT_PARTITION" = "custom" ]; then
			 custom_root
		fi
	else
		# give the user the option to manually set partition in case
		# the partition he wants was not detected
		custom_root
	fi

	# Check if the disk / is on is USB
	if [ -n "$(/usr/share/fll-installer-common/disk --usb="${ROOT_PARTITION%?}")" ]; then
		IS_DISK_USB="yes"
	fi

	# Remove the $partition from the list.
	PART_LIST=$(grep -v "$ROOT_PARTITION" <<< "$PART_LIST")
}

# Function where the user can enter a root partition in case
# the script didnt detect any partitions or the correct partition
custom_root() {
	#give the user the option to manually set a root partition in case no partitions were found
	title="  Partitioning  "
	msg="
	No partitions were detected by the installer.
	Do you want to manually set a partion?"
	if yesno "$title" "$msg"; then
		msg="
		Please input a root partition. "
		if input_string "Root partition" "$msg"; then
			ROOT_PARTITION="$SSFT_RESULT"
			while true; do
				partition_valid="true"
				if [ ! -b "$ROOT_PARTITION" ]; then
					msg="
					The partition you entered is not a block device. 
					Please enter a root partition. "
					# not a block device. not valid partition
					partition_valid="false"
				fi
				
				# Ensure that the device is truly a partition of the form
				# /dev/sd[a-z][0-9] or /dev/nvme[0-9]n[0-9]p[0-9]\+
				# with a number at the end.
				# In case of /dev/mapper/* make no checks as we
				# can not assume anything about the partition name.
				if grep -q "/mapper/" <<< "$ROOT_PARTITION"; then
					true
				elif ! grep -qE "^/dev/([sv]d[a-z][0-9]+|nvme[0-9]n[0-9]p[0-9]+)" \
					 <<< "$ROOT_PARTITION"; then
					msg="
					You have not entered a valid partition. 
					Please enter a root partition "
					# turns out partition was not valid
					partition_valid="false"
				fi
				
				# Check the result
				if [ "$partition_valid" = "true" ]; then
					# all good
					break
				else
					if input_string "  Root partition  " "$msg"; then
						ROOT_PARTITION="$SSFT_RESULT"
					else
						error_msg "Can not continue without root partition" "1"
					fi
				fi
			done
			
			# Check if the disk / is on is USB
			if [ -n "$(/usr/share/fll-installer-common/disk --usb="${ROOT_PARTITION%?}")" ]; then
				IS_DISK_USB="yes"
			fi
		else
			error_msg "Can not continue without root partition" "1"
		fi
	else
		error_msg "Can not continue without partition" "1"
	fi
}

# Ask the user to select a boot loader
input_boot_loader() {
	title="  Bootloader  "
	msg="
	Select a bootloader.
	
	Default is GRUB.
	
	I: systemd-boot requires:
	- UEFI hardware
	- GPT partition table
	- The following partitions must be on the same disk.
	-- ESP, first partition, vfat file system
	-- OS partition, mountpoint /
	-- Optional:
	-- XBOOTLDR, at least 1 GB, vfat file system
	-- (recommended for dual-boot with Windows)"
	options="GRUB systemd-boot"
	SSFT_DEFAULT="GRUB"
	if select_one "$title" "$msg" $options; then
		BOOT_LOADER="$SSFT_RESULT"
		
		if [ "$BOOT_LOADER" = "systemd-boot" ]; then
		# Verify that the requirements for systemd-boot are met.
			title="  systemd-boot installation verification  "
			msg="
			The check of the conditions for installing 
			systemd-boot will now start. "
			display_msg "$title" "$msg"
			msg=""
			
			# GPT partition table?
			check_gpt "$ROOT_PARTITION"
			if [ "$result" = "ok" ]; then
				
				# 1) XBOOTLDR
				found="$(lsblk -n -o PATH -Q 'PARTTYPE=="'"$XBOOT_UUID"'"')"
				found_xboot=""
				part_pick
				
				if [ -n "$found" ]; then
					check_xboot "$found"
					if [ "$result" = "ok" ]; then
						found_xboot="$found"
					fi
					title="  Bootloader  "
				fi
				
				# 2) ESP
				found="$(lsblk -n -o PATH -Q 'PARTTYPE=="'"$ESP_UUID"'"')"
				mnt_point=""
				part_pick
				
				if [ -n "$found" ]; then
					# Check the ESP
					check_efi "$found"
					if [ "$efi_result" -eq "0" ]; then
						if [ -n "$found_xboot" ]; then
							# ESP and XBOOTLDR
							# mount ESP on /efi and XBOOTLDR on /boot
							mnt_point="efi"
							MOUNT_POINT_LIST="$MOUNT_POINT_LIST$found:/$mnt_point "
							remove_part_point "$found"
							mnt_point="boot"
							MOUNT_POINT_LIST="$MOUNT_POINT_LIST$found_xboot:/$mnt_point "
							remove_part_point "$found_xboot"
							msg="
							SUCCESS 
							Use ESP, mounted at /efi 
							and
							XBOOTLDR, mounted at /boot "
						else
							# ESP only, mount at /boot
							MOUNT_POINT_LIST="$MOUNT_POINT_LIST$found:/boot "
							remove_part_point "$found"
							msg="
							SUCCESS 
							Use ESP, mounted at /boot "
						fi
						MNT_POINTS="home opt root tmp usr var "
						BOOT_WHERE="ESP"
					else
					# Error with the properties of the ESP.
						eval msg=\$msg_ch_efi_$efi_result
						msg="
						ERROR with the properties of the ESP. 
						$msg
						
						What would you like to do? "
						options="Fallback_to_GRUB Cancel_the_cli-installer"
						SSFT_DEFAULT="Fallback_to_GRUB"
						select_one "$title" "$msg" "$options"
						
						case "$SSFT_RESULT" in
							Fallback_to_GRUB)
							BOOT_LOADER="GRUB"
							msg="
							Switch to the GRUB boot manager. "
							# Leave the mount point and remove the unuseable partition.
							mnt_point=""
							remove_part_point "$found"
							;;
							*)
							msg=" cli-installer due to unusable ESP canceled"
							error_msg "$msg" "1"
							;;
						esac
					fi
				else
					BOOT_LOADER="GRUB"
					part_name "$ROOT_PARTITION"
					msg="
					ERROR: No ESP found on $d_name.
					Switch to the GRUB boot manager."
				fi
			else
				BOOT_LOADER="GRUB"
				title="  Bootloader  "
				msg="
				ERROR:
				No Globally Unique Identifier Partition Table (GPT)
				on the HD.
				Switch to the GRUB boot manager."
			fi
			display_msg "$title" "$msg"
		fi
	else
		msg=" cli-installer canceled in input_boot_loader"
		error_msg "$msg" "1"
	fi
}

# For GRUB only
# Ask the user where he wants to place the boot-loader
place_boot_loader() {
	local msg_noesp=""
	if [ "$BOOT_LOADER" = "systemd-boot" ]; then
		return 0
	elif [ "$IS_DISK_USB" = "yes" ]; then
	# If the disk choosen as root is USB then force partition
		BOOT_WHERE="Partition"
		return 0
	else
		part_name "$ROOT_PARTITION"
		title="  Bootloader  "
		msg="
		Where do you want the boot-loader to be installed?"
		options="ESP MBR Partition"
		SSFT_DEFAULT="ESP"
		if select_one "$title" "$msg" $options; then
			BOOT_WHERE="$SSFT_RESULT"
			
			# Installation into the ESP is done automatically
			# with add_bootmanager.bm.
			# The switch to mbr is a fallback in case the user's
			# specification is incorrect.
			if [ "$BOOT_WHERE" = "ESP" ]; then
				# Check if at least one ESP exists.
				found="$(lsblk -n -o PATH -Q 'PARTTYPE=="'"$ESP_UUID"'"')"
				if [ -z "$found" ]; then
					BOOT_WHERE="MBR"
					msg_noesp="
					No ESP found.
					"
				fi
				# Select an ESP and check its availability.
				while [ -n "$found" ]; do
					mnt_point="boot/efi"
					title="  Bootloader  "
					msg="
					Please select a partition to be used as ESP. "
					if select_one "$title" "$msg" $found; then
						partition="$SSFT_RESULT"
						check_efi "$partition"
						case "$efi_result" in
							0)
							# An useable ESP!
							# The space at the end is absolutely necessary.
							MOUNT_POINT_LIST="$MOUNT_POINT_LIST$partition:/$mnt_point "
							clear && break
							;;
							*)
							# Partition not useable as ESP.
							# Take the message that correspond to the check_efi result.
							eval msg=\$msg_ch_efi_$efi_result
							title="  $partition cannot be ESP  "
							msg="
							$msg
							
							What would you like to do? "
							options=" Repeat_selection \
									Fallback_to_MBR \
									Cancel_the_cli-installer"
							SSFT_DEFAULT="Repeat_selection"
							select_one "$title" "$msg" "$options"
							case "$SSFT_RESULT" in
								Repeat_selection)
								msg="
								$partition cannot be ESP 
								Select another partition. "
								found=$(sed -E "s|$SSFT_RESULT ?||" <<< "$found")
								continue
								;;
								Fallback_to_MBR)
								BOOT_WHERE="MBR"
								clear && break
								;;
								Cancel_the_cli-installer)
								msg="cli-installer due to unusable ESP canceled"
								error_msg "$msg" "1"
								;;
							esac
							;;
						esac
					else
						msg="cli-installer canceled in the place_boot_loader, select ESP"
						error_msg "$msg" "1"
					fi
				done
			fi
		else
			msg="cli-installer canceled in place_boot_loader"
			error_msg "$msg" "1"
		fi
		
		# Output the result.
		title="  Bootloader  "
		case "$BOOT_WHERE" in
			ESP)
			msg="
			GRUB will be installed on to ESP $partition
			mounted at /boot/efi."
			;;
			MBR)
			partition=""
			mnt_point="/efi"
			msg="$msg_noesp
			GRUB will be installed on to the MBR of $d_name. "
			;;
			Partition)
			partition=""
			mnt_point="/efi"
			msg="
			GRUB will be installed on to the root partition 
			$p_name or on a partition of your choice 
			that is mounted under /boot. 

			You can make your selection in the module 
			'Set mount points'. "
			;;
			*)
			;;
		esac
		remove_part_point "$partition"
		display_msg "$title" "$msg"
		mnt_point=""
		partition=""
	fi
}

auto_detect_swap() {
	title="  Swap autodetect  "
	msg="
	Do you want the installer to autodetect swap? "
	if yesno "$title" "$msg"; then
		SWAP_LIST="$(blkid -t TYPE=swap -o device | tr '\n' ' ')"
	else
		AUTO_SWAP="no"
		select_swap
	fi
}

select_swap() {
	title="  Select swap  "
	if [ -n "$SWAP_PART_LIST" ]; then
		msg="
		Select the partition(s) you want to be 
		used as swap by the installer "
		if select_more "$title" "$msg" $SWAP_PART_LIST; then
			# ssft returns a list with \n in it. Change it to " " and also use
			# sed to remove the space at the end of the line
			SWAP_LIST="$(echo "$SSFT_RESULT" | tr '\n' ' ' | sed 's/ $//' )"
		fi
	else
		msg="
		You have no swap partitions. 
		Nothing to select "
		display_msg "$title" "$msg"
	fi
}

# Set mount points for partitions other than root and swap
mount_points() {
	if [ -n "$MNT_POINTS" ] && [ -n "$PART_LIST" ]; then
		title="  Set mount points  "
		msg="
Do you want to set mount points for other partitions?

W: The partitions need to be already formated.

I: The root partition and, if specified earlier, 
	the swap partition, have already been set up 
	and should not be set up again."
		if yesno "$title" "$msg"; then
			set_mount_points
			msg="
			Finished setting mount points.
			$msg"
			display_msg "$title" "$msg"
		fi
	fi
}

set_mount_points() {
	mnt_point=""
	while [ -n "$PART_LIST" ]; do
		msg="
		Choose a partition to set its mount point. 
		Available partitions are:"
		
		if select_one "$title" "$msg" $PART_LIST; then
			partition="$SSFT_RESULT"
			
			if [ -n "$MNT_POINTS" ]; then
				msg="
				Choose a mount point for $partition "
				if ! select_one "$title" "$msg" $MNT_POINTS; then
				# SEE TO OFFER THE USER THE OPTION TO CANCEL
				# IN CASE HE CHOSE THE WRONG MOUNT POINT
				# If user pressed cancel break out of the loop
					msg="
					You have canceled the process. "
					break
				fi
				mnt_point="$SSFT_RESULT"
			else
				# No mount points left.
				msg="
				No mount points left. "
				break
			fi
			
			case "$mnt_point" in
				home)
				# If chosen it will be written into MOUNT_POINT_LIST after
				# check_home is called
				HOME_PARTITION="$partition"
				remove_part_point "$partition"
				CHECK_IF_HOME_EXISTS="yes"
				;;
				boot/efi)
				;;
				*)
				# The space at the end is absolutely necessary.
				MOUNT_POINT_LIST="$MOUNT_POINT_LIST$partition:/$mnt_point "
				remove_part_point "$partition"
				;;
			esac
		else
			msg=""
			break
		fi
	done
	if [ -z "$PART_LIST" ]; then
		msg="
		No partition left. "
	fi
}

# Function to ask the user if he wants the partitions to be
# auto-mounted on boot
# default is no
auto_mount_partitions() {
	title="  Automount other partitions  "
	msg=" 
	Would you like all other partitions to be mounted 
	automatically during bootup in addition to those 
	already specified? 

	I: The mount points follow the pattern 
	'/disks/sda4' or '/disks/nvme0n1p4'"
	
	SSFT_DEFAULT="no"
	options="no yes"
	if select_one "$title" "$msg" "$options"; then
		AUTOMOUNT_BOOT="$SSFT_RESULT"
	else
		AUTOMOUNT_BOOT="no"
	fi
}

# Asks the user if the installer should not check if there is
# enough space on the partition
### Currently unavailable
ignore_disk_space() {
	title="  Check for space  "
	msg=" 
	Do you want the program NOT to check if there is 
	enough space to install siduction?
	If you don't know what this means please choose 'no'"
	if yesno "$title" "$msg"; then
		IGNORE_DISK_SPACE="yes"
	fi
}

input_real_name() {
	title="  Input Real Name  "
	msg="
	Please input your real name "

	while ! input_string "$title" "$msg"; do
		if yesno "$title" "
			Continue without real name?"; then
			display_msg "$title" "
			OK, no real name"
			return 0
		else
			if ! input_string "$title" "$msg"; then
				display_msg "$title" "
				Multiple input errors for 'real name'.
				
				Continue without 'real name'."
				return 0
			fi
		fi
	done
	name="$SSFT_RESULT"
	
	# Make sure that name contains only valid characters
	while grep -q "[$NAME_NOT_ALLOWED_CHARS]" <<< "$name"; do
		title="  Real Name, not allowed Characters  "
		msg="
		Your input contains invalid characters.
		
		Characters which are not allowed are: 

		$NAME_NOT_ALLOWED_CHARS 

		Please input again your real name "
		if input_string "$title" "$msg"; then
			name="$SSFT_RESULT"
		else
			display_msg "$title" "
			Multiple input errors for 'real name'.
			
			Continue without 'real name'."
			return 0
		fi
	done

	## Used to escape spaces and quotes (')
	REAL_NAME=$(sed -e "s/ /\\\ /g" -e "s/'/\\\'/g" <<< "$name")
}

input_username() {
	title="  Input username  "
	msg="
	Please input your username "

	if input_string "$title" "$msg"; then
		USERNAME="$SSFT_RESULT"
	fi

	length_check "$USERNAME" "username" "no" "1" "$title"

	while ! grep -q "^[$USER_ALLOWED_CHARS]*$" <<< "$USERNAME"; do
		msg="
		Your input contains invalid characters.
		Allowed characters are: 

		$USER_ALLOWED_CHARS 

		Please input username "

		if input_string "$title" "$msg"; then
			USERNAME="$SSFT_RESULT"
		fi

		length_check "$USERNAME" "username" "no" "1" "$title"
	done
}

# Function to check if there is a username USERNAME in the home partition
# chosen by the user. If yes it asks for a new username or removal of the
# mount point
home_exists() {
	local user_list=""
	if  [ -n "$HOME_PARTITION" ]; then
		# Get a list of user names for the chosen home partition.
		MOUNT_POINT="$(mktemp -d /mnt/mount_check.XXXXXXXX)"
		mkdir -p "$MOUNT_POINT"
		if mount "$HOME_PARTITION" "$MOUNT_POINT"; then
			user_list="$(find "$MOUNT_POINT" -mindepth 1 -maxdepth 1 -type d -printf "%P\n")"
			umount "$MOUNT_POINT"
		else
			error_msg "Funktion home_exists: Could not mount $HOME_PARTITION. Aborting" "1"
		fi
		rmdir "$MOUNT_POINT"
	else
		# No home partition specified.
		return 0
	fi
	
	# As long as there is a HOME_PARTITION set ask for new name or mount point removal
	while [ -n "$HOME_PARTITION" ]; do
		if grep -q "$USERNAME" <<< $user_list; then
			msg="
			The username already exists on the partition you
			selected as home.
			
			Do you want to choose a new username or remove
			the mount point? "
			if select_one "  Check home  " "$msg" "new_username remove_mount-point"; then
				if [ "$SSFT_RESULT" = "new_username" ]; then
					input_username
				else
					HOME_PARTITION=""
				fi
			else
				error_msg "Check home" "Cancelling.." "1"
			fi
			
		else
		# No username USERNAME exists on HOME_PARTITION
		# The space at the end is absolutely necessary.
		MOUNT_POINT_LIST="$MOUNT_POINT_LIST$HOME_PARTITION:/home "
		break
		fi
	done
}

input_user_pass() {
	msg="
	Enter a user password. Will not be echoed.

I: Do not use a minus (-) as the first character 
	of the password, a space as the last or a 
	backslash (\) anywhere in the password 
	as those are currently not supported."
	if input_pass "$msg"; then
		USERPASS="$SSFT_RESULT"
	else
		error_msg "Can not continue without a user password" "1"
	fi

	length_check "$USERPASS" "user" "yes"

	msg="
	Please enter the password again. Will not be echoed"
	if input_pass "$msg"; then
		pass2="$SSFT_RESULT"
	else
		error_msg "Can not continue without a user password" "1"
	fi

	compare_pass "$USERPASS" "$pass2" "user"
	USERPASS="$( mkpasswd --method=sha512crypt "$USERPASS" | sed 's/\$/\\$/g' )"
}

input_root_pass() {
	pass2=""
	msg="
	Enter a ROOT password. Will not be echoed 

I: Do not use a minus (-) as the first character 
	of the password, a space as the last or a 
	backslash (\) anywhere in the password 
	as those are currently not supported. "
	if input_pass "$msg"; then
		ROOTPASS="$SSFT_RESULT"
	else
		error_msg "Can not continue without a root password" "1"
	fi

	length_check "$ROOTPASS" "root" "yes"

	msg="
	Please enter the password again. Will not be echoed"
	if input_pass "$msg"; then
		pass2="$SSFT_RESULT"
	else
		error_msg "Can not continue without a root password" "1"
	fi

	compare_pass "$ROOTPASS" "$pass2" "root"
	ROOTPASS="$( mkpasswd --method=sha512crypt "$ROOTPASS" | sed 's/\$/\\$/g' )"
}

input_hostname() {
	title="  Input hostname  "
	msg="
	Please input hostname "
	SSFT_DEFAULT="$HOST_NAME"
	if input_string "$title" "$msg"; then
		HOST_NAME="$SSFT_RESULT"
	fi

	length_check "$HOST_NAME" "hostname" "no" "2" "$title"

	while ! grep -q "^[$HOST_ALLOWED_CHARS][$HOST_ALLOWED_CHARS$HOST_ALLOWED_CHAR_OTHERS]*$" <<< "$HOST_NAME"; do
		msg="
		Hostname contains characters which are not allowed!
		Allowed characters are: 

		$HOST_ALLOWED_CHARS 

		$HOST_ALLOWED_CHAR_OTHERS 

		W: The hostname must start with a letter! 

		Please enter hostname "

		if input_string "$title" "$msg"; then
			HOST_NAME="$SSFT_RESULT"
		fi

		length_check "$HOST_NAME" "hostname" "no" "2" "$title"
	done
}

# Function so that the user can input some services that should
# start during boot
input_services() {
	title="  Services  "
	msg="
	Select the services you want to start on boot "
	#services="cups smail ssh samba"
	services="ssh"
	if select_more "$title" "$msg" $services; then
		# ssft returns a list with \n in it. Change it to " " and also use
		# sed to remove the space at the end of the line
		SERVICES_LIST="$(echo "$SSFT_RESULT" | tr '\n' ' ' | sed 's/ $//')"
	fi
}

write_config() {
	echo "REGISTERED=' SYSTEM_MODULE INET_CON HD_MODULE HD_FORMAT HD_FSTYPE HD_CHOICE HD_MAP HD_IGNORECHECK SWAP_MODULE SWAP_AUTODETECT SWAP_CHOICES NAME_MODULE NAME_NAME USER_MODULE USER_NAME USERPASS_MODULE USERPASS_CRYPT ROOTPASS_MODULE ROOTPASS_CRYPT HOST_MODULE HOST_NAME SERVICES_MODULE SERVICES_START BOOT_MODULE BOOT_LOADER BOOT_WHERE AUTOLOGIN_MODULE INSTALL_READY HD_AUTO'" > "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "SYSTEM_MODULE=configured" >> "$CONFIG_FILE"
	echo "HD_MODULE=configured" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Checks if the Internet is available." >> "$CONFIG_FILE"
	echo "# Allows you to establish a connection if one is not available." >> "$CONFIG_FILE"
	echo "# Possible are: yes|no" >> "$CONFIG_FILE"
	echo "INET_CON=$CONNECT" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Determines if the HD should be formatted. (mkfs.*)" >> "$CONFIG_FILE"
	echo "# Possible are: yes|no" >> "$CONFIG_FILE"
	echo "# Default value is: yes" >> "$CONFIG_FILE"
	echo "HD_FORMAT=$FORMAT_HD" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Sets the Filesystem type." >> "$CONFIG_FILE"
	echo "# Possible are: ext4|ext2|btrfs|xfs" >> "$CONFIG_FILE"
	echo "# Default value is: ext4" >> "$CONFIG_FILE"
	echo "HD_FSTYPE=$FILESYSTEM" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Here the siduction-System will be installed" >> "$CONFIG_FILE"
	echo "# This value will be checked by function module_hd_check" >> "$CONFIG_FILE"
	echo "HD_CHOICE=$ROOT_PARTITION" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Here you can give additional mappings." >> "$CONFIG_FILE"
	echo "# You need to have the partitions formatted yourself" >> "$CONFIG_FILE"
	echo "# and give the correct mappings like:" >> "$CONFIG_FILE"
	echo "# '"'/dev/hda4:/boot /dev/hda5:/var /dev/hda6:/tmp'"'" >> "$CONFIG_FILE"
	echo "HD_MAP=\"$MOUNT_POINT_LIST\"" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# If set to yes, the program will NOT check if there is" >> "$CONFIG_FILE"
	echo "# enough space to install siduction on the selected partition(s)." >> "$CONFIG_FILE"
	echo "# Use at your own risk! Useful for example with HD_MAP" >> "$CONFIG_FILE"
	echo "# if you only have a small root partition." >> "$CONFIG_FILE"
	echo "# Possible are: yes|no" >> "$CONFIG_FILE"
	echo "# Default value is: yes" >> "$CONFIG_FILE"
	echo "HD_IGNORECHECK=$IGNORE_DISK_SPACE" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "SWAP_MODULE='configured'" >> "$CONFIG_FILE"
	echo "# If set to yes, the swap partitions will be autodetected." >> "$CONFIG_FILE"
	echo "# Possible are: yes|no" >> "$CONFIG_FILE"
	echo "# Default value is: yes" >> "$CONFIG_FILE"
	echo "SWAP_AUTODETECT=$AUTO_SWAP" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# The swap partitions to be used by the installed siduction." >> "$CONFIG_FILE"
	echo "SWAP_CHOICES=\"$SWAP_LIST\"" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "NAME_MODULE='configured'" >> "$CONFIG_FILE"
	echo "NAME_NAME='$REAL_NAME'" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "USER_MODULE='configured'" >> "$CONFIG_FILE"
	echo "USER_NAME='$USERNAME'" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "USERPASS_MODULE='configured'" >> "$CONFIG_FILE"
	echo "USERPASS_CRYPT='$USERPASS'" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "ROOTPASS_MODULE='configured'" >> "$CONFIG_FILE"
	echo "ROOTPASS_CRYPT='$ROOTPASS'" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "HOST_MODULE='configured'" >> "$CONFIG_FILE"
	echo "HOST_NAME='$HOST_NAME'" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "SERVICES_MODULE='configured'" >> "$CONFIG_FILE"
	echo "# Possible services are for now: cups smail ssh samba" >> "$CONFIG_FILE"
	echo "# Default value is: cups" >> "$CONFIG_FILE"
	echo "SERVICES_START=\"$SERVICES_LIST\"" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "BOOT_MODULE='configured'" >> "$CONFIG_FILE"
	echo "# Chooses the Boot-Loader" >> "$CONFIG_FILE"
	echo "# Possible are: GRUB or systemd-boot" >> "$CONFIG_FILE"
	echo "# Default value is: GRUB" >> "$CONFIG_FILE"
	echo "BOOT_LOADER=$BOOT_LOADER" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Where the Boot-Loader will be installed" >> "$CONFIG_FILE"
	echo "# Possible are: ESP|MBR|Partition" >> "$CONFIG_FILE"
	echo "BOOT_WHERE=$BOOT_WHERE" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"

	echo "# Mount partitions on boot." >> "$CONFIG_FILE"
	echo "# Possible are: yes|no" >> "$CONFIG_FILE"
	echo "# Default value is: no" >> "$CONFIG_FILE"
	echo "HD_AUTO=$AUTOMOUNT_BOOT" >> "$CONFIG_FILE"
	
	echo "" >> "$CONFIG_FILE"

	echo "AUTOLOGIN_MODULE='configured'" >> "$CONFIG_FILE"
	echo "INSTALL_READY=yes" >> "$CONFIG_FILE"

	echo "" >> "$CONFIG_FILE"
}

### MAIN
#-----------------------------------------------------------------------

format_disk

set_timezone

check_con

non_free_sources

format_root

if [ -z "$ROOT_PARTITION" ]; then
	# root partition was not selected manually above
	choose_root
fi

if [ -z "$FILESYSTEM" ]; then
	# User must specify the file system of the root partition
	msg="
	Please let me know which file system
	your root partition has."
	select_fs "$msg"
fi

input_boot_loader

place_boot_loader

auto_detect_swap

mount_points

auto_mount_partitions

# ignore_disk_space ## Currently unavailable
IGNORE_DISK_SPACE="yes"

input_real_name

input_username

home_exists

input_user_pass

input_root_pass

input_hostname

input_services

write_config

display_msg "  cli-installer  " "
	Finished creating configuration file"

check_for_mounted_disks "2"

# restore stderr and close file descriptor 3 (only if the interface was dialog)
if [ "$SSFT_FRONTEND" = "dialog" ]; then

	exec 2>&3 3>&-
fi

### CALL INSTALLER
title="  Call Installer  "
msg="
 Finished all checks. 
 Do you want to start the installation? 
 I: If you choose 'no' the configuration file
  will stay in $CONFIG_FILE! "
if yesno "$title" "$msg"; then
	clear
	# Return to the previous tty setting.
#	/usr/bin/setupcon

	fll-installer || echo "Some error occured. Looking at $ERROR_LOG might provide you with answers" && exit 1
else
	clear
fi

# if all goes well remove error log and resets $TERM and ssft.
rm -f "$ERROR_LOG"
TERM=$term_org
mv /usr/bin/ssft-bak.sh /usr/bin/ssft.sh

exit 0
