#	START NEW ARIX SCCS HEADER
#
#	@(#) makefsys: version 25.1 created on 12/2/91 at 16:55:15
#
#	Copyright (c) 1990 by Arix Corporation
#	All Rights Reserved
#
#	@(#)makefsys	25.1	12/2/91 Copyright (c) 1990 by Arix Corporation
#
#	END NEW ARIX SCCS HEADER
#
#	ATT: ident	"sadmin:admin/makefsys	2.12"

#ident	"@(#)sadmin/admin:makefsys	25.1"

#	Create a new file system on a drive.
#help# 
#help#	Makefsys creates a new file system on a built-in disk drive.

#!	chmod +x ${file}
PATH=/bin:/usr/bin:/usr/lbin:/etc

#	With gap == 1 the blocks/cylinder has no effect in the file system.
#	Since most formatting routines lay the sectors out in an optimal
#	way, gap == 1 is a reasonable assumption.
# gap=1		# rotational gap size
# blockcyl=32	# blocks/cylinder

trap 'exit 0' 1 2 15
flags="-qq -k$$"

usage () {
	echo >&2 "Usage:  `basename $0` [ -p pattern ]"
	exit 1
}

patterns=
case $# in
0 )
	;;
2 )
	if [ $1 = -p ]
	then
		patterns=$2
		shift 2
	else
		usage
	fi
	;;
* )
	usage
esac

dir=${0}.d
if [ -z "${patterns}"  -a  -d ${dir} ]
then
	patterns=`/bin/ls ${dir} 2>/dev/null`
fi

cddrive=`seldevice -c $$ /dev/rdsk ${patterns}`
bddrive=/dev/`expr ${cddrive} : '/dev/r\(.*\)'`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${bddrive}`
fi

/etc/fsstat ${bddrive} > /dev/null
exit=$?

if [ ${exit} -eq 2 ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${bddrive} ;s;^\\([^ ]*\\) .*;\\1;p"`
	if [ -z "${filesys}" ]
	then
		echo >&2 "${bddrive} is mounted, but is not in the mount table.
It needs to be unmounted before a new file system can  be made."
		exit 1
	fi

	echo >&2 "\n${bddrive} is already mounted on \"${filesys}\".
This command cannot run with it mounted.\n"
	if  checkyn ${flags} -H"
	${bddrive} is already in use; it is mounted on ${filesys}.
	Before you can use the drive for some other purpose, you
	must unmount it." \
		-f "Do you want to unmount it?"
	then
		if  diskumount ${bddrive} ${filesys}
		then
			unmounted=true
		else
			echo >&2 "\nThis command cannot complete because \"${filesys}\" could not be unmounted.
Unmount it and try again."
			exit 0
		fi
	else
		exit 0
	fi
fi

l=`disklabel $$ ${cddrive}`
eval `labelfsname "${l}"`

if [ -n "${label}"  -o  -n "${fsname}" -o -n "${unmounted}" ]
then
	echo "This drive already has a file system on it.
	${label:+label = ${label}		}${fsname:+file system name = ${fsname}}"
	checkyn ${flags} -f \
	'
	WARNING:  If you answer "yes", all data on this drive will be LOST!
	BE SURE THIS IS WHAT YOU WANT TO DO!!
Do you want to erase this file system and build another?'  ||  {
		echo 'This drive will not be erased.'
		exit 0
	}
fi

label=`checkre ${flags} ${label:+-D${label}} -H'
	The drive label identifies this particular drive. Usually, it is
	the name of the machine, or the initials or name of the owner and
	a number, for example "mynode" or "liz001".
	The label may be no more than 6 characters long.' -fe \
	"Enter the label to be put on the drive ${label:+(default: ${label})} [?, q]" \
	'^.\{1,6\}$' 'Must be at least one and no more than 6 characters.' \
	'^[!-~]*$' 'Must be printing characters only.'`

