#! /bin/ksh

# This script reads a layered product distribution specification file, and
# creates an installation script appropriate for Dynix/psx.
# Then the script and the appropriate product files are written onto
# distribuition media.
#
# $Header: ptxmd.sh 1.49 90/10/26 $

tapeerror='if [ "$?" != "0" ]; then echo "Error: Problem accessing tape."; echo "Err
or: No other tape access attempted."; exit 1; fi'

DYNIX="-f /dynix"				# On a Dynix 3.0 System?
DYNIXPSX="-f /unix"				# On a Dynix/psx System?
makedist=yes
cflag=no
rflag=no
specfile=
use="Usage: $0 [-cn] distribution_specification_file"
ptxdist_tools='deltacpio delta preview preload remove'

if [ $DYNIX ]; then
	if [ "`universe`" != "att" ]; then
		echo "Error: You should be executing $0 in att universe."
		exit 1
	fi
fi

while [ '' != "$1" ] ; do
	case $1 in
		-n)
			makedist=no ;;		# Do not make distribution.
		-c)
			cflag=yes ;;		# copy! do not recreate BOM etc
		-r)
			rflag=yes ;;		# do it remotely
		-*)
			echo Unknown option $1...
			echo $use
			exit 1 ;;
		*)
			specfile=$1 ;;
	esac
	shift
done

if [ "$specfile" = "" ]; then
  echo Must give distribution_specification_file.
  echo $use
  exit 1
fi

# MUST_NAME and MAY_NAME provide a list of variables that we require or allow
# to be set in the specification file.  YES_NO_NAMES are variables that have
# yes or no values; we'll do a little check for other forms of these words.

MUST_NAME="dev_host dev_name product_name product_vers product_part package_name instl_dev"
MAY_NAME="dist_sums dist_erase dist_script uninstall_script bom product_part_two"
YES_NO_NAMES="dist_erase"
export_list="product_name product_vers product_part package_name instl_dev"

SCAN=maketape_scan			# token scanner program location

# Prep work

current_step="0"
current_standalone="0"
prefix="/tmp/psxmd$$."
prefixdir="/tmp/psxmddir$$"
rm -f ${prefix}*		# to be sure
rm -rf ${prefixdir}		# to be sure
trap "rm -f ${prefix}*; rm -rf ${prefixdir}; exit" 0 1 2 3 13 15
echo "Scanning input file..."

$SCAN <$specfile | while read keyword; do
	case $keyword in

	set)
		read var
		read $var
		eval eval $var=\\\"\$$var\\\"
		#eval echo set $var to \$$var		# debug
		;;

	export)
		read var
		export_list="$export_list $var"
		#echo "export list is $export_list"	# debug
		;;

	info_message|info_msg)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "info_message $current_step file: $text"	# debug
			cat $text >>${prefix}STEP${current_step}INFO
			;;
		here)
			#echo "info_message $current_step text: $text"	# debug
			echo "$text" >>${prefix}STEP${current_step}INFO
			;;
		*)
			echo "Unexpected word $type; wanted info_message type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	step)
		current_step=`expr $current_step + 1`
		read type
		case $type in
		always)
			#echo "step $current_step always"	# debug
			touch ${prefix}STEP${current_step}ALWAYS
			echo "\$STEP${current_step} = \"yes\"" \
				>${prefix}STEP${current_step}CONDITION
			;;
		ask)
			read question
			#echo "step $current_step ask: $question"	# debug
			echo "$question" >${prefix}STEP${current_step}QUESTION
			echo "\$STEP${current_step} = \"yes\"" \
				>${prefix}STEP${current_step}CONDITION
			;;
		condition)
			read cond
			#echo "step $current_step condition: $cond"	# debug
			echo "$cond" >${prefix}STEP${current_step}CONDITION
			;;
		*)
			echo "Unexpected word $type; wanted step type:"
			echo "	always or ask"
			exit 1
			;;
		esac
		;;

	image)
		read image
		eval image=\"$image\"
		#echo "image $current_step is $image"	# debug
		echo "$image" >${prefix}STEP${current_step}IMAGE
		;;

	image_except)
		read image_except
		eval image_except=\"$image_except\"
		#echo "image_except $current_step is $image_except"	# debug
		/bin/echo "$image_except \c" >>${prefix}STEP${current_step}IMAGE_EXCEPT
		;;

	imagefiles)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "imagefiles $current_step file: $text"	# debug
			cat $text >>${prefix}STEP${current_step}IMAGEFILES
			;;
		here)
			#echo "imagefiles $current_step text: $text"	# debug
			echo "$text" | sed 's/[ 	][ 	]*/\
/g' >>${prefix}STEP${current_step}IMAGEFILES
			;;
		*)
			echo "Unexpected word $type; wanted imagefiles type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	title)
		read title
		eval title=\"$title\"
		#echo "title $current_step is $title"	# debug
		echo "$title" >${prefix}STEP${current_step}TITLE
		;;

	link)
		read from
		eval from=\"$from\"
		#echo "link $current_step from $from"	# debug
		echo "$from" >${prefix}STEP${current_step}LINK
		;;

	usual_place)
		read place
		eval place=\"$place\"
		#echo "usual_place $current_step is $place"	# debug
		echo "$place" >${prefix}STEP${current_step}USUAL
		;;

	usual_mode)
		read mode
		eval mode=\"$mode\"
		#echo "usual_mode $current_step is $mode"	# debug
		echo "chmod $mode" >${prefix}STEP${current_step}USUAL_MODE
		;;

	usual_owner)
		read owner
		eval owner=\"$owner\"
		#echo "usual_owner $current_step is $owner"	# debug
		echo "chown $owner" >${prefix}STEP${current_step}USUAL_OWNER
		;;

	usual_group)
		read group
		eval group=\"$group\"
		#echo "usual_group $current_step is $group"	# debug
		echo "chgrp $group" >${prefix}STEP${current_step}USUAL_GROUP
		;;

	fixed_place)
		read place
		eval place=\"$place\"
		#echo "fixed_place $current_step is $place"	# debug
		echo "$place" >${prefix}STEP${current_step}FIXED
		;;

	pre_cmd|pre_command)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "pre_cmd $current_step file: $text"	# debug
			cat $text >>${prefix}STEP${current_step}PRE_CMD
			;;
		here)
			#echo "pre_cmd $current_step text: $text"	# debug
			echo "$text" >>${prefix}STEP${current_step}PRE_CMD
			;;
		*)
			echo "Unexpected word $type; wanted pre_cmd type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	preview_cmd|preview_command)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "pre_cmd $current_step file: $text"	# debug
			cat $text >>${prefix}STEP${current_step}PREVIEW_CMD
			;;
		here)
			#echo "pre_cmd $current_step text: $text"	# debug
			echo "$text" >>${prefix}STEP${current_step}PREVIEW_CMD
			;;
		*)
			echo "Unexpected word $type; wanted preview_cmd type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	post_cmd|post_command)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "post_cmd $current_step file: $text"	# debug
                        cat $text >>${prefix}STEP${current_step}POST_CMD
			;;
		here)
			#echo "post_cmd $current_step text: $text"	# debug
                        echo "$text" >>${prefix}STEP${current_step}POST_CMD
			;;
		*)
			echo "Unexpected word $type; wanted post_cmd type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	de_install)
		read type
		read text
		case $type in
		file)
			eval text=\"$text\"
			#echo "de_install $current_step file: $text"	# debug
                        cat $text >>${prefix}STEP${current_step}DE_INSTALL
			;;
		here)
			#echo "de_install $current_step text: $text"	# debug
                        echo "$text" >>${prefix}STEP${current_step}DE_INSTALL
			;;
		*)
			echo "Unexpected word $type; wanted de_install type:"
			echo "	file or here"
			exit 1
			;;
		esac
		;;

	bootblock)
		# This keyword is used in conjunction with bootprog keyword
		# and together they signify that this distribution is special
		# in the sense that it is a bootable distribution like the
		# os or diagnostics. These files are then the first and second
		# on the distribution rather than the INSTALL script.
		read bootblock
		eval bootblock=\"$bootblock\"
		#echo "bootblock is $bootblock"		# debug
		echo "$bootblock" > ${prefix}BOOTBLOCK
		;;

	bootprog)
		read bootprog
		eval bootprog=\"$bootprog\"
		#echo "bootprog is $bootprog"		# debug
		echo "$bootprog" > ${prefix}BOOTPROG
		;;

	standalone)
		read standalone
		eval standalone=\"$standalone\"
		#echo "standalone is $standalone"	# debug
		echo "$standalone" > ${prefix}STANDALONE${current_standalone}
		current_standalone=`expr $current_standalone + 1`
		;;

	end)	# This keyword will cause everything to get written on the
		# installation script, and cause the distribution to be made.
		# We must write the installation script here, because this
		# shell is where all the variables are defined.  If we fall
		# out, we lose all that.

		#echo "end mark read!"				# debug

		# I'm going to take the liberty of breaking the indentation
		# here and moving out to the left column.  We'll need the
		# whole line for the stuff that's coming up.
