#!/bin/sh
#	Sccsid "@(#)kits	7.1	(ULTRIX)	7/22/92"
#
#	kits -
#		make dd'able images of setld(8) readables.
#
#	kits <ringfile>
#
#	000	ccb	22-Aug-1985
#		(C) 1985 DEC
#		Thanks to robin & tarsa.
#	001	Chas. Bennett	09-05-1985
#		make sure there is '\n' on the end of all of the
#			scripts generated by tarsets.c
#	002	ccb	06-19-86
#		upgrade for new setld.
#
#	2-jun-1987	ccb
#		remove stream edit to change blocksizes from floppy
#		to tape blocking
#
#	900309	ccb
#		Compress the subsets in the TODIR directory rather
#		than the local directory.
#		Make RXMAKE default to 0.
#
umask 2
PATH="/usr/release/bin:/usr/sys/dist:/bin:/etc:/usr/bin:/usr/ucb"
export PATH

# constants
PROG=$0
USAGE="Usage: $PROG kfile build-dir master-dir [subset...]"
ID=.inv
T="	"
NL="
"

KITGLOBAL=/usr/tmp/kglbl$$
KITSUBS=/usr/tmp/ksubs$$

# write protect.
readonly PROG USAGE ID

PWD=`pwd`

# get standard args.
case $# in
[012])	echo $USAGE
	exit 1
	;;
*)	KITFILE=$1
	FROMDIR=$2
	TODIR=$3
	shift;shift;shift
	;;
esac


# do we have the parts the user specified?

[ -f $KITFILE ] ||
{
	echo "$PROG: $KITFILE not found."
	exit 1
}
[ -d $FROMDIR ] ||
{
	echo "$PROG: $FROMDIR not found."
	exit 1
}
[ -d $TODIR ] ||
{
	echo "$PROG: $TODIR not found."
	exit 1
}
[ -d $TODIR/instctrl ] || mkdir $TODIR/instctrl ||
{
	echo "$PROG: cannot create instctrl directory."
	exit 1
}


# split up the key file.
#  this writes the global section of the file to KITGLOBAL
#  and prepends ordering numbers to the subset section leaving
#  the result in KITSUBS
(
	ed - $KITFILE <<!
1,/%%/-1w $KITGLOBAL
/%%/+1,\$p
q
!
) | cat -n > $KITSUBS

# pick up global data
. $KITGLOBAL

case "~$NAME~$CODE~$VERS~$MI~$ROOT~" in
*~~*)
	echo "$PROG: $KITFILE format error"
	exit 1
	;;
esac

# check on presence of MI and possible root.
[ -f $MI ] ||
{
	echo "Inventory file $MI not found."
	exit 1
}
case "$ROOT" in
1)
	echo -n "Has the root image already been created? "
	read _X_
	case "$_X_" in
	[yY]*)	;;
	*)	echo
"You must create the root image before running this procedure."
		exit 1
		;;
	esac
	[ -f $TODIR/ROOT ] ||
	{
		echo "ROOT image $TODIR/ROOT not found"
		exit 1
	}
	;;
esac

ALLSUBS=`awk '{print $2}' $KITSUBS`
case "$*" in
"")	S_LIST=$ALLSUBS
	rm -rf $TODIR/instctrl
	mkdir $TODIR/instctrl
	;;
*)	S_LIST=$*
	;;
esac
set - $S_LIST

echo "Creating $# $NAME Subsets."

