#!/sbin/runscript
. /etc/mrgssv.sh

depend() {
	after *
}

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

Work_Around() {
	[ "_${SYSTEMTYPE} != "_linux" ] && return
	# In certain situations 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 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
}

start() {
	if [ "_${SYSTEMTYPE}" != "_linux" ]
	then
		ebegin "Starting ${MERGENAME}"
	else
		ebegin "Starting ${MERGENAME}:"

	CLEAN_TMP
	sh ${STATICMERGE}mrg_setup.sh
	Work_Around
	eend $?
}


stop() {
	if [ "_${SYSTEMTYPE}" != "_linux" ]
	then
		ebegin "Stopping ${MERGENAME}"
		eend $?
		return
	fi

	ebegin "Stopping ${MERGENAME}:"
	( cd ${STATICMERGE}/drivers ; sh tools/unloadem )
	${STATICMERGE}/rlockshm -r > /dev/null 2>&1
	eend $?
}