#!/bin/sh

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

[ -r /scripts/functions ] && . /scripts/functions

prereqs()
{
       echo "$PREREQ"
}

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

log_begin_msg "$DESCRIPTION"

# Enable autologin for getty(1).
if [ -f ${rootmnt}/lib/systemd/system/getty@.service ]; then
	sed -i -e "s|/sbin/agetty|/sbin/casper-getty|g" \
		${rootmnt}/lib/systemd/system/getty@.service
fi

# Configure GDM autologin
if [ -d ${rootmnt}/etc/gdm ]; then
    GDMCustomFile=${rootmnt}/etc/gdm/custom.conf
    AutologinParameters="AutomaticLoginEnable=true\nAutomaticLogin=$USERNAME"

    # Prevent from updating if parameters already present (persistent usb key)
    if ! `grep -qs 'AutomaticLoginEnable' $GDMCustomFile` ; then
        if ! `grep -qs '\[daemon\]' $GDMCustomFile` ; then
            echo '[daemon]' >> $GDMCustomFile
        fi
        sed -i "s/\[daemon\]/\[daemon\]\n$AutologinParameters/" $GDMCustomFile
    fi
fi
# Configure lightdm autologin. Autologin doesn't seem to work
# with lightdm-0.2.3, will be enabled when it's fixed.
log_end_msg
exit 0

if [ -r ${rootmnt}/etc/lightdm.conf ]; then
	sed -i -e "s|^\#\(default-user=\).*|\1$USERNAME|" \
		${rootmnt}/etc/lightdm.conf
	sed -i -e "s|^\#\(default-user-timeout=\).*|\10|" \
		${rootmnt}/etc/lightdm.conf
fi

log_end_msg