fsname=`checkre ${flags} ${fsname:+-D${fsname}} -H'
	The file system name is the name of the directory where you want the
	file system mounted.  It determines the first name in the full path
	names of the files it contains.  Thus if you specify "mnt" as the
	file system name, the file system will be mounted on directory "/mnt"
	and all file names will begin with "/mnt/".' -fe \
	"Enter the file system name ${fsname:+(default: ${fsname})} [?, q]" \
	'^/\{0,1\}.\{1,6\}$' 'Must be at least one and no more than 6 characters.' \
	'^/\{0,1\}[a-zA-Z0-9]*$' 'Must be letters and numbers only.'`

fsname=`expr ${fsname} : '/*\(.*\)'`

blocks=`spclsize -b ${cddrive}`
echo "******* blocks >> " ${blocks}
#	Funny calculation of inodes gives nice multiple-of-10 values.
inodes=`expr ${blocks} / 70 \* 10`
halfblocks=`expr ${blocks} / 2`
while true
do
	i=`checkre ${flags} -fe -D "${inodes}" \
		"Enter the maximum number of files and directories on this drive
	(default ${inodes}) [q]:" \
		'^[0-9]\{1,\}$' 'You must enter a number or just press RETURN.' \
		-R '^00*$' 'The number must be greater than zero.'`
	if [ ${i} -lt ${halfblocks} ]
	then
		break
	fi
	echo "The number of files and directories must be less than ${halfblocks}."
done

#	So  df -t  reports what was requested.
inodes=`expr ${i} + 8`

echo "Building '${fsname}' file system on '${label}'."
# sleep 2

trap 'exit 1' 1 2 15
trap '	trap "" 1 2
	cat -s /tmp/$$makefsys;  rm -f /tmp/$$makefsys;  exit 1' 0

while true
do
	i=`checkre ${flags} -fe -D "1" \
		"Enter the size of the file system, 1(K) or 4(K) 
	(default 1K) [q]:" \
		'^[0-9]\{1,\}$' 'You must enter a number or just press RETURN.' \
		-R '^00*$' 'The number must be greater than zero.'`
	if [ ${i} -eq 4 ]
	then
		break
	fi
	if [ ${i} -eq 1 ]
	then
		break
	fi
	echo "The size of the file system must be 1K or 4K."
done

if checkyn ${flags} -f \
	'
Do you want to make a secure file system?'  
then
if [ ${i} -eq 1 ]
then
echo "/etc/mkfs ${cddrive} ${blocks}:${inodes}"
/etc/mkfs ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
else
echo "/etc/mkfs -b4096 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -b4096 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
fi
else
if [ ${i} -eq 1 ]
then
echo "/etc/mkfs -bu1024 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -bu1024 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
else
echo "/etc/mkfs -bu4096 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -bu4096 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
fi
fi

echo "/etc/labelit ${cddrive} ${fsname} ${label}"
/etc/labelit ${cddrive} ${fsname} ${label} >> /tmp/$$makefsys 2>&1 ||  exit 1
sync
rm /tmp/$$makefsys

if [ ! -d /${fsname} ]
then
	mkdir /${fsname}
fi
chmod 777 /${fsname}
/etc/mount ${bddrive} /${fsname}  ||  {
	admerr $0 mount of ${bddrive} on /${fsname} failed.
	exit 1
}

trap "	trap '' 1 2
	cd /;  diskumount  ${bddrive} /${fsname}" 0

echo "Initializing '${fsname}' file system."
cd /${fsname}
umask 000
mkdir lost+found
cd lost+found
# set +e
#	Populate the lost+found directory to grow it to a usable size.
i=`expr ${inodes} / 400 + 1`
while [ ${i} -gt 0 ]
do
	tee ${i}1 ${i}2 ${i}3 ${i}4 ${i}5 ${i}6 ${i}7 ${i}8 ${i}9 ${i}0 </dev/null
	i=`expr ${i} - 1;  exit 0`	# exit 0 should not be needed, but is.
done
rm -f *

if checkyn ${flags} -f -H '
	If you leave the file system mounted, then you can use it right now.
	Otherwise, this procedure will unmount it.' \
	"Do you want to leave '/${fsname}' mounted?"
then
	sync; trap 0
else
	diskumount ${bddrive}
