#!/bin/sh
#Tag 0x006d
# Copyright (c) 1993 by Adobe Systems Incorporated.
# All rights reserved.
#
# Shell script for launching Acrobat.
# This script determines what type of machine/os the user is
# running and then fires off the proper version of the software.
#
# In general, this script accepts command line parameters in the form:
#	-e xxx=yyy
# where "xxx" is a shell variable name and "yyy" is the value.
# For each parameter of this form, this script does an eval on the parameter.
# The following shell variables are reasonable candidates for setting on the 
# command line (see comments below for a description of these variables):
#	DPSNX_DIR
#	ENSURE_DOUBLE_COLON
#	USEFRONTENDPROGRAM
#	PATCH_WARNINGS
#	DPSNX_PRODUCT_NAME
# Example: acroexch -e DPSNX_DIR=/usr/adobe/DPSNXBasic_2.1
###################################################################


productname="acroread"
productabbr="AcroRead"
frontendname="acroread_frontend"
execname="acroread_exec"
viewerversion="1.0.1"
EXEC_COMMAND=
XKEYSYMDB_NAME="IGNORE"
XKEYSYMDB_VALUE="IGNORE"
EXPORT_ENV=0
PATCH_WARNINGS=1

DPSNX_PRODUCT_NAME=DPSNXBasic_2.1

LS=/bin/ls

# Make sure DISPLAY variable has a value
export DISPLAY
if [ "$DISPLAY" = "" ] ; then
    DISPLAY=":0"
fi

# Determine the OS/productversion/chip/GUI
# appropriate for the current user based on the computer
# he is currently logged into.
os="UNKNOWN"
osname="UNKNOWN"
osversion="UNKNOWN"
osvers1="UNKNOWN"
osvers2="UNKNOWN"
osvers3="UNKNOWN"
error=0
if [ -r /bin/uname ]
then
	osname=`/bin/uname -s`
	osversion=`/bin/uname -r`
	check2=`echo "$osversion" | egrep '^[0-9]*\.[0-9]*'`
	check3=`echo "$osversion" | egrep '^[0-9]*\.[0-9]*\.[0-9]*'`
	if [ "$check3" != "" ]
	then
		osvers1=`echo "$osversion" | sed -e 's/^\([0-9][0-9]*\).*/\1/'`
		osvers2=`echo "$osversion" | sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/'`
		osvers3=`echo "$osversion" | sed -e 's/^[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/'`
	elif [ "$check2" != "" ]
	then
		osvers1=`echo "$osversion" | sed -e 's/^\([0-9][0-9]*\).*/\1/'`
		osvers2=`echo "$osversion" | sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/'`
	fi
fi
chip="UNKNOWN"

if [ "$osname" = "SunOS" ]
then
	# Sun machines: we only support Sparc under SunOS4.1 and SunOS5.3
	if [ -r /usr/bin/sparc ] && /usr/bin/sparc ; then
		chip="sparc"
		if [ "$osvers2" = "UNKNOWN" ] ; then
			echo Invalid operating system version returned from 'uname -r': $osversion$bell.
			error=1
		elif [ "$osvers1" = "4" ] && [ "$osvers2" -ge 1 ] ; then
			if [ "$osvers3" = "UNKNOWN" -o "$osvers3" -lt 3 ] ; then
			        if [ "$osvers3" = "UNKNOWN" -o "$osvers3" -lt 1 ] ; then
					echo Unsupported operating system version: $osversion. SunOS 4.1.3 or 5.3 is required.
					error=1
				else
					echo Unsupported operating system version: $osversion. Will continue anyway.
					os="sunos4.1"
				fi
			else
				os="sunos4.1"
			fi
		elif [ "$osvers1" = "5" ] ; then
 			if [ "$osvers2" -lt 3 ] ; then
				echo Unsupported operating system version: $osversion. SunOS 4.1.3 or 5.3 is required.
				error=1
			else
				os="sunos4.1"
			fi
		else
			echo Invalid operating system version returned from 'uname -r': $osversion$bell.
			error=1
		fi
	else
		echo $productname is not available for your type of Sun machine$bell.
		error=1
	fi