#<--------------<

# Check that all required variables are set.  Then check for other forms of
# "yes" and "no" in the YES_NO_NAMES.

for i in $MUST_NAME; do
	eval "if [ \"\$$i\" ]; then :; else echo 'Variable \"$i\" must be defined to make a distribution'; exit 1; fi"
done
# Make sure the package_name is 18 characters or less since the menuing
# systems must have made room to accomodate the string.
length=`expr $package_name : '.*'`
if [ $length -gt 18 ]; then
	echo "Error: package_name should be less than 19 characters in length"
	exit 1
fi
for i in $YES_NO_NAMES; do
	eval "if [ \"\$$i\" = \"yes\" -o \"\$$i\" = \"Y\" -o  \"\$$i\" = \"y\" -o \"\$$i\" = \"Yes\" -o \"\$$i\" = \"YES\" ]; then $i="yes"; else $i="no"; fi"
done

# Save the last step number.
last_step=$current_step

# Do some additional consistency checks.
i=1	
while [ $i -le $last_step ]; do
	if [ -f ${prefix}STEP${i}IMAGE -a ! -f ${prefix}STEP${i}TITLE ]; then
		echo "In step $i; image has no title"
		exit 1
	fi
	if [ -f ${prefix}STEP${i}USUAL ]; then
		if [ ! -f ${prefix}STEP${i}USUAL_MODE -o ! -f ${prefix}STEP${i}USUAL_GROUP -o ! -f ${prefix}STEP${i}USUAL_OWNER ];then
echo ""
echo "Warning!"
echo "In step $i; usual_place is used without the keywords usual_mode,"
echo "usual_group, usual_owner.  At install time, if an alternate install"
echo "location is chosen, it will have its mode, owner, and group set according"
echo "to the user's current umask.  This can lead to inconsistancies in the product."
echo "It's recommended you specify those keywords when using usual_place."
		echo ""
		fi
	fi
	i=`expr $i + 1`
done

if [ $cflag = yes ]; then 		# do not recreate install script
	if [ -n "$dist_script" ]; then
		cp $dist_script ${prefix}INSTALL
	fi
else
echo "Writing install script..."

