#!/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_lib_prefix`,0,-1) . '/include';

use ADE;
use Getopt::Long qw(:config no_ignore_case);

my(%adeperlf_defined_errors) = (
    adeperlf_err_misc        => { fmt => "%s" },
);

#  Options
#my();

sub adeperlf
{
    my($errstack_ref) = @_;
    my($rc, $listpaths, $optval, $perlcode, $procopts_hashref);

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

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

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

    #  Defaults for options
   
    #  Register options
    if (($rc=&ade_opt_register($errstack_ref, undef, undef, "main::adeperlf_opt_handler_%s")) != $ade_err_ok) {
        return($rc);
    }

    #  Register handler functions
    if (($rc=&ade_msg_register($errstack_ref, \&adeperlf_usage, \&adeperlf_version, \&adeperlf_listpaths)) != $ade_err_ok) {
        return($rc);
    } 

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

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

    &ade_msg_usage($errstack_ref) if (!$ARGV[0] || $ARGV[1]);
    $perlcode = $ARGV[0];

    &ade_err_debug($errstack_ref, 10, "main: \$perlcode=$perlcode");
    $rc = eval $perlcode;
    if (!defined($rc)) {
        chomp($@);
        &ade_err_error($errstack_ref, adeperlf_err_misc, "code failed to run; the error was: $@");
        return($ade_err_fail);
    } elsif ($rc == $ade_err_ok) {
        return($rc);
    } elsif ($rc == $ade_err_fail) {
        &ade_err_error($errstack_ref, adeperlf_err_misc, "code ran but returned failure");
        return($rc);
    } else {
        &ade_err_error($errstack_ref, adeperlf_err_misc, "code ran but returned a non-compliant return code; the return code was: $rc");
        return($ade_err_fail);
    }
    #  Ensure sensible return code
    return($ade_err_ok);
}

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

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

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

    ${$pathlist_text_ref} = undef;

    return($ade_err_ok);
}

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

    if ($passno == 1) {
        ${$usage_text_ref} = "<code>\n";
    } elsif ($passno == 2) {
        ${$usage_text_ref} = "";
    }

    return($ade_err_ok);
} 

&ade_gep_main(\&adeperlf);
