#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
# SPDX-FileCopyrightText: 2024-2025 Harald Sitter <sitter@kde.org>

set -eux

. /usr/lib/os-release

normal_dir=$1
# shellcheck disable=SC2034
early_dir=$2
# shellcheck disable=SC2034
late_dir=$3

exec >/dev/kmsg 2>&1

if grep "kde-linux.live=1" /proc/cmdline; then
    echo "kde-linux.live=1 in cmdline. Not generating mount units."
    exit 0
fi

if [ "$SYSTEMD_IN_INITRD" = "1" ]; then

    # The transition service in case the system needs transitioning still

    cat <<- EOF > "$late_dir/kde-linux-rootfs-transition.service"
# Generated by $(basename "$0")
[Unit]
Description=Root Partition Transition
DefaultDependencies=no
Before=initrd-root-fs.target
Requires=systemd-fsck-root.service
After=systemd-fsck-root.service
Before=sysroot.mount
After=blockdev@dev-gpt\x2dauto\x2droot.target
# Let vconsole-setup do its setup before starting user interaction:
After=systemd-vconsole-setup.service

[Service]
ExecStart=/usr/lib/rootfs-transition
Type=oneshot
StandardOutput=tty
StandardInput=tty
StandardError=tty
RemainAfterExit=yes
EOF

    mkdir "$late_dir/sysroot.mount.requires/" || true
    ln -s ../kde-linux-rootfs-transition.service "$late_dir/sysroot.mount.requires/kde-linux-rootfs-transition.service"

    # The /system directory inside the root

    cat <<- EOF > "$late_dir/sysroot-system.mount"
# Generated by $(basename "$0")
[Unit]
Description=System Volume
Before=initrd-fs.target
After=sysroot.mount
After=blockdev@dev-gpt\x2dauto\x2droot.target

[Mount]
What=/dev/gpt-auto-root
Where=/sysroot/system
Options=rw,nodev,suid,exec,subvol=/,compress=zstd:-4
DirectoryMode=0700
EOF

    mkdir "$late_dir/sysroot.mount.wants/" || true
    ln -s ../sysroot-system.mount "$late_dir/sysroot.mount.wants/sysroot-system.mount"

    # The usr in /sysusr/usr
    cat <<- EOF > "$normal_dir/sysusr-usr.mount"
# Generated by $(basename "$0")

[Unit]
Before=initrd-usr-fs.target
After=imports.target
After=blockdev@dev-gpt\x2dauto\x2droot.target

[Mount]
What=/sysroot/system/kde-linux_${IMAGE_VERSION}.erofs
Where=/sysusr/usr
Options=ro,X-mount.subdir=usr
EOF

    mkdir "$normal_dir/initrd-usr-fs.target.requires/" || true
    ln -s ../sysusr-usr.mount "$normal_dir/initrd-usr-fs.target.requires/sysusr-usr.mount"

    # The usr bound from /sysusr/usr to /sysroot/usr (this mimics systemd's generator behavior)
    cat <<- EOF > "$normal_dir/sysroot-usr.mount"
# Generated by $(basename "$0")

[Unit]
Before=initrd-usr-fs.target

[Mount]
What=/sysusr/usr
Where=/sysroot/usr
Options=bind
EOF

    mkdir "$normal_dir/initrd-fs.target.requires/" || true
    ln -s ../sysroot-usr.mount "$normal_dir/initrd-fs.target.requires/sysroot-usr.mount"

    exit 0
fi

cat <<- EOF > "$normal_dir/var-lib-extensions-debug.mount"
# Generated by $(basename "$0")
[Unit]
Description=Mount unit for /var/lib/extensions/debug
ConditionPathExists=/system/@kde-linux_debug_${IMAGE_VERSION}
Before=systemd-sysext.service
After=var.mount

[Mount]
What=/system/@kde-linux_debug_${IMAGE_VERSION}
Where=/var/lib/extensions/debug
Type=none
Options=bind,nodev,x-gdu.hide,x-gvfs-hide

[Install]
WantedBy=systemd-sysext.service
EOF

mkdir "$normal_dir/systemd-sysext.service.wants/" || true
ln -s ../var-lib-extensions-debug.mount "$normal_dir/systemd-sysext.service.wants/var-lib-extensions-debug.mount"
