#!/bin/sh
# $Header: /usr/src/sys/rt/dist/RCS/netroot,v 1.5 1994/06/11 20:36:41 roger Exp md $
# $ACIS:miniroot 12.2$
# $Source: /usr/src/sys/rt/dist/RCS/netroot,v $
#
# 5799-WZQ (C) COPYRIGHT IBM CORPORATION 1986,1987,1988
# LICENSED MATERIALS - PROPERTY OF IBM
# REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083
#
#	miniroot - script to build an installation miniroot
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/ibm:.
export PATH
CMD=`basename ${0}`
USAGE="
usage: ${CMD} [options]

available options:
	-f<special>	disk partition to use	[/dev/fd0]
	-m<mdir>	mountpoint to use	[/mnt]
	-k<file>	kernel binary to use	[/sys/compile/GENERIC.rtminiroot/vmunix]
	-d<rdir>	"root" directory tree	[/]
	-local		use local datafiles (eg, /etc/hosts)
"
#
# uses Makefile to make the "local" (miniroot specific) utilities.
# Unless local is specified, tries to use /usr/src versions of datafiles
#
DEVICE=/dev/fd0
RDEVICE=/dev/rfd0
FS=/mnt
VMUNIX=/sys/compile/GENERIC.rtminiroot/vmunix
SRC=/usr/src
ROOT=
TBUILD=/tmp/mroot.net.image
QUICK=

for i
do
	case "${i}" in
	-f?*)	DEVICE=`expr "${i}" : '-.\(.*\)'`;;
	-m?*)	FS=`expr "${i}" : '-.\(.*\)'`;;
	-k?*)	VMUNIX=`expr "${i}" : '-.\(.*\)'`;;
	-d?*)	ROOT=`expr "${i}" : '-.\(.*\)'`;;
	-quick) QUICK="-q";;
	-local)	SRC=;;
	*)	echo "${CMD}:${i}:unknown option ${USAGE}" >&2
		exit 1;;
	esac
done

# /etc/services, /etc/protocols, and /etc/disktab are data files that
# may not have had the newest versions installed yet
DATAFILES=
for i in /etc/disktab /etc/protocols /etc/services
do
	if [ ! -f ${ROOT}${SRC}/${i} ]
	then DATAFILES="${DATAFILES} ${ROOT}${i}"
	else DATAFILES="${DATAFILES} ${ROOT}${SRC}/${i}"
	fi
done

echo "${CMD}: checking if all required files exist....." >&2
# /etc/group, /etc/passwd, and /etc/hosts datafiles are stripped.
if [ ! -f ${ROOT}${SRC}/etc/group ]
then
	GROUP=/etc/group
	echo "${CMD}: warning!:using local /etc/group" >&2
else
	GROUP=${ROOT}${SRC}/etc/group
fi

if [ ! -f ${ROOT}${SRC}/etc/passwd ]
then
	PASSWD=/etc/passwd
	echo "${CMD}: warning!:using local /etc/passwd" >&2
else
	PASSWD=${ROOT}${SRC}/etc/passwd
fi

if [ ! -f ${ROOT}${SRC}/etc/hosts ]
then
	HOSTS=/etc/hosts
	echo "${CMD}: warning!:using local /etc/hosts" >&2
else
	HOSTS=${ROOT}${SRC}/etc/hosts
fi

if [ ! -f ${ROOT}${SRC}/dev/MAKEDEV ]
then MAKEDEV=/dev/MAKEDEV
else MAKEDEV=${ROOT}${SRC}/dev/MAKEDEV
fi

BOOT=${ROOT}/usr/mdec/bootfd
BOOTBLOCK=${ROOT}/usr/mdec/fd1.2mboot

# root directory
DIST=/sys/rt/dist

# original /bin files
# XXX: use the OLD bsd shell.
BIN="${ROOT}/usr/old/bin/sh"

# local /bin files
LOCALBIN="${DIST}/bin/sync ${DIST}/bin/cat.sh ${DIST}/bin/mkdir.sh \
	${DIST}/bin/chmod.sh ${DIST}/bin/echo.sh \
	${DIST}/bin/ln.sh ${DIST}/bin/df.sh \
	${DIST}/bin/rmdir.sh ${DIST}/bin/kill.sh"

# orginal /etc files
# ETC="${ROOT}/sbin/fsck ${ROOT}/sbin/restore ${ROOT}/sbin/routed"

# /sbin files
# SBIN="${ROOT}/sbin/restore"

# local /sbin files
# LOCALSBIN="${DIST}/sbin/dtype ${DIST}/sbin/init"

