#!/usr/bin/perl -w

# tetex-base preinst script.
#
# Perl version by Carey Evans <c.evans@clear.net.nz> and Christian
# Schwarz <schwarz@monet.m.isar.de>, based on tetex-base.preinst from
# tetex-base_04pl8-4 by Christoph Martin <martin@uni-mainz.de>.
#
# This script may be distributed under the terms of the GNU GPL.

my @OLDPACKAGES = qw(amsfonts amslatex amstex babel bibtex dviljk
		     dvipsk kpathsea kpathsea-dev latex latex2e-doc
		     ltxgraph ltxmisc ltxtool makeindex mfbasfnt mfbin
		     mfdcfnt mfnfss ntgclass oldgerman pandora ps2pk
		     psnfss texbin texi2html texlib texpsfnt
		     textfm xdvik tetex tetexlib tetexdoc);

my @OLDPACKAGES2 = qw(mflib xypic);

# ---end-of-configuration-part---

use strict;
use Carp;
use Dpkg::Package::List;

# ---

package Dpkg::Package::List;

sub croak {
  Carp::croak(@_);
}

# ---

package main;

$| = 1;

if (@ARGV == 0 or $ARGV[0] ne 'install') {
  if (-l "/usr/doc/texmf") {
    unlink "/usr/doc/texmf";
  }
  if (-l "/usr/share/doc/texmf") {
    unlink "/usr/share/doc/texmf";
  }
  if (-l "/usr/lib/texmf/doc") {
    unlink "/usr/lib/texmf/doc";
  }
  if (-l "/usr/share/texmf/doc") {
    unlink "/usr/share/texmf/doc";
  }
 if (@ARGV > 1 and $ARGV[1] eq "0.4pl8-5") {
    print "** fixing false remove of /var/lib/texmf **\n";
  
    # Cheat.
    my $postrm = "/var/lib/dpkg/info/tetex-base.postrm";
    if ( -f $postrm ) {
	open(A, qq($postrm));
	open(B, qq(>$postrm.$$));
	while (<A>) {
	    s(rm.*true)();
	    print B;
	}
	close B;
	close A;
	unlink $postrm;
	rename qq($postrm.$$), $postrm;
    }
  }
  if (@ARGV > 1 and $ARGV[1] lt "0.9-1") {
      unlink "/usr/doc/tetex-base";
  }
  if (-d "/usr/share/texmf/pdftex/config") {
    exec(qw(rm -fr /usr/share/texmf/pdftex/config));
  }
 exit 0;
}

print "\n checking for old tex packages ";

# Read dpkg's status file
my $list = new Dpkg::Package::List('filename' => '/var/lib/dpkg/status');

print '.';

# Convert package list to hash
my %pkgs = map {$_->{'package'} => $_} @{$list->packages()};

print '.';

# Search for old packages
my @oldpkg = grep {
  exists $pkgs{$_} and $pkgs{$_}{'status'} !~ /ok not-installed/
} @OLDPACKAGES;

print '.';

my @oldpkg2 = grep {
  exists $pkgs{$_} and $pkgs{$_}{'status'} !~ /ok not-installed/
} @OLDPACKAGES2;

unless (@oldpkg or @oldpkg2) {
  print " none.\n";

  print "** purging old tex files **\n";
  
  # Cheat.
#  exec(qw(rm -frv /etc/texmf /var/spool/texmf));
#  die "exec rm: $!\n";
   exit 0;
}

# If some packages were found, describe the problem.
print <<"__EOT__";


I see that you have one or more of the pre-1.3 TeX packages installed:

@oldpkg @oldpkg2

Because these packages had serious problems, they were replaced by teTeX.  
Unfortunately because of those problems (and because of a missing feature  
in the packaging system), you have to deinstall those packages before you  
can proceed to install teTeX.

This is how to do it:

a) In dselect [S]elect the tetex packages for installation. This will
   mark all the old packages automaticaly for removal.

   Then first [R]emove all deselected packages.

   After this [I]nstall all selected packages.

   (This method may not work. In this case use method b).)

or

b) From the commandline call:

__EOT__

print "    dpkg --purge --force-depends @oldpkg\n\n" if @oldpkg;
print "    dpkg --purge --force-depends @oldpkg2\n\n" if @oldpkg2;
print "Please press [RETURN]:";

# Read one line.
scalar(<STDIN>);

# Delete existing install-fmt-bases and symlink to /bin/true.
for (qw(/usr/bin/install-fmt-base /usr/sbin/install-fmt-base)) {
  if (-e) {
    unless (unlink $_) {
      warn "unlink $_: $!\n";
      next;
    }
  }
  symlink("/bin/true", $_)
    or warn "symlink /bin/true $_: $!\n";
}




exit 1;
