#!ADE_APP_TOKEN_PERL_CMD
use strict; 
#  leave a standard path unless there is some reason not to
$ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin';
($main::progname = $0) =~ s/^.*\/([^\/]+)/$1/;
$main::svn_id = '$HeadURL$ $LastChangedRevision$';
$main::version_scheme = 'release';
$main::release_version = 'ADE_APP_TOKEN_RELEASE_ID';
my($tmp_dir) = ($ENV{'TMPDIR'}) ? $ENV{'TMPDIR'} : "ADE_APP_TOKEN_TMPDIR";

#  Defaults for options
undef my($dflt_regexp);
undef my($dflt_outfile);

#  Option variables (needed at global level 'cos used in path reporting function)
undef my($opt_regexp);
undef my($opt_outfile);

#  Other globals
#  Extra perl library directories
#  Are there any directories where other libraries are to be found? Just use
#  a colon separated list of directories.
my($extra_perllib_dirs) = "ADE_APP_TOKEN_EXTRAPERLLIBDIRS";
#  Important bits
push(@INC,split(/[:\s]+/,$extra_perllib_dirs));

sub main
{
    my(@ARGV) = @_;
    my($rc, $infile, $in_handle, $out_handle, $optval);
    my(%attribs, %names, %madeupdevinodes, $listpaths);
    $rc = 0;

    ##########################################################################
    #
    #  PROCESS OPTIONS
    #
    ##########################################################################

    #
    #  Load option values from command line
    #
    while (defined($ARGV[0]) && $ARGV[0] =~ /^-./) {
        $_ = $ARGV[0];

        #  standard options
        if (/^(?:-V|--version)$/) {
            &ade_msg_version(\&app_version);

        } elsif (/^(-d$|--debug=)(.*)/) {
            $main::verboselevel = ($1 !~ /^-[^-]/) ? $2 : ((defined($ARGV[1])) ? scalar($optval=$ARGV[1],shift @ARGV,$optval) : &ade_msg_usage(\&app_usage, 1));

        } elsif (/^(?:-v|--verbose)$/) {
            $main::verboselevel = 3;

        } elsif (/^(?:-h|--help)$/) {
            &ade_msg_usage(\&app_usage, 0);

        } elsif (/^(?:-p|--list-paths)$/) {
            $listpaths = 1;

        } elsif (/^(-g$|--grep=)(.*)/) {
            $opt_regexp = ($1 !~ /^-[^-]/) ? $2 : ((defined($ARGV[1])) ? scalar($optval=$ARGV[1],shift @ARGV,$optval) : &ade_msg_usage(\&app_usage, 1));

        } elsif (/^(-o$|--file=)(.*)/) {
            $opt_outfile = ($1 !~ /^-[^-]/) ? $2 : ((defined($ARGV[1])) ? scalar($optval=$ARGV[1],shift @ARGV,$optval) : &ade_msg_usage(\&app_usage, 1));

        } elsif (/^--$/) {
            shift @ARGV;
	    last;

        } else {
            &ade_msg_usage(\&app_usage, 1);
        }

        shift @ARGV;
    }

    #
    #  Load option values from environment
    #

    #  (not supported by this program)

    #
    #  Load option values from config file
    #

    #  (not needed for this program)

    #
    #  hard-coded defaults
    #
    $opt_regexp  = $dflt_regexp  if (!defined($opt_regexp));
    $opt_outfile = $dflt_outfile if (!defined($opt_outfile));

    #
    #  Sanity check and option rewrites
    #

    ##########################################################################
    #
    #  ARGUMENT PROCESSING
    #
    ##########################################################################

    &ade_msg_listpaths(\&app_listpaths) if ($listpaths);
    (!$ARGV[0]) || &ade_msg_usage(\&app_usage, 1);

    ##########################################################################
    #
    #  FORKING AND LOCKING
    #
    ##########################################################################

    #  (not needed for this program)

    ##########################################################################
    #
    #  GUTS STARTS HERE
    #
    ##########################################################################

    if (defined($ARGV[0])) {
        while (defined($ARGV[0])) {
            $_ = shift @ARGV;
            &ade_msg_debug(5, "main: loading FAD stream to FAD store from $_ ...");
            if (($rc=&ade_fcm_openreadcompressed($_, \*IN_HANDLE)) != 0) {
                 &ade_msg_errorerror("can't open $_");
                 return($rc);
            }
            if (($rc=&read_fad_header(\*IN_HANDLE, undef)) != 0) {
                &ade_msg_errorerror("bad FAD header in $_");
                return($rc);
            }
            if (($rc=&fadstream_to_fadstore(\*IN_HANDLE, \%attribs, \%names, \%madeupdevinodes, $opt_regexp)) != $ade_err_ok) {
                return($rc);
            }
            close IN_HANDLE;
        }
    } else {
        &ade_msg_debug(5, "main: loading FAD stream to FAD store from stdin ...");
        if (($rc=&read_fad_header(\*STDIN, undef)) != 0) {
            &ade_msg_errorerror("bad FAD header in stdin");
            return($rc);
        }
        if (($rc=&fadstream_to_fadstore(\*STDIN, \%attribs, \%names, \%madeupdevinodes, $opt_regexp)) != $ade_err_ok) {
            return($rc);
        }
    }

    if (defined($opt_outfile)) {
        &ade_tmp_register($opt_outfile);
        if (($rc=&ade_fcm_openwritecompressed($opt_outfile, \*OUT_HANDLE)) != 0) {
            &ade_msg_errorerror("can't open $opt_outfile");
            return($rc);
        }
        $out_handle = \*OUT_HANDLE;
    } else {
        $out_handle = \*STDOUT;
    }

    &ade_msg_debug(10, "main: dumping FAD store to " . (defined($opt_outfile) ? $opt_outfile : "stdout") .  " ...");
    if (($rc=&fadstore_to_fadstream($errstack_ref, $out_handle, \%attribs, \%names, \%madeupdevinodes)) != $ade_err_ok) {
        return($rc);
    }

    if (defined($opt_outfile)) {
        close OUT_HANDLE;
        &ade_tmp_deregister($opt_outfile);
    }

    return($ade_err_ok);
}

sub app_version
{
    my($version_ref) = @_;

    ${$version_ref} = $main::version;

    return(0);
}

sub app_usage
{
    print "Usage:   $main::progname [ <options> ] [ <file> ...]\n";
    print "\n";
    #  standard options
    print "Options: -V | --version         display program version\n";
    print "         -v | --verbose         verbose\n";
    print "         -d | --debug=<level>   set debug level\n";
    print "         -h | --help            display this text\n";
    print "         -p | --list-paths      list used paths\n";
    #  application-specific option
    print "         -g | --grep=<regexp>   grep out lines containing <regexp>\n";
    print "         -o | --file=<outfile>  send output to <outfile> instead of stdout\n";
    print "\n";

    return(0);
}

sub app_listpaths
{
    return(0);
}
