#!/bin/sh
# $Header:miniroot 12.2$
# $ACIS:miniroot 12.2$
# $Source: /ibm/acis/usr/sys/dist/RCS/miniroot,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=/etc:/usr/ucb:/bin:/usr/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/MINIROOT/vmunix]
	-d<rdir>	"root" directory tree	[/]
	-local		use local datafiles (eg, /etc/hosts)
"
#
# uses Makefile to make the "local" (miniroot specific) utilities
# if the string ATR is found in the name of the kernel binary,
#	an ATR miniroot is created
# unless local is specified, tries to use /usr/src versions of datafiles
#
DEVICE=/dev/fd0
FS=/mnt
VMUNIX=/sys/MINIROOT/vmunix
SRC=/usr/src
ROOT=/

for i
do
	case "${i}" in
	-f?*)	DEVICE=`expr "${i}" : '-.\(.*\)'`;;
	-m?*)	FS=`expr "${i}" : '-.\(.*\)'`;;
	-k?*)	VMUNIX=`expr "${i}" : '-.\(.*\)'`;;
	-d?*)	ROOT=`expr "${i}" : '-.\(.*\)'`;;
	-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

# /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

case "${VMUNIX}" in
*ATR*)	ATR=true
	BOOT=
	BOOTBLOCK=;;
*)	ATR=false
	BOOT=${ROOT}/usr/mdec/bootfd
	BOOTBLOCK=${ROOT}/usr/mdec/fd1.2mboot;;
esac

# root directory
DIST=/sys/dist

# original /bin files
BIN="${ROOT}/bin/sh"

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

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

# local /etc files
LOCALETC="${DIST}/etc/onechar \
	${DIST}/etc/mkfs ${DIST}/etc/sizeck.sh ${DIST}/etc/whichdev.sh \
	${DIST}/etc/init.sh ${DIST}/etc/halt.sh ${DIST}/etc/reboot.sh \
	${DIST}/etc/mount.sh ${DIST}/etc/umount.sh ${DIST}/etc/mask.sh \
	${DIST}/etc/mkconfig.sh ${DIST}/etc/syscall ${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 ${DIST}/usr/ucb/rsh"

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

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

MISSING=
for i in ${VMUNIX} ${DEVICE} ${BOOT} ${BOOTBLOCK} ${DATAFILES} ${GROUP} ${PASSWD} \
        ${HOSTS} ${MAKEDEV} ${BIN} ${LOCALBIN} ${ETC} ${LOCALETC} ${LOCALLIB} \
        ${LOCALUSRBIN} ${LOCALUCB} ${MDEC} ${IBM}
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 ${RDEVICE};;
esac

# make a bootable file system
if ${ATR}
then
	# zero the device
	../dist_atr/writeinstall -z -l${SIZE} ${DEVICE}

	case "${DEVICE}" in
	/dev/fd[01])
		# install proper header via a doswrite
		doswrite -i -v -f ${DEVICE};;
	esac

	${ROOT}/etc/mkfs ${DEVICE} 2880 18 2 4096 512 32 0 6 4096 s
else
	${ROOT}/usr/mdec/installboot ${BOOTBLOCK} ${BOOT} ${DEVICE}
	${ROOT}/etc/mkfs ${DEVICE} 2367 15 2 4096 512 32 0 6 2048 s
fi

if ${ROOT}/etc/fsck -n ${DEVICE}
then : all is well
else
	echo "${CMD}:fsck ${DEVICE} failed" >&2
	exit 1
fi

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

# set up the directories; make the device special files
cd ${FS}
rmdir lost+found
chmod 775 .
chgrp staff .
mkdir bin etc 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 sc0 sc1
cd ..

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

# the miniroot uses this link to distinguish between ATR & RT
if ${ATR}
then
	ln vmunix vmunix.atr
fi

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

for i in ${ETC}
do
	install -c -s ${i} etc/`basename ${i}` || exit ${?}
done
for i in ${LOCALETC}
do
	install -c -s ${i} etc/`basename ${i} .sh` || exit ${?}
done

# /etc/mkfs, /etc/newfs, and /etc/ifconfig are merged on miniroot
ln etc/mkfs etc/newfs || exit ${?}
ln etc/mkfs etc/ifconfig || exit ${?}

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

# /etc/hosts -> /tmp/hosts -> /etc/hosts.base
# /etc/hosts.base is a minimal /etc/hosts
# when /tmp is mounted on a writable file system,
#	/etc/hosts.base is copied to /etc/hosts -> /tmp/hosts,
#	and we have a writable /etc/hosts
ln -s /tmp/hosts etc/hosts || exit ${?}
ln -s /etc/hosts.base tmp/hosts || exit ${?}
egrep -v '^#' ${HOSTS}			> etc/hosts.base || 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 -s ${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 -s ${i} usr/ibm/`basename ${i}` || exit ${?}
done

for i in ${LOCALLIB}
do
	install -c ${i} lib/`basename ${i} .sh` || exit ${?}
done

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

sync; sync
cd /
umount ${DEVICE}
if fsck -n ${DEVICE}
then : fsck succeeded
else
	echo "${CMD}:fsck ${DEVICE} failed" >&2
	exit 1
fi
df ${DEVICE}