(
 echo "#! /bin/ksh"
 echo "#"
 echo "# Installation script for $product_name $product_vers PN: $product_part"
 echo "# Tape generated on: `date`"
 echo ""
 echo "# This script will install Dynix/ptx layered product software."
 echo "# It is usually invoked by either the Dynix/ptx sysadm(1) or ptx/ADMIN menu"
 echo "# interfaces. The current working directory when this script is invoked"
 echo "# is /usr/options/pkg, where pkg is the name of software being installed."
 echo "# This script is usually invoked as \"INSTALL preview\", \"INSTALL preload\" or"
 echo "# \"INSTALL delta\" that control the actions of this script."

 echo "#"
 echo "# Set HOME so that in single user (where HOME is not defined)"
 echo "# the cd command works for steps where STEP[i]LOC is null (without image)."
 echo ""
 echo "HOME=\`pwd\`"

 for i in $export_list ; do
	eval echo "$i=\\\"\$$i\\\""
 done
 # compute the location of the first image, compensating for fact that
 # this distribution may be a bootable format and contain arbitrary
 # standalone programs ahead of the first image.
 firstimage=2		#INSTALL file and BOM file always exist regardless
 if [ -f ${prefix}BOOTBLOCK ] ;then
	firstimage=`expr $firstimage + 1`
 fi
 if [ -f ${prefix}BOOTPROG ] ;then
	firstimage=`expr $firstimage + 1`
 fi
 firstimage=`expr $firstimage + $current_standalone`
 echo "firstimage=$firstimage"
 echo "case \$1 in"
 echo "preview)"
 echo "	echo \"Installation script, preview step, for \$product_name \$product_vers\""
 echo "	#"
 echo "	# Read the Bill of Materials file and tools from distribution"
 echo "	#"
 echo "	/etc/mt -f \$instl_dev rew"
 echo '	/etc/mt -f $instl_dev fsf `expr $firstimage - 1`'
 echo "	if [ -f BOM.\$product_vers ]; then"
 echo "		rm BOM.\$product_vers"
 echo "	fi"
 echo "	if [ -f OPTS.\$product_vers ]; then"
 echo "		rm OPTS.\$product_vers"
 echo "	fi"
 echo "	(cd /usr/options; /bin/cpio -imdu -C 65536 < \$instl_dev > /dev/null 2>&1)"
 echo "	/bin/chown bin . ./*"
 echo "	/bin/chgrp sys . ./*"
 echo "	/bin/chmod 755 ."
 echo "	/bin/chmod 555 $ptxdist_tools"

 if [ -f ${prefix}STEP0INFO ]; then
	echo "	# ------ begin provided text ------"
	echo "	cat << 'xyzzy'"
	cat ${prefix}STEP0INFO
	echo ""
	echo "xyzzy"
	echo "	# ------- end provided text -------"
 fi

 # PRE_CMDs given in the header come before taking any other actions.

 if [ -f ${prefix}STEP0PRE_CMD ]; then
	echo "	# ------ begin provided text ------"
	cat ${prefix}STEP0PRE_CMD
	echo "	# ------- end provided text -------"
 fi
 echo "	cd \$HOME"

 echo "	cat /dev/null > OPTS.\$product_vers"
 echo "	cat /dev/null > STEPS"

 # Cycle through each of the steps, and decide what questions to ask.
 i=1	
 while [ $i -le $last_step ]; do
	if [ -f ${prefix}STEP${i}QUESTION ]; then
		echo "	echo \"`cat ${prefix}STEP${i}QUESTION`? \\\\c\""
		echo "	STEP${i}=wrong"
		echo "	while [ \"\$STEP${i}\" = \"wrong\" ] "
		echo "	do "
		echo "	  read STEP${i}"
		echo "	  case \"\$STEP${i}\" in "
		echo "	    [yY]*) STEP${i}=\"yes\" ; echo \"STEP${i}=yes\" >> STEPS ;; "
		echo "	    [nN]*) STEP${i}=\"no\" ; echo \"STEP${i}=no\" >> STEPS ;; "
		echo "	    *) STEP${i}=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
		echo "	  esac"
		echo "	done"
	elif [ -f ${prefix}STEP${i}ALWAYS ]; then
		echo "	STEP${i}=yes; echo \"STEP${i}=yes\" >> STEPS"
	fi
	
	echo "	STEP${i}CONDITION='`cat ${prefix}STEP${i}CONDITION`'"
	echo "	echo STEP${i}CONDITION='`cat ${prefix}STEP${i}CONDITION`' >> STEPS"
	echo "	cat STEPS | sed \"s+^STEP${i}CONDITION=+STEP${i}CONDITION='+\" | sed \"/^STEP${i}CONDITION/ s+\$+\'+\" > STEPS.\$\$; mv STEPS.\$\$ STEPS"

	if [ -f ${prefix}STEP${i}IMAGE -a -f ${prefix}STEP${i}FIXED ]; then
		echo "	STEP${i}LOC=\"`cat ${prefix}STEP${i}FIXED`\""
		echo "	echo STEP${i}LOC='`cat ${prefix}STEP${i}FIXED`' >> STEPS"
		echo "	cat STEPS | sed \"s+^STEP${i}LOC=+STEP${i}LOC='+\" | sed \"/^STEP${i}LOC/ s+\$+\'+\" > STEPS.\$\$; mv STEPS.\$\$ STEPS"
	fi

	if [ -f ${prefix}STEP${i}PREVIEW_CMD ]; then
		/bin/sed 's/\(.*\)/	if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
		echo "				# ------ begin provided text ------"
		cat ${prefix}STEP${i}PREVIEW_CMD
		echo "				# ------- end provided text -------"
		echo "	fi"		
	fi


	if [ -f ${prefix}STEP${i}IMAGE -a -f ${prefix}STEP${i}USUAL ]; then
		/bin/sed 's/\(.*\)/	if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
		echo "		echo \"Should the `cat ${prefix}STEP${i}TITLE` be installed in `cat ${prefix}STEP${i}USUAL`?  \\\\c\""
		echo "		ok=wrong"
		echo "		while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
		echo "		do "
		echo "		  read ok"
		echo "		  case \"\$ok\" in "
		echo "		    [yY]*) ok=\"yes\" ;; "
		echo "		    [nN]*) ok=\"no\" ;; "
		echo "		    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
		echo "		  esac"
		echo "		done"

		echo "		if [ \"\$ok\" = \"yes\" ]; then"
		echo "			STEP${i}LOC=\"`cat ${prefix}STEP${i}USUAL`\""
		echo "			echo STEP${i}LOC='`cat ${prefix}STEP${i}USUAL`' >> STEPS"
		echo "			cat STEPS | sed \"s+^STEP${i}USUAL=+STEP${i}USUAL='+\" | sed \"/^STEP${i}USUAL/ s+\$+\'+\" > STEPS.\$\$; mv STEPS.\$\$ STEPS"
		echo "		fi"
		echo "	fi"		
	fi

	if [ -f ${prefix}STEP${i}IMAGE ]; then	
		/bin/sed 's/\(.*\)/	if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
		echo "		while [ \"\$STEP${i}LOC\" = '' ]; do"
		echo "			echo \"Enter install directory for the `cat ${prefix}STEP${i}TITLE`:  \\\\c\""
		echo "			read STEP${i}LOC"
		echo "			echo \"\$STEP${i}LOC is the install location? \\\\c\""
		echo "			ok=wrong"
		echo "			while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
		echo "			do "
		echo "			  read ok"
		echo "			  case \"\$ok\" in "
		echo "			    [yY]*) ok=\"yes\" ;; "
		echo "			    [nN]*) ok=\"no\" ;; "
		echo "			    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
		echo "			  esac"
		echo "			done"
		echo "			if [ \"\$ok\" != \"yes\" ]; then"
		echo "				STEP${i}LOC=''"
		echo "			fi"
		echo "		done"
		echo "	fi"

# Save the install location for this product

		echo "	echo \"STEP${i}LOC=\$STEP${i}LOC\" >> STEPS"
	fi


	i=`expr $i + 1`
 done

 # Create OPTS file that indicates which optional images to install.
 # At the same time eliminate unwanted entries from the BOM file for
 # optional images that are not installed.

 i=1	
 while [ $i -le $last_step ]; do
	if [ -f ${prefix}STEP${i}IMAGE ]; then
		/bin/sed 's/\(.*\)/	if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
		echo "		echo \"LOC${i} \$STEP${i}LOC\" >> OPTS.\$product_vers"
		echo "	else"
		echo "		egrep -v \"^LOC${i}\" BOM.\$product_vers > /tmp/BOM.\$\$"
		echo "		mv /tmp/BOM.\$\$ BOM.\$product_vers"
		echo "	fi"
	fi
	i=`expr $i + 1`
 done

 # Invoke preview tool to check for files that may be overwritten by doing
 # the installation.

 echo "	if [ -f OPTS.current -a -f BOM.current ] ;then "
 echo "		./preview BOM.\$product_vers OPTS.\$product_vers BOM.current OPTS.current > preview.log"
 echo "	else"
 echo "		./preview BOM.\$product_vers OPTS.\$product_vers > preview.log"
 echo "	fi"
 # Hack to support the menu system.  It does things to preview.log,
 # and in certain cases it doesn't show up.
 echo "	touch preview.log"
 echo "		echo \$product_name \$product_vers \$product_part - Ready to Install > \$package_name.name.preview"
 echo "	break ;;"

 # Allow the user to perform a preload of the distribution for inspection, etc.
 # Ask user if they want files preloaded and if so, where to preload.
 echo "preload)"
 echo "	echo \"Installation script, preload step, for \$product_name \$product_vers\""
 echo "	. ./STEPS"
 echo "	/etc/mt -f \$instl_dev rew"
 echo "	/etc/mt -f \$instl_dev fsf \$firstimage"
 echo "	newfilesloc=''"
 echo "	deltanewarg=''"
 echo "	while [ true ] ; do"
 echo "		echo \"Your options for continuing are:\""
 echo "		echo \"	1) Preload only those files from the distribution media that are\""
 echo "		echo \"	   in conflict.\""
 echo "		echo \"	2) Preload all new files from the distribution media.\""
 echo "		echo \"How would you like to proceed? \\\\c\""
 echo "		read reply"
 echo "		case \$reply in"
 echo "			1|2) 	while [ \"\$newfilesloc\" = '' ]; do"
 echo "					echo \"Default directory into which files should be preloaded is:\""
 echo "					echo \"\$HOME/newfiles\""
 echo "					echo \"Enter directory, which should be empty, into which files should\""
 echo "					echo \"be preloaded or press <RETURN> for default directory: \\\\c\""
 echo "					read newfilesloc"
 echo "					if [ ! \"\$newfilesloc\" ]; then"
 echo "						rm -rf \$HOME/newfiles"
 echo "						mkdir -p \$HOME/newfiles"
 echo "						newfilesloc=\$HOME/newfiles"
 echo "						continue"
 echo "					fi"
 echo "					if [ ! -d \"\$newfilesloc\" ]; then"
 echo "                  			echo \"\$newfilesloc does not exist, shall I create it? \\\\c\""
 echo "         				ok=wrong"
 echo "          				while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
 echo "          				do "
 echo "            				read ok"
 echo "            				case \"\$ok\" in "
 echo "          				    [yY]*) ok=\"yes\" ;; "
 echo "          				    [nN]*) ok=\"no\" ;; "
 echo "          				    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
 echo "          				  esac"
 echo "          				done"
 echo "          				if [ \"\$ok\" = \"yes\" ]; then"
 echo "         					mkdir -p \$newfilesloc"
 echo "          				else"
 echo "							newfilesloc=''"
 echo "          				fi"
 echo "					fi"
 echo "				done"
 echo "				if [ -d \"\$newfilesloc\" -a \"\$newfilesloc\" != \"\$HOME/newfiles\" ]; then"
 echo "					rm -rf \$HOME/newfiles"
 echo "					ln -s \$newfilesloc \$HOME/newfiles"
 echo "				fi"
 echo "				deltanewarg=\"-n \$newfilesloc\""
 echo "				echo deltanewarg=\\\"-n \$newfilesloc\\\" >> STEPS"
 echo "				if [ \"\$reply\" = "1" ]; then"
 echo "					preloadflag=-c"
 echo "				else"
 echo "					preloadflag=-n"
 echo "				fi"
 echo "				images=0"
 echo "		echo \"\""
 echo "		echo \"Preloading Files...\""
 echo "		echo \"\""
 i=1	
 while [ $i -le $last_step ]; do
	/bin/sed 's/\(.*\)/				if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
	if [ -f ${prefix}STEP${i}IMAGE ]; then
 		echo "					base=\`grep \"LOC${i} \" OPTS.\$product_vers | awk '{ print \$2 }'\`"
		echo "					awk \"/^\\\\*\\\\*\\\\*\\\\*/ { image++ } !/^\\\\*\\\\*\\\\*\\\\*/ { if (image == \$images) print }\" preview.log | ./preload \$instl_dev \$base \$preloadflag \$newfilesloc"
		echo "				return=\$?"
		echo " 				if [ "\$return" != "0" ] ; then"
		echo "					echo Return Value is: \$return"
		echo "					reply=bogus"
		echo " 				fi"
	else
		echo "					:"
	fi
	if [ -f ${prefix}STEP${i}IMAGE ]; then
		echo "				else"
		echo "					/etc/mt -f \$instl_dev fsf 1"
	fi
	echo "				fi"
	echo '				images=`expr $images + 1 `'
	i=`expr $i + 1`
 done
 echo "				if [ \"\$reply\" = "2" ]; then"
 echo "					touch .preloaded"
 echo "				fi"
 echo "				break ;;"
 echo "			*)"
 echo "				echo Enter 1 or 2. ;;"
 echo "		esac"
 echo "	done"
 echo "	if [ -f .preloaded ]; then"
 echo "		echo \"Rewinding tape ...\""
 echo "		mt -f \$instl_dev rew"
 echo "		echo \"You can remove the tape now.\""
 echo "	fi"
 echo "	break ;;"
 echo "delta)"

 echo "	echo \"Installation script, install step, for \$product_name \$product_vers\""
 echo "	. ./STEPS"
 echo "	# Move back to first image on distribution in preparation for installing files."
 echo "	#"
 echo "	if [ ! -f .preloaded ]; then "
 echo "		/etc/mt -f \$instl_dev rew"
 echo "		/etc/mt -f \$instl_dev fsf \$firstimage"
 echo "		device=\$instl_dev"
 echo "	else"
 echo "		device=/dev/null"
 echo "	fi"
 echo "	oldfilesloc=''"
 echo "	deltaoldarg=''"
 echo "	while [ true ] ; do"
 echo "		echo \"Your options for continuing are:\""
 echo "		echo \"	1) Install files without preserving locally modified files that are\""
 echo "		echo \"	   overwritten or deleted.\""
 echo "		echo \"	2) Install files preserving locally modified files that are\""
 echo "		echo \"	   overwritten or deleted.\""
 echo "		echo \"How would you like to proceed? \\\\c\""
 echo "		read reply"
 echo "		case \$reply in"
 echo "			1|2)"
 echo "				if [ \"\$reply\" = "2" ]; then"
 echo "					while [ \"\$oldfilesloc\" = '' ]; do"
 echo "						echo \"Default directory in which to preserve files being overwritten or deleted is:\""
 echo "						echo \"\$HOME/oldfiles\""
 echo "						echo \"Enter directory into which files should be preserved or\""
 echo "						echo \"press <RETURN> for default directory: \\\\c\""
 echo "						read oldfilesloc"
 echo "						if [ ! \"\$oldfilesloc\" ]; then"
 echo "							if [ ! -d oldfiles ]; then"
 echo "								mkdir oldfiles"
 echo "							fi"
 echo "							oldfilesloc=\$HOME/oldfiles"
 echo "							continue"
 echo "						fi"
 echo "						if [ ! -d \"\$oldfilesloc\" ]; then"
 echo "                                         	echo \"\$oldfilesloc does not exist, shall I create it? \\\\c\""
 echo "                                         	ok=wrong"
 echo "                                         	while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
 echo "                                         	do "
 echo "                                         	read ok"
 echo "                                         	case \"\$ok\" in "
 echo "                                         	    [yY]*) ok=\"yes\" ;; "
 echo "                                         	    [nN]*) ok=\"no\" ;; "
 echo "                                         	    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
 echo "                                         	  esac"
 echo "                                         	done"
 echo "                                         	if [ \"\$ok\" = \"yes\" ]; then"
 echo "                                         		mkdir -p \$oldfilesloc"
 echo "                                       		else"
 echo "								oldfilesloc=''"
 echo "                                       		fi"
 echo "						fi"
 echo "					done"
 echo "					deltaoldarg=\"-o \$oldfilesloc\""
 echo "				fi"
 echo "				images=0"
 i=1	
 while [ $i -le $last_step ]; do
	echo " "
	echo "				# STEP $i"

	/bin/sed 's/\(.*\)/				if [ \1 ]; then/' <${prefix}STEP${i}CONDITION
	if [ -f ${prefix}STEP${i}INFO ]; then
		echo "				# ------ begin provided text ------"
		echo "				cat << 'xyzzy'"
		cat ${prefix}STEP${i}INFO
		echo "xyzzy"
		echo "				# ------- end provided text -------"
	fi

	if [ -f ${prefix}STEP${i}IMAGE ]; then
		echo "		while [ ! -d \"\$STEP${i}LOC\" ]; do"
		echo "			if [ -a \"\$STEP${i}LOC\" ]; then"
		echo "				mv \$STEP${i}LOC \$STEP${i}LOC.prev"
		echo "      			if [ -a \"\$STEP${i}LOC\" ]; then"
		echo "					echo \"Could not rename file \$STEP${i}LOC\""
		echo "					continue"
		echo "				else"
		echo "					echo \"\$STEP${i}LOC renamed to \$STEP${i}LOC.prev\""
		echo "					echo \"	Remove it after successful installation\""
		echo "				fi"
		echo "			fi"

		echo "			echo \"\$STEP${i}LOC does not exist, shall I create it? \\\\c\""
		echo "			ok=wrong"
		echo "			while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
		echo "			do "
		echo "			  read ok"
		echo "			  case \"\$ok\" in "
		echo "			    [yY]*) ok=\"yes\" ;; "
		echo "			    [nN]*) ok=\"no\" ;; "
		echo "			    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
		echo "			  esac"
		echo "			done"
		echo "			if [ \"\$ok\" = \"yes\" ]; then"
		echo "				mkdir -p \$STEP${i}LOC"
		echo "			else"
		echo "				STEP${i}LOC=''"
		echo "			fi"
		echo "		while [ \"\$STEP${i}LOC\" = '' ]; do"
		echo "			echo \"Enter install directory for the `cat ${prefix}STEP${i}TITLE`:  \\\\c\""
		echo "			read STEP${i}LOC"
		echo "		done"
		echo "		done"
	fi
	if [ -f ${prefix}STEP${i}USUAL_MODE ]; then
		echo "		`cat ${prefix}STEP${i}USUAL_MODE` \$STEP${i}LOC"
	fi
	if [ -f ${prefix}STEP${i}USUAL_OWNER ]; then
		echo "		`cat ${prefix}STEP${i}USUAL_OWNER` \$STEP${i}LOC"
	fi
	if [ -f ${prefix}STEP${i}USUAL_GROUP ]; then
		echo "		`cat ${prefix}STEP${i}USUAL_GROUP` \$STEP${i}LOC"
	fi

	if [ -f ${prefix}STEP${i}PRE_CMD ]; then
		echo "				if [ -d \"\$STEP${i}LOC\" ];then"
		echo "				cd \$STEP${i}LOC"
		echo "				# ------ begin provided text ------"
		cat ${prefix}STEP${i}PRE_CMD
		echo "				# ------- end provided text -------"
		echo "				cd \$HOME"
		echo "				fi"
	fi

	if [ -f ${prefix}STEP${i}IMAGE ]; then
 		echo "					base=\`grep \"LOC${i} \" OPTS.\$product_vers | awk '{ print \$2 }'\`"
		echo "					awk \"/^\\\\*\\\\*\\\\*\\\\*/ { image++ } !/^\\\\*\\\\*\\\\*\\\\*/ { if (image == \$images) print }\" preview.log | ./delta \$device \$base \$deltaoldarg \$deltanewarg"
	else
		echo "					:"
	fi

	if [ -f ${prefix}STEP${i}IMAGE -a -f ${prefix}STEP${i}LINK ]; then
		echo "	if [  \"\$STEP${i}LOC\" != \"`cat ${prefix}STEP${i}LINK`\"  ]; then"
		echo "      if [ -a \"`cat ${prefix}STEP${i}LINK`\" ]; then"

		echo "		echo \"This installation normally creates a symbolic link at \"`cat ${prefix}STEP${i}LINK`\", but it already exists.\""
		echo "		echo \"Proceed anyway?  \\\\c\""
		echo "		ok=wrong"
		echo "		while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
		echo "		do"
		echo "		  read ok"
		echo "		  case \"\$ok\" in "
		echo "		    [yY]*) ok=\"yes\" ;; "
		echo "		    [nN]*) ok=\"no\" ;; "
		echo "		    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
		echo "		  esac"
		echo "		done"

		echo "		if [ \"\$ok\" = \"no\" ]; then"
		echo "			exit 1"
		echo "		else"
		echo "			mv `cat ${prefix}STEP${i}LINK` `cat ${prefix}STEP${i}LINK`.prev"
		echo "      		if [ -a \"`cat ${prefix}STEP${i}LINK`\" ]; then"
		echo "				echo \"Could not rename file or directory.\""
		echo "				echo \"Remove or rename `cat ${prefix}STEP${i}LINK`\""
		echo "				echo \"and restart the installation.\""	
		echo "				exit 1"
		echo "			else"
		echo "				echo \"`cat ${prefix}STEP${i}LINK` renamed to `cat ${prefix}STEP${i}LINK`.prev\""
		echo "				echo \"Remove it after successful installation\""
		echo "			fi"
		echo "		fi"
		echo "	    fi"
		echo "	ln -s \$STEP${i}LOC `cat ${prefix}STEP${i}LINK`"
		echo "	fi"
	fi

	if [ -f ${prefix}STEP${i}POST_CMD ]; then
		echo "				# ------ begin provided text ------"
		cat ${prefix}STEP${i}POST_CMD
		echo "				# ------- end provided text -------"
	fi
	if [ -f ${prefix}STEP${i}IMAGE ]; then
		echo "				else"
		echo "					if [ ! -f .preloaded ]; then"
		echo "						/etc/mt -f \$instl_dev fsf 1"
		echo "					fi"
	fi
	echo "				fi"
	echo '				images=`expr $images + 1 `'
	i=`expr $i + 1`
 done
 echo "				break ;;"
 echo "			*)"
 echo "				echo Enter 1 or 2. ;;"
 echo "		esac"
 echo "	done"
 if [ -f ${prefix}STEP0POST_CMD ]; then
	echo "	# ------ begin provided text ------"
	cat ${prefix}STEP0POST_CMD
	echo "	# ------- end provided text -------"
 fi

 echo '	echo "`/bin/date`  - $product_name $product_vers PN: $product_part" >> /etc/versionlog'
 echo "	cd \$HOME"
 echo ""
 echo "	if [ -f DELETE ]; then"
 echo "		echo '"
 echo "To complete the installation of $product_name you should remove the"
 echo "following files after your system has been rebooted. These files were"
 echo "unable to be removed at the time the installation occurred."
 echo "' | cat - /usr/options/\$package_name/DELETE | mail root"
 echo "		rm /usr/options/\$package_name/DELETE"
 echo "	fi"
 echo "grep catman preview.log > /dev/null"
 echo "if [ \$? = 0  ]; then"
 echo "echo \"The online man pages use two databases to support the apropos, whatis and\""
 echo "echo \"man -k commands.  You have the option to update them now, or run makealiases\""
 echo "echo \"and makewhatis by hand.  All the man pages are scanned, so this is only required\""
 echo "echo \"for the last layered product you install.\""
 echo "echo \"Do it now? \\\\c\""
 echo "	ok=wrong"
 echo "	while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
 echo "	do "
 echo "	  read ok"
 echo "	  case \"\$ok\" in "
 echo "	    [yY]*) ok=\"yes\" ;; "
 echo "	    [nN]*) ok=\"no\" ;; "
 echo "	    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
 echo "	  esac"
 echo "	done"
 echo "	if [ \"\$ok\" = \"yes\" ]; then"
 echo "		echo \"Updating the man page databases for man, apropos, and whatis.\""
 echo "		echo \"Starting them in the background, it takes awhile.\""
 echo "		(trap 1 2 3 15;/usr/lib/makewhatis)&"
 echo "		(trap 1 2 3 15;/usr/lib/makealiases)&"
 echo "	else"
 echo "		echo \"Please see makealiases(1M) for information about the man page databases.\""
 echo "	fi"
 echo "	fi"
 echo "	echo \$product_name \$product_vers \$product_part > \$package_name.name"
 echo "	rm -f \$package_name.name.preview"
 echo "	rm -f BOM.current OPTS.current"
 echo "	ln BOM.\$product_vers BOM.current"
 echo "	ln OPTS.\$product_vers OPTS.current"
 echo "	mv preview.log preview.log.\$product_vers"
 echo "	if [ ! -f .preloaded ]; then"
 echo '		echo "Rewinding tape..."'
 echo "		/etc/mt -f \$instl_dev rewind"
 echo "	else"
 echo "		rm -f .preloaded"
 echo "	fi"
 echo " if [ -f CONFLICTS ]; then"
 echo "	sort +1 CONFLICTS > /tmp/CONFLICTS\$\$"
 echo "	mv /tmp/CONFLICTS\$\$ CONFLICTS"
 echo "	fi"
 echo "	break ;;"
 echo "*)"
 echo "	echo Invalid argument: \$1"
 echo "esac"

 exit 0

) >${prefix}INSTALL
if [ $? != 0 ]; then
	echo install exited incorrectly
	exit 1
