#! /usr/bin/perl

# test if all package names in package_config files are available in a certain distribution
# packages from dists/proposed-updates are not listed in allpackages.html
# usage: chkdebnames stable *
#
# (c) 2001-2004 by Thomas Lange, lange@informatik.uni-koeln.de

use Getopt::Std;

getopts('aw');
$dist=shift;
$tmp="/tmp/$dist";
defined @ARGV or usage();

# - - - - - - - - - - - - - - - - - - - - - - - -
sub usage {

  print << "EOF";
chkdebnames, test if packages listed in a package_config file are still available

   Copyright (C) 2001-2004 by Thomas Lange

Usage: chkdebnames distribution file ...

There are two methods to get the list of all packages for a certain distribution.
First you can create a file using apt-cache dumpavail. If this file does not exists,
this script will get the list from packages.debian.org/$dist/allpackages.en.txt.gz
using wget. Using this method you can check even if have not install the
distribution you want to test.


Examples:

apt-cache dumpavail >/tmp/sarge
chkdebnames -a sarge /usr/local/share/fai/package_config/*

or

chkdebnames -w testing /usr/local/share/fai/package_config/*

EOF
  exit 0;
}
# - - - - - - - - - - - - - - - - - - - - - - - -

# when a file is created with: apt-cache dumpavail > /tmp/sarge
$opt_a  && do {
   print "Using apt-cache dumpavail output from $tmp\n";
};

$opt_w && do {
  if (-f $tmp ) {
    print "$tmp already exists. Skipping download and using this file.\n";
  } else {
   print "Saving package list for $dist from packages.debian.org to $tmp\n";
   system "wget -O allpackages.en.txt.gz -P/tmp http://packages.debian.org/$dist/allpackages.en.txt.gz";
   system "zcat /tmp/allpackages.en.txt.gz | tail +6 | awk '{print $1}' > $tmp";
  }

};

open (PACK,$tmp);
while (<PACK>) {
  $opt_a && /^Package: (\S+)/ and $avail{$1}=1;
  $opt_w && /(\S+)/ and $avail{$1}=1;
}

# now read a configuration files and check if packages are known
while (<>) {
  next if /^#/;
  next if /^PACKAGES/;
  next if /`/;
  s/#.*$//;
  chomp;
  foreach $p (split) {
    $p =~ s/[+-]$//;
    push @notavail,$p unless $avail{$p};
  }
}

print "The following packages are not known for the distribution $dist:\n";
print "Task names are also shown as unkown.\n";
foreach (@notavail) {
  print "$_\n";
}
