#!/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({
    fadcat_err_access        => { fmt => "%s: can't %s" },
    fadcat_err_internal        => { fmt => "%s" },
});

my($opt_regexp, $opt_outfile);
my(@fadcat_config_hasharray) = (
    { dsc => "opt_regexp",  var => \$opt_regexp,  dfl => "undefined" },
    { dsc => "opt_outfile", var => \$opt_outfile, dfl => "undefined" },
);
 
sub fadcat
{
    my($errstack_ref) = @_;
    #my($rc, $in_handle, $out_handle, %attribs, %names, %madeupdevinodes, $listpaths);
    my($rc, $in_handle, $out_handle, %data_store, $procopts_hashref);

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

    $procopts_hashref = {
        "g|grep=s" => \$opt_regexp, 
        "o|file=s" => \$opt_outfile 
    };
    if (($rc=&ade_spc_procopts($errstack_ref, \&fadcat_listpaths, \&fadcat_usage, \&fadcat_version, \@fadcat_config_hasharray, $procopts_hashref)) != $ade_err_ok) {
        return($rc); 
    } 

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

    #  All arguments are optional.

    ##########################################################################
    #
    #  Guts
    #
    ##########################################################################

    if (defined($ARGV[0])) {
        while (defined($ARGV[0])) {
            $_ = shift @ARGV;
            &ade_err_debug($errstack_ref, 5, "fadcat: calling ade_fcm_openreadcompressed() to open $_ ...");
            if (($rc=&ade_fcm_openreadcompressed($errstack_ref, $_, \*IN_HANDLE)) != $ade_err_ok) {
                 &ade_err_error($errstack_ref, fadcat_err_access, $_, "open");
                 return($rc);
            }
            &ade_err_debug($errstack_ref, 5, "fadcat: calling fad_load() ...");
            if (($rc=&fad_load($errstack_ref, \*IN_HANDLE, \%data_store, undef, ($opt_regexp eq "undefined") ? undef : $opt_regexp)) != $ade_err_ok) {
                return($rc);
            }
            &ade_err_debug($errstack_ref, 5, "fadcat: fad_load() returned");
            close IN_HANDLE;
        }
    } else {
        &ade_err_debug($errstack_ref, 5, "fadcat: calling fad_load() ...");
        if (($rc=&fad_load($errstack_ref, \*STDIN, \%data_store, undef, ($opt_regexp eq "undefined") ? undef : $opt_regexp)) != $ade_err_ok) {
            return($rc);
        }
        &ade_err_debug($errstack_ref, 5, "fadcat: fad_load() returned");
    }

    &ade_err_debug($errstack_ref, 10, "fadcat: opt_outfile=$opt_outfile");
    if ($opt_outfile ne "undefined") {
        &ade_err_debug($errstack_ref, 10, "fadcat: opening output file ...");
        &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, fadcat_err_access, $opt_outfile, "open");
            return($rc);
        }
        $out_handle = \*OUT_HANDLE;
    } else {
        $out_handle = \*STDOUT;
    }

    &ade_err_debug($errstack_ref, 10, "fadcat: dumping FAD store to " . (defined($opt_outfile) ? $opt_outfile : "stdout") .  " ...");
    if (($rc=&fad_dump($errstack_ref, $out_handle, \%data_store)) != $ade_err_ok) {
        return($rc);
    }

    if ($opt_outfile ne "undefined") {
        close OUT_HANDLE;
        &ade_tmp_deregisterfile($errstack_ref, $opt_outfile);
    }

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

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

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

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

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

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

    if ($passno == 1) {
        print "[ <file> ... ]\n";
    } elsif ($passno == 2) {
        print "         -g        | --grep=<regexp>         grep out lines containing <regexp>\n";
        print "         -o        | --file=<outfile>        send output to <outfile>\n";
    } else {
        &ade_err_error($errstack_ref, fadcat_err_internal, "fadcat_usage: $passno: bad pass number");
        return($ade_err_fail);
    }

    return($ade_err_ok);
}

&ade_gep_main(\&fadcat);