fi	# don't continue if inner shell aborted
fi

#echo "---------------- INSTALLATION SCRIPT ----------------"	# debug
#cat ${prefix}INSTALL						# debug
#echo "-------------- END INSTALLATION SCRIPT --------------"	# debug

if [ $cflag = yes ]; then 		# do not recreate uninstall script
	if [ -n "$uninstall_script" ]; then
		cp $uninstall_script ${prefix}UNINSTALL
	fi
else
echo "Writing uninstall script..."

(
 echo "#! /bin/ksh"
 echo "#"
 echo "# Deinstallation script for $product_name $product_vers PN: $product_part"
 echo ""
 echo "# This script will deinstall Dynix/ptx layered product software."
 echo "# It is usually invoked by either the Dynix/ptx sysadm(1) or ptx/ADMIN menu"
 echo "# interfaces. The current working directory when this script is invoked"
 echo "# is /usr/options/pkg, where pkg is the name of software being deinstalled."
 echo ""
 echo "forceflag="

 for i in $export_list ; do
	eval echo "$i=\\\"\$$i\\\""
 done
 echo ""
 echo "echo \"Deinstallation script for \$product_name \$product_vers\""
 if [ -f ${prefix}STEP0DE_INSTALL ]; then
 	echo "# preamble"
 	echo "# ------ begin provided text ------"
 	cat ${prefix}STEP0DE_INSTALL
 	echo "# ------- end provided text -------"
 fi
 echo "echo \"Do you want to remove files in conflict? \\\\c\""
 echo "	while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
 echo "	do "
 echo "	  read ok"
 echo "	  case \"\$ok\" in "
 echo "	    [yY]*) ok=\"yes\" ;; "
 echo "	    [nN]*) ok=\"no\" ;; "
 echo "	    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
 echo "	  esac"
 echo "	done"
 echo "	if [ \"\$ok\" = \"yes\" ]; then"
 echo "  forceflag=-f "
 echo "echo \"It is likely that some of the directories listed in the CONFLICTS file\""
 echo "echo \"are needed by other products.  Since these directories will still contain\""
 echo "echo \"files they will not be removed.  Messages such as:\""
 echo "echo \"\""
 echo "echo \"/ in conflict list, but removed\""
 echo "echo \"\""
 echo "echo \"only indicate that you have requested the directory to be removed.  An\""
 echo "echo \"rmdir for the directory will be in the mail message sent to the user.\""
 echo "echo \"This is to remind the user that the directory needs to be examined and a\""
 echo "echo \"decision made on removal.\""
 echo "fi"
 echo ""
 echo "echo \"DEINSTALLING $product_name\""
 echo ""
 echo " if [ -f CONFLICTS ]; then"
 echo "sort +1 CONFLICTS > /tmp/CONFLICTS\$\$"
 echo "mv /tmp/CONFLICTS\$\$ CONFLICTS"
 echo "	fi"
 echo "cat OPTS.current | while read locsym loc ;do"
 # Eliminate the trailing / after the location where the image is to be
 # installed in the first entry for each set of files in an image. This
 # is required so that this entry then will mismatch with the entry in
 # the CONFLICTS file and thus result in preserving this directory in
 # which the files were installed upon deinstallation of that software.
 echo "case \$locsym in"
 i=1	
 while [ $i -le $last_step ]; do
	echo "  LOC${i})	egrep \"^\$locsym/\" BOM.current | sed \"s+^\$locsym/+\$loc/+\" | \\"
	echo "	sed \"s+//+/+\" | ./remove \$forceflag CONFLICTS"
	if [ -f ${prefix}STEP${i}LINK ]; then
		echo "if [ -L \"`cat ${prefix}STEP${i}LINK`\" -a ! -d \"`cat ${prefix}STEP${i}LINK`\" ]; then"
		echo "	rm -f `cat ${prefix}STEP${i}LINK`"
		echo "	fi"
	fi
	if [ -f ${prefix}STEP${i}DE_INSTALL ]; then
		cat ${prefix}STEP${i}DE_INSTALL
	fi
	echo "  ;;"
	i=`expr $i + 1`
 done

 echo "  *)	echo UNINSTALL script can not match location sequence number \$locsymnum found"
 echo "   	echo in OPTS file with known images. Files potentially not removed."
 echo "esac"
 echo "done"
 echo ""
 echo "if [ -f DELETE ]; then"
 echo "	echo '"
 echo "To complete the deinstallation of $product_name you should remove the"
 echo "following files after your system has been rebooted. These files were"
 echo "unable to be removed at the time the deinstallation occurred."
 echo "' | cat - /usr/options/\$package_name/DELETE | mail root"
 echo "	rm /usr/options/\$package_name/DELETE"
 echo "fi"
 echo "echo \"The online man pages use two databases to support the apropos, whatis and\""
 echo "echo \"man -k commands.  You have the option to update them now, or run makealiases\""
 echo "echo \"and makewhatis by hand.  All the man pages are scanned, so this is only required\""
 echo "echo \"for the last layered product you deinstall.\""
 echo "echo \"Do it now? \\\\c\""
 echo "	ok=wrong"
 echo "	while [ \"\$ok\" != \"yes\" -a \"\$ok\" != \"no\" ] "
 echo "	do "
 echo "	  read ok"
 echo "	  case \"\$ok\" in "
 echo "	    [yY]*) ok=\"yes\" ;; "
 echo "	    [nN]*) ok=\"no\" ;; "
 echo "	    *)  ok=\"wrong\"; echo 'Enter \"yes\" or \"no\"'  ;; "
 echo "	  esac"
 echo "	done"
 echo "	if [ \"\$ok\" = \"yes\" ]; then"
 echo "		echo \"Updating the man page databases for man, apropos, and whatis.\""
 echo "		echo \"Takes a few minutes.\""
 echo "		(trap 1 2 3 15;/usr/lib/makewhatis)&"
 echo "		(trap 1 2 3 15;/usr/lib/makealiases)&"
 echo "	else"
 echo "		echo \"Please see makealiases(1M) for information about the man page databases.\""
 echo "	fi"
 echo "cd /usr/options"
 echo "if [ -n \"\$package_name\" ]; then"
 echo "	rm -rf /usr/options/\$package_name"
 echo "fi"
 echo 'echo "`/bin/date`  - $product_name $product_vers PN: $product_part (deinstalled)" >> /etc/versionlog'
 echo "echo \"All done!\""
 echo "exit 0"

) >${prefix}UNINSTALL
if [ $? != 0 ]; then
	echo uninstall exited incorrectly
	exit 1
