#!/usr/bin/perl -w
#
# update_slim_wmlist, based on:
# 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>
# Modified to use the freedesktop.org .desktop like kdm and gdm
#
# This script will read the list of installed window managers from
# the freedesktop .desktop files in <etc>/X11/sessions/:<etc>/dm/Sessions/:
# <share>/xsessions/
# and update the sessions line in /etc/slim.conf.
# BEWARE: It doesn't ask any questions about this. It just does it. It
# takes an optional list of window managers.

use strict;
use File::DesktopEntry;

my $wm_list='';
my %desktop_files;

unless (@ARGV) {
    #my @wm_list = ('default');
    my @wm_list;
    foreach my $dir ('/etc/X11/sessions/','/etc/dm/Sessions/','/usr/share/xsessions/') {
	    next unless (opendir DIR, $dir);
	    my @files;
	    @files = grep { /\.desktop$/ && -r "$dir/$_" } readdir(DIR);
	    foreach my $file (@files) {
		   push @{$desktop_files{$file}}, "$dir/$file";
	    }
    }
    DESKTOP: foreach my $desktop_file (keys(%desktop_files)) {
	    foreach my $file (@{$desktop_files{$desktop_file}}) {
		    my $entry = File::DesktopEntry->new_from_file($file);
		    next DESKTOP if (defined($entry->get_value('Hidden'))
			and $entry->get_value('Hidden') eq 'true');
		    if ($entry->get_value('Name') =~ /^gnome$/i) {
			    push (@wm_list, 'gnome-session');
		    }
		    elsif ($entry->get_value('Name') =~ /^kde$/i) {
			    push (@wm_list, 'startkde');
		    }
		    elsif ($entry->get_value('Name') =~ /^Linux Mint Fluxbox$/i) {
			    push (@wm_list, 'startmintfluxbox');
		    }
		    elsif ($entry->get_value('Name') =~ /^fluxbox$/i) {
			    push (@wm_list, 'startfluxbox');
		    }
		    elsif ($entry->get_value('Name') =~ /^xfce4$/i) {
			    push (@wm_list, 'startxfce4');
		    }
		    elsif ($entry->get_value('Name') =~ /^Openbox Session$/i) {
			    push (@wm_list, 'openbox-session');
		    }
		    elsif ($entry->get_value('Comment') =~ /^Use the Openbox window manager inside of the GNOME desktop environment$/i) {
			    push (@wm_list, 'openbox-gnome-session');
		    }
		    elsif ($entry->get_value('Comment') =~ /^Use the Openbox window manager inside of the K Desktop Environment$/i) {
			    push (@wm_list, 'openbox-kde-session');
		    }
		    elsif ($entry->get_value('Name') =~ /^IceWM$/i) {
			    push (@wm_list, 'icewm-session');
		    }
		    elsif ($entry->get_value('Name') =~ /^Window maker$/i) {
			    push (@wm_list, 'wmaker');
		    }
		    elsif ($entry->get_value('Name') =~ /^Secure Remote connection$/i) {
			    push (@wm_list, 'gdm-ssh-session');
		    }
		    elsif ($entry->get_value('Name') =~ /^LXDE$/i) {
			    push (@wm_list, 'startlxde');
		    }
		    elsif ($entry->get_value('Name') =~ /^IceWM-Experimental$/i) {
			    push (@wm_list, 'icewm-session-experimental');
		    }
		    elsif ($entry->get_value('Name') =~ /^IceWM-Lite$/i) {
			    push (@wm_list, 'icewm-session-lite');
		    }
		    elsif (defined($entry->get_value('Exec'))) {
			    push (@wm_list, $entry->get_value('Exec'));
		    }
		    else { # not found, go to next file
			    next;
		    }
		    # found, proceed to next destop file
		    next DESKTOP;
	    }
    }
   $wm_list = join (',', sort @wm_list) . ',';
} else {
    $wm_list = join (',', sort @ARGV);
}

if ( ! -e "/etc/slim.conf")
{
	use File::Copy;
	copy '/usr/share/doc/slim/slim.conf', '/etc/slim.conf' or die "Can't copy /usr/share/doc/slim/slim.conf to /etc/slim.conf: $!";
}

if ( -e "/etc/slim.conf")
{
	open (SLIM_CONFIG_FILE, '</etc/slim.conf') or die "Can't open /etc/slim.conf for reading: $!";
        open (NEW_SLIM_CONFIG_FILE, '>/etc/slim.conf.new') or die "Can't open /etc/slim.conf.new for writing: $!";
        while (<SLIM_CONFIG_FILE>) { s|^(sessions\s).*|$1$wm_list|;
                 print NEW_SLIM_CONFIG_FILE;
                            };
   close(SLIM_CONFIG_FILE);
   close(NEW_SLIM_CONFIG_FILE);
   rename '/etc/slim.conf.new', '/etc/slim.conf' or die "Can't rename /etc/slim.conf.new: $!";
}

if ( -e "/etc/slim-cdd.conf")
{
	open (SLIM_CONFIG_FILE, '</etc/slim-cdd.conf') or die "Can't open /etc/slim-cdd.conf for reading: $!";
        open (NEW_SLIM_CONFIG_FILE, '>/etc/slim-cdd.conf.new') or die "Can't open /etc/slim-cdd.conf.new for writing: $!";
        while (<SLIM_CONFIG_FILE>) { s|^(sessions\s).*|$1$wm_list|;
                 print NEW_SLIM_CONFIG_FILE;
                            };
   close(SLIM_CONFIG_FILE);
   close(NEW_SLIM_CONFIG_FILE);
   rename '/etc/slim-cdd.conf.new', '/etc/slim-cdd.conf' or die "Can't rename /etc/slim-cdd.conf.new: $!";
}

if ( -e "/etc/slim-custom.conf")
{
	open (SLIM_CONFIG_FILE, '</etc/slim-custom.conf') or die "Can't open /etc/slim-custom.conf for reading: $!";
	open (NEW_SLIM_CONFIG_FILE, '>/etc/slim-custom.conf.new') or die "Can't open /etc/slim-custom.conf.new for writing: $!";
	while (<SLIM_CONFIG_FILE>) {
    s|^(sessions\s).*|$1$wm_list|;
    print NEW_SLIM_CONFIG_FILE;
				};
	close(SLIM_CONFIG_FILE);
	close(NEW_SLIM_CONFIG_FILE);
	rename '/etc/slim-custom.conf.new', '/etc/slim-custom.conf' or die "Can't rename /etc/slim-custom.conf.new: $!";
}
exit 0;
