#! /sbin/sh
#
# Granada Microcare Helpdesk
#
# mknewfs.. A script to create a floppy containing sufficient utilities to
# prepare a scsi disk drive from the floppy device.
# The disk is used to format, section, partition and newfs the scsi drive.


cat << INTRO
                         -- mknewfs--

 > This script will create a floppy containing sufficient utilities
 > to prepare a scsi hard disk drive to accept files
 >
 >   ( Type any character key/s followed by Return to abort )

INTRO

read STRING
if [ ${STRING}X != "X" ]
then
     echo; echo $0 aborted; echo
     exit 1
fi

# Only Return has been pressed so go ahead and make the newfs disc.....

echo; echo " > Formatting floppy....."; echo
ffd 
if [ $? -gt 1 ]
then
	echo; echo " > Error formatting floppy"; echo;
	exit
fi

echo; echo " > Making filesystem....."; echo

newfs /dev/fdf1024 fdf1024
# Check for errors during newfs.....
if [ $? -gt 0 ]
then
	echo; echo " > Error in newfs"; echo;
	exit
fi

echo; echo " > Making space.........."; echo
tunefs -o space -A -m 0 /dev/fdf1024

echo
mount /dev/fdf1024 /mnt 
# Check for errors during mount.....
if [ $? -gt 0 ]
then
	echo; echo " > Error mounting floppy"; echo;
	exit
fi

# First the minimum equipment ( ?? ) required to get the system
# up off the floppy drive......

echo; echo " > Creating directories....."; echo
mkdir /mnt/etc /mnt/dev /mnt/sbin /mnt/usr /mnt/tmp
mkdir /mnt/usr/sbin /mnt/usr/lib /mnt/usr/bin 

echo; echo " > Creating device driver sub set....."; echo
mknod /mnt/dev/console  c  0 0;
mknod /mnt/dev/fdf1024  b  9 0;
mknod /mnt/dev/rfdf1024 c 19 0;
mknod /mnt/dev/kmem     c  1 1;
mknod /mnt/dev/null     c  1 2;
mknod /mnt/dev/cmos     c  1 3;
mknod /mnt/dev/tty      c  3 0;
mknod /mnt/dev/rsd0a    c  21 0;
mknod /mnt/dev/rsd0b    c  21 1;
mknod /mnt/dev/rsd0c    c  21 2;
mknod /mnt/dev/rsd0h    c  21 7;
mknod /mnt/dev/sd0a     b  12 0;
mknod /mnt/dev/sd0b     b  12 1;
mknod /mnt/dev/sd0c     b  12 2;

echo; echo " > Copying some files....."; echo
( cd /usr/lib ; tar cf - c:9010241403.11 ) | ( cd /mnt/usr/lib ; tar xf - )
( cd /usr/sbin ; tar cf - mkfs newfs fsirand ) | ( cd /mnt/usr/sbin ; tar xf - )
( cd /usr/bin ; tar cf - awk cat df ) | ( cd /mnt/usr/bin ; tar xf - )
( cd /sbin ; tar cf - sh init halt scsidm ls fsck ) | ( cd /mnt/sbin ; tar xf - )
cat /dev/null > /mnt/etc/mtab


# awk out the termacap entry for avc....
echo; echo " > Creating mini termcap....."; echo
awk ' /^AV/, /^#/ { print $0 } ' /etc/termcap > /mnt/etc/termcap

cat > /mnt/mknewfs << MKNEWFS
cat << WARNING
				!! WARNING !!

             This process destroys any data on the hard disk !

      This shell script will first run scsidm for you to set up the drive,
      and then show the disktab entry to be used in newfs.

      Press Return to continue or press Ctrl+C to abort ..............
WARNING
read JUNK

scsidm rsd0h
scsidm rsd0h mkdisktab /etc/disktab

cat << INFO_A

         The script will now show the disktab entry to be used in newfs.
                        Press Return to continue.....

INFO_A
read JUNK

cat /etc/disktab

DNAME=\`awk -F: '/^.*\\\\\\\\/ {print \\\$1; exit} ' /etc/disktab\`

# The following grave accents are needed because there is no /bin/sh
\`newfs -v /dev/sd0a \${DNAME}\`

cat << INFO_B

  The target device now contains an empty RISCiX filesystem. To make the 
  device 'bootable' it is necessary to....

  1. Type 'halt'
  2. Replace RISCOS files in the SCSIFS section.
     Minimum required !Boot, !RISCiX, App1, and setcmos.
  3. Change Option on drive to *Opt 4,2
  4. Boot by typing...
     *boot vmunix fd0(0,0) sd0(0,1)
     using the kernel floppy disc and then insert the 'mkboot' floppy when
     prompted.
  5. run 'mksystem' when prompted.
      

INFO_B
MKNEWFS

cat > /mnt/.profile << MKPROFILE
PATH=/usr/local/bin:/sbin:/usr/sbin:/usr/ucb:/usr/bin:/usr/5bin:/usr/acorn:/usr/new:/usr/bin/X11::
HOME=/
export PATH HOME

if [ "${TERM}" = "" ]; then
	TERM=avc
	export TERM
fi
cat << USE_SCSIDM

 This disc contains scsidm and newfs which could be used on their own.
 The disk does not contain a disktab file. It uses scsidm to generate one;
 therfore ensure the diskette is not write protected when using it.

 A script exists on this disc called 'mknewfs' which will run both scsidm 
 and newfs and will automate some of the process of setting up a scsi disc.
 To use this script type 'mknewfs' at the # prompt.  It will run scsidm
 on /dev/rsd0h.  After identifying the drive successfully type 'format', if
 this is required.  When formatting is complete type 'section' and say 'yes'
 to include RISCiX partitions. Now enter the section size required. Then
 type 'partition 0,1' and define partitions 0 for root and 1 for swap by
 entering the partition sizes.
 Then type 'quit'.

USE_SCSIDM

MKPROFILE
chmod 777 /mnt/mknewfs

cd /
umount /mnt

cat <<  FINISHED
 > The mknewfs disk has been successfully created. Note that in future this
 > disc will be referred to as the 'mknewfs disc' and you may wish to label
 > it accordingly..
FINISHED

#      ================= END OF SCRIPT =====================
