#!/bin/sh
# Copyright (c) 1994 by Adobe Systems Incorporated.
# Script called immediately after a package is installed.
# $1=Install directory for package.
# $2=Directory original script was run from.
########################################################

###########################################################################
#
#			BEGIN SUBROUTINES
#
###########################################################################

tell_user_not_enough_space()
{
	${ECHO} ""
	${ECHO} ""
	${ECHO} "There is not enough disk space for installation of the PPD database."
	${ECHO} "The PPD database requires $total_kbytes kilobytes of disk space"
	${ECHO} "and you currently have only $disk_kbytes kilobytes of available disk space"
	${ECHO} "in directory ${PPDDIR}."
	${ECHO} ""
}

#
# status at completion:
#
# 0 - o.k.
# 1 - no directory and could not create
# 2 - inadequate permissions for installation
#

validate_ppddir()
{
	# benefit of doubt
	status=$STATUS_OK

	# Remove trailing '/' if any
	PPDDIR=`${ECHO} "$PPDDIR" | $SED -e 's/\/$//'`
	if [ ! -d $PPDDIR  ] ; then
		$MKDIR $PPDDIR || { status=$STATUS_DIRERR; return; }
	fi

	if [ ! -w $PPDDIR  ] ; then
		status=$STATUS_PERMERR
	fi
}

compute_ppddir()
{
	PPDDIR="$DFTPPDDIR"

	validate_ppddir

	case $status in
		$STATUS_OK)
			;;
		$STATUS_DIRERR)
			${ECHO} ""
			${ECHO} "Could not create directory $PPDDIR."
			${ECHO} ""
			exit 1
			;;
		$STATUS_PERMERR)
			${ECHO} ""
			${ECHO} "You do not have write permissions for the directory"
			${ECHO} "$PPDDIR.  Please correct and re-install."
			${ECHO} ""
			exit 1
			;;
		*)
			${ECHO} ""
			${ECHO} "Internal error. Unable to validate PPD directory."
			eval ${ABORTCMD}
			;;
	esac

}

prompt_for_ppddir()
{
	rootdir_loop=0
	while : ; do
		rootdir_loop=`expr $rootdir_loop + 1`
		if [ $rootdir_loop -ge $LOOP_LIMIT ] ; then
			${ECHO} ""
			${ECHO} "LOOP_LIMIT exceeded."
			eval ${ABORTCMD}
		fi
		${ECHO} ""
		${ECHO} ""
		${ECHO} "Enter the full pathname for the directory into which the PPD database"
		${ECHO} $EB"should be installed [press $RETURNKEYNAME for $DFTPPDDIR]: "$EE
		read A
		if [ "$A" = ""  ] ; then
			PPDDIR="$DFTPPDDIR"
		else
			PPDDIR="$A"
		fi

		validate_ppddir
	
		case $status in
			$STATUS_OK)
				break
				;;
			$STATUS_DIRERR)
				${ECHO} ""
				${ECHO} "Could not create directory $PPDDIR."
				${ECHO} "Please change permissions as needed or specify another directory or"
				${ECHO} "press your interrupt key (often, Ctrl/C) to cancel PPD installation."
				;;
			$STATUS_PERMERR)
				${ECHO} ""
				${ECHO} "You do not have write permissions for the directory"
				${ECHO} "$PPDDIR.  Please change permissions as needed or"
				${ECHO} "choose another directory or press your interrupt key"
				${ECHO} "(often Ctrl/C) to cancel PPD installation."
				;;
			*)
				${ECHO} ""
				${ECHO} "Internal error. Unable to validate PPD directory."
				eval ${ABORTCMD}
				;;
		esac

		# if we got here, we have to do it again
		${ECHO} "Press $RETURNKEYNAME to continue"
		read A

	done
}

