#!SPPSYM_PERL_CMD
#spp comment vim: set filetype=perl: #
#spp include ../bldcfg/paths.spp
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'} : "SPPSYM_TMPDIR";

#  Defaults for options
#my($dflt_whatever) = "defaultvalue";
#my($dflt_configfile) = "SPPSYM_DIR/$main::progname.conf";
#  Don't say 'undef my(@dflt_somearray)' 'cos this does *NOT* undefine the array,
#  instead say 'undef my(@dflt_somearray)'.

#  Option variables (needed at global level 'cos used in path reporting function)
#undef my($opt_whatever);
#undef my($opt_configfile);
#  Don't say 'undef my(@opt_somearray)' 'cos this does *NOT* undefine the array,
#  instead say 'undef my(@opt_somearray)'.

#  Other globals
my($current_mode) = "normal";

sub main
{
    my(@ARGV) = @_;
    my($rc, $listpaths, $perlcode, $optval);
    my($count, @buffer, $c, $mode);

    ##########################################################################
    #
    #  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 (/^--$/) {
            shift @ARGV;
	    last;

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

        shift @ARGV;
    }

    #
    #  Load option values from environment
    #
    #$opt_whatever   = $ENV{'ADEPERLF_WHATEVER'}   if (!defined($opt_whatever)   && $ENV{'ADEPERLF_WHATEVER'});
    #$opt_configfile = $ENV{'ADEPERLF_CONFIGFILE'} if (!defined($opt_configfile) && $ENV{'ADEPERLF_CONFIGFILE'});

    #
    #  Load option values from config file
    #
    #  (not needed for this program - yet!)
    #if (($rc=&ade_fcm_loadconfigfile($opt_configfile, $dflt_configfile, "whatever", \%confighash)) != 0) {
    #    &ade_msg_errorerror("failed to load config file");
    #    return($rc);
    #}
    #$opt_whatever = $confighash{'whatever'} if (!defined($opt_whatever) && defined($confighash{'whatever'}));
    ##  don't load the config file name from the config file!
    
    #
    #  hard-coded defaults
    #
    #  (not needed for this program - yet!)
    #$opt_whatever   = $dflt_whatever   if (!defined($opt_whatever));
    #$opt_configfile = $dflt_configfile if (!defined($opt_configfile));

    #
    #  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 - yet!)
    #if ($opt_fork) {
    #    if (($pid=fork) < 0) {
    #        &ade_msg_errorerror("can't fork");
    #        return(1);
    #    } elsif ($pid > 0) {
    #        return(0);
    #    }
    #}
    #
    #if (($rc=&ade_lck_lock("", \&ade_lck_getgenericlockfilename)) != 0) {
    #    &ade_msg_errorerror("another $main::progname is running");
    #    return($rc);
    #}
    #
    # if ($opt_fork) {
    #    #  And only *now* switch to being a daemon.
    #    &ade_msg_debug(10, "main: turning into a normal daemon ...");
    #    $main::facility = "daemon";
    #    $main::i_am_a_daemon = 1;
    #    $POSIX::setsid;
    #    chdir('/');
    #    close(STDIN);
    #    close(STDERR);
    #    close(STDOUT);
    #    $main::verboselevel = 3 if ($main::verboselevel < 3);
    #    &ade_msg_info("$main::progname version $main::version started");
    #}

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

    open(PIPE, "nroff -man|") || &ade_msg_error("nroff: can't execute");
    print "<html>\n";
    if (($rc=&ade_spc_autogendheader(\*STDOUT, "<!--", "-->", undef)) != 0) {
        close(PIPE);
        return($rc);
    }

    print "<body>\n";
    print "<pre>\n";
    for ($count=0,$mode="normal";defined($c = $buffer[$count++] = getc PIPE);) {
    
        &ade_msg_debug(10, "[READ:$c]");
    
        if ($count == 1) {
    
            if ($buffer[$count-1] eq " ") {
                &ade_msg_debug(10, "[1SP]");
                &safeprint("$buffer[--$count]");
    
            } elsif ($buffer[$count-1] eq "\n") {
                &ade_msg_debug(10, "[1NL]");
                #print "<br>";
                &safeprint("$buffer[--$count]");
    
            } elsif ($buffer[$count-1] eq "") {
                &ade_msg_error("control sequence starts with ^H");
    
            } else {
                #  else must wait for more
                &ade_msg_debug(10, "[1WAIT]");
    
            }
    
        } elsif ($count == 2) {
        
            if ($buffer[$count-1] eq " ") {
                &ade_msg_debug(10, "[2SPNL]");
                &switchto("normal");
                &safeprint("$buffer[0]");
                &safeprint("$buffer[1]");
                $count = 0;
    
            } elsif ($buffer[$count-1] eq "\n") {
                &ade_msg_debug(10, "[2SPNL]");
                &switchto("normal");
                &safeprint("$buffer[0]");
                #print "<br>";
                &safeprint("$buffer[1]");
                $count = 0;
    
            } elsif ($buffer[$count-1] eq "") {
    
                ; # must wait for end of X^H? sequence
                &ade_msg_debug(10, "[2WAIT]");
    
            } else {
                &ade_msg_debug(10, "[2NORMSHUF]");
                &switchto("normal");
                #  print oldest char and shuffle
                &safeprint("$buffer[0]");
                $buffer[0] = $buffer[1];
                $count--;
             
            }
    
        } elsif ($count == 3) {
    
            if ($buffer[$count-1] eq " " or $buffer[$count-1] eq "\n") {
                
                &ade_msg_error("<char>^H{<space>|<newline>: invalid control sequence");
    
            } elsif ($buffer[0] eq "_") {
                &ade_msg_debug(10, "[3ITALIC]");
                &switchto("italic");
                &safeprint("$buffer[2]");
                $count = 0;
    
            } elsif ($buffer[$count-1] eq $buffer[0]) {
                &ade_msg_debug(10, "[3BOLD]");
                &switchto("bold");
                &safeprint("$buffer[0]");
                $count = 0;
    
            } elsif ($buffer[$count-1] eq "o" && $buffer[0] eq "+") {
                &ade_msg_debug(10, "[BULLET]");
                &switchto("normal");
                &safeprint("o");
                $count = 0;
    
            } else {
                &ade_msg_error("$buffer[0]^H$buffer[2]: invalid control sequence");
    
            }
    
        } else {
            &ade_msg_error(sprintf("%s: invalid control sequence", join("", @buffer)));
        }
    }
    
    if ($count == 0) {
    
        ; # nothing to do
        
    } elsif ($count == 1) {
    
        &switchto("normal");
        &safeprint("$buffer[--$count]");
        
    } elsif ($count == 2) {
    
        &ade_msg_error("$buffer[0]^H: control sequence incomplete");
    
    }
    print "</pre>\n";
    print "</body>\n";
    print "</html>\n";
    close(PIPE);

    #&ade_lck_unlock("", \&ade_lck_getgenericlockfilename);
    return 0;
}

sub app_usage
{
    print "Usage:   $main::progname [ <options> ]\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 options
    print "\n";

    return(0);
}

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

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

    return(0);
}

sub app_listpaths
{
    return(0);
}

sub safeprint
{
    my($c) = @_;

    if ($c eq ">") {
        print "&gt;";

    } elsif ($c eq "<") {
        print "&lt;";

    } else {
        print $c;
    }

    return(0);
}

sub switchto
{
    my($mode) = @_;

    if ($mode eq $current_mode) {
        return;
    }

    if ($current_mode eq "bold") {
        print "</b>";
        $current_mode = "normal";
    } elsif ($current_mode eq "italic") {
        print "</i>";
        $current_mode = "normal";
    }

    if ($mode eq "bold") {
        print "<b>";
        $current_mode = "bold";
    } elsif ($mode eq "italic") {
        print "<i>";
        $current_mode = "italic";
    }
}

#spp include SPPSYM_ADEROOT_LIB/ade_utils.pl.spp
#spp include SPPSYM_ADEROOT_LIB/ade_gep.pl.spp