fi
#	START NEW ARIX SCCS HEADER
#
#	@(#) makefsys: version 1.2 created on 6/7/90 at 18:33:35
#
#	Copyright (c) 1990 by Arix Corporation
#	All Rights Reserved
#
#	@(#)makefsys	1.2	6/7/90 Copyright (c) 1990 by Arix Corporation
#
#	END NEW ARIX SCCS HEADER
#
#	ATT: ident	"sadmin:admin/makefsys	2.12"

#ident	"@(#)sadmin/admin:makefsys	1.2"

#	Create a new file system on a drive.
#help# 
#help#	Makefsys creates a new file system on a built-in disk drive.

#!	chmod +x ${file}
PATH=/bin:/usr/bin:/usr/lbin:/etc

#	With gap == 1 the blocks/cylinder has no effect in the file system.
#	Since most formatting routines lay the sectors out in an optimal
#	way, gap == 1 is a reasonable assumption.
# gap=1		# rotational gap size
# blockcyl=32	# blocks/cylinder

trap 'exit 0' 1 2 15
flags="-qq -k$$"

usage () {
	echo >&2 "Usage:  `basename $0` [ -p pattern ]"
	exit 1
}

patterns=
case $# in
0 )
	;;
2 )
	if [ $1 = -p ]
	then
		patterns=$2
		shift 2
	else
		usage
	fi
	;;
* )
	usage
esac

dir=${0}.d
if [ -z "${patterns}"  -a  -d ${dir} ]
then
	patterns=`/bin/ls ${dir} 2>/dev/null`
fi

cddrive=`seldevice -c $$ /dev/rdsk ${patterns}`
bddrive=/dev/`expr ${cddrive} : '/dev/r\(.*\)'`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${bddrive}`
fi

/etc/fsstat ${bddrive} > /dev/null
exit=$?

if [ ${exit} -eq 3 ]
then
	echo >&2 "\n/etc/fsstat: unexpected error. Contact your System Administrator."
	exit 1
fi

if [ ${exit} -eq 2 ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${bddrive} ;s;^\\([^ ]*\\) .*;\\1;p"`
	if [ -z "${filesys}" ]
	then
		echo >&2 "${bddrive} is mounted, but is not in the mount table.
It needs to be unmounted before a new file system can  be made."
		exit 1
	fi

	echo >&2 "\n${bddrive} is already mounted on \"${filesys}\".
This command cannot run with it mounted.\n"
	if  checkyn ${flags} -H"
	${bddrive} is already in use; it is mounted on ${filesys}.
	Before you can use the drive for some other purpose, you
	must unmount it." \
		-f "Do you want to unmount it?"
	then
		if  diskumount ${bddrive} ${filesys}
		then
			unmounted=true
		else
			echo >&2 "\nThis command cannot complete because \"${filesys}\" could not be unmounted.
Unmount it and try again."
			exit 0
		fi
	else
		exit 0
	fi
fi

l=`disklabel $$ ${cddrive}`
eval `labelfsname "${l}"`

if [ -n "${label}"  -o  -n "${fsname}" -o -n "${unmounted}" ]
then
	echo "This drive already has a file system on it.
	${label:+label = ${label}		}${fsname:+file system name = ${fsname}}"
	checkyn ${flags} -f \
	'
	WARNING:  If you answer "yes", all data on this drive will be LOST!
	BE SURE THIS IS WHAT YOU WANT TO DO!!
Do you want to erase this file system and build another?'  ||  {
		echo 'This drive will not be erased.'
		exit 0
	}
fi

label=`checkre ${flags} ${label:+-D${label}} -H'
	The drive label identifies this particular drive. Usually, it is
	the name of the machine, or the initials or name of the owner and
	a number, for example "mynode" or "liz001".
	The label may be no more than 6 characters long.' -fe \
	"Enter the label to be put on the drive ${label:+(default: ${label})} [?, q]" \
	'^.\{1,6\}$' 'Must be at least one and no more than 6 characters.' \
	'^[!-~]*$' 'Must be printing characters only.'`