elif [ "$osname" = "IRIX" ]
then
	if [ -r /bin/mips ] && /bin/mips
	then
		chip="mips"
		if [ "$osvers1" = "5" -a "$osvers2" -ge "3" ] ; then
			os="irix5.3"
		else
			echo Invalid operating system version returned from 'uname -r': $osversion$bell.
			error=1
		fi
	else
		echo $productname is not available for your type of SGI machine$bell.
		error=1
	fi
fi
if [ $error != 0 ] ; then
	exit
fi

# <INSTALL_DIR> holds the directory into which the product
# was installed (e.g., /usr/adobe/AcroExch_1.0).
# This script computes the value of INSTALL_DIR as follows:
#	1) If environment variable ACROEXCH_HOME has a value, then that is used.
#	2) Otherwise, try to determine it automatically by determining the
#		location of this launch script and using its parent directory.
#		If file <parent directory>/exec/acroexch_exec exists, then assume
#		we have determined the parent directory correctly.
#	3) Otherwise, use the value hardcoded into this script.
# INSTALL_DIR=""
# -- hard code to install on /usr/adobe -- nyung 
INSTALL_DIR=""
if [ "$ACROEXCH_HOME" != "" ] ; then
	INSTALL_DIR="$ACROEXCH_HOME"
else
	# Try to determine automatically the parent directory to this launch script.
	thisscriptname=$0

	# Follow any symbolic links in thisscriptname.
	while : ; do
		lsresult=`$LS -l $thisscriptname`
		symlink=`expr "$lsresult" : '.*>'.*`

		if [ "$symlink" -gt 0 ] ; then
			oldscriptname="$thisscriptname"
			thisscriptname=`echo $lsresult | sed -e 's/^.*-> *\(.*\) *$/\1/g'`
			abspath_new=`expr "$thisscriptname" : '^/'`
			if [ $abspath_new != 1 ] ; then
				abspath_old=`expr "$oldscriptname" : '^/'`
				if [ $abspath_old != 1 ] ; then
					olddir=`pwd`
				else
					olddir=`expr "$oldscriptname" : '\(.*\)/'`
				fi
				thisscriptname="$olddir/$thisscriptname"
			fi
		else
			break
		fi
	done

	# Determine directory containing this shell script.
	scriptdir=`expr $thisscriptname : '\(.*\)/'` 
	if [ "$scriptdir" = "" -o "$scriptdir" = "." ] ; then
		scriptdir=`pwd`
	fi

	# Determine root directory for installation ($scriptdir/..).
	parentdir=`expr $scriptdir : '\(.*\)/'` 

	if [ -r "$parentdir/exec/$execname" ] ; then
		INSTALL_DIR="$parentdir"
	fi
fi

# <LM_LICENSE_FILE> is the file containing the license keys for this app.
LM_LICENSE_FILE="$INSTALL_DIR/license/license.dat"
export LM_LICENSE_FILE

# <SITE_PSRESOURCEPATH> is a list of directories contain *.upr files that
# point to site-specific PostScript resources (such as fonts) that might
# be useful to this application.
# This launch script first looks for a file of the name
# $INSTALL_DIR/custom/SITE_PSRESOURCEPATH and looks for a line in this file
# of the form "SITE_PSRESOURCEPATH=xxx" (must start in column 1)
# and sets environment variable SITE_PSRESOURCEPATH to xxx.
# If this application does not find it PostScript resources properly, it could
# be that this file is missing or has an incorrect value.
# If so, edit this file with a text editor.
# Alternatively, you can override the value of SITE_PSRESOURCEPATH by launching
# this script with '-e SITE_PSRESOURCEPATH=xxx' command line arguments.
# Finally, if $INSTALL_DIR/custom/SITE_PSRESOURCEPATH does not have a valid
# entry and if no '-e SITE_PSRESOURCEPATH=xxx' option is given, then this
# script will use the value of SITE_PSRESOURCEPATH that was inherited from the
# user's current environment.
if [ -f "$INSTALL_DIR/custom/SITE_PSRESOURCEPATH" ] ; then
	site_path=`grep '^SITE_PSRESOURCEPATH' "$INSTALL_DIR/custom/SITE_PSRESOURCEPATH"  | sed -e 's/^SITE_PSRESOURCEPATH= *\([^ ]\)/\1/'`
	if [ "$site_path" != "" ] ; then
		eval SITE_PSRESOURCEPATH="$site_path"
	fi