# local /etc files
LOCALETC="${DIST}/etc/fstab ${DIST}/etc/onechar ${DIST}/etc/syscall \
	${DIST}/etc/sizeck.sh ${DIST}/etc/whichdev.sh \
	${DIST}/etc/ask.sh ${DIST}/etc/yesno.sh \
	${DIST}/etc/rc.sh ${DIST}/etc/halt.sh ${DIST}/etc/reboot.sh \
	${DIST}/etc/umount.sh ${DIST}/etc/mask.sh \
	${DIST}/etc/mkconfig.sh ${DIST}/etc/type.sh"

# local /lib files
LOCALLIB="${DIST}/lib/install.driver.sh ${DIST}/lib/install.mainmenu.sh \
	${DIST}/lib/install.options.sh ${DIST}/lib/install.welcome.sh"

# local /usr/bin files
LOCALUSRBIN="${DIST}/usr/bin/sleep.sh ${DIST}/usr/bin/basename.sh"

# local /usr/ucb files
LOCALUCB="${DIST}/usr/ucb/page.sh"

# original /usr/mdec files
MDEC="${ROOT}/usr/mdec/boothd ${ROOT}/usr/mdec/hd* ${ROOT}/usr/mdec/installboot"

# original /usr/ibm files
# local /usr/ucb files
# IBM="${ROOT}/usr/ibm/ftc"

# and of course the GLOB
LOCALGLOB="${DIST}/glob/glob/glob"
GLOBLINKS="
	bin/cp \
	bin/date \
	bin/dd \
	sbin/dtype \
	bin/expr \
	usr/ibm/ftc \
	sbin/ifconfig \
	sbin/init \
	bin/ls \
	sbin/mfs \
	sbin/mount \
	sbin/newfs \
	sbin/restore \
	bin/rm \
	usr/bin/rsh \
	sbin/route \
	bin/stty \
	etc/tar \
	bin/test \
	bin/["

#	etc/syscall 

MISSING=
for i in ${VMUNIX} ${BOOT} ${BOOTBLOCK} ${DATAFILES} ${GROUP} ${PASSWD} \
        ${HOSTS} ${MAKEDEV} ${BIN} ${LOCALBIN} ${ETC} ${LOCALETC} ${LOCALLIB} \
        ${LOCALUSRBIN} ${LOCALUCB} ${MDEC} ${IBM} ${LOCALGLOB}
do
	test -f ${i} || MISSING="${MISSING} ${i}"
done

if [ "${MISSING}" ]
then
	echo "${CMD}: cannot find" ${MISSING} >&2
	exit 1
fi

if [ `whoami` != root ]
then
	echo "${CMD}: can only be run by root" >&2
	exit 1
fi

case "${DEVICE}" in
/dev/fd[01])
	# format diskette (fdformat will ask before formatting)
	RDEVICE=`echo ${DEVICE} | sed s'@fd@rfd@'`
	fdformat -h ${QUICK} ${RDEVICE};;
esac

# make a bootable file system
while : rebuild filesystem?
do
    echo -n "${CMD}: rebuild filesystem on" ${DEVICE} " [yn]?">&2
    if [ -z "$QUICK" ] ; then
	read answer
    else
	answer="y"
	echo "y"
    fi
    case $answer in
    	"n")	break;;

    	"y")${ROOT}/usr/mdec/installboot ${BOOTBLOCK} ${BOOT} ${DEVICE}
		${ROOT}/sbin/newfs -m 0 -o space ${RDEVICE} 
#		${ROOT}/sbin/tunefs -m 0 -o space ${DEVICE} 
		break;;

    	*)	echo "Please answer 'y' or 'n'" >&2
    esac
done

echo "${CMD}: checking for a valid filesystem on" ${DEVICE} >&2
if ${ROOT}/sbin/fsck -n ${RDEVICE}
then : all is well
else
	echo "${CMD}: fsck ${RDEVICE} failed" >&2
	exit 1
fi

trap 'EXIT=${?}; cd /; umount ${DEVICE} > /dev/null 2>&1; exit ${EXIT}' 0 1 2 15

# create the temporary build directory
if [ -d ${TBUILD} ]; then
while : remove build tree?
do
    echo -n "${CMD}: the" ${TBUILD} "build directory exists.  Remove it?">&2
    if [ -z "$QUICK" ] ; then
	read answer
    else
	answer="y"
	echo "y"
    fi
    case $answer in
    	"n")	break;;

    	"y")	rm -rf ${TBUILD} || exit ${?}
		break;;

    	*)	echo "Please answer 'y' or 'n'" >&2
		continue;;
    esac
