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

#   Options
my($opt_regexp, $opt_outfile);
 
sub fadcat
{
    my($errstack_ref) = @_;
    #my($rc, $in_handle, $out_handle, %attribs, %names, %madeupdevinodes, $listpaths);
    my($rc, $in_handle, $out_handle, %data_store);

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

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

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

    #  Defaults for options
    $opt_regexp  = undef;
    $opt_outfile = undef;

    #  Register options
    if (($rc=&ade_opt_register($errstack_ref, "g:so:s", "grep:s,file:s", "main::fadcat_opt_handler_%s")) != $ade_err_ok) {
        return($rc);
    }

    #  Register handler functions
    if (($rc=&ade_msg_register($errstack_ref, \&fadcat_usage, \&fadcat_version, \&fadcat_listpaths)) != $ade_err_ok) {
        return($rc);
    } 

    #  Process options
    &ade_err_debug($errstack_ref, 10, "fadcat: processing options ...");
    if (($rc=&ade_opt_process($errstack_ref)) != $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)) != $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)) != $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 (defined($opt_outfile)) {
        &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 (defined($opt_outfile)) {
        close OUT_HANDLE;
        &ade_tmp_deregisterfile($errstack_ref, $opt_outfile);
    }

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

sub fadcat_opt_handler_g
{
    return(&fadcat_opt_handler_grep(@_));
}

sub fadcat_opt_handler_o
{
    return(&fadcat_opt_handler_file(@_));
}

sub fadcat_opt_handler_grep
{
    my($errstack_ref, $regexp) = @_;

    $opt_regexp = $regexp;

    return($ade_err_ok);
}

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

    $opt_outfile = $outfile;

    return($ade_err_ok);
}

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

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

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

    ${$pathlist_text_ref} = undef;

    return($ade_err_ok);
}

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

    if ($passno == 1) {
        ${$usage_text_ref} = "[ <file> ... ]";
    } elsif ($passno == 2) {
        ${$usage_text_ref} = "         -g        | --grep=<regexp>         grep out lines containing <regexp>\n" .
                             "         -o        | --file=<outfile>        send output to <outfile>";
    }

    return($ade_err_ok);
} 

&ade_gep_main(\&fadcat);
