#! /bin/csh -f
# @(#)printdom	1.17	12/17/92

# Print documentation for a Ptolemy domain.  Assumed to be started in
# the "doc" subdirectory of a domain.

# Command uses printer defined by environmental variable $PRINTER
# User .cshrc file must contain "setenv PRINTER whatever"

# Command line options:
#
#	-index		Generate an index only, print nothing.
#
#	-preview	Preview domain via xditview, or nroff if DISPLAY
#			environment variable isn't set.
#
#	-icons		Include postscript icons in the printout.
#			Postscript icons must already exist; see the script
#			$PTOLEMY/src/doc/makepsicons.  This can't be used
#			with the -preview option, and can really probably
#			only be used at Ptolemy Headquarters in Berkeley.
#
# Remaining arguments are taken to be files to print options.
#
# An implicit header file, $PTOLEMY/doc/headers/shared, is prepended.

# The following files are assumed to present in the documentation directory:
#	overall	  - The main documentation section.
#		    It may source other files with .so.
#	startitle - The title page for the stars section.
#	stars	  - A directory with star documentation
#	demotitle - The title page for the demo section
#	demos	  - A directory with demo documentation.
#	targettitle - The title page for the target section.
#	targets   - The directory with the target documentation.

# Index information is appended to the file $HOME/Almagest.index,
# or to whatever ALAMAGEST_INDEX is set to.

# On interrupt, remove temporary files
onintr catch

if (!($?PTOLEMY)) then
	setenv PTOLEMY ~ptolemy
endif

# File structure information:
set dirheader = $PTOLEMY/doc/headers
set printcommand = ditroff
set printoptions = "-me -tbl -eqn -P$PRINTER"
set contentscmd = $PTOLEMY/doc/bin/contents
set previewcommand = "xditview -"
set psincludecommand = $PTOLEMY/doc/bin/ditpsinc

# Variables
set indexonly = 0
set preview = 0
set icons = 0

if (!($?ALMAGEST_INDEX)) then
	setenv ALMAGEST_INDEX $HOME/Almagest.index
endif

while ($#argv)
	switch ($argv[1])
		case -index:
			set indexonly = 1
			shift
			breaksw
		case -preview:
			set preview = 1
			shift
			breaksw
		case -icons:
			set icons = 1
			shift
			breaksw
		case -*:
			set printoptions = ($printoptions $argv[1])
			shift
			breaksw
		default:
			cd $1
			shift
			breaksw
	endsw
end

if ($icons && $preview) then
	echo "printdom: -preview and -icons flags are mutually exclusive, sorry."
	exit 1
endif


soelim $dirheader/shared $dirheader/domain \
	init overall > /tmp/pt$$

# Set troff icon printing flag: 0 if not printing icons, 1 if printing them.
if ($icons) then
	echo '.nr Ic 1' >> /tmp/pt$$
else
	echo '.nr Ic 0' >> /tmp/pt$$
endif

if ( -d targets ) then
        if (-e targettitle) then
                # set type for this section
                echo '.ds TY "target' >> /tmp/pt$$
                cat targettitle >> /tmp/pt$$
        endif
        cd targets
        # print all targets
        cat -s * >> /tmp/pt$$
        cd ..
endif

if ( -d stars ) then
	if (-e starstitle) then
		# set type for this section
		echo '.ds TY "star' >> /tmp/pt$$
		cat starstitle >> /tmp/pt$$
	endif
	cd stars
	# print all stars, sorted in dictionary order
	foreach file (`ls -1 -d * | sort -f -d`)
		cat -s $file >> /tmp/pt$$
	end
	cd ..
endif

if ( -d demos ) then
	if (-e demotitle) then
		# set type for this section
		echo '.ds TY "demo' >> /tmp/pt$$
		cat demotitle >> /tmp/pt$$
	endif
	cd demos
	# print all demos
	cat -s * >> /tmp/pt$$
	cd ..
endif

if ($indexonly) then
	# if only generating index, add -t option and throw away the output
	($printcommand $printoptions -t /tmp/pt$$ > /dev/null) >& /tmp/index$$
else if ($preview) then
	# if previewing, use either X windows or nroff
	if ($?DISPLAY) then
		$printcommand $printoptions -t /tmp/pt$$ | $previewcommand
	else
		(eqn /tmp/pt$$ | tbl | nroff -me > /tmp/ptnr$$) >& /dev/null
		more /tmp/ptnr$$
	endif
else if ($icons) then
	# printing icons -- generates postscript
	($printcommand $printoptions -t /tmp/pt$$ | $psincludecommand > \
		/tmp/pt.$$.ps) >& /tmp/index$$
	echo "printdom: generated postscript in /tmp/pt.$$.ps"
else
	# otherwise just print it out
	$printcommand $printoptions /tmp/pt$$ >& /tmp/index$$
	$contentscmd /tmp/index$$
	$printcommand $printoptions $dirheader/shared title
endif
if (-r /tmp/index$$) cat /tmp/index$$ >>! $ALMAGEST_INDEX
/bin/rm -f /tmp/pt$$ /tmp/index$$ /tmp/prnr$$
exit 0

catch:
        # prevent the "no match" error message if there are no temporary files
        set nonomatch
	/bin/rm -f /tmp/pt$$ /tmp/index$$ /tmp/prnr$$
        exit 1
