#!/bin/bash -eu

export DISPLAY= WAYLAND_DISPLAY=
WITH_ACTION=1
. install2-init-functions
. install2-remount-functions
. shell-config
. /usr/lib/alterator-kopidel/progress-bars
. /usr/lib/alterator-kopidel/install-progress

export HOME=/tmp TMPDIR=/tmp INSTALLER=1 DURING_INSTALL=1
export ALTERATOR_DESTDIR="$destdir"
export ALTERATOR_DATADIR="/usr/share/alterator:$destdir/usr/share/alterator:$datadir"
export GUILE_LOAD_PATH="$destdir/usr/share/guile/site:$destdir/usr/share/guile/1.8:$destdir/usr/share/guile"
export AUTOINSTALL=/tmp/metadata/autoinstall.scm CDROMDEV=

debug_shell() (
	if ! cmdline_has_arg instdebug; then
		exit 0
	fi
	echo 'Executing shell for debug purposes'
	systemctl start kopidel-instdebug-ssh.service || true
	HOME=/tmp bash -l </dev/tty1 >/dev/tty1 2>&1
)

save_logs() (
	[[ -d "$destdir/root" ]] || exit 0
	mkdir -pv -m700 "$destdir/root/.install-log"
	for log_file in /tmp/{initinstall,preinstall,postinstall,install2,install-fs,pkg-install,remount}.log \
		/run/alteratord/alteratord.log; do
		[[ -f "$log_file" ]] || continue
		sed -r 's;( passwd_?([12]|auto) ")([^"]|[\\]["])*";\1*";g' \
			"$log_file" > "$destdir/root/.install-log/${log_file##*/}"
	done
)

finish_installation() (
	status="$1"
	trap - EXIT
	if (( status != 0 )); then
		echo "Installation failed with exit status $status" >&2
		debug_shell || true
	fi
	stop_alteratord || true
	save_logs || true
	if mountpoint -q "$destdir"; then
		umount_chroot || exit 1
	fi
	sync
	(( status == 0 )) || exit "$status"
	show_install_phase 'Installation complete.'
	if cmdline_has_arg poweroff; then
		systemctl --no-block poweroff
	else
		systemctl --no-block reboot
	fi
)

run_initinstall_scripts() (
	for script in "$@"; do
		[[ -f "$script" && -x "$script" ]] || continue
		action "Running initinstall script [${script##*/}]" "$script" || true
	done
)

run_initinstall() (
	run_initinstall_scripts "$initinstall_dir"/[0-2][0-9]-*
	run_initinstall_scripts "$initinstall_dir"/[3-9][0-9]-*
	stop_splash
)

deploy_system() (
	show_install_phase 'Deploying the system...'
	trap 'finish_deployment_progress "$?"' EXIT
	start_deployment_progress
	alterator-autoinstall "$AUTOINSTALL"
)

md_not_synced() (
	for device in /sys/block/md*/md/sync_action; do
		[[ -f "$device" ]] || continue
		read -r state < "$device"
		[[ "$state" == idle ]] || exit 0
	done
	exit 1
)

run_postinstall() (
	[[ -f "$destdir/_NEW_SYSTEM_" ]] || exit 1
	stop_alteratord
	if [[ -d "$destdir/$postinstall_dir" ]]; then
		find "$destdir/$postinstall_dir" -mindepth 1 -maxdepth 1 -type f -perm -u=x \
			-exec cp -nv {} "$postinstall_dir" \;
	fi
	for script in "$postinstall_dir"/*; do
		[[ -f "$script" && -x "$script" ]] || continue
		echo "Running postinstall script [${script##*/}]"
		"$script"
	done
	if ! cmdline_has_arg mdsync_nowait; then
		while md_not_synced; do
			sleep 1
		done
	fi
	rm -vf "$destdir/_NEW_SYSTEM_"
)

umask 077
for name in initinstall preinstall postinstall install2 install-fs pkg-install remount; do
	: > "/tmp/$name.log"
done
exec > >(tee -a /tmp/install2.log) 2>&1
trap 'finish_installation "$?"; exit "$?"' EXIT

mkdir -pv "$destdir" "$datadir" /tmp/metadata
: > "$install_variables"
for name in HOME TMPDIR INSTALLER ALTERATOR_DESTDIR AUTOINSTALL CDROMDEV; do
	shell_config_set "$install_variables" "$name" "${!name}"
done

export DEVICE
DEVICE="$(sed -n 's,^/dev/\([^[:space:]]\+\) /image .*,\1,p' /proc/mounts)"
shell_config_set "$install_variables" DEVICE "$DEVICE"
show_install_phase 'Preparing the installer...'
udevadm settle
run_initinstall >>/tmp/initinstall.log 2>&1
[[ -s "$AUTOINSTALL" ]] || fatal "No readable autoinstall profile: $AUTOINSTALL"
debug_shell
deploy_system
debug_shell
show_install_phase 'Completing installation and configuring the bootloader...'
run_postinstall >>/tmp/postinstall.log 2>&1
