#!/bin/sh

# use fred for .face
cp /usr/share/icons/hicolor/scalable/emblems/emblem-aptosid-fred.svg /etc/skel/.face

adduser --disabled-password --gecos "${FLL_LIVE_USER}" "${FLL_LIVE_USER}"

# Create sudoers.d snippet
if [ ! -e "/etc/sudoers.d/15_${FLL_LIVE_USER}" ]; then
    # append sudoers entry
    cat >> "/etc/sudoers.d/15_${FLL_LIVE_USER}" <<EOF
# WARNING: This allows the unprivileged ${FLL_LIVE_USER} user to start commands as root
# WARNING: This is totally insecure and (almost) makes ${FLL_LIVE_USER} a second root account.
# WARNING: Never allow external access to the ${FLL_LIVE_USER} user!!!
${FLL_LIVE_USER} ALL=(ALL:ALL) NOPASSWD: ALL
EOF
    chmod 0440 "/etc/sudoers.d/15_${FLL_LIVE_USER}"
fi

# disable screen lock for passwdless live user
case "${FLL_XSESSION}" in
    cinnamon)
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.cinnamon.desktop.lockdown disable-lock-screen true
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.cinnamon.desktop.screensaver lock-enabled false
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.cinnamon.desktop.session idle-delay 0
        ;;
    gnome|budgie*)
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.gnome.desktop.lockdown disable-lock-screen true
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.gnome.desktop.screensaver lock-enabled false
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- gsettings set \
            org.gnome.desktop.session idle-delay 0
        ;;
    plasma*)
        if [ ! -e /etc/xdg/kscreenlockerrc ]; then
            cat >> /etc/xdg/kscreenlockerrc <<EOF
[Daemon]
Autolock=false
LockOnResume=false
EOF
        fi
        ;;
    xfce)
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-session \
            --property /general/LockCommand --type string \
            --set /usr/bin/true
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-session \
            --property /shutdown/ShowSwitchUser --type bool \
            --set false
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-screensaver \
            --property /lock/enabled --type bool \
            --set false
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-screensaver \
            --property /lock/sleep-activation --type bool \
            --set false
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-power-manager \
            --property /xfce4-power-manager/presentation-mode --type bool \
            --set true
        sudo -u "${FLL_LIVE_USER}" dbus-run-session -- xfconf-query \
            --create --channel xfce4-power-manager \
            --property /xfce4-power-manager/lock-screen-suspend-hibernate \
            --type bool --set false
        ;;
esac
