:
# $Id: S55merge.sh,v 1.3 2002/11/26 22:37:54 rwb Exp $ 
# SCCSID(@(#)S55merge.sh	1.79) Modified 12:55:35 7/20/01
########################################################################
#	Copyright (c) 1985-2002 NeTraverse, Inc.
########################################################################

# chkconfig: 2345 77 20
# description: Do the system setup for Win4Lin/Merge that is required 
# each time the system is rebooted.

NULL=/dev/null
export NULL

# Get the SYSTEMTYPE, STATICMERGE and VARMERGE settings.
. /etc/mrgssv.sh

if [ "_${SYSTEMTYPE}" = "_linux" -a -f /etc/rc.d/init.d/functions ]
then
	. /etc/rc.d/init.d/functions
	GOT_FUNCTIONS=TRUE
else
	GOT_FUNCTIONS=FALSE
fi

CLEAN_TMP()
{
	[ -z "$VARMERGE" ] && return
	find $VARMERGE/tmp -atime +1  -type f -exec rm {} \;
	return
}

Work_Around()
{
	[ "_${SYSTEMTYPE}" != "_linux" ] && return
	# In certain stituations on Linux systems the Win4Lin installation
	# script does not complete.
	# If it looks like that situation then re-run the installation script
	# and force the making of the images.  (Even though the images
	# should have been automatically created in this situation.)

	# When the images are not there but Windows files are loaded,
	# then re-run the install script and and then explicitly make images.
	[ -r "$VARMERGE"/image/std/mono/mono.img ] && return
	[ ! -r "$VARMERGE"/dosroot/wincabs/info.txt ] && return

	# Don't continue if the modules are not loaded.
	modules=`/sbin/lsmod|grep Mmerge`
  	[ "x$modules" = "x" ] && return

	[ -r "$VARMERGE"/postinst-running ] &&  return
	touch "$VARMERGE"/postinst-running
	sh "$STATICMERGE"/postinst_rpm.sh  \
		> "$VARMERGE"/log/postinst-boot.log 2>&1
	ls -l "$VARMERGE"/image/std/* >"$VARMERGE"/log/mkimg-boot.log 2>&1
	/usr/bin/mkimg >> "$VARMERGE"/log/mkimg-boot.log 2>&1
	rm -f "$VARMERGE"/postinst-running
}


main_func()
{
    case "$1" in
    start)
	if [ "_${SYSTEMTYPE}" != "_linux" ]
	then
		echo -n "Starting $MERGENAME ... "
	else
		echo -n "Starting $MERGENAME: "
	fi
	CLEAN_TMP
	sh $STATICMERGE/mrg_setup.sh
	Work_Around

	# Indicate "OK"
	if [ "$GOT_FUNCTIONS" = TRUE ]
	then
		success
		echo
	else
		echo "done."
	fi
	;;
    stop)
	if [ "_${SYSTEMTYPE}" != "_linux" ]
	then
		# Don't do anything for Non-Linux systems
		echo -n "Stopping $MERGENAME ... "
		echo "done."
		return
	fi
	echo -n "Stopping $MERGENAME: "

	# On Linux, if this is final shutdown/reboot, don't bother
	# doing anything, since we are shutting down/rebooting.
	if [ "$RUNLEVEL" != "0" -a "$RUNLEVEL" != "6" ]
	then
		# Unload the drivers and remove the shared memory segment
		( cd $STATICMERGE/drivers ; sh tools/unloadem )
		$STATICMERGE/rlockshm -r > /dev/null 2>&1
	fi
	# Indicate "OK"
	if [ "$GOT_FUNCTIONS" = TRUE ]
	then
		success
		echo
	else
		echo "done."
	fi
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
	;;
    esac
    return
}

main_func $1

exit 0
