#!/bin/sh -ef

if [ ! -x /usr/bin/X ]; then
	echo "warning: autologin feature requested but X not installed" >&2
	exit 0
fi

USER=altlinux

# setup runlevel
INITTAB=/etc/inittab
if [ -f "$INITTAB" ]; then
	[ -z "$runlevel" ] && runlevel=5
	sed -i "s,^\(id:\)\(.*\)\(:initdefault.*\),\\1$runlevel\\3," "$INITTAB"
fi

# autologin^2
if [ -x /usr/sbin/autologin ]; then
	cat << E_O_F >> /etc/sysconfig/autologin
USER=$USER
AUTOLOGIN=yes
E_O_F
fi

# lightdm autologin
LIGHTDM_CONF=/etc/lightdm/lightdm.conf
if [ -f "$LIGHTDM_CONF" ]; then
	sed -i \
		-e "s/^#\(autologin-user=\).*$/\1$USER/" \
		-e "s/^#\(autologin-user-timeout=0\)/\1/" \
		"$LIGHTDM_CONF"
fi

# gdm2 autologin
GDM_CONF=/etc/X11/gdm/custom.conf
if [ -f "$GDM_CONF" ]; then
	sed -i -e '/\[daemon\]/aAutomaticLoginEnable=true\nAutomaticLogin='$USER \
		"$GDM_CONF"
fi

# kdm3 autologin
# TODO: iterate over kdm{,4} if feasible
KDMRC=/etc/X11/kdm/kdmrc
if [ -f "$KDMRC" ]; then
	sed -i \
		-e '/AutoLoginEnable/ s,^.*$,AutoLoginEnable=true,' \
		-e '/AutoLoginUser/ s,^.*$,AutoLoginUser='$USER',' \
		"$KDMRC"
fi
