#!/usr/bin/perl
use strict;
my($app_svnid) = '$HeadURL$ $LastChangedRevision$';
#  Allow bare words, so &ade_err_error() calls look nicer.
no strict 'subs';
use lib substr `ade-config ade_include_prefix`,0,-1;
use ADE;
use lib substr `fad-config fad_include_prefix`,0,-1;
use FAD;
use Getopt::Long qw(:config no_ignore_case);

&ade_err_registerdefderrs({
    mkfad_err_access        => { fmt => "%s: can't %s" },
    mkfad_err_internal        => { fmt => "%s" },
});

my($opt_outfile, $opt_infile, $opt_nocrcs);
my(@mkfad_config_hasharray) = (
    { dsc => "opt_outfile", var => \$opt_outfile, dfl => "undefined" },
    { dsc => "opt_infile",  var => \$opt_infile,  dfl => "undefined" },
    { dsc => "opt_nocrcs",  var => \$opt_nocrcs,  dfl => 0           },
);
 
sub mkfad
{
    my($errstack_ref) = @_;
    my($rc, $in_handle, $out_handle);
    my(%data_store, $collected_info_ref, $procopts_hashref);

    ##########################################################################
    #
    #  Process options
    #
    ##########################################################################

    $procopts_hashref = {
        "no-crcs" => sub { $opt_nocrcs = 1; }, 
        "i|infile=s" => \$opt_infile, 
        "o|outfile=s" => \$opt_outfile 
    };
    if (($rc=&ade_spc_procopts($errstack_ref, \&mkfad_listpaths, \&mkfad_usage, \&mkfad_version, \@mkfad_config_hasharray, $procopts_hashref)) != $ade_err_ok) {
        return($rc);
    }

    ##########################################################################
    #
    #  Process arguments
    #
    ##########################################################################


    $rc = 0;
    #  Open file list if specified
    if ($opt_infile ne "undefined") {
        if (($rc=&ade_fcm_openreadcompressed($errstack_ref, $opt_infile, \*IN_HANDLE)) != $ade_err_ok) {
            &ade_err_error($errstack_ref, mkfad_err_access, $opt_infile, "open");
            return($rc);
        }
        $in_handle = \*IN_HANDLE;
    } else {
        $in_handle = \*STDIN;
    }

    #  Slurp file list, generate FAD records, and store in internal store
    while (<$in_handle>) {
        chomp;
        return($rc) if (($rc=&fad_collect_info($errstack_ref, $_, $opt_nocrcs, \$collected_info_ref)) != $ade_err_ok);
        return($rc) if (($rc=&fad_insert_info($errstack_ref, \%data_store, $collected_info_ref)) != $ade_err_ok);
    }

    #  Close input file if necessary
    if ($opt_infile ne "undefined") {
        close IN_HANDLE;
    }
 
    #  Open output file if specified
    if ($opt_outfile ne "undefined") {
        &ade_err_debug($errstack_ref, 10, "mkfad: registering outfile $opt_outfile ...");
        &ade_tmp_registerfile($errstack_ref, $opt_outfile);
        if (($rc=&ade_fcm_openwritecompressed($errstack_ref, $opt_outfile, \*OUT_HANDLE)) != $ade_err_ok) {
            &ade_err_error($errstack_ref, mkfad_err_access, $opt_outfile, "open");
            return($rc);
        }
        $out_handle = \*OUT_HANDLE;
    } else {
        &ade_err_debug($errstack_ref, 10, "mkfad: outfile is stdout");
        $out_handle = \*STDOUT;
    }

    #  Print all records to the output handle.
    &ade_err_debug($errstack_ref, 10, "mkfad: dumping records to outfile ...");
    if (($rc=&fad_dump($errstack_ref, $out_handle, \%data_store)) != $ade_err_ok) {
        return($rc);
    }

    #  Close output file if necessary
    if (defined($opt_outfile)) {
        close OUT_HANDLE;
        &ade_err_debug($errstack_ref, 10, "mkfad: deregistering $opt_outfile");
        &ade_tmp_deregisterfile($errstack_ref, $opt_outfile);
    }

    #  Now that we have finished with the FAD store, we can clear it of all records.
    undef %data_store;

    #  Ensure sensible return code
    return($ade_err_ok);
}

sub mkfad_version
{
    my($errstack_ref, $version_ref) = @_;

    return(&ade_smf_extractversionfromsvnstring($errstack_ref, $app_svnid, $version_ref));
}

sub mkfad_listpaths
{
    my($errstack_ref, $pathlist_ref) = @_;
    my($rc);

    %{$pathlist_ref} = ();
    return($ade_err_ok);
}

sub mkfad_usage
{
    my($errstack_ref, $passno) = @_;
   
    if ($passno == 1) {
        print "\n";
    } elsif ($passno == 2) {
        print "         -i <file> | --infile=<file>         read file list from <file>\n";
        print "         -o <file> | --outfile=<file>        send output to <file>\n";
        print "                     --no-crcs               suppress content checksums\n";
    } else {
        &ade_err_error($errstack_ref, mkfad_err_internal, "mkfad_usage: $passno: bad pass number");
        return($ade_err_fail);
    }
    
    return($ade_err_ok);
}

&ade_gep_main(\&mkfad);
