#!/bin/sh
#  @(#)buildroot	1.1 (ULTRIX) 3/2/89
#  Usage: example: sh buildroot ra0 /library/Ultrix
#
#  Description: build the Generic ROOT file system in a dumpimage
#------------------------------------------------------------------
#
#  002 - Nov, 1987 - Tom Tresvik.  
#	Change the interface so that it can be called by the build 
#	script 'buildultrix'.  The ROOT image is produced on the 
#	output pack in .../usr/sys/dist/kitimages/ROOT
#
#  001 - June, 1986 - Lea Fottfredsen & Tung-Ning Cherng
#	   Added in the network remote support.
#
#  000 - March, 31, 1986 - Tung-Ning Cherng
#	   Revised for V2.0.
#
#   	Based on V1.2 'buildroot' - by Greg Tarsa and Bob Fontaine 
#
PATH=.:/etc:/bin:/usr/bin:/usr/ucb
export PATH
umask 022

# We must have 2 command line args

case $# in
2)	;;
*)	echo "Usage: example: sh buildroot ra1 /library/Ultrix"
	exit 1
	;;
esac
	
GENROOT=$1				# for example, ra0
MASTERROOT=$2				# for example, /library/Ultrix
DEST=${MASTERROOT}/usr/sys/dist/kitimages
UPGRADE=upgrade2
#UPGRADE=upgrade64

#
# Define the lists of files to be installed in the ROOT image.
#
ROOT_FILES="
	vmunix
	ultrixboot
	vmb.exe
	pcs750.bin
"
ETC_FILES="
	etc/newfs
	etc/mkfs
	etc/halt
	etc/mount
	etc/mknod
	etc/fsck
	etc/umount
	etc/ufs_mount
	etc/chpt
	etc/radisk
	etc/passwd
	etc/disktab
	etc/ifconfig
	etc/protocols
	etc/rshd
	etc/services
"
BIN_FILES="
	bin/sh
	bin/sh5
	bin/ls
	bin/mv
	bin/sync
	bin/cat
	bin/mkdir
	bin/stty
	bin/echo
	bin/rm
	bin/cp
	bin/expr
	bin/awk
	bin/make
	bin/test
	bin/ed
	bin/true
	bin/false
	bin/ln
	bin/tee
	bin/date
	bin/chmod
	bin/sed
	bin/pwd
	bin/passwd
	bin/mt
	bin/hostname
	bin/ps
	bin/df
	bin/init
	bin/restore
	bin/dump
"
INSTALL_FILES="
	usr/sys/dist/install.2
	usr/sys/dist/install.3
	usr/sys/dist/ask_filesys
	usr/sys/dist/ask_part
	usr/sys/dist/delet_part
	usr/sys/dist/size_part
	usr/sys/dist/finder
	usr/ucb/compress
	usr/ucb/rsh
	usr/ucb/rcp
	usr/ucb/grep
	usr/ucb/whoami
	usr/bin/egrep
	usr/bin/sleep
	etc/eli
	etc/sizer
	etc/setld
	etc/doconfig
	etc/fverify
	etc/fitset
	etc/ready
	bin/tar
	bin/dd
"

#
# test the environment, can we access the device and write to the
# destination directory
#

[ -f /dev/${GENROOT} ] ||
{
	echo "/dev/${GENROOT}a does not exist"
	exit 1
}

[ -d $DEST ] || mkdir $DEST
( > ${DEST}/foo ) ||
{
	echo "Cannot write ${MASTERROOT}/usr/sys/dist/kitimages"
	exit 1
}
rm -f ${DEST}/foo

#
# The environment looks ok so let's proceed
#

#echo -n "ARE YOU SURE YOU WANT TO DESTROY /dev/${GENROOT}? y/n"
#read xx
#case $xx in
#y|Y)
#;;
#*)
#exit 1
#;;
#esac
	
umount /dev/${GENROOT} > /dev/null 2>&1

echo "Making and checking the Generic ROOT filesystem on ${GENROOT}" 
mkfs /dev/r${GENROOT} 16384 16384 1 8192 1024 ||
{ 
	echo "Cannot make the Generic ROOT file system" 
	exit 1 
}

[ -d /ROOT_mnt ] || mkdir /ROOT_mnt
echo "Mounting "${GENROOT}" on /ROOT_mnt."
mount /dev/${GENROOT} /ROOT_mnt ||
{ 
	echo "Cannot mount the Generic ROOT file system" 
	rmdir /ROOT_mnt
	exit 1 
}
cd /ROOT_mnt
rm -rf lost+found

#
# Install files to the Generic ROOT file system
#

echo "Starting the build of the Generic ROOT."

# make top level directories

for K in bin etc tmp usr dev install.tmp mnt 
{
	mkdir $K
}
chmod 777 tmp

for prog in $ROOT_FILES
do
	cp ${MASTERROOT}/$prog  .
done

# Special files

cp ${MASTERROOT}/usr/sys/dist/.rootprofile .profile
cp ${MASTERROOT}/.profile real.profile
cp ${MASTERROOT}/usr/sys/dist/log tmp
cp ${MASTERROOT}/usr/sys/dist/upgrade/${UPGRADE} upgrade

for prog in $INSTALL_FILES
do
	cp ${MASTERROOT}/$prog install.tmp
done

for prog in $ETC_FILES
do
	cp ${MASTERROOT}/$prog etc
done

for prog in $BIN_FILES
do
    cp ${MASTERROOT}/$prog bin
done

ln bin/test bin/[	# put the square bracket into the directory
ln bin/stty bin/STTY
(cd etc ; ln ../bin/init init)

# build necessary special files

cp ${MASTERROOT}/dev/MAKEDEV dev	
cd dev
./MAKEDEV DECstation
cd .. 

echo "Root build is complete."

cd ${DEST}
echo "Unmounting and checking ${GENROOT}."
umount /dev/${GENROOT}
rmdir /ROOT_mnt
fsck /dev/r${GENROOT}
rm -f ROOT
echo "Dumping /dev/r${GENROOT} to ROOT."
dump 0f ROOT /dev/r${GENROOT}
echo "Buildroot done."