fi	# don't continue if inner shell aborted
fi

#echo "---------------- UNINSTALLATION SCRIPT ----------------"	# debug
#cat ${prefix}UNINSTALL						# debug
#echo "-------------- END UNINSTALLATION SCRIPT --------------"	# debug

if [ $DYNIX ] ;then
	CPIO=cpio
	FIND=/usr/.bin/find
	export MT=/bin/mt
	SUM=/usr/bin/sum
	RSH=rsh
elif [ $DYNIXPSX ] ;then
	CPIO='/bin/cpio -C 65536'
	FIND=/bin/find
	export MT=/etc/mt
	SUM='/bin/sum -r'
	RSH=resh
else
	echo $0: confused as to what host machine is
	exit 1
fi

# Write the bom file.
if [ $cflag = yes ]; then 		# do not recreate bom file
	if [ -n "$bom" ]; then
		cp $bom ${prefix}BOM
	fi
else

	echo "Creating BOM file..."
	i=1	
	while [ $i -le $last_step ]; do
		if [ -f ${prefix}STEP${i}IMAGE ]; then
			EGREP=cat
			if [ -f ${prefix}STEP${i}IMAGE_EXCEPT -a ! -f ${prefix}STEP${i}IMAGEFILES ]; then
				thisimage=`cat ${prefix}STEP${i}IMAGE`
				exceptions=`cat ${prefix}STEP${i}IMAGE_EXCEPT`
				exceptions=`echo $exceptions | sed "s+$thisimage+^.+g"`
				exceptions=`echo $exceptions | /bin/sed 's+ $++'`
				EGREP=`echo $exceptions | sed 's+ + | egrep -v +g'`
				EGREP="egrep -v $EGREP"
				#exceptions=`echo $exceptions | tr ' ' '|'`
				#EGREP="egrep -v \"$exceptions\""
			fi
			#echo "DEBUG $EGREP"
			bomdir=`cat ${prefix}STEP${i}IMAGE`
			cd $bomdir
			if [ -f ${prefix}STEP${i}IMAGEFILES ]; then
				cat ${prefix}STEP${i}IMAGEFILES | sort | bomgen | \
				/bin/sed -e "s;^\.;LOC${i};" -e "s; \./; LOC${i}/;" >> ${prefix}BOM
			else
				$FIND ./ \( ! \( -type b -o -type c \) \) -print | \
				eval $EGREP | sort | bomgen | \
				/bin/sed -e "s;^\.;LOC${i};" -e "s; \./; LOC${i}/;" >> ${prefix}BOM
			fi
			if [ ! -s ${prefix}BOM ]; then
				echo 'Error: Problem creating BOM of the tape contents.'
				echo 'Error: No other steps taken, check the image contents and/or tape creation tools'
			fi
		fi
		i=`expr $i + 1`
	done