fi

# <DPSNX_DIR> holds directory into which DPS/NX is installed.
# If this front-end shell script does not work in your environment, then you
# can provide a hard-coded value via a command line argument.
# If the value is "", then this script will determine a value automatically.
# If <DPSNX_DIR> does not begin with a slash, then the directory is assumed
# to be relative to $rootdir.
if [ -f "$INSTALL_DIR/custom/DPSNX_DIR" ] ; then
	nxdir=`grep '^DPSNX_DIR' "$INSTALL_DIR/custom/DPSNX_DIR"  | sed -e 's/^DPSNX_DIR= *\([^ ]\)/\1/'`
	if [ "$nxdir" != "" ] ; then
		eval DPSNX_DIR="$nxdir"
	fi
fi

# if "ENSURE_DOUBLE_COLON" == 1, then we'll modify PSRESOURCEPATH to contain
# a "::" if necessary; otherwise we will not modify PSRESOURCEPATH
ENSURE_DOUBLE_COLON=0

# if "USEFRONTENDPROGRAM" == 1, then a small front end program is
# executed in front of the regular main program.
# The front end program checks to see if a communication link is
# set up with a running regular main program. If so, then it
# simply passes file names to the regular main program. Otherwise,
# it launches a new main program.
# To turn off the front end program, set USEFRONTENDPROGRAM to 0.
USEFRONTENDPROGRAM=1
if [ -f "$INSTALL_DIR/custom/USEFRONTENDPROGRAM" ] ; then
	usefrontend=`grep '^USEFRONTENDPROGRAM' "$INSTALL_DIR/custom/USEFRONTENDPROGRAM"  | sed -e 's/^USEFRONTENDPROGRAM= *\([^ ]\)/\1/'`
	if [ "$usefrontend" != "" ] ; then
		USEFRONTENDPROGRAM="$usefrontend"
	fi
fi

# Save tail of argv[0] in environment variable RESOURCE_NAME.  This value
# will be used by Xt to establish the name of the app, instead of using
# the argv[0] found by the executable (i.e., acroexch_exec).  This allows
# users to get the expected app name, based on the name of the script
# they are invoking.  (This also allows the "-name" command-line argument
# to take precedence, as it should.)
RESOURCE_NAME=`basename $0`
if [ "$EXPORT_ENV" = 1 ] ; then
	export RESOURCE_NAME
fi

if [ "$os" = "sunos4.1" ]
then
	# <XKEYSYMDB> holds the osf Motif keysyms that this app looks for.

	# Default keysym file for SunOS is missing Motif keysyms.
	XKEYSYMDB_NAME="XKEYSYMDB"
	XKEYSYMDB_VALUE="$INSTALL_DIR/installscripts/XKeysymDB"
	XKEYSYMDB="$XKEYSYMDB_VALUE"
	if [ "$EXPORT_ENV" = 1 ] ; then
		export XKEYSYMDB
	fi
fi

bell=`echo a | /usr/bin/tr a '\007'`

fontsdir=$INSTALL_DIR/fonts

