
# This script makes image copies of the release floppies.
# It assumes that mkproto has already been run and the proto
# files are in /install subdirectories.
# This script builds the unix file system into a tmp hard disk
# file system (TMPDEV), then copies that file to "opfil" on a
# scratch high density floppy.
# The floppy is then labeled in DOS before the
# raw disk image is "dd'd" to a unix file. This floppy image file
# name is then output to standard out which is piped to cpio.
# This will stream each image out to tape as it is created.
# Use: mkopfil -t | cpio -ocvC1024000 > /dev/rmt/0m
# Results are output to LOGFILE.
LOGFILE=/tmp/OPLOG

if [ "$1" != "-f" -a "$1" != "-t" ]; then
	echo "usage: mkopfil [-f] [-t] where -f means make floppies." >&2
	echo "       -t means make the floppies, then stream the floppy" >&2
	echo "       images to tape for use with the Auto-duplicator." >&2
	echo "       Results are logged in $LOGFILE." >&2
	exit 1
fi

# Hi density floppy image size for dd
BCNT=50
BS=48b


SLEEPTIME=60
RELDIR=/usr/src/local/release
PATH=$PATH:$RELDIR
#CLEAR="\033[2J"
#CLEAR="$CLEAR\c"
CWD=`pwd`
DOSFIL=\\opus\\opfs\\opfil5
FLOP=/dev/rflpA
INSTALL=/install
TMPDEV=/dev/rdsk/c1d5s0
cd $CWD

clear()
{
	echo $CLEAR >&2
}

invld()
{
	echo "\n\007Invalid response." >&2
	sleep 2
}

ulimit 1000000
if [ `ulimit` -lt 2000 ]; then
	echo "\nCannot change ulimit - must be greater than 2000." >&2
	echo "If you run this script as root, it will automatically" >&2
	echo "be changed." >&2
	exit 1
fi

cd $INSTALL
DIRS=""
for i in *; do
	if [ -d $i ]; then
		DIRS=1
	fi
done
if [ ! "$DIRS" ]; then
	echo "\nNo directories found in /install." >&2
	echo "The script 'mkproto' must be run before running this one." >&2
	exit 1
fi

FLOPPY=""
TAPE=""
if [ "$1" = "-f" ]; then
	FLOPPY=1
elif [ "$1" = "-t" ]; then
	TAPE=1
	clear
	echo "\n\nCHECK - image filenames are sent to stdout as they are created." >&2
	echo "If you are making a tape, be sure to pipe \"mkopfil\" to \"cpio\"." >&2
	echo "Press RETURN to continue: \c" >&2
	read ANS
fi

while :; do
	clear
	echo "\n\n$TMPDEV will be used to build each file system into." >&2
	echo "This device should be set up as a DOS file named $DOSFIL." >&2
	echo "Is this OK? [y/n]: \c" >&2
	read ANS
	case $ANS in
		y)
			break
			;;
		n)
			exit 1
			;;
		*)
			invld
			;;
	esac
done

get_level()
{
	while :; do
		clear
		if [ $FULL ]; then
			echo "\n\nEnter the release name (ie, F1, F2) : \c" >&2
		else
			# SMALL or XWINDOWS
			echo "\n\nEnter the rev level of the release (ie, A0, A1) : \c" >&2
		fi
		read REL
		REL=`echo $REL | tr '[a-z]' '[A-Z]'`
		case $REL in
			[A-Z][0-9])
				break
				;;
			*)
				echo "\nMust be a letter followed by a number.\c" >&2
				invld
				sleep 2
				;;
		esac
	done
}

