#!/usr/bin/perl -w

# tetex-bin preinst script.
#
# Perl version by Carey Evans <c.evans@clear.net.nz> and Christian
# Schwarz <schwarz@monet.m.isar.de>, based on tetex-bin.preinst from
# tetex-bin_04pl8-3 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 "/var/lib/texmf/web2c") {
      unlink "/var/lib/texmf/web2c";
  }
  if (-l "/usr/lib/texmf/web2c") {
      unlink "/usr/lib/texmf/web2c";
  }
  if (-l "/usr/share/texmf/web2c") {
      unlink "/usr/lib/texmf/web2c";
  }
  if (@ARGV > 1 and $ARGV[1] lt "0.9-1") {
      unlink "/usr/doc/tetex-bin";
  }
  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";

  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>);


exit 1;