rm -f *.ctrl .Volume*
rm -f stderr
CTRLS=
I=0
for SUB in $S_LIST
{
	# clean up
	rm -f Volume*
	rm -f $TODIR/$SUB* $TODIR/instctrl/$SUB*

	CTRLS="$CTRLS ./$SUB.ctrl"

	I=`expr $I + 1`
	echo "$I.	Subset $SUB"
	INV=$SUB$ID
	CTRL=$SUB.ctrl

	echo "${T}Generating media creation information...$NL"

	grep "	$SUB\$" $MI | invcutter -v $VERS $FROMDIR |
		tee $INV | tarsets $FROMDIR > ts.$SUB ||
	{
		echo "failed."
		exit 1
	}


	echo "done."

	chmod 744 ts.$SUB
	echo -n "${T}Create $SUB Control File..."

	LINE=`awk '$2 == "'$SUB'"' $KITSUBS`
	case "$LINE" in
	"")	echo "No such subset in $KITFILE: subset $SUB"
		continue
		;;
	esac

	# break up descriptor from key file
	set - $LINE
	TLOC=$1
	shift;shift
	DEPS=`echo $1|sed 's/|/ /'g`
	shift
	FLAGS=` case $1 in
		FLGEXP*)
			eval echo \\\$${1}
			;;
		*)
			echo $1
			;;
		esac`
	shift
	DESC="$*"

	# figure out how many floppy volumes
	NVOLS=`grep -c '^tar' ts.$SUB`

	cat > $CTRL <<!
NAME='$NAME $SUB'
DESC=$DESC
NVOLS=1:$NVOLS
MTLOC=1:$TLOC
DEPS="$DEPS"
FLAGS=$FLAGS
!

	echo -n "done.

	Create $NVOLS Volume Files..."

	J=0; while :
	do
		case $J in
		$NVOLS)
			break
			;;
		esac
		J=`expr $J + 1`
		echo "$PROD
$VERS
$SUB" > Volume$J
		echo -n ".$J"
	done

	case "$RXMAKE" in
	1)	echo -n "
	Make RX50 Kit Images..."

		# make the floppy tar images.
		TO=$TODIR ./ts.$SUB &
		echo "in the background."
		;;
	esac

	echo -n "
	Make Images for Tape and RA60..."

	# edit the ts file for making tape images.
	sed '2,$s/^tar c/tar r/
		/^Volume[0-9][0-9]*\\$/d
		1,$s/'$SUB'[0-9]*/'$SUB'/' ts.$SUB > ts.$SUB.tape
	chmod 744 ts.$SUB.tape
	TO=$TODIR ./ts.$SUB.tape
	echo "done."

	(cd $TODIR
		case "$COMPRESS" in
		1)	echo -n "	Compressing "
			compress -f -v $SUB ||
			{
				echo "compression failed. status = $?"
				exit 1
			}
			SPC=`expr $SUB : '\(...\).*'`
			SVC=`expr $SUB : '.*\(...\)'`
			> instctrl/$SPC$SVC.comp
		#	echo "done."
		esac
	)

	echo "

	*** Finished Creating Media Images for $SUB.
"
	if [ -f scps/$SUB.scp ]
	then
		cp scps/$SUB.scp .
	else
		touch $SUB.scp
		chmod 744 $SUB.scp
		echo "
Null subset control program created for $SUB.  Proceeding...
"
	fi
}

case "$COMPRESS" in
1)	wait
	(cd $TODIR
		for SUB in $S_LIST
		{
			dd if=$SUB.Z of=$SUB bs=10k conv=sync 2> /dev/null
			rm $SUB.Z
		}
	)
esac

# make the inventory control shtooka.
echo -n "Moving Control and Volume Files to Master Pack..."
mkdir $TODIR/instctrl 2> /dev/null
mv *.ctrl *.inv *.scp $TODIR/instctrl
echo -n "done.$NL$NL${T}Creating $CODE.image..."

case "$ROOT" in
1)	ALLSUBS="ROOT $ALLSUBS"
	;;
esac


(	# subshell for cd only
	cd $TODIR
	set - $ALLSUBS
	case "$#" in
	1)	SUM=`sum $ALLSUBS`
		echo "$SUM $ALLSUBS" > instctrl/$CODE.image
		;;
	*)	sum $ALLSUBS > instctrl/$CODE.image
		;;
	esac
	echo "Creating INSTCTRL"
	cd instctrl; tar cpvf ../INSTCTRL *
	cp $CODE.image ..
)
rm -f Volume* ts.* $KITGLOBAL $KITSUBS
echo "done.

Media Image production for $PROD complete."

#!debug rm -f $.*
exit 0
