#!/bin/sh
set -eu
. /usr/lib/newworld-installer/mode
ensure_normal_install
state=/run/newworld-install
trap 'exitcode=$?; if [ "$exitcode" != 0 ]; then echo "NewWorld installation could not finish." > /run/newworld-install/error; /usr/lib/newworld-installer/usb-log finish failed >/dev/null 2>&1 || true; while :; do sleep 60; done; fi' EXIT
test -s "$state/decision"
echo packages >"$state/stage"
/bin/sh /cdrom/newworld/setup-target
echo configure >"$state/stage"
case "$(cat "$state/language")" in
    he) in-target update-locale LANG=he_IL.UTF-8 LANGUAGE=he:en ;;
    en) in-target update-locale LANG=en_US.UTF-8 LANGUAGE=en ;;
    *) exit 1 ;;
esac
login=$(cat "$state/login")
hostname=$(cat "$state/hostname")
printf '%s\n' "$hostname" >/target/etc/hostname
# Replace only the installation's own local hostname alias.
/usr/lib/newworld-installer/python /usr/lib/newworld-installer/identity.py --write-hosts /target/etc/hosts "$hostname"
in-target /usr/local/lib/newworld-initial-access/provision --generate "$login" lightdm
test -s "/target/home/$login/.local/state/newworld/initial-admin-secret"
test -s /target/etc/lightdm/lightdm.conf.d/95-newworld-first-login.conf
test -x /target/usr/bin/firefox-esr
test -x /target/usr/bin/startxfce4
test -s /target/boot/grub/grub.cfg
touch "$state/target-ready"
echo finalize >"$state/stage"
