#!/usr/local/bin/perl
#
#	@(#)docviewer.sh 2.0 92/07/17 SMI

# Display AnswerBook document pages using ndbm database keys and data

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

# This is Version 1.0 (meaning, it's the first thing that I got to work)

# 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
#
# Requirements:

# 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 to work!) or "gspreview" (requires 2.1)
# 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>) {
		($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'}/) {
		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);
	@data = split(/:/, $val);

	$page = @data[$#data];
	$doc = @data[$#data-1];
	last if (($datum eq $ARGV[$#ARGV]) || ($datum eq $dbtoken));
}

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