#!/usr/bin/perl
#
# $Id: prtcheckmissing,v 1.1 2003/10/27 15:26:50 opel Exp $
#      prtcheckmissing,v 1.2 2022/05/31 18:41:19 jmq Exp $

use warnings;
use strict;

local $/ = ""; # read files paragraph-wise; see ``perldoc perlvar''

my @mask = parse_pkgadd_conf();

open my $fh, "< /var/lib/pkg/db"
  or die "Couldn't open package database!\n";

while(<$fh>) {
  my ($pkg_name, $pkg_version, @pkg_file) = split /\n/;
  # apply the pkgadd rules to eliminate false positives
  my @missing = grep { (! -e "/$_") && wanted($_, @mask) } @pkg_file;
  next if not @missing;

  print map "/$_ $pkg_name\n", @missing;
}
 
close($fh);

sub parse_pkgadd_conf {
  my @unwanted;
  local $/ = "\n";
  open my $pah, "< /etc/pkgadd.conf"
    or die "Couldn't open pkgadd install rules!\n";

  while(<$pah>) {
    next if /^\s*#/ or /^$/;
    my ($event, $pattern, $choice) = split;
    push (@unwanted, qr/$pattern/s) if ($event eq "INSTALL" and $choice eq "NO");
  }
  close ($pah);

  return @unwanted;
}

sub wanted {
  my $testfile = shift;
  my $retval = 1;
  while ((my $regexp=shift) and ($retval==1)) {
    $retval = 0 if ($testfile =~ $regexp);
  }
  return $retval;
}