# Set up environment variable XFILESEARCHPATH
# so Acrobat can find its resource files
viewerX11dir=$INSTALL_DIR/lib/X11
viewersearchpath=$viewerX11dir/%L/%T/%N%S:$viewerX11dir/%l/%T/%N%S/:$viewerX11dir/%T/%N%S
XFILESEARCHPATH="$viewersearchpath${XFILESEARCHPATH+:$XFILESEARCHPATH}"
if [ "$EXPORT_ENV" = 1 ] ; then
	export XFILESEARCHPATH
fi

# up environment variable XBMLANGPATH
# so Acrobat can find its bitmap files
# note: we stick it at the END of any pre-existing XBMLANGPATH, so that
# the user can have his bitmaps chosen over the built-in versions.  For
# this to work in a sane way, we ought to rename our bitmap files
# to contain the class name.
XBMLANGPATH="$XBMLANGPATH${XBMLANGPATH+:}$INSTALL_DIR/include/X11/bitmaps/%B"
if [ "$EXPORT_ENV" = 1 ] ; then
	export XBMLANGPATH
fi

# put a double colon at end of PSRESOURCEPATH, if needed and requested
if [ "$ENSURE_DOUBLE_COLON" = "1" ]
then
	case "$PSRESOURCEPATH" in
	"") ;;
	*::) export PSRESOURCEPATH ;;
	*:) PSRESOURCEPATH="$PSRESOURCEPATH": export PSRESOURCEPATH ;;
	*) PSRESOURCEPATH="$PSRESOURCEPATH":: export PSRESOURCEPATH ;;
	esac
fi

