aboutsummaryrefslogtreecommitdiffstats
path: root/sbin/chksession
blob: d0bec64b82c75a048a460d951944be51ee9b5b3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/perl
# (c) MandrakeSoft, Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Copyright under GPL blah blah blah.
## you'll don't find much info here, see --help.

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.
    -f=FILE, --file=FILE:	Specify an alternarte config files other
				than /etc/X11/window-manager
-x=ENTRY, --xsession=ENTRY:	Product window-managers script of ENTRY.
-r=ENTRY, --remove=ENTRY:	remove an entry (product to stdin).
	      -k,  --kdm:	Product window-managers list for kdm sessions.
	      -g,  --gdm:	Product window-managers script for gdm sessions.
	      -h, --help:	Product this help.

EOF
  exit($e);
}
  
sub parse_file {
  my $f=shift @_;
  open F, $f or die "Can't open $f\n";
  local $/ = "--@@--";
  while (<F>) {
     $n = $1 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; $exec{$n} = $e; $desc{$n} = $d; $icon{$n} = $i; push @lf, $n; }
  }
}

sub remove_entry {
  my $e = shift @_;
  my $f = shift @_;
  open F, $f or die "Can't open $f\n";
  local $/ = "--@@--";
  while (<F>) {
    next if /^NAME=$e/m;
    print;
  }
}

usage(1)
  if @ARGV < 1;

while ($ARGV[0] =~ /^--/ || $ARGV[0] =~ /^-/) {
  $_ = shift;
  if (/^--xsession=([^ ]+)/ || /^-x=([^ ]+)/) {
    $xsession = $1;
  } elsif (/^--first/ || /^-F/) {
    $first++;
  } elsif (/^--gdm/ || /^-g/) {
    die "You should be root to build gdm session\n" if $< != 0;
    $gdm++;
  } elsif (/^--list/ || /^-l/) {
    $list++;
  } elsif (/^--kdmsess/ || /^-k/) {
    $kdm++;
  } elsif (/^--test/ || /^-t/) {
    $test++;
  } elsif (/^--file=([^ ]+)/ || /^-f=([^ ]+)/) {
    $file = $1;
  } elsif (/^--remove=([^ ]+)/ || /^-r=([^ ]+)/) {
    $remove=$1;
  } elsif (/^--help/ || /^-h/ || /^-\?/) {
    usage(0);
  } else {
    print STDERR "Unrecognized switch: $_\n";
    usage(1);
  }
}
  
$file = $test ? './window-managers' : '/etc/X11/window-managers' unless $file;
die "$file don't exist\n" unless -r $file;
$remove ? remove_entry($remove, $file) : parse_file ($file);

sub cat { my ($f) = @_; local *F; open F, $f; join '', <F> }
my ($e) = cat("/etc/sysconfig/desktop") =~ /(\S+)/;
@lf = sort { $b =~ /$e/i <=> $a =~ /$e/i } @lf;

if ($kdm) {
  $session="SessionTypes=";
  for(@lf) { $session .= /$lf[-1]/ ? "$_;failsafe;default" : "$_;"  }
  print "$session\n";
  exit(0);
}


if ($gdm) {
  my $d = '/etc/X11/gdm/Sessions/';
  chdir $d;

  #Any better way ?
  opendir D, $d or die "Can't open $d\n";
  for ( readdir(D)) { next if /^\.\.?/;next if /^Failsafe/; chmod 0644, $_; }
  closedir D;

  for my $file (@lf) {
    open FH, ">/etc/X11/gdm/Sessions/$file" or die "Can't write to /etc/X11/gdm/Sessions/$file\n";
    print FH "#!/bin/sh";
    print FH $script{$file};
    close FH;
    chmod 0755, $file;
  }
  unlink 'Default';
  symlink shift @lf, 'Default';
}

if ($xsession) {
  if ($script{$xsession}) {
    print "#!/bin/sh\n";
    print $script{$xsession}
  } else {
    print "xterm -geometry 100x25+0+0 &\n";
    -x '/usr/X11R6/bin/icewm' ? print "icewm\n" : print "xterm -geometry 67x14+384+446\n";
  }
  exit (0);
}

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

if ($list) { 
  if (@lf) {
    my $p; for(@lf) { $p .= /$lf[-1]/ ? "$_ default failsafe" : "$_ " } print "$p\n";
 } else {
    print "Default\n";
  }
}