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

steam_pipe=/tmp/steampipe
[ -p "${steam_pipe}" ] || /usr/bin/mkfifo "${steam_pipe}"
[ -d "${steam_dir}/mnt" ] || prun /usr/bin/mkdir -p "${steam_dir}/mnt"

steam_progress() {
    log "[steam_progress] Monitoring Steam setup progress"
    (tail -f ${steam_pipe}) | d --progress --title="Bootstrapping Steam" --auto-close --no-cancel --width=400
    rm ${steam_pipe}
    log "[steam_progress] Steam setup progress reached 100%"
}

find_crypt() {
    echo "# Finding encrypted partition" > ${steam_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 "10" > ${steam_pipe}
}

open_partition() {
    echo "# Opening ${cryptpart}" > ${steam_pipe}
    echo "25" > ${steam_pipe}
    log "[open_partition] opening ${cryptpart} and mounting for chroot"
    prun /usr/sbin/cryptsetup luksOpen --key-file="${tik_keyfile}" "${cryptpart}" yuga_root
    prun /usr/bin/mount -o compress=zstd:1 /dev/mapper/yuga_root "${steam_dir}"/mnt
    for i in proc dev sys tmp 'sys/firmware/efi/efivars' 'sys/fs/cgroup'; do
        prun /usr/bin/mount --bind "/$i" "${steam_dir}/mnt/$i"
    done
    prun /usr/bin/mount -o compress=zstd:1,subvol=/@/.snapshots /dev/mapper/yuga_root "${steam_dir}"/mnt/.snapshots
    prun /usr/bin/mount -o compress=zstd:1,subvol=/@/var /dev/mapper/yuga_root "${steam_dir}"/mnt/var
    etcmountcmd=$(grep "overlay /etc" "${steam_dir}"/mnt/etc/fstab | sed 's/\/sysroot\//${steam_dir}\/mnt\//g' | sed 's/\/work-etc.*/\/work-etc ${steam_dir}\/mnt\/etc\//' | sed 's/overlay \/etc overlay/\/usr\/bin\/mount -t overlay overlay -o/')
    eval prun "$etcmountcmd"
    prun /usr/bin/mount -t tmpfs tmpfs "${steam_dir}/mnt/run"
    prun /usr/bin/mount -t securityfs securityfs "${steam_dir}/mnt/sys/kernel/security"
    echo "40" > ${steam_pipe}
}
configure_steam() {
    echo "# Bootstrapping Steam" > ${steam_pipe}
    echo "55" > ${steam_pipe}
    # Check if the bootstrap file already exists
    [ -f "${bootstrap_file}" ] || error "Steam Setup file not found. Please try again."
    log "[configure_steam] Bootstrap file exists. Proceeding..."
    prun mkdir -p "${steam_dir}/mnt/etc/first-boot"
    prun /usr/bin/cp "${bootstrap_file}" "${steam_dir}/mnt/etc/first-boot/bootstraplinux_ubuntu12_32.tar.xz"
    echo "70" > ${steam_pipe}
}

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

steam_progress &
find_crypt
open_partition
configure_steam
close_partition