#!/bin/sh

PREREQ=""
DESCRIPTION="Setting up automatic login..."

prereqs()
{
       echo "$PREREQ"
}

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

. /scripts/casper-functions

log_begin_msg "$DESCRIPTION"

GDMCustomFile=/root/etc/gdm3/daemon.conf
if [ -f $GDMCustomFile ]; then
    # Configure GDM autologin
    sed -i -r \
        -e "s/^#[ ]*AutomaticLoginEnable =.*\$/AutomaticLoginEnable=true/" \
        -e "s/^#[ ]*AutomaticLogin =.*\$/AutomaticLogin=$USERNAME/" \
        -e "s/^#[ ]*TimedLoginEnable =.*\$/TimedLoginEnable=false/" \
        $GDMCustomFile
fi

if [ -f /root/usr/bin/sddm ]; then
    # Configure SDDM autologin
    cat >>/root/etc/sddm.conf <<EOF
[Users]
MinimumUid=999

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

if [ -d /root/etc/lightdm ]; then
    # Configure LightDM autologin
    LightDMCustomFile=/root/etc/lightdm/lightdm.conf
    AutologinParameters="allow-guest=false\n\
autologin-guest=false\n\
autologin-user=$USERNAME\n\
autologin-user-timeout=0"

    # Prevent from updating if parameters already present (persistent usb key)
    if ! `grep -qs '^autologin-user' $LightDMCustomFile` ; then
        if ! `grep -qs '\[SeatDefaults\]' $LightDMCustomFile` ; then
            echo '[SeatDefaults]' >> $LightDMCustomFile
        fi
        sed -i "s/\[SeatDefaults\]/\[SeatDefaults\]\n$AutologinParameters/" $LightDMCustomFile
    fi
fi

log_end_msg
