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

PREREQ=""
DESCRIPTION="Disabling unnecessary Plasma services and setting up desktop..."

prereqs()
{
       echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
       prereqs
       exit 0
       ;;
esac

. /scripts/casper-functions

log_begin_msg "$DESCRIPTION"

# disable some features we don't want on the live system
SETTINGSDIR=/root/etc/xdg/
if [ -d ${SETTINGSDIR} ]; then
    # No auto locking of the session. User doesn't know password.
    cat << EOF > ${SETTINGSDIR}kscreenlockerrc
[Daemon]
Timeout=0
Autolock=false
RequirePassword=false
EOF
    # No search indexing. Useless and eating away at already meh performance
    cat << EOF > ${SETTINGSDIR}baloofilerc
[Basic Settings]
Indexing-Enabled=false
EOF
fi
# Disable release notifier. Useless on a live session.
rm -fv /root/etc/xdg/autostart/distro-release-notifier.desktop
# Disable discover notifier. Updates on a live session have no use.
rm -fv /root/etc/xdg/autostart/org.kde.discover.notifier.desktop
rm -fv /root/usr/share/kservices5/plasma-applet-org.kde.discovernotifier.desktop
rm -fv /root/usr/share/plasma/plasmoids/org.kde.discovernotifier/metadata.*
# Also disable all of snapd refreshing.
rm -fv /root/lib/systemd/system/snapd.refresh*
# Also disable snapd itself (can still be activated via socket). But we want
# no auto-start as it will also refresh itself after a while.
# The /*.wants/ are the symlink dirs where services get sylinked to associate
# with a target.
rm -fv /root/etc/systemd/system/*.wants/snapd.service
rm -fv /root/lib/systemd/system/*.wants/snapd.service

# Make sure ubiquity-dm is not going to run! It's the service which lets you
# choose "try" or "install now". In neon we opt to have the try mode by default
# (i.e. a regular desktop with the installer icon on the desktop).
rm -fv /root/lib/systemd/system/ubiquity.service
rm -fv /root/lib/systemd/system/*.wants/ubiquity.service

# Override ubuntu's sddm autologin config, it's not good enough. Notably
# we'll want to set Relogin to not ever end up in a scenario where sddm shows
# the login prompt as the users will not know what the password is or what to
# do.
if [ -f /root/usr/bin/sddm ] && [ -f /root/usr/bin/startplasmamobile ]; then
    cat << EOF > /root/etc/sddm.conf
[Users]
MinimumUid=999

[Autologin]
User=$USERNAME
Session=/usr/share/wayland-sessions/plasma-mobile.desktop
Relogin=true
EOF
elif [ -f /root/usr/bin/sddm ] && [ -f /root/usr/bin/plasma-bigscreen-wayland ]; then
    cat << EOF > /root/etc/sddm.conf
[Users]
MinimumUid=999

[Autologin]
User=$USERNAME
Session=plasma-bigscreen-wayland.desktop
Relogin=true
EOF
elif [ -f /root/usr/bin/sddm ]; then
    cat << EOF > /root/etc/sddm.conf
[Users]
MinimumUid=999

[Autologin]
User=$USERNAME
Session=plasma.desktop
Relogin=true
EOF
fi


# Lower the minimum UID so software (e.g. Dolphin's Share tab) doesn't trip over the fact that the system
# says the lowest "user" uid is 1000 but then the current user is clearly below that.
sed -i -E 's/(^\s*UID_MIN\s+)([0-9]+)/\1 999/' /root/etc/login.defs

# Replace discover with ubiquity or calamares, cheap hack to get the installers
# into kickoff.
if [ -f /root/usr/share/applications/kde4/ubiquity-kdeui.desktop ]; then
    sed -i s/org.kde.discover/org.kde.discover,kde4-ubiquity-kdeui/ /root/usr/share/plasma/plasmoids/org.kde.plasma.kickoff/contents/config/main.xml
elif [ -f /root/usr/share/applications/calamares.desktop ]; then
    sed -i s/org.kde.discover/org.kde.discover,calamares/ /root/usr/share/plasma/plasmoids/org.kde.plasma.kickoff/contents/config/main.xml

    # Make sure calamares.desktop gets added to the Desktop so it's readily available
    # For Ubiquity this is done in some other upstream casper script.
    file=/usr/share/applications/calamares.desktop
    chroot /root install -d -o $USERNAME -g $USERNAME $file /home/$USERNAME/Desktop
    chroot /root install -D -o $USERNAME -g $USERNAME $file /home/$USERNAME/Desktop/calamares.desktop
fi
# Force the plasma containment to display the Folder content (installer link).
sed -i s/Containment=org.kde.desktopcontainment/Containment=org.kde.plasma.folder/ /root/usr/share/plasma/shells/org.kde.plasma.desktop/contents/defaults

# Hack casper-stop to pause progress while waiting for the user to press enter.
# This should make it harder to miss the need for input.
sed -i 's/plymouth watch-keystroke/plymouth pause-progress; &/g' /sbin/casper-stop

chroot /root install -d -o $USERNAME -g $USERNAME $file /home/$USERNAME/.config || true
cat << EOF > /root/home/$USERNAME/.config/kwalletrc
[Wallet]
Enabled=false
EOF

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

log_end_msg