# Process command line parameters.
BYPASSFRONTENDPROGRAM=0
while [ $# -gt 0 ] ; do

	# "-help" prints a usage message and then exits.
	if [ "$1" = "-help" -o "$1" = "-helpall" ] ; then
		echo ""
		echo "Usage: $RESOURCE_NAME [options] [list of files]"
		echo ""
		echo "Acrobat launch script. Options:"
		echo "    -display <display>"
		echo "        This option specifies the host and display to use."
		echo "    -e LM_LICENSE_FILE=<licensefile>"
		echo "        Tells $RESOURCE_NAME that <licensefile> holds the product's license."
		echo "    -e USEFRONTENDPROGRAM=<0|1>"
		echo "        By default, $RESOURCE_NAME launches a small front-end program which"
		echo "        determines if the application is already running on this"
		echo "        machine and displaying on this display. If the application"
		echo "        is already running, then the front-end program sends"
		echo "        messages to the application telling it to load any files"
		echo "        specified on the command line. If the application is not"
		echo "        already running, the front-end program launches the"
		echo "        application."
		echo ""
		echo "        If you want to disable the front-end program and instead"
		echo "        launch the main application directly, either change file"
		echo "        <installdir>/<product_version>/custom/USEFRONTENDPROGRAM"
		echo "        to set USEFRONTENDPROGRAM to 0 or launch the application"
		echo "        with: $RESOURCE_NAME -e USEFRONTENDPROGRAM=0"
		if [ "$1" = "-helpall" ] ; then
			echo "    -e ENSURE_DOUBLE_COLON=<0|1>"
			echo "        If environment variable PSRESOURCEPATH has a value and"
			echo "        the string :: is not part of that value, then Acrobat may"
			echo "        not be able to find its own default font resources."
			echo "        If you want this launch script to automatically add ::"
			echo "        to PSRESOURCEPATH, launch the application with:"
			echo "        $RESOURCE_NAME -e ENSURE_DOUBLE_COLON=1"
		fi
		echo "    -help"
		echo "        Prints the more common command-line options."
		echo "    -helpall"
		echo "        Prints out all possible command-line options."
		echo "    -iconic"
		echo "        Launches $RESOURCE_NAME in an iconic state on the desktop."
		echo "    -print [-P<printer>]"
		echo "        Tells $RESOURCE_NAME to print all files listed on the command line."
		echo "        If -P<printer> is specified, then the files will be printed"
		echo "        on <printer>."
		if [ "$1" = "-helpall" ] ; then
			echo "    -setenv <var>=<value>"
			echo "        Tells the main application to perform the equivalent of C-shell"
			echo "        setenv <var> <value>. (Not available on all platforms.)"
		fi
		echo "    -toPostScript pdf_file ... [ps_dir]"
		echo "    -toPostScript -pairs pdf_file_1 ps_file_1 ..."
		echo "    -toPostScript"
		echo "        Tells $RESOURCE_NAME to convert the given pdf_files to PostScript."
		echo ""
		echo "        In the first form, if the last file specified is a directory,"
		echo "        then all preceding files will be converted to PostScript"
		echo "        and the generated PostScript files will be placed into ps_dir."
		echo "        If a directory is not specified, then the PostScript files"
		echo "        will be placed in the same directory as the original file."
		echo ""
		echo "        In the second form, the file list contains pairs, each consisting"
		echo "        of a PDF filename and a corresponding PostScript filename."
		echo ""
		echo "        The third form allows $RESOURCE_NAME to be used as a filter, reading a"
		echo "        PDF file from standard input and writing the PostScript"
		echo "        file to standard output."
		echo "    -xrm <X resource specification>"
		echo "        Standard X Window System resource specification on command line."
		if [ "$1" = "-helpall" ] ; then
			echo ""
			echo "    FRONT-END PROGRAM OPTIONS:"
			echo "    The following options only apply if the front-end program is used"
			echo "    (see USEFRONTENDPROGRAM above):"
			echo ""
			echo "    -acrobatprogram <filename>"
			echo "        Used to pass main application name to front-end program."
			echo "        Do not use this option."
			echo "    -fifoname <filename>"
			echo "        The front-end program uses a Unix named-pipe/FIFO to pass"
			echo "        messages to the application. The default name of the FIFO"
			echo "        is $RESOURCE_NAME.fifo.<display>. You can specify a different name for"
			echo "        the FIFO with this option."
			echo "    -plimit <seconds>"
			echo "        How long front-end program should wait for application to launch."
			echo "        Default: 30 seconds."
			echo "    -firsttimeout <seconds>"
			echo "        How long front-end program should wait for acknowledgement from"
			echo "        application that it received the first filename passed as"
			echo "        a message via FIFO. Default: 10 seconds."
			echo "    -subseqtimeout <seconds>"
			echo "        How long front-end program should wait for acknowledgement from"
			echo "        application that it received a subsequent filename passed as"
			echo "        a message via FIFO. Default: 100 seconds."
		fi
		echo ""
		exit 0

	# "-e" allows particular values from this script
	# to be overwritten from the command line.
	# For example, -e LM_LICENSE_FILE=/usr/local/licenses/license.dat
	#
	# We insist that all of these "-e" args must come at the start
	# of the command line.  Any others will be passed along to the
	# application
	elif [ "$1" = "-e" ] ; then
		shift
		eval "$1"
		shift
	elif [ "$1" = "-toPostScript" ] ; then
		BYPASSFRONTENDPROGRAM=1
		break
	elif [ "$1" = "-print" ] ; then
		BYPASSFRONTENDPROGRAM=1
		break
	elif [ "$1" = "-n" ] ; then
		shift
		EXEC_COMMAND=echo
	else
		break
	fi
done

# Determine base directory for installation of Adobe products. Default: /usr/adobe.
baseinstalldir=`expr $INSTALL_DIR : '\(.*\)/'` 
if [ "$baseinstalldir" = "" ] ; then
	baseinstalldir="/usr/adobe"
fi

if [ "$DPSNX_DIR" = "" ] ; then
	DPSNX_DIR="$baseinstalldir/$DPSNX_PRODUCT_NAME"
fi
DPSNXBC=1.0
export DPSNXBC


# Do not use front-end program if -toPostScript or -print specified.
if [ "$BYPASSFRONTENDPROGRAM" = 1 ] ; then
	USEFRONTENDPROGRAM=0
fi

# set up default PSRes Path for viewer.  This is the value the viewer
# will will use in place of "::" in PSRESOURCEPATH environment variable
case "$SITE_PSRESOURCEPATH" in
"") defaultPSResPath="$fontsdir" ;;
*:) defaultPSResPath="$SITE_PSRESOURCEPATH$fontsdir" ;;
*) defaultPSResPath="$SITE_PSRESOURCEPATH:$fontsdir" ;;
esac

