#!/usr/bin/perl -w
# update_gdm_session, (c) 2002 David Dooling <banjo@users.sourceforge.net>
# adapted from
# update_wdm_wmlist, (c) 1998 Marcelo Magallón <mmagallo@debian.org>
# rewriten to use the x-window-manager alternative
# modified to also use the x-session-manager alternative by Arthur Korn
# Copyright 2000 Wichert Akkerman <wakkerma@debian.org>
#
# This script will read the list of installed window managers from
# the update-alternatives output for the x-window-manager alternative
# and add any window managers that do not have a Gnome session to
# /etc/gdm/Sessions.  Any window managers that already have sessions
# will not be affected

use strict;

my @wm_list;
unless (@ARGV) {
    foreach my $alternative ( qw(x-window-manager) ) {
	open(WINDOW_MANAGERS, "update-alternatives --display $alternative |")
	    or die "Can't run update-alternatives: $!";
	
	while (<WINDOW_MANAGERS>) {
	    push(@wm_list, $1) if ( m,^(/\S+) - ,);
	}
	
	close(WINDOW_MANAGERS);
    }
} else {
    @wm_list = @ARGV;
}

foreach my $wm_path (@wm_list) {
    my $wm = $wm_path;
    $wm =~ s,/.*/,,;
    my $session = "/etc/gdm/Sessions/$wm";
    unless (-f $session) {
	open(SESSION, ">$session") or die "Couldn't open $session: $!";
	print SESSION &wm_session_text($wm_path);
	close(SESSION);
	chmod 0755, $session or die "Couldn't chmod $session: $!";
    }
}

exit 0;

sub wm_session_text
{
    my ($wm_path) = @_;
    my $text = <<'EOF';
#!/bin/sh
# start up x window manager

case $# in
1)
	case $1 in
	failsafe)
		exec xterm -geometry 80x24-0-0
		;;
	esac
esac

# redirect errors to a file in user's home directory if we can
for errfile in "$HOME/.xsession-errors" "${TMPDIR-/tmp}/xses-$USER" "/tmp/xses-$USER"
do
	if ( cp /dev/null "$errfile" 2> /dev/null )
	then
		chmod 600 "$errfile"
		exec > "$errfile" 2>&1
		break
	fi
done

# clean up after xbanner
if which freetemp 2> /dev/null ; then
	freetemp
fi

startup=$HOME/.xsession

userresources=$HOME/.Xresources 
usermodmap=$HOME/.Xmodmap 
userxkbmap=$HOME/.Xkbmap

sysresources=/etc/X11/Xresources 
sysmodmap=/etc/X11/Xmodmap 
sysxkbmap=/etc/X11/Xkbmap

rh6sysresources=/etc/X11/xinit/Xresources 
rh6sysmodmap=/etc/X11/xinit/Xmodmap 


# merge in defaults
if [ -f "$rh6sysresources" ]; then
    xrdb -merge "$rh6sysresources"
fi

if [ -f "$sysresources" ]; then
    xrdb -merge "$sysresources"
fi

if [ -f "$userresources" ]; then
    xrdb -merge "$userresources"
fi

# merge in keymaps
if [ -f "$sysxkbmap" ]; then
    setxkbmap `cat "$sysxkbmap"`
    XKB_IN_USE=yes
fi

if [ -f "$userxkbmap" ]; then
    setxkbmap `cat "$userxkbmap"`
    XKB_IN_USE=yes
fi

#
# Eeek, this seems like too much magic here
#
if [ -z "$XKB_IN_USE" -a ! -L /etc/X11/X ]; then
    if grep '^exec.*/Xsun' /etc/X11/X > /dev/null 2>&1 && [ -f /etc/X11/XF86Config ]; then
       xkbsymbols=`sed -n -e 's/^[     ]*XkbSymbols[   ]*"\(.*\)".*$/\1/p' /etc/X11/XF86Config`
       if [ -n "$xkbsymbols" ]; then
           setxkbmap -symbols "$xkbsymbols"
           XKB_IN_USE=yes
       fi
    fi
fi

# xkb and xmodmap don't play nice together
if [ -z "$XKB_IN_USE" ]; then
    if [ -f "$rh6sysmodmap" ]; then
       xmodmap "$rh6sysmodmap"
    fi

    if [ -f "$sysmodmap" ]; then
       xmodmap "$sysmodmap"
    fi

    if [ -f "$usermodmap" ]; then
       xmodmap "$usermodmap"
    fi
fi

unset XKB_IN_USE

# start xscreensaver
/usr/bin/xscreensaver -no-splash &

# set window manager
EOF

    $text .= "xwm='$wm_path'\n";
    $text .= <<'EOF';
if [ -x "$xwm" ]; then
    exec $xwm
fi
exit 1
EOF
    return $text;
}
