# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright 2024 Tobias Görgens

factory_reset_dir="/var/lib/tik/post"
factory_reset_pipe="/tmp/postpipe"
[ -d "${factory_reset_dir}/mnt" ] || prun /usr/bin/mkdir -p "${factory_reset_dir}/mnt"
[ -p "${factory_reset_pipe}" ] || /usr/bin/mkfifo "${factory_reset_pipe}"

factory_reset_progress() {
    log "[factory_reset_progress] Monitoring post install setup progress"
    (tail -f "${factory_reset_pipe}") | d --progress --title="Configuring System" --auto-close --no-cancel --width=400
    rm "${factory_reset_pipe}"
    log "[factory_reset_progress] Post install setup progress reached 100%"
}

find_crypt() {
    echo "# Finding encrypted partition" > ${factory_reset_pipe}
    log "[find_crypt] finding encrypted partition"
    probe_partitions "${TIK_INSTALL_DEVICE}" "Linux\x20root\x20(x86-64)"
    if [ -z "${probedpart}" ]; then
        error "encrypted partition not found"
    fi
    cryptpart=${probedpart}
    log "[find_crypt] found ${cryptpart}"
    echo "14" > ${factory_reset_pipe}
}

find_esp() {
    echo "# Finding installation ESP partition" > "${factory_reset_pipe}"
    log "[find_esp] finding ESP"
    probe_partitions "${TIK_INSTALL_DEVICE}" "EFI\x20System"
    [ -n "${probedpart}" ] || error "POST INSTALL FAILED: ESP Partition NOT FOUND"
    esppart="${probedpart}"
    log "[find_esp] found ${esppart}"
    echo "28" > "${factory_reset_pipe}"
}

open_partition() {
    echo "# Opening ${cryptpart}" > ${factory_reset_pipe}
    log "[open_partition] opening ${cryptpart} and mounting for chroot"
    prun /usr/sbin/cryptsetup luksOpen --key-file="${tik_keyfile}" "${cryptpart}" yuga_root
    echo "35" > ${factory_reset_pipe}
    prun /usr/bin/mount -o compress=zstd:1 /dev/mapper/yuga_root ${factory_reset_dir}/mnt
    for i in proc dev sys tmp 'sys/firmware/efi/efivars' 'sys/fs/cgroup'; do
        prun /usr/bin/mount --bind "/$i" "${factory_reset_dir}/mnt/$i"
    done
    prun /usr/bin/mount -o compress=zstd:1,subvol=/@/.snapshots /dev/mapper/yuga_root ${factory_reset_dir}/mnt/.snapshots
    prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var /dev/mapper/yuga_root ${factory_reset_dir}/mnt/var
    etcmountcmd=$(cat ${factory_reset_dir}/mnt/etc/fstab | grep "overlay /etc" | sed 's/\/sysroot\//${factory_reset_dir}\/mnt\//g' | sed 's/\/work-etc.*/\/work-etc ${factory_reset_dir}\/mnt\/etc\//' | sed 's/overlay \/etc overlay/\/usr\/bin\/mount -t overlay overlay -o/')
    eval prun "$etcmountcmd"
    prun /usr/bin/mount "${esppart}" ${factory_reset_dir}/mnt/boot/efi
    prun /usr/bin/mount -t tmpfs tmpfs "${factory_reset_dir}/mnt/run"
    prun /usr/bin/mount -t securityfs securityfs "${factory_reset_dir}/mnt/sys/kernel/security"
    prun /usr/bin/mount -t btrfs -o subvol=/ /dev/mapper/yuga_root "${factory_reset_dir}/mnt/mnt"
    echo "42" > ${factory_reset_pipe}
}

