#!/usr/bin/perl
use strict;
use warnings;
my($app_svnid) = '$HeadURL$ $LastChangedRevision$';  ## no critic (RequireInterpolationOfMetachars)
use lib substr(`ade-config ade_share_prefix`,0,-1) . '/include';  ## no critic (ProhibitBacktickOperators)
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, $perlcode);

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

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

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

    #  Defaults for options

    #  Register options
    if (($rc=ADE::ade_opt_register($errstack_ref, undef, undef, 'main::adeperlf_opt_handler_%s')) != $ADE::ADE_ERR_OK) {
        return($rc);
    }

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

    #  Process options
    ADE::ade_err_debug($errstack_ref, 10, 'adeperlf: processing options ...');
    if (($rc=ADE::ade_opt_process($errstack_ref)) != $ADE::ADE_ERR_OK) {
        return($rc);
    }

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

    ADE::ade_msg_usage($errstack_ref) if ((not $ARGV[0]) or $ARGV[1]);
    $perlcode = $ARGV[0];

    ADE::ade_err_debug($errstack_ref, 10, "main: perlcode=$perlcode");
    $rc = eval $perlcode;  ## no critic (ProhibitStringyEval)
    if (not defined $rc) {
        chomp $@;
        ADE::ade_err_error($errstack_ref, 'adeperlf_err_misc', "code failed to run; the error was: $@");
        return($ADE::ADE_ERR_FAIL);
    } elsif ($rc == $ADE::ADE_ERR_OK) {
        return($rc);
    } elsif ($rc == $ADE::ADE_ERR_FAIL) {
        ADE::ade_err_error($errstack_ref, 'adeperlf_err_misc', 'code ran but returned failure');
        return($rc);
    } else {
        ADE::ade_err_error($errstack_ref, 'adeperlf_err_misc', "code ran but returned a non-compliant return code; the return code was: $rc");
        return($ADE::ADE_ERR_FAIL);
    }
    #  Ensure sensible return code
    return($ADE::ADE_ERR_OK);
}

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

    return(ADE::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::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::ADE_ERR_OK);
}

ADE::ade_gep_main(\&adeperlf);
