#!/usr/bin/perl -w
#
# update_wdm_wmlist, (c) 1998 Marcelo Magalln <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 update the DisplayManager*wdmWm resource in /etc/X11/wdm/wdm-config.
# BEWARE: It doesn't ask any questions about this. It just does it. It
# takes an optional list of window managers.

use strict;

my $wm_list='';

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

open (WDM_CONFIG_FILE, '</etc/X11/wdm/wdm-config')
    or die "Can't open /etc/X11/wdm/wdm-config for reading: $!";
open (NEW_WDM_CONFIG_FILE, '>/etc/X11/wdm/wdm-config.new')
    or die "Can't open /etc/X11/wdm/wdm-config.new for writing: $!";

while (<WDM_CONFIG_FILE>) {
    s|^!?\s*(.*wdmWm:\s*).*|$1$wm_list|;
    print NEW_WDM_CONFIG_FILE;
}

close(WDM_CONFIG_FILE);
close(NEW_WDM_CONFIG_FILE);

rename '/etc/X11/wdm/wdm-config.new', '/etc/X11/wdm/wdm-config'
    or die "Can't rename /etc/X11/wdm/window-managers.new: $!";

exit 0;
