#!/usr/bin/perl -w

use lib '/usr/lib/a2b';

use A2B::Tools;
use TSV::Reader;
use IO::File;
use File::Basename;

use strict;

my $jf;
my $tools;
my %which;

my $base = dirname(`which a2b`)."/../share/a2b/tools";
for my $filename (`find $base -type f`) {
	chomp $filename;
	my $reader = TSV::Reader->new($filename);
	my $first = 0;
	my $fields = $reader->read;
	my $joined_fields = join "\0", $fields;
	if ($jf) {
		$jf ne $joined_fields and
			die "files in /usr/share/a2b/tools have different sets of fields";
	} else {
		$jf = $joined_fields;
		$tools = A2B::Tools->new(fields => $fields);
	}
	while (my $row = $reader->read) {
		my $tool = $row->[0];   # this assumes that the tool field is the first one,
					# and doesn't check multi-tool jobs
					# sensibly
		exists $which{$tool} or
			$which{$tool} = `which $tool`;
		if ($which{$tool}) {
			$tools->add_row($row);
		}
	}
}

$tools->write_tsv("/etc/a2b/tools");
my $mimetypes = dirname(`which a2b`)."/../share/a2b/mime.types";
system("cat \Q$mimetypes\E/* >/etc/a2b/mime.types");

__END__

=head1 NAME

a2b_build_etc - create /etc/a2b/tools from files in /usr/share/a2b/tools, and copy /etc/a2b/mime.types

=head1 SYNOPSIS

a2b_build_etc

=head1 SEE ALSO

a2b

