:
# SCCSID(@(#)s85merge.sh	1.44 LCC) Modified 14:37:53 2/5/93
########################################################################
#
#	Copyright (c) 1985-1993 Locus Computing Corporation.
#	All rights reserved.
#	This is an unpublished work containing CONFIDENTIAL INFORMATION
#	that is the property of Locus Computing Corporation.
#	Any unauthorized use, duplication or disclosure is prohibited.
#
########################################################################

# On ODT systems, make sure the copyright files are in place.
# Clean up Merge temporary files.
# Find out what kind of display the console is.
# Install DOS if the DOS disk images exist.
# Make standard DOS images if they don't exist.

LIBM=/usr/lib/merge
VARMERGE=/usr/merge

STDMONOIMG=$VARMERGE/image/std/mono/mono.img
STDCGAIMG=$VARMERGE/image/std/cga/cga.img
STDVGAIMG=$VARMERGE/image/std/vga/vga.img

########################################################################
# Function definitions
######################

CLEAN_TMP()
{
	rm -f /tmp/ae[0-9][0-9]*.bat
	rm -f /tmp/save_msg.*
	return 0
}

GET_DISPLAY()
{
	if [ -d /usr/merge/ss ]
	then
		for i in `ls /usr/merge/ss`
		do
			if [ -x /usr/merge/ss/$i ]
			then
				/usr/merge/ss/$i	>> $NULL 2>&1
			fi
		done
	fi
	[ ! -x /usr/lib/merge/display ] && return 0
	if [ -c /dev/console ]
	then
		CON_DEVICE=/dev/console
	elif [ -c /dev/con ]
	then
		CON_DEVICE=/dev/con
	else
		return 0
	fi
	CON_FILE=$VARMERGE/console.disp
	/usr/lib/merge/display $CON_DEVICE > $CON_FILE 2>>$NULL
	chmod 644 $CON_FILE
	# For backwards compatibility, copy the console.disp
	# to /usr/lib/merge. (Old Xsight "startx" scripts use it.)
	if [ -w /usr/lib/merge ]
	then
		# (Don't care if this copy fails)
		cp $CON_FILE /usr/lib/merge/console.disp >>$NULL 2>&1
	fi
	return 0
}

INSTALL_DOS()
{
	[ -f /command.com ] && return 0
	if [ -f /usr/dbin/MS50.720.1.dsk -o \
	     -f /usr/dbin/MS50.360.1.dsk -o \
	     -f /usr/dbin/MS33.360.1.dsk -o \
	     -f /usr/dbin/DR60.mini.dsk  -o \
	     -f /usr/dbin/DR60.360.1.dsk -o \
	     -f /usr/dbin/DR60.12.1.dsk  ]
	then
		if [ -f /usr/merge/doing_dosinst ]
		then
			# dosinstall crashed the system the last time
			# is was run automatically.
			lmfmsg $MFILE $DM.NOT_DOSINST >>$NULL 2>&1
			if [ $? -eq 0 ]
			then
			    lmfmsg $MFILE $DM.NOT_DOSINST
			else
			    echo "Warning: Not automatically installing DOS."
			fi
			return 0
		fi
		touch /usr/merge/doing_dosinst
		touch /usr/merge/doing_mkimg
		sync
		sleep 3
		dosinstall noprompt
			# Note: dosinstall removes the file
			# "/usr/merge/doing_dosinst" when it finishes.
			# Note: mkimg (which is called by dosinstall)
			# removes the file "/usr/merge/doing_mkimg"
			# when it successfully makes images.
	fi
	return 0
}

