#!/usr/bin/perl
# (c) littletux <littletux@mandriva.org> 2005-2006
# inspired from chksession 
# (c) MandrakeSoft, Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Copyright under GPL blah blah blah.
# For info, see "entrance_config_update --help"
# or "man entrance_config_update" ( well, no man page yet:( )

my @lf;

sub usage {
  my $e = shift @_;
  $0 =~ s|.*/||;
  print {  $e ? STDERR : STDOUT } << "EOF";
Usage: $0 [OPTION]...

              -F --first: Print only first available entry.
	      -t, --test: Go in test mode.
	      -l, --list: List window-managers.
                      -L: List window-managers including the order number
       -d=DIR, --dir=DIR: Specifies a directory of w-m configuration files.
                          Default is /etc/X11/wmsession.d/
   -i=DIR, --icondir=DIR: Specifies a directory of session icon files.
                          Default is /usr/share/entrance/images/sessions/
  -c=FILE, --config=FILE: Specifies a config file for entrance
                          Default is /etc/entrance_config.cfg
         -e,  --entrance: Actually produce window-managers config for entrance sessions.
	     -h,  --help: Produce this help.

Warning: This utility does not generate a complete entrance config file.
         It just updates window manager sessions. This version of this tool
	 is intended to work for entrance 0.9.0.007 only (afaik).
EOF
  exit($e);
}

sub cat {   # returns content of argument file as a single string
  my ($f) = @_;
  local *F;
  open F, $f or die "Can't open $f\n";
  local $/ = "";
  <F>
  }
  
sub parse_file {         # parse a session descriptor file
    my ($fn) = @_;
     $_ = cat ($fn);
     ($n = $1) =~ s| ||g if /^NAME=(.*)/m;
     $e = $1 if /^EXEC=(.*)/m;
#     $d = $1 if /^DESC=(.*)/m;
#     $i = $1 if /^ICON=(.*)/m;
     $s = $1 while /SCRIPT:(.*?)$/gs; chomp $s;
     if (-x $e) { $script{$n} = $s; $exe{$n} = $e; push @lf, $n; ($order{$n}) = $fn =~ m/(^[0-9][0-9])/; }
#     if (-x $e) { $script{$n} = $s; $exec{$n} = $e; $desc{$n} = $d; $icon{$n} = $i; push @lf, $n; }
}

sub add_wm ($) {
  $sessionname = shift;
  system("ecore_config -c $config -k /entrance/session/$nbsess/session -s $sessionname");
  system("ecore_config -c $config -k /entrance/session/$nbsess/title -s $sessionname");  
  $iconfile=$icondir . "/" . lc($sessionname) . ".png";
  if (-f $iconfile) {
    $iconname=lc($sessionname) . ".png";
  }else{
    $iconname="default.png";
  }
  system("ecore_config -c $config -k /entrance/session/$nbsess/icon -s $iconname");
  $nbsess++;
}

sub update_entrance_config {
  $nbsess=0;
  if (@lf) {
    for my $session (@lf) {
      add_wm($session);
    }
  }else{
    print "No session found ! Only Failsafe will be available...\n";
  }
  add_wm("Failsafe");
  system("ecore_config -c $config -k /entrance/session/count -i $nbsess");
}

usage(1)
  if @ARGV < 1;

while ($ARGV[0] =~ /^--/ || $ARGV[0] =~ /^-/) {
  $_ = shift;
  if (/^--first/ || /^-F/) {
    $first++;
  } elsif (/^--list/ || /^-l/) {
    $list++;
  } elsif (/^-L/) {
    $list_order++;
  } elsif (/^--entrance/ || /^-e/) {
    die "You should be root to build entrance session\n" if $< != 0;
    $entrance++;
  } elsif (/^--test/ || /^-t/) {
    $test++;
  } elsif (/^--dir=([^ ]+)/ || /^-d=([^ ]+)/) {
    $dir = $1;
  } elsif (/^--config=([^ ]+)/ || /^-c=([^ ]+)/) {
    $config = $1;
  } elsif (/^--icondir=([^ ]+)/ || /^-i=([^ ]+)/) {
    $icondir = $1;
  } elsif (/^--help/ || /^-h/ || /^-\?/) {
    usage(0);
  } else {
    print STDERR "Unrecognized switch: $_\n";
    usage(1);
  }
}
  
# Parse all relevant files in session directory $dir
$dir = $test ? './wmsession.d/' : '/etc/X11/wmsession.d/' unless $dir;
chdir $dir; 
for (<*>) {
    next if /.*~/;
    next if /.*\.rpm(save|old)/;
    parse_file ("$_");
}

$config  = "/etc/entrance_config.cfg" unless $config;

$icondir = "/usr/share/entrance/images/sessions/" unless $icondir;

my ($e) = eval {cat("/etc/sysconfig/desktop")} =~ /DESKTOP=(\S+)/;
# The first string (without spaces) in the file is copied to $e.

# If $e is one of the names in @lf, then it is placed first (leftmost).
# Order of names in @lf is otherwise unchanged.
@lf = sort { $b =~ /^$e$/i <=> $a =~ /^$e$/i } @lf;

if ($entrance) {
    update_entrance_config();
}

@lf ? print shift @lf, "\n" : print "Default\n"
  if $first;

if ($list) { 
  if (@lf) {
    print join(' ', @lf, 'failsafe'), "\n";
 } else {
    print "Default\n";
  }
} elsif ($list_order) { 
    print join(' ', map { "$_=$order{$_}" } @lf), "\n";
}

