#!/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-label/KDELinuxLive)" != "$(readlink --canonicalize /dev/gpt-auto-root)" ]; then
    echo "gpt-auto-root is not KDELinuxLive"
    exit 1
fi

mkdir --mode 0755 /run/extensions/
ln -s /system/@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

mkdir /etc/sddm.conf.d/
cat << EOF > /etc/sddm.conf.d/live.conf
[Autologin]
User=live
Session=plasma.desktop
# Do not relogin for now it may cause login loops of death
# Relogin=true
EOF

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

# 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

# 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 -p /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