compute_kbytes_avail()
{
	# Get last line of df statement.
	# Prepend X because if the mount point is too long,
	# the df command will result in two lines, with the
	# second line having blanks for the first field.
	lastdfline="X"`$DFCMD $PPDDIR | $LASTLINECMD`
	
	# Determine if there is enough disk space for this install.
	case $KBYTES_AVAIL_FIELD in
		1)
			disk_kbytes=`${ECHO} $lastdfline | awk '{ print $1 }'`
			;;
		2)
			disk_kbytes=`${ECHO} $lastdfline | awk '{ print $2 }'`
			;;
		3)
			disk_kbytes=`${ECHO} $lastdfline | awk '{ print $3 }'`
			;;
		4)
			disk_kbytes=`${ECHO} $lastdfline | awk '{ print $4 }'`
			;;
		5)
			disk_kbytes=`${ECHO} $lastdfline | awk '{ print $5 }'`
			;;
		*)
			${ECHO} ""
			${ECHO} "Internal error. Unable to determine disk space."
			eval ${ABORTCMD}
			;;
	esac

}

###########################################################################
# Determine and compare versions of source and destination
# PPD files.  Compare major versions.  If they differ, then
# we know the result.  If they are the same, compare minor
# versions.  If they differ, then we know the result.  If
# they are the same, then the versions of the (2) files
# are assumed to be identical.
###########################################################################

compare_versions()
{
	src_version=`cat $srcfile | awk '/\*FileVersion/ { print $2 }'`
	dst_version=`cat $dstfile | awk '/\*FileVersion/ { print $2 }'`

	sV=`${ECHO} $src_version | sed 's%\"\([0-9][0-9]*\)\.\([0-9][0-9]*\)\"%\1%'`
	sv=`${ECHO} $src_version | sed 's%\"\([0-9][0-9]*\)\.\([0-9][0-9]*\)\"%\2%'`
	dV=`${ECHO} $dst_version | sed 's%\"\([0-9][0-9]*\)\.\([0-9][0-9]*\)\"%\1%'`
	dv=`${ECHO} $dst_version | sed 's%\"\([0-9][0-9]*\)\.\([0-9][0-9]*\)\"%\2%'`

	if [ $sV -lt $dV ]
	then
		status=$LT
		return
	fi

	if [ $sV -gt $dV ]
	then
		status=$GT
		return
	fi

	if [ $sv -lt $dv ]
	then
		status=$LT
		return
	fi

	if [ $sv -gt $dv ]
	then
		status=$GT
		return
	fi

	status=$EQ
}

diff_em()
{
	${DIFF} $srcfile $dstfile > /dev/null

	status=$?
}

copy_hard()
{
	${CP} $1 $2
	:
}

copy_file()
{
	${ECHO} "Copying $fnam to $dstdir ..."

	copy_hard ${srcfile} ${dstdir}/
}

copy_safe()
{
	${ECHO} "Warning:  ${fnam} versions agree, but contents differ."

	safe_fnam=${fnam}.Photoshop_3.0
	safe_dstfile=${dstdir}/${safe_fnam}

	${ECHO} "Copying $fnam to ${safe_dstfile} ..."

	copy_hard ${srcfile} ${safe_dstfile}
}

skip_older_version()
{
	${ECHO} "Skipping older version of $fnam ..."
}

skip_by_request()
{
	${ECHO} "Skipping $fnam by request ..."
}

skip_identical_files()
{
	${ECHO} "Skipping $fnam, since src/dst files are identical ..."
}

search_list()
{
	gotIt=0

	for R in $*
	do
		if [ "$AA" = "$R" ]
		then
			gotIt=1
			break
		fi
	done
}

parse_response()
{
	AA=`${ECHO} $A | ${TR} '[a-z]' '[A-Z]'`

	gotIt=0

	search_list "Y" "YES" "YE"

	if [ $gotIt = 1 ]
	then
		doIt=1
		doEmAll=0
		return
	fi

	search_list "A" "ALL" "AL"

	if [ $gotIt = 1 ]
	then
		doIt=1
		doEmAll=1
		return
	fi

	search_list "N" "NO"

	if [ $gotIt = 1 ]
	then
		doIt=0
		doEmAll=0
		return
	fi
}

ask_user_copy_newer()
{
	gotIt=0

	while [ $gotIt = 0 ]
	do
		${ECHO} ""
		${ECHO} "You currently have an older version of ${fnam}."
		${ECHO} "OK to replace ${fnam} with a newer version?"
		${ECHO} "Enter YES, NO, or ALL.  If you enter ALL, then"
		${ECHO} "all older PPDs will be replaced with newer PPDs"
		${ECHO} ${EB}"without further prompting [Y/N/A]: "${EE}
		read A
		${ECHO} ""
		parse_response
	done
}

