#!/usr/local/bin/perl
#
#	@(#)docviewer.sh 2.0.1 93/08/13 earle@isolar.Tujunga.CA.US

# Display AnswerBook document pages using ndbm database keys and data

# If you don't have "perl", too bad.  Go get it.

# Revision history:
#
# 1.0Alpha	92/06/06	Initial release.  First thing I got to work
# 1.0Beta	92/06/07	Changed several things thanks to Wayne Thompson
#				All changes due to my own Perl brain-death
# 1.0		92/06/08	BOOKINFO and GSVIEWER now have default values
# 2.0		92/07/17	Fixed bugs that showed up when attempting to
#				use with Solaris 2.0 AnswerBook 2.0 - things
#				were not quite as they seemed
# 2.0.1		93/08/13	Trivial changes - (1) Added a work around for
#				brain-damaged database entries that forgot to
#				put the trailing colon and starting page # in
#				(See end of script - ever try running 2.0 on
#				 the 4.1.3 AB 1.4 Product Notes?); (2) added
#				the "study" command to speed up string matches;
#				and (3) added check to kill off those rogue
#				"gs" processes that get left hanging around.
#
# Requirements:

# Obviously, you need an AnswerBook (-:  This version supports all versions of
# AnswerBook from Solaris 1.0 (AB 1.1) to Solaris 2.1 (AB 2.3).  IT DOES NOT
# SUPPORT THE AnswerBook THAT COMES WITH Solaris 2.2 OR 2.3!  Sun changed the
# format of the AnswerBook drastically in the Solaris 2.2 AB release; blame
# them, not me.  I have no plans to try and support this mutant new version.
# If you are using the Solaris 2.1 AnswerBook (AB 2.3), I have a massaged
# version of the "answerbook" shell script that works with this under 4.1.x.

# Needs the XView libraries installed (libxview.s[oa].3.x and libolgx.s[oa].3.x)
# somewhere reachable, either in /usr/lib or /usr/local/lib or in your X11 R[45]
# lib directory or ... etc. - if the latter, e.g. $OPENWINHOME/lib, you'll
# probably have to set the LD_LIBRARY_PATH variable to point to that directory
# in order to pick the libraries up.  Otherwise, the Navigator program won't
# come up.

# Needs BOOKINFO set (default is /etc/bookinfo) either in the environment or in
# the "answerbook" script so it can find where the AnswerBook data files are

# Needs a new environment variable named GSVIEWER set to your preferred
# GhostScript previewer that's available in your $PATH - common choices are
# "ghostview" (requires version 1.3 or later to work) or "gspreview" (requires
# version 2.1 or later).
#
# Default is "gspreview" (I flipped a coin (-: )

# Warning: normal "answerbook" script puts DocViewer directory at the front of
# the search path, so to get this version first, I recommend changing it; e.g.:
#
# *** /cdrom/AnswerBook1.3/answerbook     Thu Dec 19 11:06:28 1991
# --- /usr/local/bin/answerbook   Sat Jun  6 04:20:29 1992
# ***************
# *** 49,53 ****
#   echo $PATH | fgrep -s  "$dvbin" ||
#   {
# !       PATH="$dvbin:$PATH"
#   }
#
# --- 60,65 ----
#   echo $PATH | fgrep -s  "$dvbin" ||
#   {
# ! #     PATH="$dvbin:$PATH"
# !       PATH="$PATH:$dvbin"
#   }

sub getbookinfo {

	$ENV{'BOOKINFO'} = $ENV{'BOOKINFO'} || "/etc/bookinfo";

	open(BOOKINFO, $ENV{'BOOKINFO'}) || die "Can't open bookinfo file!\n";

	while (<BOOKINFO>) {
		study;
		($db, $dbpathhead, $pspathhead) = split(/:/);
		if ($db eq $dbname) {
			return $dbpathhead;
		}
	}
	close(BOOKINFO);
}

$ENV{'GSVIEWER'} = $ENV{'GSVIEWER'} || "gspreview";

# Kill off any running ghostview/gspreview/whatever processes

open(PS, "/bin/ps -xc 2>&1 |");
while (<PS>) {
	if (/$ENV{'GSVIEWER'}/ || / gs/) {
		split(' ');
		push(@pids, $_[0]);
	}
}
close(PS);
$nkilled = kill("SIGTERM", @pids);

($dbname) = ((join($",@ARGV)) =~ /^.*<(\S+)>/);
($dbtoken) = (join('', '<', $dbname, '>', ((join($",@ARGV)) =~ /^.*>(\S+)/)));

$dbhead = &getbookinfo();

$dbpath = "$dbhead/$dbname";
$pspath = "$pspathhead/$dbname";

dbmopen(ABDB, $dbpath, undef) || die "can't dbmopen AnswerBook database";

while (($key, $val) = each %ABDB) {
	local ($data, $datum);

	$datum = unpack('@8A*', $key);
	study($datum);
	@data = split(/:/, $val);
	$page = @data[$#data];
	$doc = @data[$#data-1];
	last if (($datum eq $ARGV[$#ARGV]) || ($datum eq $dbtoken));
}

# Check for AnswerBook database brain damage.  There is at least one entry
# where the trailing colon and page number are missing (should come right after
# the file name).  If the page number is a file name by mistake (i.e., not
# numeric), assume the file name should be the page number and set the default
# page number to 1.  (Thanks to popo@sunbird.Central.Sun.COM for spotting it.)

if (!int($page)) {
	$doc = $page;
	$page = 1;
}

dbmclose(ABDB);
exec($ENV{GSVIEWER}, '-page', $page, "$pspath/$doc");