while :; do
	XNAME=""
	FULL=""
	SMALL=""
	XWINDOWS=""
	clear
	echo "\n\nWhat type of release are you working with?" >&2
	echo "\n1) All or part of a full Opus5 release." >&2
	echo "\n2) An X Windows release." >&2
	echo "\n3) A smaller release." >&2
	echo "\nEnter [1-3]: \c" >&2
	read ANS
	case $ANS in
		1)
			FULL=1
			get_level
			break
			;;
		2)
			while :; do
				get_level
				clear
				echo "\n\nEnter the name you would like to use for the X release, (i.e., X10, X11) ?" >&2
				echo "This name will be used as the release name on the duplicator\n(with '_$REL' appended)." >&2
				echo "\nEnter name (3 character limit): \c" >&2
				XNAME=`line | tr '[a-z]' '[A-Z]'`
				case $XNAME in
					[0-Z][0-Z][0-Z])
						break
						;;
					*)
						invld
						;;
				esac
			done
			XWINDOWS=1
			break
			;;
		3)
			get_level
			SMALL=1
			break
			;;
		*)
			invld
			;;
	esac
done

clear
echo "\n\nExtracting groups...\c" >&2

>/tmp/GRP$$
i=1
RELEASE_GROUPS=""
if [ "$FULL" -o "$XWINDOWS" ]; then
	if [ $FULL ]; then
		EXT=$REL
	else
		EXT=$XNAME
	fi
	if [ ! -f $RELDIR/setup/CONTENTS.$EXT ]; then
		echo "\n\007$RELDIR/setup/CONTENTS.$EXT not found." >&2
		exit 1
	fi
	GROUPS=`cat $RELDIR/setup/CONTENTS.$EXT`
	cd /install; ls -d $GROUPS > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "\n\n\007The following directories are needed but missing." >&2
		for GRP in $GROUPS; do
			if [ ! -d $GRP ]; then
				echo $GRP >&2
			fi
		done
		echo "\nRun mkproto for these groups.\n" >&2
		exit 1
	fi
fi

if [ $SMALL ]; then
	GROUPS=""
	cd $INSTALL
	for i in *; do
		if [ -d $i ]; then
			GROUPS="$GROUPS $i"
		fi
	done
	if [ ! "$DIRS" ]; then
		echo "\n\n\007No directories found under $INSTALL." >&2
		echo "\nRun mkproto to proper directories.\n" >&2
		exit 1
	fi
	GROUPS=`(for GRP in $GROUPS; do	
		echo $GRP
	done) | sort`
fi

NUM=1
for GRP in $GROUPS; do
	TOTAL=$NUM
	echo "$NUM) $GRP" >> /tmp/GRP$$
	NUM=`expr $NUM + 1`
done

if [ `expr $NUM % 2` = 1 ]; then
	NUM=`expr $NUM / 2`
	NUM=`expr $NUM + 1`
else
	NUM=`expr $NUM / 2`
fi

