#! /bin/sh

# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.

#	Copyright (c) 1984 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"$Header: bcheckrc.sh 1.17 90/02/13 $@(#)initpkg:./bcheckrc.sh	1.5"

echo "#! /bin/sh
# ***** This file has those commands necessary to check the file
# system, date, and anything else that should be done before mounting
# the file systems.
" >bcheckrc
if i386
then cat << 'END_BCHECKRC_TEXT' >> bcheckrc
/etc/online -a

rootfs=/dev/root
CTAB=/etc/constab
rootdev=`/etc/devnm / | sed -e 's/ .*$//'`	# Keep first field only

/etc/fsstat ${rootdev}  >/dev/null 2>&1
status=$?

if [ $status -ne 0 ]
then
	echo "\n\tfsstat: root file system (${rootdev}) needs checking."
	echo "\tThe root file system will be checked automatically.\n"
	/etc/fsck -p -b ${rootdev}
	status=$?
fi

#---- BEGIN Error Recovery
if [ $status -ne 0 ]
then

echo "\nATTENTION:\tTHE ROOT FILESYSTEM IS CORRUPTED IN A WAY THAT CANNOT BE"
echo "\t\tCORRECTED AUTOMATICALLY.  THE ROOT FILESYSTEM MUST BE"
echo "\t\tFIXED MANUALLY BY RUNNING:"

echo "\n\t\t\t/etc/fsck $rootdev\n"

echo "\nEntering /bin/sh.  Avoid unnecessary writes to the filesystem."
echo "Please type \"/etc/fsck $rootdev\" to repair the root filesystem."
echo "When repair is complete, type \"exit\" to exit the shell and"
echo "reboot the system.\n"

	PS1="(fix filesystem)# " /bin/sh

echo "\n  ***** SYSTEM RETURNING TO FIRMWARE *****"
echo "  ***** will REBOOT if AUTO enabled *****\n"
/etc/uadmin 1 4 4
exit 1
fi
#---- END Error Recovery

# create a hard link to the proper rootfs (after it checks clean)
rm -f ${rootfs}
ln `echo ${rootdev}` ${rootfs}

# add root disk to $CTAB
# If the root disk and the swap disk differ, add the swap disk as well.
echo ${rootdev} |
	sed -e 's/\/dev\/dsk\/\(.*\)s.*$/vtoc \/dev\/rdsk\/\1/' > $CTAB

swapdev=`/etc/swap -l | sed -n '2s/^\([^ ]*\) *.*$/\1/p'`
swapdisk=`echo ${swapdev} | sed 's/\([0-9][0-9]*\)s[0-9][0-9]*/\1/'`
rootdisk=`echo ${rootdev} | sed 's/\([0-9][0-9]*\)s[0-9][0-9]*/\1/'`

if [ "${rootdisk}" != "${swapdisk}" ]
then
	echo ${swapdev} |
	sed -e 's/\/dev\/dsk\/\(.*\)s.*$/vtoc \/dev\/rdsk\/\1/' >> $CTAB
fi

# set the system nodename
if [ -f /etc/nodename ]
then
	/bin/sh /etc/nodename
fi
END_BCHECKRC_TEXT
else echo "


#!	chmod +x \${file}
trap \"\" 2

# **** Check date

while :
do
	echo \"Is the date \`date\` correct? (y or n) \\\c\"
	read reply
	if
		[ \"\$reply\" = y ]
	then
		break
	else
		echo \"Enter the correct date:  \\\c\"
		read reply
		date \"\$reply\"
	fi
done

# ***** Auto check, if necessary

while :
do
	echo \"Do you want to check the file systems? (y or n) \\\c\"
	read reply
	case \"\$reply\" in
		 y )
			;;

		n* )
			break
			;;

		* )
			echo \"Invalid input. Try again.\"
			continue
			;;
	esac
	trap \"echo Interrupt\" 2
	/etc/fsck
	trap \"\" 2
	break
done
" >>bcheckrc
fi


echo "
echo \${rootdev} / | /etc/setmnt	# put root into mount table
exit 0
" >> bcheckrc
