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

if [ -d /root/etc/gdm ]; then
    # Configure GDM autologin
    GDMCustomFile=/root/etc/gdm/custom.conf
    AutologinParameters="AutomaticLoginEnable=true\n\
AutomaticLogin=$USERNAME\n\
TimedLoginEnable=true\n\
TimedLogin=$USERNAME\n\
TimedLoginDelay=10"

    # 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

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

if [ -f /root/etc/lxdm/lxdm.conf ]; then
    # Configure LXDM autologin with LXDE session
   sed -i -r \
        -e "s/^# autologin=dgod/autologin=$USERNAME/" \
        -e "s/^# session/session/" \
        -e "s/startlxde/startlubuntu/" \
        /root/etc/lxdm/lxdm.conf
fi

if [ -f /root/etc/xdg/lubuntu/lxdm/lxdm.conf ]; then
    # Configure LXDM autologin with Lubuntu session
   sed -i -r \
        -e "s/^# autologin=dgod/autologin=$USERNAME/" \
        -e "s/^# session/session/" \
        -e "s/startlxde/startlubuntu/" \
        /root/etc/xdg/lubuntu/lxdm/lxdm.conf
fi

log_end_msg
