#!/bin/bash
# -*- coding: utf-8 -*-
#
#   Copyright 2022, Torsten Wohlfarth <two@tuxedocomputers.com>
#
#   This module is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This module is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this module. If not, see <http://www.gnu.org/licenses/>.

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

sysid=""
bootid=""
cryptid=""
cryptdev=""
efiid=""
fs=""
disk=$(cat /root/disk)
boot=$(cat /root/boot)
efi=$(cat /root/efi)
crypt=$(cat /root/crypt)
sysid=$(lsblk -l -o NAME,UUID,LABEL,FSTYPE | awk '/system-root/{ print $2}')
bootid=$(lsblk -lf -o NAME,FSTYPE,UUID,LABEL | grep "$boot" | awk '/BOOT/{print $3}')
efiid=$(lsblk -lf -o NAME,FSTYPE,UUID,LABEL | grep "$efi" | awk '/EFI/{print $3}')
cryptdev=$(lsblk -lf | grep "$crypt" | awk '/crypto_/{print $1}')
cryptid=$(lsblk -lf -o NAME,FSTYPE,UUID | grep "$crypt" | awk '/crypto_/{print $3}')
fs=$(lsblk -lf -o NAME,FSTYPE | awk '/system-root/{ print $2}')
rm -f /etc/sudoers.d/15*
echo "Write the fstab"
if [ "$fs" = "btrfs" ]; then
    printf "UUID=$sysid         /               $fs            defaults,subvol=@,noatime,space_cache=v2,autodefrag,compress=zstd:11        0  1\n" > /etc/fstab
    printf "UUID=$sysid         /home           $fs            defaults,subvol=@home,noatime,space_cache=v2,autodefrag,compress=zstd:11        0  1\n" >> /etc/fstab
else
    printf "UUID=$sysid         /               $fs            defaults,noatime        0  1\n" > /etc/fstab
fi
printf "UUID=$bootid                /boot           ext3            defaults,noatime        0  2\n" >> /etc/fstab
printf "UUID=$efiid                 /boot/efi       vfat            umask=007               0  0\n" >> /etc/fstab
echo "write the crypttab"
printf "crypt_dev_$cryptdev		UUID="$cryptid"		none    luks,discard\n" > /etc/crypttab

echo "make sure all is on the right place"
cleanup() {
update-initramfs -c -k all
if [[ -e "/sys/block/$disk/removable" && $(cat "/sys/block/$disk/removable") -eq 1 ]] || lsblk -dn -o NAME,TRAN | grep "$disk" | grep -q usb; then
    sed -i 's/OS_PROBER=false/OS_PROBER=true/' /etc/default/grub.d/tuxedo.cfg
    grub-install --removable --boot-directory=/boot --efi-directory=/boot/efi --no-uefi-secure-boot --bootloader-id "TxOS USB"
else
    grub-install --boot-directory=/boot --efi-directory=/boot/efi
    dpkg -i /usr/share/tuxedo/packages/tuxedo-webfai*.deb
fi
sed -i 's/quiet/quiet splash/' /etc/default/grub
dpkg -i /usr/share/tuxedo/packages/tuxedo-grub-theme*.deb
rm -rf /usr/share/tuxedo/packages
update-grub
rm -f /etc/calamares/branding/tuxedo/packagechooserq@session
apt-get -y purge *calamares*
}

cleanup 
#| zenity --progress --title="Working ..." --text="generate initrd for installed system" --pulsate --no-cancel --width=400 --auto-close
exit 0
