#!/usr/bin/perl -l

my $dm = shift
  if $ARGV[0] =~ /^[xg]dm$/;

unless (defined &{$ARGV[0]}) {
    print STDERR <<EOF;
usage: xbannerconfig [xdm | gdm] install | remove | force
  install: add entries to xdm or gdm cofiguration to start xbanner
      automatically if they do not already exist
  remove: remove entries from xdm or gdm configuration to start
      xbanner automatically
  force: remove then re-add entries to xdm or gdm configuration to
      start xbanner automatically

If you do not explicitly choose a display manager, xbannerconfig will
attempt to determine which display manager you are using and configure
it for you.

EOF
    exit 1;
}

unless ($dm) {
  die "Cant' select a display manager.  Are you sure you have one installed?"
    unless (open FH, "/etc/X11/default-display-manager");

  chomp (($dm = <FH>) =~ s!^.*/!!);

  close FH;

  die "Unknown display manager: $dm"
    unless $dm =~ /^[xg]dm$/;
}

&{$dm}();
&{$ARGV[0]}();

foreach (%setup, %startup) {
    if (! ref) {
	close FH;
	open FH, "> $_.$$" or exit 1;
    }
    else {
	print FH join "\n", @$_;
    }
} 
close FH;

foreach (keys %stat) {
    rename "$_.$$", $_ or next;
    chmod ${$stat{$_}}[2], $_;
    chown @{$stat{$_}}[4,5], $_;
    utime @{$stat{$_}}[8,9], $_;
}

print "WARNING: Modification of $dm files incomplete! Please check."
    if grep {-f "$_.$$" and unlink "$_.$$"} keys %stat;

print '\u$dm files modified ',
    ($ARGV[0] eq 'remove' && 'not '),'to run xbanner.';

exit 0;

sub xdm {
  die "Can't find xdm config files. Won't try to add xbanner to xdm."
    unless (open FH, "/usr/lib/X11/xdm/xdm-config");

  foreach (<FH>) {
    next unless m{^\s*
		  DisplayManager
		  (?:\._\d+\.|\*)
		  (setup|startup)
		  \s*:\s*(?:\\\n)?\s*
		  (\S+)}x;

    die "Can't find xdm config files. Won't try to add xbanner to xdm."
      unless (-f $2  and open IN, $2);

    $stat{$2} = [stat (_)];

    my $file = $$1{$2} = [];
    push @$file, <IN>;
    chomp @$file;
    close IN;
  }
}

sub gdm {
  die "Can't find gdm config files. Won't try to add xbanner to gdm."
    unless (open FH, "/etc/X11/gdm/gdm.config");

  my ($presession, $init);
  foreach (<FH>) {
    next unless m{^\s*
		  (?:(PreSession)ScriptDir|Display(Init)Dir)
		  \s*=\s*
		  (\S+)/?}x;

    $presession = $3
      if $1 and -d $3;

    $init = $3
      if $2 and -d $3;
  }
  $init ||= "/etc/gdb/Init";
  $presession ||= "/etc/gdb/PreSession";


  for (<$init/*>) {
    die "Can't find gdm config files. Won't try to add xbanner to gdm."
      unless (-f $_  and open IN, $_);
    $stat{$_} = [stat (_)];

    my $file = $setup{$2} = [];
    push @$file, <IN>;
    chomp @$file;
    close IN;
  }

  for (<$presession/*>) {
    die "Can't find gdm config files. Won't try to add xbanner to gdm."
      unless (-f $_  and open IN, $_);
    $stat{$_} = [stat (_)];

    my $file = $startup{$2} = [];
    push @$file, <IN>;
    chomp @$file;
    close IN;
  }
}

sub force {
    remove();
    install();
}

sub remove {
    foreach (values %setup, values %startup) {
	@$_ = grep !(m!^#\s*XBanner -! ||
		     m!^\s*/usr/X11R6/bin/(?:xbanner|freetemp)!), @$_;
    }
}

sub install {
    foreach (values %setup) {
      die "xbanner already in use in $dm.  You may want the force option."
	if grep m!^\s*/usr/X11R6/bin/xbanner!, @$_;
    }

    print <<EOF;

Xbanner configuration
---------------------

A popular use of xbanner is to make it run when $dm is run, to beautify
the $dm login screen. I can modify some files to make this work, if you
want.
EOF

    {
      local $\ = undef;
      print "Should I modify $dm files to make xbanner be launched on $dm startup? [Y/n] ";
    }
    if (<STDIN> =~ m!^\s*[nN]!) {
      print "All right, I won't do that.";
      exit 0;
    }

    foreach (values %setup) {
      my $i = @$_;
      while ($i--) {
	next if /^\s*(?:$|#)/;
        $_->[$i] =~ m!^\s*(?:exit(?:\s+0)?\s*)?$!; and --$i;
	splice @$_, $i, 0, split "\n", q{
# XBanner - begin
/usr/X11R6/bin/freetemp
/usr/X11R6/bin/xbanner -file /etc/X11/XBanner.ad
# XBanner - end
}, -1;
	last;
      }
    }

    foreach (values %startup) {
      my $i = @$_;
      while ($i--) {
	next if /^\s*(?:$|#)/;
        $_->[$i] =~ m!^\s*(?:exit(?:\s+0)?\s*)?$!; and --$i;
	splice @$_, $i, 0, split "\n", q{
# XBanner - begin
/usr/X11R6/bin/freetemp
# XBanner - end
}, -1;

	last;
      }
    }
}