if [ "$PATCH_WARNINGS" = 1 ] ; then
	# Check for patches 101362-04 or greater.
	if [ "$osname" = SunOS -a "$osvers1" -eq 5 -a "$osvers2" -le 3 ] ; then
    		# Patches will end up with '<patchnum> <rev> <patchnum> <rev>...'
    		patches=`/usr/bin/showrev -p | /usr/bin/awk '{ print $2; }' | /usr/bin/sed 's/-/ /g' | /usr/bin/tr '\012' ' '`
		patch_101362_04=0
		patchnum=0
		for value in $patches; do
			if [ $patchnum = 0 ] ; then
				patchnum=$value
			else
				rev=$value
				if [ "$patchnum" -eq 101362 -a "$rev" -ge 04 ] ; then
					patch_101362_04=1
				fi
				patchnum=0
			fi
		done
		if [ $patch_101362_04 = 0 ] ; then
			echo ""
			echo 'Warning: patch 101362-04 (or greater) is not installed on this system.'
			echo 'This software might hang if this patch is not installed.'
			echo 'Refer to Getting Started guide and readme for more details.'
		fi
	fi
fi


# Execute the binary for the application.
# The incantation ${1+"$@"} is black-magic to pass args correctly,
# even if some args contain embedded spaces
if [ "$USEFRONTENDPROGRAM" = 1 ] ; then
	# Invoke small front end program which checks for a running
	# Acrobat and passes file names to it if it exists.
	# Otherwise, it launches a new viewer.
	# The XKEYSYMDB line results in IGNORE=IGNORE for all but SunOS4.1
	$EXEC_COMMAND $INSTALL_DIR/exec/$frontendname \
		-acrobatprogram $INSTALL_DIR/exec/$execname \
		-xrm "*defaultPsresPath:$defaultPSResPath" \
		-xrm "*installPath:$INSTALL_DIR" \
		-xrm "*dpsnxAgentExec:dpsnx.agent" \
		-xrm "*dpsnxPSResDir:$DPSNX_DIR/psres" \
		-setenv XFILESEARCHPATH="$XFILESEARCHPATH" \
		-setenv XBMLANGPATH="$XBMLANGPATH" \
		-setenv LM_LICENSE_FILE="$LM_LICENSE_FILE" \
		-setenv RESOURCE_NAME="$RESOURCE_NAME" \
		-setenv $XKEYSYMDB_NAME="$XKEYSYMDB_VALUE" \
		${1+"$@"} 
else
	# Directly invoke the viewer.
	# The XKEYSYMDB line results in IGNORE=IGNORE for all but SunOS4.1
	$EXEC_COMMAND $INSTALL_DIR/exec/$execname \
		-xrm "*defaultPsresPath:$defaultPSResPath" \
		-xrm "*installPath:$INSTALL_DIR" \
		-xrm "*dpsnxAgentExec:dpsnx.agent" \
		-xrm "*dpsnxPSResDir:$DPSNX_DIR/psres" \
		-setenv XFILESEARCHPATH="$XFILESEARCHPATH" \
		-setenv XBMLANGPATH="$XBMLANGPATH" \
		-setenv LM_LICENSE_FILE="$LM_LICENSE_FILE" \
		-setenv RESOURCE_NAME="$RESOURCE_NAME" \
		-setenv $XKEYSYMDB_NAME="$XKEYSYMDB_VALUE" \
		${1+"$@"} 
fi