MAKE_IMAGES()
{
	# Don't make images if they are already all made.
	[ -f $STDMONOIMG -a -f $STDCGAIMG -a -f $STDVGAIMG ] \
		&& return 0

	# Merge must be completely installed before images can be made.
	[ ! -x /usr/lib/merge/dosdisk -o ! -f /usr/dbin/boot.dsk ] \
		&& return 0

	unset MDA CGA VGA

	# DM is the domain for messages for this script.
	DM="LCC.MERGE.UNIX.S85MERGE"
	MSG1="`lmfmsg $MFILE $DM.NOT_ENUF_1 2>>$NULL`"
	[ $? -ne 0 ] && MSG1="WARNING: Did Not Make DOS Images."

	#
	# Initialize variable for needed space (For DOS 5.0)
	# Needed disk space for the DOS images: ($VARMERGE)
	   # low image + umb image = 144 blocks
	   # high image = 128 blocks.
	   # vga rom = 64 blocks
	#
	NEED=0
	# Pad in case our drivers get bigger and/or we add more
	# drivers to the image.  We would rather err on the safe
	# side in this script.
	PAD=40

	if [ ! -s $STDMONOIMG ]
	then
		MDA=mda
		NEED=`expr $NEED + 144 + 128 + $PAD`
	fi

	if [ ! -s $STDCGAIMG ]
	then
		CGA=cga
		NEED=`expr $NEED + 144 + 128 + $PAD`
	fi

	if [ ! -s $STDVGAIMG ]
	then
		VGA=vga
		# Size needed for image plus vga rom file.
		NEED=`expr $NEED + 144 + 128 + 64 + $PAD`
	fi
	# Calc needed size in tmp.
	TMPNEED=`expr 144 + 128 + 64 + $PAD`

	[ -z "${MDA}${CGA}${VGA}" ] && return 0

	HAVE_TMP=0
	HAVE_UM=0
	export NEED HAVE_TMP HAVE_UM
	#########################################################
	# Make sure there is enough space to make needed images.
	HAVE_TMP=`$LIBM/dfmerge -b /tmp`
	DEV_TMP=`$LIBM/dfmerge -n /tmp`
	HAVE_UM=`$LIBM/dfmerge -b $VARMERGE`
	DEV_UM=`$LIBM/dfmerge -n $VARMERGE`
	HAVE_UTMP=`$LIBM/dfmerge -b /usr/tmp`
	DEV_UTMP=`$LIBM/dfmerge -n /usr/tmp`

	DEV_USR=`$LIBM/dfmerge -n /usr`
	DEV_ULIB=`$LIBM/dfmerge -n /usr/lib`

	if [ "$DEV_USR" = "$DEV_UM" ]
	then
		UM_FS="/usr"
	else
		UM_FS="$VARMERGE"
	fi

	VDSK_BLKS=840
	# Two virtual disks are created in /usr/tmp. ($VDSK_BLKS)
	# If /usr/tmp is in same filesystem as $VARMERGE, add that
	# amount to the space needed for $VARMERGE.
	# When in another filesystem, check for that amount there.
	if [ "$DEV_UTMP" = "$DEV_UM" ]
	then
		NEED=`expr $NEED + $VDSK_BLKS`
	elif [ $HAVE_UTMP -lt $VDSK_BLKS ]
	then
		echo "$MSG1"
		lmfmsg $MFILE $DM.NOT_ENUF_2B /usr/tmp >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_2B /usr/tmp
		else
			echo "     Not Enough Space in /usr/tmp."
		fi
		lmfmsg $MFILE $DM.NOT_ENUF_3 $VDSK_BLKS $HAVE_UTMP >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_3 $VDSK_BLKS $HAVE_UTMP
		else
			echo "     Need: $VDSK_BLKS Blocks, Have:$HAVE_UTMP."
		fi
	fi
	#
	# If /usr and /tmp are in different file systems, then check
	# for needed space ($NEED) in /usr and in /tmp ($TMPNEED).
	# (The temporary image files are made in /tmp)
	# If both in the same file system, add TMPNEED blocks to the need.
	#
	if [ "$DEV_TMP" = "$DEV_UM" ]
	then
		# Same filesystem
		NEED=`expr $NEED + $TMPNEED`
		if [ $HAVE_UM -lt $NEED ]
		then
			echo "$MSG1"
			lmfmsg $MFILE $DM.NOT_ENUF_2A >>$NULL 2>&1
			if [ $? -eq 0 ]
			then
				lmfmsg $MFILE $DM.NOT_ENUF_2A
			else
				echo "     Not Enough Space."
			fi
			lmfmsg $MFILE $DM.NOT_ENUF_3 $NEED $HAVE_UM >>$NULL 2>&1
			if [ $? -eq 0 ]
			then
				lmfmsg $MFILE $DM.NOT_ENUF_3 $NEED $HAVE_UM
			else
				echo "     Need: $NEED Blocks, Have:$HAVE_UM."
			fi
			return 1
		fi
	fi
	if [ $HAVE_TMP -lt $TMPNEED ]
	then
		echo "$MSG1"
		lmfmsg $MFILE $DM.NOT_ENUF_2B /tmp >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_2B /tmp
		else
			echo "     Not Enough Space in /tmp."
		fi
		lmfmsg $MFILE $DM.NOT_ENUF_3 $TMPNEED $HAVE_TMP >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_3 $TMPNEED $HAVE_TMP
		else
			echo "     Need: $TMPNEED Blocks, Have:$HAVE_TMP."
		fi
		return 1
	fi
	if [ $HAVE_UM -lt $NEED ]
	then
		echo "$MSG1"
		lmfmsg $MFILE $DM.NOT_ENUF_2B $UM_FS >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_2B $UM_FS
		else
			echo "     Not Enough Space in $UM_FS."
		fi
		lmfmsg $MFILE $DM.NOT_ENUF_3 $NEED $HAVE_UM >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
			lmfmsg $MFILE $DM.NOT_ENUF_3 $NEED $HAVE_UM
		else
			echo "     Need: $NEED Blocks, Have:$HAVE_UM."
		fi
		return 1
	fi

	if [ -f /usr/merge/doing_mkimg ]
	then
		# mkimg crashed the system the last time
		# is was run automatically.
		lmfmsg $MFILE $DM.NOT_MKIMG >>$NULL 2>&1
		if [ $? -eq 0 ]
		then
		    lmfmsg $MFILE $DM.NOT_MKIMG
		else
		    echo "Warning: Not automatically creating DOS image files."
		fi
		return 0
	fi
	touch /usr/merge/doing_mkimg
	sync
	sleep 3
	#
	# If all OK then make images:
	#
	mkimg $MDA $CGA $VGA 2>>$NULL
		# Note: mkimg removes the file "/usr/merge/doing_mkimg"
		# when it successfully makes images.
	return $?
}

