#!/bin/sh
# QA only, launched by the Debian Installer partitioner's early command.
# The existing test filesystem is mounted by name. No partitioning commands run.
set -eu
exec >/dev/ttyS1 2>&1
echo NEWWORLD_RECOVERY_BEGIN
test -s /cdrom/newworld/firmware.list
mkdir -p /target
modprobe ext4 || true
mount -t ext4 -o rw /dev/vda1 /target
grep -q '^qa:x:1000:1000:NewWorld QA' /target/etc/passwd
test -s /target/boot/grub/grub.cfg
test -s /target/boot/initrd.img-6.1.0-50-686-pae
test "$(chroot /target dpkg --print-architecture)" = i386
for swapdisk in /dev/vda5; do
    test -b "$swapdisk" && swapon "$swapdisk" || true
done
# Restore any temporary dpkg helper diversion left by the interrupted d-i process.
. /lib/chroot-setup.sh
if [ -f /target/sbin/start-stop-daemon.REAL ]; then undivert /sbin/start-stop-daemon; fi
if [ -f /target/sbin/initctl.REAL ]; then undivert /sbin/initctl; fi
rm -f /target/usr/sbin/policy-rc.d
mkdir -p /target/dev /target/opt/newworld-recovery-media
mount -o bind /dev /target/dev
mount -o bind /cdrom /target/opt/newworld-recovery-media
echo 'deb [trusted=yes] file:/opt/newworld-recovery-media/newworld/repo ./' >/target/etc/apt/newworld-recovery.list
rescue_apt() {
    in-target env DEBIAN_FRONTEND=noninteractive apt-get -o Dir::Etc::sourcelist=/etc/apt/newworld-recovery.list \
        -o Dir::Etc::sourceparts=- -o Acquire::Languages=none -o Acquire::Retries=0 \
        -o APT::Get::List-Cleanup=false "$@"
}
in-target env DEBIAN_FRONTEND=noninteractive dpkg --configure -a || true
rescue_apt update
rescue_apt -y --no-install-recommends --no-remove --fix-broken install
rm -f /target/etc/apt/newworld-recovery.list
umount /target/opt/newworld-recovery-media
rmdir /target/opt/newworld-recovery-media
echo NEWWORLD_FINAL_TRANSACTION_BEGIN
/bin/sh /cdrom/newworld/setup-target
echo NEWWORLD_FINAL_TRANSACTION_FINISHED
chroot /target dpkg --audit
chroot /target dpkg-query -W firefox-esr firefox-esr-l10n-he xfce4 lightdm wpasupplicant wireless-regdb \
    cups cups-filters pulseaudio-module-bluetooth alsa-utils guvcview v4l-utils systemd-timesyncd
chroot /target locale -a
df -m /target
grep -F 'session-wrapper=/usr/local/lib/newworld/xfce-session' /target/etc/lightdm/lightdm.conf.d/85-newworld.conf
test -x /target/usr/bin/newworld-settings32
test -x /target/usr/bin/newworld-applications
test -x /target/usr/bin/newworld-artcraft
test -s /target/usr/local/share/newworld-artcraft/anicraft-web.zip
test -z "$(find /target/home/qa -name 'agreement-*.json' -print -quit)"
mkdir -p /target/etc/default/grub.d
echo 'GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"' >/target/etc/default/grub.d/99-qa-serial.cfg
in-target update-grub
in-target systemctl enable serial-getty@ttyS0.service
mkdir -p /target/var/log/installer
cp /var/log/syslog /target/var/log/installer/newworld-qa-recovery.log || true
echo NEWWORLD_I386_INSTALLED_OK
sync
umount /target/dev || true
umount /target || true
poweroff -f
