#!/usr/bin/perl
# DrakxDM -- Display Manager chooser
# Copyright (C) 2003-2005 Mandriva (tvignaud@mandriva.com)
#
# 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use strict;
use lib qw(/usr/lib/libDrakX);

use standalone;     #- warning, standalone must be loaded very first, for 'explanations'
use common;
use any;
use interactive;
use services;

$ugtk2::wm_icon = "/usr/share/mcc/themes/default/drakedm-mdk.png";

my $in = 'interactive'->vnew('su');

# directory in which display managers put a config file
my $dm_dir = '/etc/X11/dm.d';

my $cfg_file = '/etc/sysconfig/desktop';

my %dm = ('GNOME' => [ N("GDM (GNOME Display Manager)"), '/usr/bin/gdm', 'gdm' ],
          'KDM'   => [ N("KDM (KDE Display Manager)"), '/usr/bin/kdm', 'kdebase-kdm' ],
          'KDE'   => [ N("MdkKDM (Mandriva Linux Display Manager)"), '/usr/bin/mdkkdm', 'mdkkdm' ],
          'XDM'   => [ N("XDM (X Display Manager)"), '/usr/bin/X11/xdm', 'xorg-x11' ],
          );

# add non hard-wired display managers
foreach (grep { !/\.rpm/ } glob("$dm_dir/*")) {
    my %l = map { /(\S+)=(.*)/ } cat_($_);
    if ($l{NAME}) {
	$dm{$l{NAME}} ||= [ $l{DESCRIPTION}, $l{EXEC}, $l{PACKAGE} ];
    }
}

my $dm;

foreach (cat_($cfg_file)) {
    $dm = uc($1) if /^DISPLAYMANAGER=(.*)$/; 
}

if (!$dm) {
    $dm = 'KDE';
    log::explanations("Defaulting to $dm for display manager")
}

my @raw_list = sort keys %dm;
my @list = $::expert ? @raw_list : (grep { -e $dm{$_}->[1] } @raw_list);

start:
if ($in->ask_from(N("Choosing a display manager"),
                               formatAlaTeX(N("X11 Display Manager allows you to graphically log
into your system with the X Window System running and supports running
several different X sessions on your local machine at the same time.")),
                               [
                                {
                                 allow_empty_list => 1,
                                 list => \@list,
                                 val => \$dm,
                                 type => 'list',
                                 format => sub { $dm{$_[0]}[0] },
                                 sort => 1,
                                }
                               ]
                              )
   ) {
    ! -x $dm{$dm}[1] and do { $in->do_pkgs->ensure_is_installed($dm{$dm}[2], $dm{$dm}[1]) or goto start };
    substInFile {
        s/^(DISPLAYMANAGER)=.*(\n|)//;
        s/^\n//g;
        $_ .= "\nDISPLAYMANAGER=$dm\n" if eof;
    } $cfg_file;
    log::explanations(qq(Switching to "$dm" display manager));
    if (any::running_window_manager()) {
        $in->ask_yesorno('', N("The change is done, do you want to restart the dm service?"), 1) and
				$in->ask_yesorno('', N("You are going to close all running programs and lose your current session. Are you really sure that you want to restart the dm service?"), 1) and
              run_program::run("nohup", "/etc/rc.d/init.d/dm", "restart");
    }
}

$in->exit(0);