#!/usr/bin/perl
#
# help: simple help utility
#
# Copyright (C) 1997 Mark Rigby-Jones
#
# 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.
#
# If you do not have a copy of the GNU General Public License then
# you can get one by writing to the Free Software Foundation, Inc.,
# 675 Mass Ave, Cambridge, MA 02139, USA.
#

$version = "help v2.0 by Mark Rigby-Jones, 5th March 1997";

$helpdir  = "/usr/local/lib/help";
$welcome  = "/usr/local/bin/welcome";
$helpname = "The Oxford University Computer Society Network Help System";
$pager    = "less -eis";
$quick    = "";

if ($ENV{PAGER} ne "") {
    $pager = $ENV{PAGER};
}

if ($0 eq $welcome) {
    $cmd = "welcome";
} elsif ($ARGV[0] eq "") {
    print("$helpname\nType \`help' for more information on how to use help.\n");
    $cmd = "";
} elsif (($ARGV[0] eq "-i") or ($ARGV[0] eq "--interactive")) {
    $cmd = $ARGV[1];
} elsif (($ARGV[0] eq "-h") or ($ARGV[0] eq "--help")) {
    print(<<"EOT");
usage: $0 <option> <topicname>
Help system. If no option or topicname is given, then help will enter
interactive mode.
	      
-i, --interactive    Show information on <topicname> then go interactive
-h, --help           Print this information then exit
-V, --version        Print version information then exit

For more information, try \`$0 help' or read the manual page.
EOT
    exit(0);
} elsif (($ARGV[0] eq "-V") or ($ARGV[0] eq "--version")) {
    print("$version\n");
    exit(0);
} else {
    $cmd = $ARGV[0];
    $quick = "yes";
}

while (0 == 0) {
    @cmd = split(' ', $cmd);
    $cmd = join(' ', @cmd);
    if (($cmd[0] eq "help") && ($cmd[1] ne "")) {
	$cmd = $cmd[1];
    }
    if (($cmd eq "h") or ($cmd eq "help")) {
	$helpfile = "$helpdir/help";
	if (stat($helpfile)) {
	    system("$pager $helpfile");
	} else {
	    print("$helpname\n");
		print(<<"EOT");
Type one of the following commands:
  <topicname> - for information on that topic
  list        - for a list of topics
  man <args>  - read / search manual pages
  info <args> - view GNU info pages
  help        - to display this information
  quit        - exit help
EOT
	}
    } elsif (($cmd eq "l") or ($cmd eq "list") or ($cmd eq "topics")) {
	print("Help is available on the following topics:\n");
	system("ls $helpdir");
    } elsif ($cmd eq "man") {
	print("Try \`man <command>' to read a manual page\n");
	print(" or \`man -k <string>' to search for relevant manual pages\n");
    } elsif (($cmd[0] eq "man") or ($cmd[0] eq "info")) {
	system($cmd);
    } elsif (($cmd eq "q") or ($cmd eq "quit") or ($cmd eq "x") or ($cmd eq "exit") or ($cmd eq "bye")) {
	exit(0);
    } elsif ($cmd ne "") {
	$helpfile = "$helpdir/$cmd";
	if (stat($helpfile)) {
	    system("$pager $helpfile") ;
	} else {
	    open(RPM, "rpm -qi $cmd 2>&1 |");
            $line = <RPM>;
            if ($line !~ /Name/) {
		print("No help exists on \`$cmd'. Type \`list' for a list of topics.\n");
	    } else {
                open(OUT, "| $pager");
                print(OUT $line, <RPM>);
                close(OUT);
            }
            close(RPM);
	}
    }
    if ($quick ne "") {
	exit(0);
    }
    print("help> ");
    chop($cmd = <STDIN>);
}
