#!/usr/bin/perl
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
    if $running_under_some_shell;
##
##  WMd -- Website META Language Documentation Browser
##  
##  Copyright (c) 1996-1997 Ralf S. Engelschall, All Rights Reserved. 
##  
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##  
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to
##  
##      Free Software Foundation, Inc.,
##      675 Mass Ave, Cambridge,
##      MA 02139, USA.
##  
##  Notice, that ``free software'' addresses the fact that this program
##  is __distributed__ under the term of the GNU General Public License
##  and because of this, it can be redistributed and modified under the
##  conditions of this license, but the software remains __copyrighted__
##  by the author. Don't intermix this with the general meaning of 
##  Public Domain software or such a derivated distribution label.
##  
##  The author reserves the right to distribute following releases of
##  this program under different conditions or license agreements.
##

require 5.003;

$VERSION = "1.4.1 (24-11-1997)";

use lib "/var/tmp/perl-root/usr//lib/wml/perl/lib";
use lib "/var/tmp/perl-root/usr//lib/wml/perl/lib/i386-linux/5.00401";
use lib "/var/tmp/perl-root/usr//lib/wml/perl/lib/site_perl";
use lib "/var/tmp/perl-root/usr//lib/wml/perl/lib/site_perl/i386-linux";

use Getopt::Long 2.12;


##
##  INIT
##

if ($ENV{'PATH'} !~ m|/usr//bin|) {
    $ENV{'PATH'} .= ':/usr//bin';
}


##
##  PROCESS ARGUMENT LINE
##

sub usage {
    my ($progname) = @_;
    my ($o);

    print STDERR "Usage: $progname [options] [path ...]\n";
    print STDERR "\n";
    print STDERR "Giving Feedback:\n";
    print STDERR "  -V, --version[=NUM]    display version and build information\n";
    print STDERR "  -h, --help             display this usage summary\n";
    print STDERR "\n";
    exit(1);
}

sub version {
    print STDERR "This is WML/WMd Version $VERSION\n";
    print STDERR "Copyright (c) 1996-1997 Ralf S. Engelschall, All Rights Reserved.\n";
    print STDERR "\n";
    print STDERR "This program is distributed in the hope that it will be useful,\n";
    print STDERR "but WITHOUT ANY WARRANTY; without even the implied warranty of\n";
    print STDERR "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n";
    print STDERR "GNU General Public License for more details.\n";
    if ($opt_V > 1) {
        print STDERR "\n";
        print STDERR "Built Environment:\n";
        print STDERR "    Host: ".'i686-pc-linux-gnu'."\n";
        print STDERR "    Perl: ".'5.004_01 (/usr/bin/perl)'."\n";
        print STDERR "    User: ".'jhebert@college.comm.fsu.edu'."\n";
        print STDERR "    Date: ".'Thu Nov 27 00:38:25 EST 1997'."\n";
        print STDERR "Built Location:\n";
        print STDERR "    Prefix: ".'/usr/'."\n";
        print STDERR "    BinDir: ".'/usr//bin'."\n";
        print STDERR "    LibDir: ".'/usr//lib/wml/wml'."\n";
        print STDERR "    ManDir: ".'/usr//man'."\n";
    }
    if ($opt_V > 2) {
        print STDERR "\n";
        print STDERR "Used Perl System:\n";
        print STDERR `/usr/bin/perl -V`;
    }
    exit(0);
}

#   options
$opt_V = -1;
$opt_h = 0;

sub ProcessOptions {
    $Getopt::Long::bundling = 1;
    $Getopt::Long::getopt_compat = 0;
    $SIG{'__WARN__'} = sub { 
        print STDERR "WMd:Error: $_[0]";
    };
    if (not Getopt::Long::GetOptions(
            "V|version:i",
            "h|help"
    )) {
        print STDERR "Try `$0 --help' for more information.\n";
        exit(0);
    }
    &usage($0) if ($opt_h);
    $SIG{'__WARN__'} = undef;
}
&ProcessOptions();

#   fix the version level
if ($opt_V == 0) {
    $opt_V = 1; # Getopt::Long sets 0 if -V only
}
if ($opt_V == -1) {
    $opt_V = 0; # we operate with 0 for not set
}
&version if ($opt_V);


##
##  Find browser
##

$browser      = 'wml_aux_iselect';
$browser_file = '/usr//lib/wml/wml/aux/wmd.txt';

$reader_man  = 'MANPATH="$MANPATH:/usr//man"; export MANPATH; man';
$reader_url  = 'lynx';

$p = 12;
while (1) {
    $rc = `$browser -n "Website META Language" -t "Documentation Browser" -p$p -P <$browser_file`;
    last if ($rc eq '');
    $rc =~ m|^(\d+):(.*)|;
    ($p, $line) = ($1, $2);
    if ((($page, $sec) = $line =~ m|^\s*(\S+)\((\d)\)\s+|)) {
        system("$reader_man $page");
    }
    elsif ((($url) = $line =~ m|^(http://\S+)|)) {
        system("$reader_url $url || (echo 'Sorry, no Lynx installed'; sleep 5)");
    }
}

#   exit gracefully
exit(0);

##EOF##