configure_factory_reset() {
    echo "# Preparing system" > "${factory_reset_pipe}"
    directories=(home opt root srv var)

    prun /usr/bin/mkdir "${factory_reset_dir}/mnt/.snapshots/.ori/"
    prun /usr/bin/mkdir "${factory_reset_dir}/mnt/.snapshots/.fr/"

    prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/.snapshots/1/snapshot" "${factory_reset_dir}/mnt/.snapshots/.ori/snapshot"
    prun /usr/bin/cp "${factory_reset_dir}/mnt/.snapshots/1/info.xml" "${factory_reset_dir}/mnt/.snapshots/.ori/info.xml"

    prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/.snapshots/.ori/snapshot" "${factory_reset_dir}/mnt/.snapshots/.fr/snapshot"
    prun /usr/bin/cp "${factory_reset_dir}/mnt/.snapshots/.ori/info.xml" "${factory_reset_dir}/mnt/.snapshots/.fr/info.xml"
    echo "84" > "${factory_reset_pipe}"
    echo "# Finalizing system" > "${factory_reset_pipe}"

    for dir in "${directories[@]}"
    do
        prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/mnt/@/$dir" "${factory_reset_dir}/mnt/mnt/@/.${dir}ori"
        prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/mnt/@/.${dir}ori" "${factory_reset_dir}/mnt/mnt/@/.${dir}fr"
        prun /usr/bin/sed -i "s|subvol=@/$dir|subvol=@/.${dir}fr|g" "${factory_reset_dir}/mnt/.snapshots/.fr/snapshot/etc/fstab"
    done
    prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/mnt/@/usr/local" "${factory_reset_dir}/mnt/mnt/@/usr/.localori"
    prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/mnt/@/usr/.localori" "${factory_reset_dir}/mnt/mnt/@/usr/.localfr"
    prun /usr/bin/sed -i "s|subvol=@/usr/local|subvol=@/usr/.localfr|g" "${factory_reset_dir}/mnt/.snapshots/.fr/snapshot/etc/fstab"

    prun /usr/sbin/btrfs subvolume create "${factory_reset_dir}/mnt/.snapshots/.fr/bori"

    prun /usr/bin/cp -r "${factory_reset_dir}/mnt/boot/efi" "${factory_reset_dir}/mnt/.snapshots/.fr/bori"
    prun /usr/sbin/btrfs subvolume snapshot "${factory_reset_dir}/mnt/.snapshots/.fr/bori" "${factory_reset_dir}/mnt/.snapshots/.fr/bfr"

    for file in "${factory_reset_dir}"/mnt/boot/efi/loader/entries/*
    do
        frpath="/mnt/.snapshots/.fr/bfr/efi/loader/entries/$(basename file)"
        # Check if it's a regular file
        if [ -f "$frpath" ]; then
            prun /usr/bin/sed -i "s/subvol=@\/.snapshots\/1/subvol=@\/.snapshots\/.fr/g" "$frpath"
        fi
    done
    prun /usr/bin/touch "${factory_reset_dir}/mnt/.snapshots/.fr/snapshot/.factory_reset"
    prun /usr/sbin/btrfs property set "${factory_reset_dir}/mnt/.snapshots/.ori/snapshot" ro true
    prun /usr/sbin/btrfs property set "${factory_reset_dir}/mnt/.snapshots/.fr/snapshot" ro true
    prun /usr/sbin/btrfs property set "${factory_reset_dir}/mnt/.snapshots/.fr/bori" ro true
    prun /usr/sbin/btrfs property set "${factory_reset_dir}/mnt/.snapshots/.fr/bfr" ro true
    echo "91" > "${factory_reset_pipe}"
}

close_partition() {
    echo "# Closing ${cryptpart}" > ${factory_reset_pipe}
    log "[close_partition] unmounting and closing ${cryptpart}"
    for i in proc dev run tmp 'boot/efi' etc var '.snapshots' 'sys/kernel/security' 'sys/firmware/efi/efivars' 'sys/fs/cgroup' sys mnt; do
        prun /usr/bin/umount "${factory_reset_dir}/mnt/$i"
    done
    prun /usr/bin/umount ${factory_reset_dir}/mnt
    prun /usr/sbin/cryptsetup luksClose yuga_root
    echo "100" > ${factory_reset_pipe}
}

factory_reset_progress &
find_crypt
find_esp
open_partition
configure_factory_reset
close_partition