fsname=`checkre ${flags} ${fsname:+-D${fsname}} -H'
	The file system name is the name of the directory where you want the
	file system mounted.  It determines the first name in the full path
	names of the files it contains.  Thus if you specify "mnt" as the
	file system name, the file system will be mounted on directory "/mnt"
	and all file names will begin with "/mnt/".' -fe \
	"Enter the file system name ${fsname:+(default: ${fsname})} [?, q]" \
	'^/\{0,1\}.\{1,6\}$' 'Must be at least one and no more than 6 characters.' \
	'^/\{0,1\}[a-zA-Z0-9]*$' 'Must be letters and numbers only.'`

fsname=`expr ${fsname} : '/*\(.*\)'`

blocks=`spclsize -b ${cddrive}`
#	Funny calculation of inodes gives nice multiple-of-10 values.
inodes=`expr ${blocks} / 70 \* 10`
halfblocks=`expr ${blocks} / 2`
while true
do
	i=`checkre ${flags} -fe -D "${inodes}" \
		"Enter the maximum number of files and directories on this drive
	(default ${inodes}) [q]:" \
		'^[0-9]\{1,\}$' 'You must enter a number or just press RETURN.' \
		-R '^00*$' 'The number must be greater than zero.'`
	if [ ${i} -lt ${halfblocks} ]
	then
		break
	fi
	echo "The number of files and directories must be less than ${halfblocks}."
done

#	So  df -t  reports what was requested.
inodes=`expr ${i} + 8`

echo "Building '${fsname}' file system on '${label}'."
# sleep 2

trap 'exit 1' 1 2 15
trap '	trap "" 1 2
	cat -s /tmp/$$makefsys;  rm -f /tmp/$$makefsys;  exit 1' 0

while true
do
	i=`checkre ${flags} -fe -D "1" \
		"Enter the size of the file system, 1(K) or 4(K) 
	(default 1K) [q]:" \
		'^[0-9]\{1,\}$' 'You must enter a number or just press RETURN.' \
		-R '^00*$' 'The number must be greater than zero.'`
	if [ ${i} -eq 4 ]
	then
		break
	fi
	if [ ${i} -eq 1 ]
	then
		break
	fi
	echo "The size of the file system must be 1K or 4K."
done

if checkyn ${flags} -f \
	'
Do you want to make a secure file system?'  
then
if [ ${i} -eq 1 ]
then
echo "/etc/mkfs ${cddrive} ${blocks}:${inodes}"
/etc/mkfs ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
else
echo "/etc/mkfs -b4096 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -b4096 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
fi
else
if [ ${i} -eq 1 ]
then
echo "/etc/mkfs -bu1024 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -bu1024 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
else
echo "/etc/mkfs -bu4096 ${cddrive} ${blocks}:${inodes}"
/etc/mkfs -bu4096 ${cddrive} ${blocks}:${inodes} > /tmp/$$makefsys 2>&1 || exit 1
fi
fi

echo "/etc/labelit ${cddrive} ${fsname} ${label}"
/etc/labelit ${cddrive} ${fsname} ${label} >> /tmp/$$makefsys 2>&1 ||  exit 1
sync
rm /tmp/$$makefsys

if [ ! -d /${fsname} ]
then
	mkdir /${fsname}
fi
chmod 777 /${fsname}
/etc/mount ${bddrive} /${fsname}  ||  {
	admerr $0 mount of ${bddrive} on /${fsname} failed.
	exit 1
}

trap "	trap '' 1 2
	cd /;  diskumount  ${bddrive} /${fsname}" 0

echo "Initializing '${fsname}' file system."
cd /${fsname}
umask 000
mkdir lost+found
cd lost+found
# set +e
#	Populate the lost+found directory to grow it to a usable size.
i=`expr ${inodes} / 400 + 1`
while [ ${i} -gt 0 ]
do
	tee ${i}1 ${i}2 ${i}3 ${i}4 ${i}5 ${i}6 ${i}7 ${i}8 ${i}9 ${i}0 </dev/null
	i=`expr ${i} - 1;  exit 0`	# exit 0 should not be needed, but is.
done
rm -f *

if checkyn ${flags} -f -H '
	If you leave the file system mounted, then you can use it right now.
	Otherwise, this procedure will unmount it.' \
	"Do you want to leave '/${fsname}' mounted?"
then
	sync; trap 0
else
	diskumount ${bddrive}
fi
