#!/sbin/sh

#
#	Copyright (c) 1994, Sun Microsystems Inc.
#

#ident	"@(#)cacheos	1.22	94/11/15 SMI"

cocdevconfig() {
	#
	# drvconfig stuff needs /usr/kvm mounted, so we
	# mount it now.  It will get mounted again in S40,
	# after we have done the overlay mount of /usr
	#
	exec < ${vfstab}; readvfstab "/usr/kvm"
	if [ "${mountp}" ]; then
		if [ "${fstype}" = "cachefs" ]; then
			case "$mntopts" in
			*backfstype=nfs*)
				cfsbacktype=nfs
				;;
			*backfstype=hsfs*)
				cfsbacktype=hsfs
				;;
			*)
				echo "invalid vfstab entry for /usr/kvm"
				cfsbacktype=nfs
				;;
			esac
			/sbin/mount -m -F ${cfsbacktype} -o ro ${special} ${mountp}
		else
			/sbin/mount -m -o ro /usr/kvm
		fi
	fi
	_INIT_RECONFIG="set"; export _INIT_RECONFIG
	/sbin/sh /etc/init.d/drvconfig
	/sbin/sh /etc/init.d/devlinks
	_INIT_RECONFIG=""; export _INIT_RECONFIG
	if [ -f /reconfigure ]
	then
		/usr/bin/rm -f /reconfigure
	fi
	/sbin/umount /usr/kvm >/dev/null 2>&1
}

dodots() {
	echo "Configuring cache and swap:\c"
	(while :
	 do
		sleep 6
		echo ".\c"
	 done)&
	dotpid=$!
}

coc_runprofile() {
	# process the disk configuration profile
	if [ -f /.autoinstall/preinstall ]; then
		/sbin/sh /.autoinstall/preinstall
		/usr/bin/mv /.autoinstall/preinstall /.autoinstall/preinstall.done
	fi
	if [ -f /.autoinstall/profile ]; then
		exec < ${vfstab}; readvfstab "/dev/fd"
		if [ "${mountp}" -a -d /dev/fd ]; then
			/sbin/mount -m /dev/fd >/dev/null 2>&1
		fi
		SWAPSIZE=`/sbin/bpgetfile swapsize | \
			(read junk size junk; echo $size)`
		SYS_SWAPSIZE=${SWAPSIZE}; export SYS_SWAPSIZE
		/usr/sbin/install.d/cosinstall -c none \
			/.autoinstall/profile >/dev/null
		if [ $? -ne 0 ]; then
			fatal_err "Error in disk configuration."
		fi
		/sbin/umount /dev/fd >/dev/null 2>&1
	fi
}

get_cachedev() {
	# get cache device from vfstab
	exec < ${vfstab}; readvfstab "$CACHEMNT"
	CACHEDEV=${special}
	RCACHEDEV=${fsckdev}
	if [ -z "$CACHEDEV" -o -z "$RCACHEDEV" ]; then
		#
		# this is either a bug in cosinstall or the user made
		# a bad modification to vfstab.  Not much we can do
		# here
		#
		fatal_err "Could not find $CACHEMNT entry in vfstab!"
	fi
	/usr/lib/fs/cachefs/cfstagchk $RCACHEDEV >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		fatal_err "Improperly tagged cache device!"
	fi
}

fatal_err() {
	if [ $dotpid -ne 0 ]; then
		kill $dotpid >/dev/null 2>&1
		kill $dotpid >/dev/null 2>&1
	fi
	echo "\nFATAL: $1"
	/usr/sbin/halt
}

#
# See if the root file system type is "cachefs".  If so, we do the
# work necessary for caching root and /usr on a "cache-only-client".
# We either create a new cache or validate an existing cache, and
# get the cache "front file system" mounted so that everything is
# in place when S40standardmounts runs and does (re)mounts using cachefs.
#
ROOTFS=`/usr/lib/fs/cachefs/cfsfstype -r 2>/dev/null`
if [ "$ROOTFS" = "cachefs" ]
then
	>/etc/mnttab
	dotpid=0
	CACHEMNT=/.cache
	CACHEDIR=$CACHEMNT/rootcache
	INSTMARKER=/COSINSTALL
	newcache=0
	cfsfsckflags=""
	if [ -f $INSTMARKER -o "${_CACHEFS_FLUSHCACHE}" = "YES" ]; then
		newcache=1
	fi
	if [ $newcache -ne 0 ]; then
		cocdevconfig
		dodots
		coc_runprofile
		get_cachedev
		/usr/bin/rm -f $INSTMARKER
	else
		dodots
		get_cachedev
		#
		# check and mount the cache
		#
		/usr/sbin/fsck -m $RCACHEDEV >/dev/null 2>&1
		fsckerr=$?
		if [ $fsckerr -ne 0 ]
		then
			/usr/sbin/fsck -o p $RCACHEDEV >/dev/null 2>&1
			fsckerr=$?
			cfsfsckflags="-o noclean"
		fi

		if [ $fsckerr -ne 0 ]; then
			> $INSTMARKER
			fatal_err "Could not fsck $RCACHEDEV."
		fi
		/usr/sbin/mount -m $CACHEDEV $CACHEMNT
		if [ $? -ne 0 ]; then
			fatal_err "Could not mount cache device $CACHEDEV."
		fi
	fi
	if [ -d $CACHEDIR ]; then
		/usr/sbin/fsck -F cachefs $cfsfsckflags $CACHEDIR
		if [ $? -ne 0 ]; then
			/usr/bin/rm -rf $CACHEDIR
		fi
	fi
	if [ ! -d $CACHEDIR ]; then
		/usr/lib/fs/cachefs/cfsadmin -c $CACHEDIR
		if [ $? -ne 0 ]; then
			fatal_err "Could not create cache in $CACHEDIR."
		fi
	fi
	kill $dotpid >/dev/null 2>&1
	kill $dotpid >/dev/null 2>&1
	echo "done."
	mntlist="${mntlist}$CACHEDEV $CACHEMNT\n"
fi