fi

# Write the distribution.

if [ $makedist = yes ]; then
    if [ $rflag = no ]; then
	$MT -f $dev_name rewind
	eval $tapeerror

	if [ "$dist_erase" = "yes" ]; then
		echo "Erasing distribution..."
		$MT -f $dev_name erase
		eval $tapeerror
	fi

	echo "Writing distribution..."
	# first the boot block then boot program if this is a bootable dist
	if [ -f ${prefix}BOOTBLOCK ]; then
		if [ ! -f ${prefix}BOOTPROG ]; then
			echo "Distribution description file must specifiy \"bootprog\" if \"bootblock\" is specified."
			exit 1
		fi
		cat `cat ${prefix}BOOTBLOCK` > $dev_name
		eval $tapeerror
	fi
	if [ -f ${prefix}BOOTPROG ]; then
		if [ ! -f ${prefix}BOOTBLOCK ]; then
			echo "Distribution description file must specifiy \"bootblock\" if \"bootprog\" is specified."
			exit 1
		fi
		cat `cat ${prefix}BOOTPROG` > $dev_name
		eval $tapeerror
	fi

	# now the INSTALL script
	mkdir ${prefixdir} ${prefixdir}/$package_name
	cd    ${prefixdir}
	cp ${prefix}INSTALL $package_name/INSTALL
	chmod 744 $package_name/INSTALL
	echo $package_name/INSTALL | $CPIO -oa -O $dev_name
	eval $tapeerror
	rm $package_name/INSTALL

	# then standalone programs if this is a bootable distribution
	standtmp=0
	while [ $standtmp -lt $current_standalone ]; do
		if [ ! -f `cat ${prefix}STANDALONE${standtmp}` ]; then
			echo "`cat ${prefix}STANDALONE${standtmp}`: file not found"
			exit 1
		fi
		cat `cat ${prefix}STANDALONE${standtmp}` > $dev_name
		eval $tapeerror
		standtmp=`expr $standtmp + 1`
	done

	# then BOM, UNINSTALL and delta tools...
	cp ${prefix}BOM $package_name/BOM.$product_vers
	cp ${prefix}UNINSTALL $package_name/UNINSTALL
	for i in $ptxdist_tools; do
		cp $TOOLS/ptxdist/$i $package_name
	done
	chmod 444 $package_name/BOM.$product_vers
	chmod 744 $package_name/UNINSTALL
	$FIND $package_name -print | $CPIO -oa -O $dev_name
	eval $tapeerror
	rm $package_name/BOM.$product_vers
	rm $package_name/UNINSTALL

	# then all the images...
	i=1	
	while [ $i -le $last_step ]; do
		if [ -f ${prefix}STEP${i}IMAGE ]; then
			EGREP=cat
			if [ -f ${prefix}STEP${i}IMAGE_EXCEPT -a ! -f ${prefix}STEP${i}IMAGEFILES ]; then
				exceptions=`cat ${prefix}STEP${i}IMAGE_EXCEPT`
				thisimage=`cat ${prefix}STEP${i}IMAGE`
				exceptions=`echo $exceptions | sed "s+$thisimage+^.+g"`
				exceptions=`echo $exceptions | sed 's+ $++'`
				EGREP=`echo $exceptions | sed 's+ + | egrep -v +g'`
				EGREP="egrep -v $EGREP"
				#exceptions=`echo $exceptions | tr ' ' '|'`
				#EGREP="egrep -v \"$exceptions\""
			fi
			#echo "DEBUG $EGREP"
			cd `cat ${prefix}STEP${i}IMAGE`
			if [ -f ${prefix}STEP${i}IMAGEFILES ]; then
				cat ${prefix}STEP${i}IMAGEFILES | eval $EGREP | sort | $CPIO -oa -O $dev_name
				eval $tapeerror
			else
				$FIND ./ -print | eval $EGREP | sort | $CPIO -oa -O $dev_name
				eval $tapeerror
			fi
		fi
		i=`expr $i + 1`
	done
    else
