#!/usr/bin/perl

my $dir = '/usr/share/ldetect-lst';
my @files = qw(pcitable usbtable isatable pcmciatable dmitable);

unlink("$dir/$_", "$dir/$_.gz") foreach @files;

$ARGV[0] eq '--clean' and exit 0;

foreach (@files) {
    my $d = "$dir/$_.d";
    -d $d or next;
    my @l = sort(glob("$d/*.lst"), glob("$d/*.lst.gz")) or next;

    if (@l == 1 && $l[0] =~ /gz$/) {
	link $l[0], "$dir/$_.gz";
    } else {
	open(my $OUT, "| gzip -9 > $dir/$_.gz");
	foreach (@l) {
	    open(my $IN, /gz$/ ? "gzip -dc $_ |" : $_) or next;
	    print $OUT $_ foreach <$IN>;
	}
    }
}