#!/bin/sh

CHROOT=$(mount | grep proc | grep calamares | awk '{print $3}' | sed -e "s#/proc##g")

. /etc/default/distro

# remove /etc/default/distro
rm -f "${CHROOT}"/etc/default/distro

# During live boot an unfixated home was blessed, and the calamares users
# module was disabled via settings.conf. This means a local block device was
# present matching the systemd-homed key pair included on the live media.
# Pass this identity into the installed system.
if [ -r "/var/lib/systemd/home/${FLL_LIVE_USER}.identity" ]; then
    cp --archive "/var/lib/systemd/home/${FLL_LIVE_USER}.identity" \
        "${CHROOT}/var/lib/systemd/home/${FLL_LIVE_USER}.identity"
    if [ -d "/var/cache/systemd/home/${FLL_LIVE_USER}" ]; then
        cp --archive --recursive "/var/cache/systemd/home/${FLL_LIVE_USER}" \
            "${CHROOT}/var/cache/systemd/home/${FLL_LIVE_USER}"
    fi
fi

# regenerate default snakeoil with new hostname
if chroot $CHROOT dpkg-query -f'${db:Status-Status}\n' -W ssl-cert 2>/dev/null | grep -q ^installed; then
    chroot $CHROOT make-ssl-cert generate-default-snakeoil --force-overwrite
fi

if chroot $CHROOT dpkg-query -f'${db:Status-Status}\n' -W openssh-server 2>/dev/null | grep -q ^installed; then
    # recreate openssh keys for new host
    rm -f $CHROOT/etc/ssh/ssh_host_*
    chroot $CHROOT dpkg-reconfigure --frontend=noninteractive openssh-server
fi

if ! systemd-detect-virt --quiet; then
    DEBIAN_FRONTEND=noninteractive chroot $CHROOT dpkg --purge spice-vdagent qemu-guest-agent
fi

if [ -d "$CHROOT/.snapshots" ] && [ -x "$CHROOT/usr/bin/snapper" ]; then
    # chroot is on btrfs - configure snapper
    chroot $CHROOT umount /.snapshots
    chroot $CHROOT rmdir /.snapshots
    chroot $CHROOT snapper --no-dbus --config root create-config --fstype btrfs /
    chroot $CHROOT btrfs subvolume delete /.snapshots
    chroot $CHROOT mkdir /.snapshots
    # https://github.com/kdave/btrfsmaintenance?tab=readme-ov-file#tuning-periodic-snapshotting
    sed -i 's#TIMELINE_LIMIT_HOURLY="10"#TIMELINE_LIMIT_HOURLY="12"#' "$CHROOT/etc/snapper/configs/root"
    sed -i 's#TIMELINE_LIMIT_DAILY="10"#TIMELINE_LIMIT_DAILY="7"#' "$CHROOT/etc/snapper/configs/root"
    sed -i 's#TIMELINE_LIMIT_WEEKLY="0"#TIMELINE_LIMIT_WEEKLY="1"#' "$CHROOT/etc/snapper/configs/root"
    sed -i 's#TIMELINE_LIMIT_MONTHLY="10"#TIMELINE_LIMIT_MONTHLY="0"#' "$CHROOT/etc/snapper/configs/root"
    sed -i 's#TIMELINE_LIMIT_YEARLY="10"#TIMELINE_LIMIT_YEARLY="0"#' "$CHROOT/etc/snapper/configs/root"
 else
     DEBIAN_FRONTEND=noninteractive chroot $CHROOT dpkg --purge snapper btrfs-assistant btrfsmaintenance
 fi

# setup root home dotfiles
for dotfile in '.bash_logout' '.bash_profile' '.bashrc' '.profile'; do
	if [ -f "/etc/skel/${dotfile}" ]; then
		cp "/etc/skel/${dotfile}" "${CHROOT}/root/${dotfile}"
	fi
done

# sync live skel conffiles with chroot to support desktop configuration
rsync -a /etc/skel/. "${CHROOT}"/etc/skel/.

# suport greetd configuration for sway, hyprland, labwc
if [ -d /etc/greetd ]; then
    rsync -a /etc/greetd/.  "${CHROOT}"/etc/greetd/.
    sed -i '/\[initial_session\]/,/user = .*/d' \
        "${CHROOT}"/etc/greetd/config.toml
fi

# update lxqt configuration on installed system
if [ -r /etc/xdg/lxqt/session.conf ]; then
    cp /etc/xdg/lxqt/session.conf "${CHROOT}"/etc/xdg/lxqt/session.conf
fi

# update labwc configuration on installed system
if [ -r /etc/xdg/labwc/environment ]; then
    cp /etc/xdg/labwc/environment "${CHROOT}"/etc/xdg/labwc/environment
fi

exit 0
