#!/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);

my(%mkfad_defined_errors) = (
    mkfad_err_access        => { fmt => "%s: can't %s" },
);

#  Options
my($opt_outfile, $opt_infile, $opt_nocrcs);
 
sub mkfad
{
    my($errstack_ref) = @_;
    my($rc, $in_handle, $out_handle);
    my(%data_store, $collected_info_ref);

    ###########################################################################
    #
    #  Set ADE options
    #
    ###########################################################################

    #  Register application-specific errors
    &ade_err_registerdefderrs(\%mkfad_defined_errors);

    ##########################################################################
    #
    #  Process options
    #
    ##########################################################################
 
    #  Defaults for options
    $opt_outfile = undef;
    $opt_infile = undef;
    $opt_nocrcs = undef;

    #  Register options
    if (($rc=&ade_opt_register($errstack_ref, "i:so:s", "no-crcs,infile:s,outfile:s", "main::mkfad_opt_handler_%s")) != $ade_err_ok) {
        return($rc);
    }

    #  Register handler functions
    if (($rc=&ade_msg_register($errstack_ref, \&mkfad_usage, \&mkfad_version, \&mkfad_listpaths)) != $ade_err_ok) {
        return($rc);
    } 

    #  Process options
    &ade_err_debug($errstack_ref, 10, "mkfad: processing options ...");
    if (($rc=&ade_opt_process($errstack_ref)) != $ade_err_ok) {
        return($rc);
    }

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

    &ade_msg_usage($errstack_ref) if (defined($ARGV[0]));

    $rc = 0;
    #  Open file list if specified
    if (defined($opt_infile)) {
        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 (defined($opt_infile)) {
        close IN_HANDLE;
    }
 
    #  Open output file if specified
    if (defined($opt_outfile)) {
        &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_opt_handler_i
{
    return(&mkfad_opt_handler_infile(@_));
}

sub mkfad_opt_handler_o
{
    return(&mkfad_opt_handler_outfile(@_));
}

sub mkfad_opt_handler_no_crcs
{
    my($errstack_ref) = @_;

    $opt_nocrcs = 1;

    return($ade_err_ok);
}

sub mkfad_opt_handler_infile
{
    my($errstack_ref, $infile) = @_;

    $opt_infile = $infile;

    return($ade_err_ok);
}

sub mkfad_opt_handler_outfile
{
    my($errstack_ref, $outfile) = @_;

    $opt_outfile = $outfile;

    return($ade_err_ok);
}

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

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

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

    ${$pathlist_text_ref} = undef;

    return($ade_err_ok);
}

sub mkfad_usage
{
    my($errstack_ref, $usage_text_ref, $passno) = @_;

    if ($passno == 1) {
        ${$usage_text_ref} = undef;
    } elsif ($passno == 2) {
        ${$usage_text_ref} = "         -i <file> | --infile=<file>         read file list from <file>\n" .
                             "         -o <file> | --outfile=<file>        send output to <file>\n" .
                             "                     --no-crcs               suppress content checksums";
    }

    return($ade_err_ok);
} 

&ade_gep_main(\&mkfad);
