#!/bin/sh

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

. /scripts/casper-functions

prereqs()
{
       echo "$PREREQ"
}

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

log_begin_msg "$DESCRIPTION"

# chroot needed to handle symlinks correctly
if chroot /root [ -f /etc/gdm/gdm-cdd.conf ]; then
    GDMCONF=/etc/gdm/gdm-cdd.conf
else
    GDMCONF=/etc/gdm/gdm.conf
fi

# chroot needed to handle symlinks correctly
if chroot /root [ -f ${GDMCONF} ]; then
    # Configure GDM autologin
    chroot /root sed -i \
        -e "s/^AutomaticLoginEnable=.*\$/AutomaticLoginEnable=true/" \
        -e "s/^AutomaticLogin=.*\$/AutomaticLogin=$USERNAME/" \
        -e "s/^TimedLoginEnable=.*\$/TimedLoginEnable=true/" \
        -e "s/^TimedLogin=.*\$/TimedLogin=$USERNAME/" \
        -e "s/^TimedLoginDelay=.*\$/TimedLoginDelay=10/" \
        ${GDMCONF}
fi

if [ -f /root/etc/kde4/kdm/kdmrc ]; then
    # Configure KDM autologin
    sed -i -r \
        -e "s/^#?AutoLoginEnable=.*\$/AutoLoginEnable=true/" \
        -e "s/^#?AutoLoginUser=.*\$/AutoLoginUser=$USERNAME/" \
        -e "s/^#?AutoReLogin=.*\$/AutoReLogin=true/" \
        /root/etc/kde4/kdm/kdmrc
fi

log_end_msg