done
fi

if mkdir ${TBUILD}
then : all is well
else
	echo "${CMD}: can't create ${TBUILD}" >&2
	exit 1
fi

echo "${CMD}: making build tree in ${TBUILD}......" >&2

# set up the directories; make the device special files
cd ${TBUILD}
chmod 775 .
chgrp staff .
mkdir bin etc sbin dev lib mnt mnt/site tmp usr usr/bin usr/ucb usr/mdec usr/ibm
cd dev
sh -${-} ${MAKEDEV} std st0 fd0 hd0 hd1 hd2 xt0 xt1 xt2 cs0
cd ..

# install kernel binary, use ${TBUILD} to make install happy
install -c -s ${VMUNIX} ${TBUILD}/vmunix || exit ${?}
ln vmunix boot || exit ${?}

# set up the bin, sbin, etc, usr/bin, usr/ucb, and usr/mdec directories
for i in ${BIN}
do
	install -c ${i} bin/`basename ${i}` || exit ${?}
done

for i in ${LOCALBIN}
do
	j=`basename ${i} .sh`
	install -c ${i} bin/$j || exit ${?}
#	install -c ${i} bin/`basename ${i} .sh` || exit ${?}
done

for i in ${SBIN}
do
	j=`basename ${i}`
	install -c ${i} sbin/$j || exit ${?}
done
for i in ${LOCALSBIN}
do
	j=`basename ${i} .sh`
	install -c ${i} sbin/$j || exit ${?}
done
#ln sbin/newfs sbin/mfs || exit ${?}

for i in ${LOCALETC}
do
	j=`basename ${i} .sh`
	install -c ${i} etc/$j || exit ${?}
	ln etc/$j sbin/$j
done

for i in ${DATAFILES}
do
	sed '/^#/d' ${i} > etc/`basename ${i} .sh` || exit ${?}
done

# use whatever hosts file we found above
egrep -v '^#' ${HOSTS}			> etc/hosts || exit ${?}

# create minimal /etc/group and /etc/passwd
egrep '^wheel:|^staff:'	${GROUP}	> etc/group || exit ${?}
egrep '^root:'		${PASSWD}	> etc/passwd || exit ${?}

for i in ${LOCALUSRBIN}
do
	install -c ${i} usr/bin/`basename ${i} .sh` || exit ${?}
done

for i in ${LOCALUCB}
do
	install -c ${i} usr/ucb/`basename ${i} .sh` || exit ${?}
done

for i in ${MDEC}
do
	install -c ${i} usr/mdec/`basename ${i}` || exit ${?}
done

for i in ${IBM}
do
	install -c ${i} usr/ibm/`basename ${i}` || exit ${?}
done

for i in ${LOCALLIB}
do
	install -c ${i} lib/`basename ${i} .sh` || exit ${?}
done
# XXX (TEMP)
install -c ${DIST}/lib/install.driver.net.sh lib/install.driver || exit ${?}

# do the GLOB
install -c -s ${LOCALGLOB} ${TBUILD}/glob || exit ${?}
for i in ${GLOBLINKS}
do
	ln ${TBUILD}/glob ${TBUILD}/$i || exit ${?}
done

install -c ${DIST}/restore.tape.sh ${TBUILD}/restore.tape || exit ${?}
install -c ${DIST}/restore.net.sh ${TBUILD}/restore.net || exit ${?}
install -c ${DIST}/xconf.sh ${TBUILD}/xconf || exit ${?}
#ln restore.tape restore.net || exit ${?}

tsize=`BLOCKSIZE=1024 du -s ${TBUILD} | awk '{ print $1}'`
# this seems to fit on a 1.44M disk, 1.2M disks are just too small....
maxsize=1150
if [ $tsize -gt $maxsize ]; then
	echo "${CMD}: build tree is" $tsize "KB which is larger than $maxsize KB" >&2
	exit
else
	echo "${CMD}: copying build tree (" $tsize "KB)......" >&2
fi

cd /
mount ${DEVICE} ${FS} || exit ${?}
rmdir ${FS}/lost+found 2>/dev/null
cd ${TBUILD}
find . -depth -print | cpio -pdlmv ${FS} || exit ${?}
sync; sync
cd /
df ${DEVICE}
umount ${DEVICE}
if fsck -n ${RDEVICE}
then : fsck succeeded
	echo "${CMD}: completed successfully" >&2
else
	echo "${CMD}: fsck ${RDEVICE} failed" >&2
	exit 1
fi
