#! /usr/bin/perl
use warnings;
use strict;
use integer;
use FindBin;
use lib $FindBin::RealBin;
use Def;

# This extremely obscure helper script checks the packages named in
# cmdsel-debs.data against one or more lists of selected packages (the
# lists each having one binary package name per line).  If you don't
# know what this is about, you can ignore this script.

our $file_data = "${FindBin::RealBin}/cmdsel-debs.data";

our $usage = <<END;
usage: $0 [-h] [selections file...]
    -h print this usage message
END

# Read command-line arguments and options.
my @opt;
my @arg;
push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
my %opt = map {
  my $o = $_;
  map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
} @opt;
if ( $opt{'?'} || $opt{h} ) {
  print $usage;
  exit 0;
}

# Read in the selections.
my %sel;
for ( @arg ) {
  open  F, '<', $_;
    while (<F>) {
      chomp;
      $sel{$_} = 1 if /\S/ && !/^#/;
    }
  close F;
}

# Object to unselected packages in the data file.
open  F, '<', $file_data;
{
  my $cmd2  = '';
  my $sect2 = '';
  while (<F>) {
    my( $cmd, $sect, $pkg ) = /^(\S+)\(([^()\s]+)\)\s+(\S+)\s*$/
      or die "$0: badly formatted data line\n$_\n";
    $sel{$pkg} or warn "$0: package $pkg unselected or unknown\n";
    $sect gt $sect2 || ( $sect eq $sect2 && $cmd gt $cmd2 )
      or warn
      "$0: commands $cmd2($sect2) and $cmd($sect) are misordered\n";
    $cmd2 = $cmd; $sect2 = $sect;
  }
}
close F;