process_ppd_file()
{
	##############################################
	# If file does not exist in destination
	# directory, then we definitely copy it.
	##############################################

	if [ ! -f $dstfile ]
	then
		copy_file
		return 0
	fi

	##############################################
	# A file with the same name is already there.
	#
	# We compare versions to determine whether or
	# not we need to copy the file.
	#
	##############################################

	compare_versions

	case $status in
		$LT)
			skip_older_version
			;;
		$GT)
			if [ $doEmAll = 1 ]
			then
				copy_file
			else
				ask_user_copy_newer
				if [ $doIt = 1 ]
				then
					copy_file
				else
					skip_by_request
				fi
			fi
			;;
		*)  ##  i.e. EQ
			diff_em
			if [ $status = 0 ]
			then
				# files are identical
				skip_identical_files
			else
				# Uh-oh!  They have the same version,
				# but they differ in content.
				#
				# Issue warning and copy to "safe" place.
				copy_safe
			fi
			;;
	esac
}

copy_ppd_files()
{
	srcdir=$1
	dstdir=$2

	##############################################
	# Loop through PPD files in source directory.
	# See if we need to copy each PPD file and,
	# if so, copy it.
	##############################################

	for i in $srcdir/*.PPD
	do

		fnam=`${BASENAME} $i`

		srcfile=${srcdir}/${fnam}
		dstfile=${dstdir}/${fnam}

		process_ppd_file
	done
}

copy_ppd_database()
{
	${ECHO} ""
	${ECHO} $EE"Copying PPD database to $PPDDIR..."$EE

	#find $PPD_SOURCE_DIR -name '*.PPD' -print | xargs -i cp \{} $PPDDIR
	copy_ppd_files $PPD_SOURCE_DIR/ppddb $PPDDIR

	cp $PPD_SOURCE_DIR/readme.ppd $PPD_SOURCE_DIR/ppddb/README $PPD_SOURCE_DIR/ppddb/Filename.MAP $PPDDIR
}

###########################################################################
#
#			BEGIN RUNNABLE CODE
#
###########################################################################

PPD_KBYTES="8462"
ECHO=echo

# return codes for directory validation
STATUS_OK=0
STATUS_DIRERR=1
STATUS_PERMERR=2

#return codes for version comparison
LT=-1
EQ=0
GT=1

#some control variables for copying ppd files
doEmAll=0
doIt=0

LOOP_LIMIT=25
LASTLINECMD="tail -1"
ABORTCMD='${ECHO} ""; ${ECHO} "Canceling PPD installation."; ${ECHO} ""; exit 0'
trap 'eval ${ABORTCMD}' 2
EB="-n "
EE=""
if [ -x /bin/uname ]
then
	osname=`/bin/uname -s`
        # 64 bit IRIX6 returns IRIX64 for uname -s
        firstfour=`${ECHO} "$osname" | sed -e 's/^\(....\).*$/\1/g'`
        if [ "$firstfour" = "IRIX" ] ; then
                osname="IRIX"
        fi

	osversion=`/bin/uname -r`
	check=`${ECHO} "$osversion" | egrep '^[0-9]\.[0-9]*'`
	if [ "$check" != "" ]; then
		osmajor=`${ECHO} "$osversion" | sed -e 's/^\([0-9][0-9]*\).*/\1/'`
	fi
fi
if [ $osname = "SunOS" ] ; then
	STDBINDIR="/usr/bin"
	RETURNKEYNAME="Return"
	if [ $osmajor -ge 5 ] ; then
		# For echo without newline under Sys V, set EB and EE variables
		EB=""
		EE="\c"
		PATH=/usr/sbin:/usr/bin:/bin:/usr/bin/X11:/usr/openwin/bin:/sbin:/usr/sbin:/etc:/usr/etc
		DFCMD="$STDBINDIR/df -k"
		DUCMD="$STDBINDIR/du -sk"
		WHOAMI=`$STDBINDIR/id | $STDBINDIR/sed 's/^[^(]*.\([^)]*\).*/\1/'`
	else
		PATH=/usr/ucb:/usr/bin:/bin:/usr/bin/X11:/usr/openwin/bin:/sbin:/etc:/usr/etc
		DFCMD="$STDBINDIR/df"
		DUCMD="$STDBINDIR/du -s"
		WHOAMI=`/usr/ucb/whoami`
	fi
	KBYTES_AVAIL_FIELD=4
elif [ $osname = "IRIX" ] ; then
	STDBINDIR="/usr/bin"
	RETURNKEYNAME="Enter"
	# For echo without newline under Sys V, set EB and EE variables
	EB=""
	EE="\c"
	PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/bsd:/usr/bin/X11:/usr/openwin/bin:/etc:/usr/etc
	DFCMD="$STDBINDIR/df -k"
	DUCMD="$STDBINDIR/du -sk"
	KBYTES_AVAIL_FIELD=5
	WHOAMI=`$STDBINDIR/id | $STDBINDIR/sed 's/^[^(]*.\([^)]*\).*/\1/'`
fi

BASENAME="$STDBINDIR/basename"
CP="$STDBINDIR/cp"
DIFF="$STDBINDIR/diff"
ECHO="$STDBINDIR/echo"
MKDIR="$STDBINDIR/mkdir -p"
SED="$STDBINDIR/sed"

# If there is a third command line parameter, then assume
# that this script was invoked as part of an Adobe product installation.
# Otherwise, assume this script is run standalone.
STANDALONE=0
DFTPPDDIR=`dirname $1`/PPD
if [ "$3" != "" ] ; then
	PPD_SOURCE_DIR="$3/../ppd"
elif [  "$2" != "" ] ; then
	PPD_SOURCE_DIR="$2/../ppd"
elif [ $# -lt 2 ] ; then
	STANDALONE=1
	PPD_SOURCE_DIR="$1"
	DFTPPDDIR=/usr/adobe/PPD
fi

if [ -z "$PPD_SOURCE_DIR" -o ! -d "$PPD_SOURCE_DIR" ] ; then
	${ECHO} ""
	${ECHO} "PPD post installation script failure."
	exit 0
fi

if [ "$WHOAMI" != root ] ; then
	${ECHO} ""
	${ECHO} "Warning: you are not logged in as root. You usually need to be"
	${ECHO} "superuser/root have sufficient permissions to install PPDs."
	${ECHO} "Press $RETURNKEYNAME to continue or your interrupt key (often, Ctrl/C)"
	${ECHO} "to cancel PPD installation."
	read A
fi

# Always install PPDs, but only prompt for directory when
# doing "difficultinst" (as opposed to easyinst :-).
doing_easyinst=0

if [ "$doing_easyinst" = 1 ] ; then
	compute_ppddir
	compute_kbytes_avail
	total_kbytes="$PPD_KBYTES"

	if [ $total_kbytes -ge $disk_kbytes ] ; then
		tell_user_not_enough_space
		# just bail
		exit 1
	fi
else
	# Loop around prompts to handle case where user had insufficient
	# space or permissions the first time through.
	while : ; do
		if [ "$PPDDIR" != "" ] ; then
			DFTPPDDIR="$PPDDIR"
		fi

		if [ $STANDALONE = 0 ] ; then
			PPDDIR="$DFTPPDDIR"
			validate_ppddir

			case $status in
				$STATUS_OK)
					;;
				$STATUS_DIRERR)
					${ECHO} ""
					${ECHO} "Could not create directory $PPDDIR."
					${ECHO} ""
					exit 1
					;;
				$STATUS_PERMERR)
					${ECHO} ""
					${ECHO} "You do not have write permissions for the directory"
					${ECHO} "$PPDDIR.  Please correct and re-install."
					${ECHO} ""
					exit 1
					;;
				*)
					${ECHO} ""
					${ECHO} "Internal error. Unable to validate PPD directory."
					eval ${ABORTCMD}
					;;
			esac
		else
			# Ask user where to install PPD database
			prompt_for_ppddir
		fi

		# Compute kilobytes available
		compute_kbytes_avail

		total_kbytes="$PPD_KBYTES"

		if [ $total_kbytes -ge $disk_kbytes ] ; then

			tell_user_not_enough_space

			${ECHO} "Please free up space or choose a different directory or press"
			${ECHO} "your interrupt key (often, Ctrl/C) to cancel PPD installation."
			${ECHO} "Press $RETURNKEYNAME to continue"
			read A
		else
			break
		fi
	done
fi

copy_ppd_database

${ECHO} done

exit 0