# Do a remote tape create
	REMOTE_MACH=`$RSH $dev_host -n "sh -c 'if [ -f /dynix ] ;then echo dynix; else echo ptx; fi'"`

	if [ "$REMOTE_MACH" = "dynix" ] ;then
		MT=/bin/mt
		SUM=/usr/bin/sum
	elif [ "$REMOTE_MACH" = "ptx" ] ;then 
		MT=/etc/mt
		SUM='/bin/sum -r'
	else
		echo $0: confused as to what remote machine is
		exit 1
	fi

# Write the distribution.

# A bug in rsh exit status handling causes this crude method to be used.
	return_from_rsh=`$RSH $dev_host -n "$MT -f $dev_name rewind" 2>&1`
# For any reason this initial tape access fails, exit out of ptxmd.
# Other tape accesses should probably be also have exit status tested.
	if [ "$return_from_rsh" != "" ]; then
		echo "Error: Problem accessing tape during rewind of tape."
		echo "Error: No other tape access attempted."
		exit 1
	fi

	if [ "$dist_erase" = "yes" ]; then
		echo "Erasing distribution..."
		$RSH $dev_host -n "$MT -f $dev_name erase"
	fi

	echo "Writing distribution..."
# first the boot block then boot program if this is a bootable dist
	if [ -f ${prefix}BOOTBLOCK ]; then
		if [ ! -f ${prefix}BOOTPROG ]; then
			echo "Distribution description file must specifiy \"bootprog\" if \"bootblock\" is specified."
			exit 1
		fi
		cat `cat ${prefix}BOOTBLOCK` | $RSH $dev_host "/bin/cat > $dev_name"
	fi
	if [ -f ${prefix}BOOTPROG ]; then
		if [ ! -f ${prefix}BOOTBLOCK ]; then
			echo "Distribution description file must specifiy \"bootblock\" if \"bootprog\" is specified."
			exit 1
		fi
		cat `cat ${prefix}BOOTPROG` | $RSH $dev_host "/bin/cat > $dev_name"
	fi

	# now the INSTALL script
	mkdir ${prefixdir} ${prefixdir}/$package_name
	cd    ${prefixdir}
	cp ${prefix}INSTALL $package_name/INSTALL
	chmod 744 $package_name/INSTALL
	echo $package_name/INSTALL | $CPIO -oa | $RSH $dev_host "/bin/cat > $dev_name"
	rm $package_name/INSTALL

	# then standalone programs if this is a bootable distribution
	standtmp=0
	while [ $standtmp -lt $current_standalone ]; do
		if [ ! -f `cat ${prefix}STANDALONE${standtmp}` ]; then
			echo "`cat ${prefix}STANDALONE${standtmp}`: file not found"
			exit 1
		fi
		cat `cat ${prefix}STANDALONE${standtmp}` | \
		$RSH $dev_host "/bin/cat > $dev_name"
		standtmp=`expr $standtmp + 1`
	done

	# then BOM, UNINSTALL and delta tools...
	cp ${prefix}BOM $package_name/BOM.$product_vers
	cp ${prefix}UNINSTALL $package_name/UNINSTALL
	for i in $ptxdist_tools; do
		cp $TOOLS/ptxdist/$i $package_name
	done
	chmod 444 $package_name/BOM.$product_vers
	chmod 744 $package_name/UNINSTALL
	$FIND $package_name -print | $CPIO -oa | $RSH $dev_host "/bin/cat > $dev_name"
	rm $package_name/BOM.$product_vers
	rm $package_name/UNINSTALL

	# then all the images...
	i=1	
	while [ $i -le $last_step ]; do
		if [ -f ${prefix}STEP${i}IMAGE ]; then
			EGREP=cat
			if [ -f ${prefix}STEP${i}IMAGE_EXCEPT -a ! -f ${prefix}STEP${i}IMAGEFILES ]; then
				exceptions=`cat ${prefix}STEP${i}IMAGE_EXCEPT`
				thisimage=`cat ${prefix}STEP${i}IMAGE`
				exceptions=`echo $exceptions | sed "s+$thisimage+^.+g"`
				exceptions=`echo $exceptions | sed 's+ $++'`
				EGREP=`echo $exceptions | sed 's+ + | egrep -v +g'`
				EGREP="egrep -v $EGREP"
				#exceptions=`echo $exceptions | tr ' ' '|'`
				#EGREP="egrep -v \"$exceptions\""
			fi
			#echo "DEBUG $EGREP"
			cd `cat ${prefix}STEP${i}IMAGE`
			if [ -f ${prefix}STEP${i}IMAGEFILES ]; then
				cat ${prefix}STEP${i}IMAGEFILES | eval $EGREP | sort | $CPIO -oa |
				$RSH $dev_host "/bin/cat > $dev_name"
			else
				$FIND ./ -print | eval $EGREP | sort | $CPIO -oa |
				$RSH $dev_host "/bin/cat > $dev_name"
			fi
		fi
		i=`expr $i + 1`
	done

	#  Read the distribution back, and get sums.  Write into $dist_sums.
	if [ $cflag = no -a -n "$dist_sums" ]; then
		echo "Rewinding tape; getting sums..."
		$RSH $dev_host -n "$MT -f $dev_name rewind"
		rshcmd="$RSH $dev_host -n /bin/cat $dev_name | $SUM"
		( echo "Product name:		$product_name"
		  echo "Product version:	$product_vers"
		  echo "Product part number:	$product_part"
		  echo ""
		  echo "Checksums of distribution files:"
		  echo ""

		  # is this a bootable distribution?
		  if [ -f ${prefix}BOOTBLOCK ]; then
		  	$rshcmd
		  fi
		  if [ -f ${prefix}BOOTPROG ]; then
		  	$rshcmd
		  fi

		  $rshcmd		# INSTALL script etc

		  # then standalone programs if this is a bootable distribution
		  standtmp=0
		  while [ $standtmp -lt $current_standalone ]; do
		  	$rshcmd
		  	standtmp=`expr $standtmp + 1`
		  done

		  $rshcmd		# BOM file etc

		  i=1	
		  while [ $i -le $last_step ]; do
			if [ -f ${prefix}STEP${i}IMAGE ]; then
			  $rshcmd
			fi
			i=`expr $i + 1`
		  done
		  echo ""
		) > $dist_sums
	fi

	echo "Rewinding tape..."
	$RSH $dev_host -n "$MT -f $dev_name rewind" &

	wait	# for the rewind to finish
    fi