CHECK_COPYRIGHT()
{
	# Only check when on an ODT system.
	[ ! -f /etc/perms/bundle/odtps ] && return 0
	unset LCC_BAD
	unset MSC_BAD
	LCC_CW=\
"Copyright (C) 1985-1993 Locus Computing Corporation: DOS Merge"
	MSC_CW="Copyright (C) 1984-1989 Mouse Systems Corporation"
	LCC_FILE=/etc/copyrights/02.locus
	MSC_FILE=/etc/copyrights/08.merge386
	[ ! -f $LCC_FILE ] && LCC_BAD=1
	[ ! -f $MSC_FILE ] && MSC_BAD=1
	if [ -z "$LCC_BAD" ]
	then
		LINE=`grep -i "Locus Computing " $LCC_FILE 2>&1`
		echo "$LINE" | grep "^Copyright " >>$NULL 2>&1
		[ $? -ne 0 ] && LCC_BAD=1
		echo "$LINE" | grep "DOS Merge" >>$NULL 2>&1
		[ $? -ne 0 ] && LCC_BAD=1
	fi
	if [ -z "$MSC_BAD" ]
	then
		LINE=`grep -i "Mouse Systems " $MSC_FILE 2>&1`
		echo "$LINE" | grep "^Copyright " >>$NULL 2>&1
		[ $? -ne 0 ] && MSC_BAD=1
	fi
	if [ -n "$LCC_BAD" ]
	then
		rm -f $LCC_FILE
		echo $LCC_CW > $LCC_FILE
		chmod 444 $LCC_FILE
		cat $LCC_FILE
	fi
	if [ -n "$MSC_BAD" ]
	then
		rm -f $MSC_FILE
		echo $MSC_CW > $MSC_FILE
		chmod 444 $MSC_FILE
		cat $MSC_FILE
	fi
	return 0
}

