#!/bin/bash
echo "Armbian 'linux-image-current-sunxi64' for '6.1.38-sunxi64': 'postinst' starting."
set -e # Error control

function is_boot_dev_vfat() {
# When installing these packages during image build, /boot is not mounted, and will most definitely not be vfat.
# Use an environment variable to signal that it _will_ be a fat32, so symlinks are not created.
# This is passed by install_deb_chroot() explicitly via the runners.
if [[ "${ARMBIAN_IMAGE_BUILD_BOOTFS_TYPE:-"unknown"}" == "fat" ]]; then
echo "Armbian: ARMBIAN_IMAGE_BUILD_BOOTFS_TYPE: '${ARMBIAN_IMAGE_BUILD_BOOTFS_TYPE:-"not set"}'"
return 0
fi
if ! mountpoint -q /boot; then
return 1
fi
local boot_partition bootfstype
boot_partition=$(findmnt --nofsroot -n -o SOURCE /boot)
bootfstype=$(blkid -s TYPE -o value $boot_partition)
if [[ "$bootfstype" == "vfat" ]]; then
return 0
fi
return 1
}

#set -x # Debugging

export DEB_MAINT_PARAMS="$*" # Pass maintainer script parameters to hook scripts
export INITRD=Yes # Tell initramfs builder whether it's wanted
# Run the same hooks Debian/Ubuntu would for their kernel packages.
test -d /etc/kernel/postinst.d && run-parts --arg="6.1.38-sunxi64" --arg="/boot/vmlinuz-6.1.38-sunxi64" /etc/kernel/postinst.d
touch /boot/.next
if is_boot_dev_vfat; then
echo "Armbian: FAT32 /boot: move last-installed kernel to 'Image'..."
mv -v /boot/vmlinuz-6.1.38-sunxi64 /boot/Image
else
echo "Armbian: update last-installed kernel symlink to 'Image'..."
ln -sfv vmlinuz-6.1.38-sunxi64 /boot/Image
fi
# call debian helper, for compatibility. this symlinks things according to /etc/kernel-img.conf
# "install" or "upgrade" are decided in a very contrived way by Debian (".fresh-install" file)
# do NOT do this if /boot is a vfat, though.
if ! is_boot_dev_vfat; then
echo "Armbian: Debian compat: linux-update-symlinks install 6.1.38-sunxi64 boot/vmlinuz-6.1.38-sunxi64"
linux-update-symlinks install "6.1.38-sunxi64" "boot/vmlinuz-6.1.38-sunxi64" || true
fi

set +x # Disable debugging
echo "Armbian 'linux-image-current-sunxi64' for '6.1.38-sunxi64': 'postinst' finishing."
true
