#!/bin/sh
#
# Copyright (C) 2002-2006  Dmitry V. Levin <ldv@altlinux.org>
#
# Updates window manager session lists.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

[ -z "${RPM_INSTALL_NAME-}" ] || exit 0
runwmlist="$(runwm --list)" || exit 1
[ -n "$runwmlist" ] || exit 1

# KDM
if [ -s /etc/X11/kdm/kdmrc ]; then
	subst "s|^\\(SessionTypes\\)=.*|\\1=$(printf %s "$runwmlist" |tr '\n' ,)|" \
		/etc/X11/kdm/kdmrc
fi

# WDM
if [ -s /etc/X11/wdm/wdm-config ]; then
	subst "s|^\\(DisplayManager\\*wdmWm\\):.*|\\1:$(printf %s "$runwmlist" |tr '\n' :)|" \
		/etc/X11/wdm/wdm-config
fi

# GDM (old scheme)
if [ -d /etc/X11/gdm/Sessions ]; then
	rm -f /etc/X11/gdm/Sessions/*
	printf '%s\n' "$runwmlist" |while read n; do
		[ -n "$n" ] || continue
		cat >"/etc/X11/gdm/Sessions/$n" <<__EOF__
#!/bin/sh
exec /etc/X11/Xsession "$n"
__EOF__
		chmod 755 "/etc/X11/gdm/Sessions/$n"
	done
fi

# GDM (new scheme)
if [ -d /etc/X11/sessions ]; then
	rm -f /etc/X11/sessions/*
	printf '%s\n' "$runwmlist" |while read n; do
		[ -n "$n" ] || continue
		cat >"/etc/X11/sessions/$n.desktop" <<__EOF__
[Desktop Entry]
Encoding=UTF-8
Name=$n
Comment=$n session
Exec=/etc/X11/Xsession "$n"
Icon=
Type=Application
__EOF__
	done
fi

# external DM configuration
for hook in /etc/X11/wms-methods.d/*; do
	[ -x "$hook" ] && "$hook"
done

exit 0