INSTALL_XSTUFF()
{
	if [ -d $VARMERGE/xc/fonts -a -f $LIBM/xc/install/inst_font.sh ]
	then
		sh $LIBM/xc/install/inst_font.sh
		# Note: inst_font.sh removes the xc/fonts directory
		# when all the fonts install correctly.
	fi
	if [ -d $VARMERGE/xc/icon -a -f $LIBM/xc/install/inst_icon.sh ]
	then
		sh $LIBM/xc/install/inst_icon.sh
		# Note: inst_icon.sh removes the xc/icon directory
		# when all the icons install correctly.
	fi
	if [ -d $VARMERGE/xc/misc -a -f $LIBM/xc/install/inst_misc.sh ]
	then
		sh $LIBM/xc/install/inst_misc.sh
		# Note: inst_misc.sh removes the xc/misc directory
		# when all the items in the xc/misc install correctly.
	fi
	return 0
}

DOS_PARTITIONS()
{
	# Scan the disk partitions to find all the DOS partitions, and
	# create aliases for them in "/etc/dosdev", and make them writeable
	# by everyone.  The "part_set.sh" script does all of this.
	# (It also creates the script "$VARMERGE/initpart.sh" which can
	# be run after a user runs "idmkdev" to fix up the ownership
	# and links of the partition devices, because "idmkdev" can
	# change the mode of the files and might remove our links.)

	/usr/lib/merge/part_set.sh

	return 0
}

FIXUPS()
{
	# On some versions of SCO Unix, "idmknod" changes the modes
	# and ownerships of the Merge device directories to invalid
	# settings.  So here we make sure they are correct.
	chmod 755 /dev/vmouse >>$NULL 2>&1
	chmod 755 /dev/vems   >>$NULL 2>&1
	chmod 755 /dev/vdsp   >>$NULL 2>&1
	chmod 755 /dev/vkbd   >>$NULL 2>&1
	chmod 755 /dev/vsdsp  >>$NULL 2>&1
	chmod 755 /dev/vskbd  >>$NULL 2>&1

	chgrp bin /dev/vmouse >>$NULL 2>&1
	chgrp bin /dev/vems   >>$NULL 2>&1
	chgrp bin /dev/vdsp   >>$NULL 2>&1
	chgrp bin /dev/vkbd   >>$NULL 2>&1
	chgrp bin /dev/vsdsp  >>$NULL 2>&1
	chgrp bin /dev/vskbd  >>$NULL 2>&1

	chown bin /dev/vmouse >>$NULL 2>&1
	chown bin /dev/vems   >>$NULL 2>&1
	chown bin /dev/vdsp   >>$NULL 2>&1
	chown bin /dev/vkbd   >>$NULL 2>&1
	chown bin /dev/vsdsp  >>$NULL 2>&1
	chown bin /dev/vskbd  >>$NULL 2>&1
}

########################################################################
# End of function definitions
#############################

########################################################################
# Start of Main part of script
##############################

NULL=/dev/null
# The script "set_nls.sh" uses LIBM to set MFILE.
if [ ! -r $LIBM/set_nls.sh -o ! -f $VARMERGE/Version ]
then
	# Required Merge file missing.
	exit 0
fi
. $VARMERGE/Version
. $LIBM/set_nls.sh

FIXUPS
CHECK_COPYRIGHT
CLEAN_TMP
GET_DISPLAY
INSTALL_XSTUFF
INSTALL_DOS
MAKE_IMAGES
DOS_PARTITIONS
