:
#
# 
# 
#   @(#)raminit	1.5 Copyright (C) B.M.Goodheart 1987, 1888, 1989, 1991 
# 
# 
# 
#   		      DISCLAIMER OF WARRANTY. 
#   This software is distributed "as is" and without warranties as to 
#   performance of merchantability or any other warranties whether expressed 
#   or implied. In no event shall the author (the copyright holder) be held 
#   liable for any loss of profit or any other commercial damage resulting 
#   from the use or misuse of this software, including but not limited to 
#   special, incidental, consequential or other damages.  The user must 
#   assume the entire risk of using this software.
# 
# 
# 			LISCENSE AGREEMENT
# 	This software is placed	into the public domain and 
# 	may be copied or distributed freely provided no profit or 
# 	gain is made and is used for personal use only and the code 
# 	as distributed retains all copyright notices in the code. 
# 	This includes any copyright statements in the target
# 	binary and executable code produced from the distributed source. 
# 
# 
# 	                DISTRIBUTION NOTE
#  	This file contains the code for a RAM disk driver for UNIX System 5.2
# 	and System V.3.	It has been designed specifically for Microport's 
# 	System V/AT iAPX286 and Interactive Systems V/386 i386 systems
#  	but should work with other PC based System V.? ports with only 
# 	minor modifications, specifically the BUFSEL selector on the 80286.
# 	This driver should work on any V.3 implementation on a PC. 
# 
# 
# 	               IMPORTANT NOTE !!!
# 	Any files or data stored within the ram disks will be 
# 	LOST FOR EVER if the system is brought down for ANY reason.
# 
# 	There are also two other programs distributed associated 
# 	with the driver, they are ramstat(1M) and raminit(1M). 
# 	These are used to control the ram disk driver in user mode 
# 	See also ram(7).
# 
# 	Berny Goodheart (berny@tndsyd.oz@munnari.oz.au)
# 
#
# initialise or remove ram disks ( reads /etc/ramtab )
#
#

trap "echo Interrupt ignored" 1 2 3 15

RAMRC=/etc/ramtab
view=0

if test $# -lt 1 
then
	echo "Usage: raminit -ir [ -v ]"
	exit
fi

if test $# -eq 2
then 
	if [ "$2" != "-v" ]
	then
		echo "Usage: raminit -ir [ -v ]"
		exit
	fi
	view=1
fi

case $1 in
	"-v")
		echo "Usage: raminit -ir [ -v ]"
		exit
		;;
	"-i")

		for x in `/bin/cat $RAMRC | /bin/grep "^\/"`
		do
			block=
			raw=
			size=
			inodes=
			label=
			mnt=
			rnum=

			block=`/bin/echo $x | /usr/bin/awk -F: '{print $1}'`
			raw=`/bin/echo $x | /usr/bin/awk -F: '{print $2}'`
			size=`/bin/echo $x | /usr/bin/awk -F: '{print $3}'`
			inodes=`/bin/echo $x | /usr/bin/awk -F: '{print $4}'`
			label=`/bin/echo $x | /usr/bin/awk -F: '{print $5}'`
			mnt=`/bin/echo $x | /usr/bin/awk -F: '{print $6}'`
			rnum=`/bin/echo $x | /usr/bin/awk -F: '{print $7}'`


			if [ "$rnum" = "" ]
			then
				echo "$0: error in /etc/ramtab, can't continue"
				exit 1
			fi

			# test if the driver is already open
			ramstat -n${rnum} | grep open > /dev/null
			if [ $? = 0 ]
			then
				continue
			fi

			if test -f $mnt/*
			then
				echo "$0: cannot mount on \"$mnt\", directory is not empty"
				continue
			elif test ! -d $mnt
			then
				echo "$0: Directory \"$mnt\" does not exist, entry ignored"
				continue
			else
				:
			fi

			if [ "$block" = "" ]
			then
				echo "$0: Block device not specified, entry ignored"
				continue
			fi

			if [ "$raw" = "" ]
			then
				echo "$0: Raw device not specified, entry ignored"
				continue
			fi

			if [ "$size" = "" ]
			then
				echo "$0: Device size not specified, entry ignored ***"
				continue
			fi

			if [ $view = 1 ]
			then
				echo "Trying for Space for $size * \c"
				if i386
				then
					echo "4k byte blocks"
				else
					echo "1k byte blocks"
				fi
			fi

			# test if we have enough space
			/bin/ramstat -i $size $raw
			if [ $? != 0 ]
			then
				exit
			fi

			echo "Please Wait ...."

			if i386
			then
				size=`expr ${size} \* 4096`
				size=`expr ${size} / 1024`
				inodes=`expr ${size} / 10`
			fi

			if [ $view = 1 ]
			then
				echo "Making file system on $block"
				echo "size = $size, inodes = $inodes\n"
			fi

			/etc/mkfs $block $size:$inodes > /dev/null
			if [ "$label" != "" ]
			then
				if [ "$mnt" != "" ]
				then
					j=`basename $mnt`
					if [ $view = 1 ]
					then
						echo "Labeling file system $label\n"
					fi
					/etc/labelit $block ${j} $label > /dev/null
				fi
			fi

			/etc/fsck $block
			if [ "$mnt" != "" ]
			then
				if [ $view = 1 ]
				then
					echo "\nMounting file system on $mnt\n"
				fi
				/etc/mount $block $mnt
			fi
		done
		;;
	"-r")
		ramstat | grep open > /dev/null
		if [ $? != 0 ]
		then
			exit
		fi

		for x in `/bin/cat $RAMRC | /bin/grep "^\/"`
		do
			block=
			raw=
			mnt=
			rnum=

			block=`/bin/echo $x | /usr/bin/awk -F: '{print $1}'`
			raw=`/bin/echo $x | /usr/bin/awk -F: '{print $2}'`
			mnt=`/bin/echo $x | /usr/bin/awk -F: '{print $6}'`
			rnum=`/bin/echo $x | /usr/bin/awk -F: '{print $7}'`

			if [ "$rnum" = "" ]
			then
				echo "$0: error in /etc/ramtab, can't continue"
				exit 1
			fi

			# test if the driver is open
			ramstat -n${rnum} | grep open > /dev/null
			if [ $? != 0 ]
			then
				continue
			fi

			if [ "$block" = "" ]
			then
				echo "$0: Block device not specified, entry ignored ***"
				continue
			fi

			if [ "$raw" = "" ]
			then
				echo "$0: Raw device not specified, entry ignored ***"
				continue
			fi

			if [ "$mnt" = "" ]
			then
				/bin/ramstat -r $block $raw
			else
				if /etc/umount $block 
				then
					/bin/ramstat -r $block $raw
				fi
			fi
		done
		;;
	*) /bin/echo "$0: illegal option $1" ;;
esac

