#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
# SPDX-FileCopyrightText: 2016 Jonathan Riddell <jr@jriddell.org>
# SPDX-FileCopyrightText: 2017-2021 Harald Sitter <sitter@kde.org>

set -eux

if ! grep "kde-linux.live=1" /proc/cmdline; then
    echo "kde-linux.live=1 not in cmdline"
    exit 1
fi

if [ "$(readlink --canonicalize /dev/disk/by-partlabel/KDELinuxLive)" != "$(readlink --canonicalize /dev/gpt-auto-root)" ]; then
    echo "gpt-auto-root is not KDELinuxLive"
    exit 1
fi

mkdir --mode 0755 -p /run
mkdir --mode 0755 -p /run/extensions/
# Mind that /live is only available if the erofs is mounted as rootfs. On the final system we selectively only mount usr from the erofs!
ln -s /live /run/extensions/live
systemd-sysext refresh

useradd --create-home --comment "Live User" live
usermod --append --groups wheel live
passwd --delete live

# systemd is a bit unreliable with creating the file if the timezone is UTC, so make sure it's in place
ln -s ../usr/share/zoneinfo/UTC /etc/localtime

timedatectl set-timezone UTC
localectl set-locale C.UTF-8

cat << EOF >> /etc/plasmalogin.conf
[Autologin]
User=live
Session=plasma.desktop
Relogin=true
EOF

mkdir /home/live/.config/
mkdir /home/live/Desktop/

# Disable automatic screen locking
cat << EOF > /home/live/.config/kscreenlockerrc
[Daemon]
Autolock=false
LockOnResume=false
Timeout=0
EOF

# No search indexing. Useless and eating away at already meh performance
cat << EOF > /home/live/.config/baloofilerc
[Basic Settings]
Indexing-Enabled=false
EOF

cat << EOF > /home/live/.config/plasma-welcomerc
[General]
LiveEnvironment=true
LiveInstaller=calamares
EOF

# No updates check for live session
cat << EOF > /home/live/.config/PlasmaDiscoverUpdates
[Global]
RequiredNotificationInterval=-1
EOF

# KWallet is also useless in a live environment
cat << EOF > /home/live/.config/kwalletrc
[Wallet]
Enabled=false
EOF

# Allow sudo in the live environment for troubleshooting
mkdir --parents /etc/sudoers.d
cat << EOF > /etc/sudoers.d/04_live
%wheel ALL=(ALL:ALL) NOPASSWD: ALL
EOF

cp /usr/share/applications/calamares.desktop /home/live/Desktop/
chmod +x /home/live/Desktop/calamares.desktop # without this there'd be a dialog asking if it really should execute

chown -R live:live /home/live/

# Start the basic-test (if it wants to be started)
systemctl daemon-reload
systemctl start --no-block kde-linux-basic-test.service