fi

# Copy install/uninstall/bom if needed
if [ $cflag = no -a -n "$dist_script" ]; then
	cp ${prefix}INSTALL $dist_script
fi
if [ $cflag = no -a -n "$uninstall_script" ]; then
	cp ${prefix}UNINSTALL $uninstall_script
fi
if [ $cflag = no -a -n "$bom" ]; then
	cp ${prefix}BOM $bom
fi

# Return to normal indentation

#>-------------->

		break
		;;

	"")	# Allow a null word (e.g., blank line) to be ignored
		;;

	*)	# Check name list for "known" variables; if not found, abort
		ok="no"
		for i in $MUST_NAME $MAY_NAME ; do
			if [ $i = $keyword ] ; then
				read $keyword
				eval eval $keyword=\\\"\$$keyword\\\"
				export $keyword
				#eval echo set $keyword to \$$keyword	# debug
				ok="yes"
				break
			fi
		done
		if [ $ok != "yes" ] ; then
			echo "Unexpected keyword: $keyword"
			exit 1
		fi
		;;
	esac
done

if [ $makedist = yes -a $rflag = no ]; then
#  Read the distribution back, and get sums.  Write into $dist_sums.
if [ $cflag = no -a -n "$dist_sums" ]; then
	i=1
	echo "Computing Sums"
	while [ $i != 999 ]; do
		echo "Hit \"yes\" to compute sums for the tape $i."
		echo "(Be sure it's in the drive)"
		echo "Hit \"no\" to end. \c"
		ok=wrong
		while [ "$ok" != "yes" -a "$ok" != "no" ] 
		do
		read ok
		case "$ok" in
			[yY]*) ok="yes" ;;
			[nN]*) ok="no" ;;
			*)  ok="wrong"; echo 'Enter "yes" or "no"'  ;;
		esac
		done
		if [ "$ok" = "yes" ]; then
			echo "Rewinding tape; getting sums..."
			$MT -f $dev_name rewind
			eval $tapeerror
			( echo "Product name:		$product_name"
			  echo "Product version:	$product_vers"
			  echo "Product part number:	$product_part"
			  echo ""
			  echo "Checksums of distribution files:"
			  echo ""

			  set `/bin/cat $dev_name | sum -r`
			  eval $tapeerror

			  while [ "$2" != "0" ]; do
				echo $1 $2
				set `/bin/cat $dev_name | sum -r`
				eval $tapeerror
			  done

			) > $dist_sums.$i
			echo "Rewinding tape..."
			$MT -f $dev_name rewind
			eval $tapeerror
			i=`expr $i + 1`
			export product_part=$product_part_two
		else
			i=999
		fi
	done
fi
echo "Rewinding tape..."
$MT  -f $dev_name rewind
eval $tapeerror
fi

if [ $? != 0 ]; then exit 1; fi		# don't continue if inner shell aborted

# Check that we came out of the loop because of an "end" keyword.
if [ ! -f ${prefix}INSTALL ]; then
	echo "Incomplete specification file; expected \"end\" keyword"
	exit 1
fi

echo "Done!"