FIRST=""
LAST=""
while :; do
	clear
	echo "\n\nThe following groups are available in /install:\n" >&2
	cat /tmp/GRP$$ | pr -$NUM -o4 -t >&2
	echo "\nSelect a group or groups (number, number-number, q) : \c" >&2
	read ANS
	if [ "$ANS" = "q" -o "$ANS" = "Q" ]; then
		rm -f /tmp/*$$
		exit 1
	elif [ ! "$ANS" ]; then
		invld
	fi
	case $ANS in
		[1-9])
			if [ $ANS -gt $TOTAL ]; then
				echo "\n\007Choice must be less than or equal to $TOTAL." >&2
				sleep 2
			else
				FIRST=$ANS
				break
			fi
			;;
		[1-9]-[0-9]|[1-9]-[1-9][0-9]|[1-9][0-9]-[1-9][0-9])
			LAST=`echo $ANS | cut -d"-" -f2`
			if [ $LAST -gt $TOTAL ]; then
				echo "\n\007Second number must be less than or equal to $TOTAL." >&2
				sleep 2
				continue
			fi
			FIRST=`echo $ANS | cut -d"-" -f1`
			if [ $FIRST -ge $LAST ]; then
				echo "\n\007The first number must be smaller than the second." >&2
				sleep 2
			else
				break
			fi
			;;
		*)
			invld
			;;
	esac
done
		
if [ "$FIRST" -a "$LAST" ]; then
	# Create sequence list
	SEQ=""
	CNT=$FIRST
	while [ $CNT -le $LAST ]; do
		SEQ="$SEQ $CNT"
		CNT=`expr $CNT + 1`
	done
else
	SEQ=$FIRST
fi
GROUPS=""
for NUM in $SEQ; do
	NEW=`grep "^$NUM) " /tmp/GRP$$ | sed 's/^[0-9]*) //'`
	GROUPS="$GROUPS $NEW"
done

GROUPS=`echo $GROUPS`
if [ $TAPE ]; then
	clear
	if [ $FULL ]; then
		echo "\n\nReady to make a mkopfil tape for release $REL." >&2
		echo "($GROUPS)" >&2
	elif [ $XWINDOWS ]; then
		echo "\n\nReady to make a mkopfil tape for release ${XNAME}_$REL." >&2
		echo "($GROUPS)" >&2
	else
		if [ $TOTAL -eq 1 ]; then
			echo "\n\nReady to make a mkopfil tape for $GROUPS." >&2
			echo "\n$GROUPS" >&2
		else
			echo "\n\nReady to make a mkopfil tape for the following packages:" >&2
			echo "\n$GROUPS" >&2
		fi
	fi
	echo "\nPress RETURN when a formatted, empty, high density diskette has been"  >&2
	echo "inserted in the $FLOP drive: \c" >&2
	read ANS
fi

echo "\n** Results are logged in $LOGFILE." >&2
> $LOGFILE

#if [ $TAPE ]; then
#	tape rew > /dev/null 2>&1
#	REWSTAT=`/opus/bin/tape rew 2>&1 | grep "^bot"` 
#	while [ ! "$REWSTAT" ]; do
#		clear
#		echo "\n\nPlease disable the write protect wheel on the backup tape." >&2
#		echo "\nPress RETURN when ready: \c" >&2
#		line
#		REWSTAT=`tape rew 2>&1 | grep "^bot"` 
#	done
#fi
for SW_PKG in $GROUPS; do
	cd $CWD
	trap "rm -f /tmp/*$$ ${REL}_${SW_PKG}.li ${SW_PKG}_${REL}.li $XNAME.li;exit" 1 2 3 15
	if [ $FULL ]; then
		LISTFILE=$REL.li
	elif [ $XWINDOWS ]; then
		LISTFILE=${XNAME}_$REL.li
	else
		LISTFILE=${SW_PKG}_$REL.li
	fi
	>$LISTFILE

	rm -f /install/* > /dev/null 2>&1
	cp /install/$SW_PKG/* /install
 	proto_list=`(cd $INSTALL; ls proto*)`
 	NUM=`(cd $INSTALL; ls proto.${SW_PKG}* | grep -c '^')`
	echo "==== $SW_PKG" >&2
	for proto_file in ${proto_list}; do
		n=`expr $proto_file : "proto\.${SW_PKG}\([0-9].*\)" \| ${proto_file} : 'proto\([0-9]*\).*'`
		if test "$n" = 0; then n=`expr ${proto_file} : 'proto\.[a-z]*\([0-9]*\)'`; fi
		if [ ! "$SMALL" -a "$NUM" -eq 1 ]; then
			n=""
		fi
		if [ $FLOPPY ]; then
			echo "\nInsert diskette $SW_PKG$n and press RETURN: \c" >&2
			read ANS
		fi
		echo "\n*** mkfs $TMPDEV ${INSTALL}/$proto_file" >&2
		echo "\n\n*** mkfs $TMPDEV ${INSTALL}/$proto_file" >> $LOGFILE
/opus/bin/opdos asciiwrite \\opus\\opx.bat <<!
echo off
if exist $DOSFIL del $DOSFIL
exit
!
dos REMOTE \\opus\\opx
		mkfs $TMPDEV $INSTALL/$proto_file >> $LOGFILE 2>&1

		labelit $TMPDEV install $SW_PKG$n >> $LOGFILE 2>&1

		echo "\n*** fsck of $TMPDEV" >> $LOGFILE 2>&1
		fsck $TMPDEV >> $LOGFILE 2>&1

		echo "Copying opfil to diskette" >&2
/opus/bin/opdos asciiwrite \\opus\\opx.bat <<!
echo off
if exist a:opfil del a:opfil
exit
!
dos REMOTE \\opus\\opx
		dos REMOTE "copy $DOSFIL a:opfil" >> $LOGFILE 2>&1
		if [ $FULL ]; then
			echo "${REL}_$SW_PKG$n\n" > format.inp
		else
			echo "${SW_PKG}_${REL}_$n\n" > format.inp
		fi
		udcp -a format.inp format.inp >> $LOGFILE 2>&1
		ducp -a format.inp >&2
		echo "\n\n" >&2
		dos REMOTE "label a: < format.inp" >> $LOGFILE 2>&1
		dos REMOTE "del format.inp" >> $LOGFILE 2>&1
		rm -f format.inp >> $LOGFILE 2>&1
		if [ $TAPE ]; then
			if [ $FULL ]; then
				echo "Doing image copy of diskette to ${REL}_$SW_PKG$n" >&2
				echo "\nDoing image copy of diskette to ${REL}_$SW_PKG$n" >> $LOGFILE
				dd if=$FLOP of=${REL}_$SW_PKG$n bs=$BS count=$BCNT >> $LOGFILE 2>&1
				echo "Sending \"${REL}_$SW_PKG${n}\" to stdout" >&2
				echo "${REL}_$SW_PKG$n"
				echo "${REL}_$SW_PKG$n" >> $LISTFILE
				# Do cpio out to tape now
				sync; sync
				sleep $SLEEPTIME
				echo "Done sleeping" >&2
				rm -f "${REL}_$SW_PKG$n"
			else
				echo "Doing image copy of diskette to ${SW_PKG}_${REL}_$n" >&2
				echo "\nDoing image copy of diskette to ${SW_PKG}_${REL}_$n" >> $LOGFILE
				dd if=$FLOP of=${SW_PKG}_${REL}_$n bs=$BS count=$BCNT >> $LOGFILE 2>&1
				echo "Sending \"${SW_PKG}_${REL}_$n\" to stdout" >&2
				echo "${SW_PKG}_${REL}_$n"
				echo "${SW_PKG}_${REL}_$n" >> $LISTFILE
				# Do cpio out to tape now
				sync; sync
				sleep $SLEEPTIME
				echo "Done sleeping" >&2
				rm -f "${SW_PKG}_${REL}_$n"
			fi
		fi
	done

	# Write out list file
	if [ $TAPE ]; then
		cat $LISTFILE >> LIST_SUM
		rm -f $LISTFILE
	fi
done

if [ $TAPE ]; then
	# Write out composite list file
	if [ $FULL ]; then
		pr -n2 -l99 -t LIST_SUM | sed -e 's/^\ /0/' | sort -r | cut -f2 > "$REL.li"
		echo "$REL.li"
	elif [ $XWINDOWS ]; then
		pr -n2 -l99 -t LIST_SUM | sed -e 's/^\ /0/' | sort -r | cut -f2 > "${XNAME}_$REL.li"
		echo "${XNAME}_$REL.li"
	else
		pr -n2 -l99 -t LIST_SUM | sed -e 's/^\ /0/' | sort -r | cut -f2 > "${SW_PKG}_$REL.li"
		echo "${SW_PKG}_$REL.li"
	fi
	sleep $SLEEPTIME
fi

echo "** Results are logged in $LOGFILE." >&2
rm -f /tmp/*$$ > /dev/null 2>&1
rm -f LIST_SUM "${SW_PKG}_$REL.li" "${REL}_$SW_PKG.li ${XNAME}_$REL.li"
if [ $FULL ]; then
	echo "\n** Copying $LOGFILE to $RELDIR/OPLOG.$REL" >&2
	cp $LOGFILE $RELDIR/OPLOG.$REL
elif [ $XWINDOWS ]; then
	echo "\n** Copying $LOGFILE to $RELDIR/OPLOG.${XNAME}_$REL" >&2
	cp $LOGFILE $RELDIR/OPLOG.${XNAME}_$REL
else
	echo "\n** Copying $LOGFILE to $RELDIR/OPLOG.${SW_PKG}_$REL" >&2
	cp $LOGFILE $RELDIR/OPLOG.${SW_PKG}_$REL
fi
echo "\n\007DONE\n" >&2
exit 0
