#!/bin/sh
# $Id: mkimg.sh,v 1.4 2003/04/22 23:48:00 rwb Exp $ 
# SCCSID(@(#)mkimg	1.146) Modified 12:42:00 10/16/01

#****************************************************************************
# Script to make the Merge DOS image files.
#****************************************************************************
#	Copyright (c) 1985-2000 NeTraverse, Inc.
#****************************************************************************
# New mkimg options for DOS 5.0 support.
# New feature: "use-dosdev".  This feature makes sure that
# DOS drivers are not loaded in the UMB area where a memory mapped io
# device might latter need to be attached.  Mkimg does this by looking at
# the /etc/dosdev file for direct attach memory mapped io definitions, and
# preventing drivers from being loaded at the defined memory locations when
# making the DOS images.
#
# There are two new options disable "use-dosdev":
#
# no_dosdev	(NO_DOSDEV variable)
#		Turn off the "use-dosdev" feature.
#		This option is not to be documented for end users.
# devicelow	(DEV_LOW variable)
#		Edit config.sys to change any "devicehigh" lines to "device"
#		lines.  This results in no drivers not being loaded in
#		the UMB area as part of the DOS image.
#		This option also disables "use-dosdev", as if the "no_dosdev"
#		option was used.
#****************************************************************************
cd /
unset ONUNIX

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

DOS_ROOT=$VARMERGE/dosroot
DOSROOTDOS=${DOS_ROOT}/dos

#
# Disable HD0 emulation, since it might get in the way
#
MERGE_DISABLE_HD0_EMULATION=on
export MERGE_DISABLE_HD0_EMULATION

IMAGE_ROOT=$VARMERGE/image
if [ ! -d $IMAGE_ROOT ]
then
	mkdir -p $IMAGE_ROOT
	chmod 775 $IMAGE_ROOT
	chgrp bin $IMAGE_ROOT
	chown bin $IMAGE_ROOT
fi
DEFAULT_IMAGEDIR=$IMAGE_ROOT/std

# The script "set_nls.sh" uses STATICMERGE to set MFILE.
. $STATICMERGE/set_nls.sh

# DM is the domain for messages for this script.
DM="LCC.MERGE.UNIX.MKIMG"
NULL=/dev/null
> $NULL
export NULL
STD_VGAROM=$STATICMERGE/std_vga.rom
VIDEO_ROM=
THISPROG=$0
VGAFLAG=
CGAFLAG=
MDAFLAG=
VISUALMKIMG=
VISUAL2=
CONSOLE=
UNKNOWN=
NATIVE=
NONATIVE=
VGAOK=
NEW_ROM=
DEV_LOW=
NO_DOSDEV=
UMB_OPTION=
PROBLEMS=0
ALLOPTIONS=0
JDOS=0
KOPT="+kmerge-internal-config:snap"
BG_ID=

# Force the home drive to be C and the unix root drive to be D,
# because until we can implement the J drive without using subst,
# snap.asm uses this assumtion.
DOS_HOME_DRIVE=c
DOS_UROOT_DRIVE=d
MERGE_CLASSIC_DRIVES=off
export DOS_HOME_DRIVE DOS_UROOT_DRIVE MERGE_CLASSIC_DRIVES

EXIT_NOW()
{
	rm -f $VARMERGE/doing_mkimg
	rm -f $VARMERGE/auto_mkimg
	if [ $1 -eq 0 ]
	then
		usrMsg ECHO MKIMG_SUCCESS "Images creation completed successfully.\n"
	else
		usrMsg WARN MKIMG_FAILED "Images creation failed.\n"
		sleep 3
	fi
	exit $1
}

# TMP_DIR is where we put our temporary files, and where "snap"
# creates the image files.
TMP_DIR=${DOS_ROOT}/mkimgtmp
export TMP_DIR
if [ ! -d ${TMP_DIR} ]
then
	# This should never happen.  So not much need to NLSize this message.
	# This check is needed because very bad things can happen if the
	# directory is missing.
	echo "Error: Missing ${TMP_DIR}"
	EXIT_NOW 1
fi

# Function "Exit_Clean"
# Cleans up and then exits with the code in arg $1
Exit_Clean()
{
	trap 1 2 3 15
	if [ -d ${TMP_DIR} ]
	then
		cd ${TMP_DIR}
		rm -f a *
	fi
	if [ "x$BG_ID" != "x" ]
	then
		if [ $BG_ID -gt 10 ]
		then
			kill $BG_ID 2>/dev/null
		fi
	fi
	EXIT_NOW $1
}

interruptProc()
{
	trap 1 2 3 15
	usrMsg LOG INST_CANCEL "Installation cancelled by the user.\n"
	Exit_Clean 2
}
trap interruptProc 1 2 3 15

# Do a sync and sleep here to give enough time so that later checks
# for filesystem space are more likely to usefully accurate.
sync
sleep 2

TMPFILE=${TMP_DIR}/mkimg$$
TMPCONFIG=${TMP_DIR}/config$$
TMPAUTOE=${TMP_DIR}/autoe$$
ERR_FILE=${TMP_DIR}/err$$

# "ROM snap" directory and filenames.
RS_DIR=${TMP_DIR}
RS_FILE=${RS_DIR}/rs$$

# Virtual floppy directory and filenames.
VFLP_DIR=${TMP_DIR}
VFLOP=${VFLP_DIR}/vflp$$
VSNAP=${VFLP_DIR}/vsnp$$


MERGEFILE=/etc/default/merge

if [ -c /dev/con ]
then
	CONSOLE=/dev/con	### CT console device
else
	CONSOLE=/dev/console
fi

# determine whether it is Japanese dos
grep 'jdos\.dsk' $MERGEFILE >>$NULL 2>&1
if [ $? = 0 ]
then
   JDOS=1 
else
   JDOS=0 
fi


touch $VARMERGE/doing_mkimg >> $NULL 2>&1


# Function "Writeable"
# Return 0 if able to write to the file (arg $1)
# Does not change the time stamp an existing file.
# Does not leave a zero length file if the file did not exist.
Writeable()
{
	RETVAL=1
	if [ -f $1 ]
	then
		[ -w $1 ] && RETVAL=0
	else
		eval "> $1" 2>> $NULL
		[ -w $1 ] && RETVAL=0
		rm -f $1
	fi
	return $RETVAL
}

# Space consumed by image files:
#   low image + umb image = 144 blocks
#   high image = 128 blocks.
#   vga rom = 64 blocks
# So for mono or cga, 272 blocks.
# So for vga, 336 blocks.

# Function Check_Room
# Checks for needed space.
# Zero return means ok,  non-zero means not enough room.
Check_Room()
{
	# Make sure there is enough space.
	# First parameter is the directory.
	# Second parameter is the number of blocks needed in the directory.
	#
	WHERE=$1
	NEED=$2
	export HOWMUCH
	# Do a sync so that the statfs that dfmerge does returns
	# accurate numbers.
	sync
	Blocks=`$STATICMERGE/dfmerge -b $WHERE`
	[ $Blocks -lt $NEED ] && return 1
	return 0
}

Copy_To_Vsnap()
{
	# First parameter: file to copy.
	# Second parameter: name to copy it to on the virtual floppy.
	echo "copyto $2 $1" | $STATICMERGE/dosdisk -s $VSNAP
	return 0
}

Make_config_mki()
{
	if [ -f $IMAGE_ROOT/config.mki ]
	then
		# Already made.
		return
	fi
	# Need the correct config.mki file to make images.
	if [ -f  $STATICMERGE/for_image/$MKIFILE ]
	then
		cp $STATICMERGE/for_image/$MKIFILE $IMAGE_ROOT/config.mki
	else
		usrMsg ERROR MISSING \
			"Missing file $STATICMERGE/for_image/$MKIFILE\n" \
			$STATICMERGE/for_image/$MKIFILE
		# Use the lowest common denominatior mki file (if it exists).
		if [ ! -f  $STATICMERGE/for_image/config3.mki ]
		then
			usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
			usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
			Exit_Clean 1
		fi
		usrMsg ECHO USING_INSTEAD \
			"Using $STATICMERGE/for_image/config3.mki instead.\n" \
			$STATICMERGE/for_image/config3.mki
		cp $STATICMERGE/for_image/config3.mki $IMAGE_ROOT/config.mki
	fi
	chmod 664 $IMAGE_ROOT/config.mki
	chgrp bin $IMAGE_ROOT/config.mki
	chown bin $IMAGE_ROOT/config.mki
}

Determine_displaycmd()
{
	DISPLAYCMD=
	[ "$1" != "vga" ] && return
	[ "x$DOS_NO_NLS_IMAGE" = "xon" ] && return

	[ "x$DOS_VGA_HARDWARE_CODEPAGE" = "x" ] && return
	DISPLAYCMD="device=display.sys con=(ega,${DOS_VGA_HARDWARE_CODEPAGE},1)"
}

Make_Config_File()
{
	# Put together a config.sys file to put on the snap disk.
	# Create the config.sys by concatinating "config.mki"
	# with a "config.sys".
	# First look for "config.mki" in the image subdirectory.
	# If it is not in the subdirectory then use the file from
	# the IMAGE_ROOT ("$VARMERGE/image").  The "config.sys"
	# file always comes from the image subdirectory.
	# Note: The first parameter to this function is the image type.

	CONFIG_MKI=$DIRECTORY/$1/config.mki
	if [ ! -f $CONFIG_MKI -o ! -r $CONFIG_MKI -o ! -s $CONFIG_MKI ]
	then
		CONFIG_MKI=$IMAGE_ROOT/config.mki
		if [ ! -f $CONFIG_MKI -o ! -r $CONFIG_MKI -o ! -s $CONFIG_MKI ]
		then
			usrMsg ECHO CANNOT_COPY \
				"Cannot copy $CONFIG_MKI file.\n" $CONFIG_MKI
			return 1
		fi
	fi

	CONFIG_SYS=$DIRECTORY/$1/config.sys
	if [ ! -f $CONFIG_SYS -o ! -r $CONFIG_SYS -o ! -s $CONFIG_SYS ]
	then
		CONFIG_SYS=
	fi

	Determine_displaycmd $1

	rm -f $TMPCONFIG
	# Do DOS to UNIX conversion to get rid of any trailing ^Z.
	# (Note: use our private copy of charconv, because an installation of
	# PCI might have overwritten an installed an incompatable version.)
	$STATICMERGE/charconv -d -x -z $CONFIG_MKI > $TMPCONFIG 2>>$NULL

	if [ "x${DISPLAYCMD}" != "x" ]
	then
		echo "${DISPLAYCMD}" >> $TMPCONFIG
	fi

	if [ -n "$CONFIG_SYS" ]
	then	
		$STATICMERGE/charconv -d -x -z $CONFIG_SYS > $TMPFILE 2>>$NULL
		cat $TMPFILE >> $TMPCONFIG
	fi

	# When DEV_LOW is set:	Edit config.sys to change any "devicehigh"
	#	lines to "device" lines.  This results in those drivers
	#	not being loaded in the UMB area.
	if [ -n "$DEV_LOW" ]
	then
		sed "s/devicehigh/device/g" $TMPCONFIG > $TMPFILE
		sed "s/DEVICEHIGH/DEVICE/g" $TMPFILE > $TMPCONFIG
	fi

	# Do a UNIX to DOS conversion.
	cp $TMPCONFIG $TMPFILE
	$STATICMERGE/charconv -p -x $TMPFILE $TMPCONFIG > /dev/null 2>&1
	rm -f $TMPFILE

	cat $TMPCONFIG >> $NULL

	PSN=`$STATICMERGE/charconv -d -x "$TMPCONFIG" \
		| grep part.sys \
		| sed -e 's/ //g' \
		| sed -e 's/=/foo/g' \
		| sed -e 's/part.sys/=/' \
		| cut -d= -f2 \
		`
	PSN=`echo $PSN | sed -e 's/ / + /g'`
	PSN=`expr $PSN`
	UPDATE_IT=yes
	if [ "$1" = "vga" ]
	then
		grep "^MERGE_VGA_DRIVES=" "$MERGEFILE" > "$TMPFILE" 2>> "$NULL"
		if [ -s "$TMPFILE" ]
		then
			. "$TMPFILE"
			[ $MERGE_VGA_DRIVES -eq $PSN ] && UPDATE_IT=no
		fi
		if [ "$UPDATE_IT" = "yes" ]
		then
			grep -v "^MERGE_VGA_DRIVES=" "$MERGEFILE" > "$TMPFILE"
			echo "MERGE_VGA_DRIVES=$PSN" >> "$TMPFILE"
			cp "$TMPFILE" "$MERGEFILE"
		fi
	elif [ "$1" = "cga" ]
	then
		grep "^MERGE_CGA_DRIVES=" "$MERGEFILE" > "$TMPFILE" 2>> "$NULL"
		if [ -s "$TMPFILE" ]
		then
			. "$TMPFILE"
			[ $MERGE_CGA_DRIVES -eq $PSN ] && UPDATE_IT=no
		fi
		if [ "$UPDATE_IT" = "yes" ]
		then
			grep -v "^MERGE_CGA_DRIVES=" "$MERGEFILE" > "$TMPFILE"
			echo "MERGE_CGA_DRIVES=$PSN" >> "$TMPFILE"
			cp "$TMPFILE" "$MERGEFILE"
		fi
	elif [ "$1" = "mono" ]
	then
		grep "^MERGE_MDA_DRIVES=" "$MERGEFILE" > "$TMPFILE" 2>> "$NULL"
		if [ -s "$TMPFILE" ]
		then
			. "$TMPFILE"
			[ $MERGE_MDA_DRIVES -eq $PSN ] && UPDATE_IT=no
		fi
		if [ "$UPDATE_IT" = "yes" ]
		then
			grep -v "^MERGE_MDA_DRIVES=" "$MERGEFILE" > "$TMPFILE"
			echo "MERGE_MDA_DRIVES=$PSN" >> "$TMPFILE"
			cp "$TMPFILE" "$MERGEFILE"
		fi
	fi
	rm -f "$TMPFILE"

	Copy_To_Vsnap "$TMPCONFIG" config.sys
	RETVAL=$?
	rm -f $TMPCONFIG
	if [ $RETVAL -ne 0 ]
	then
		usrMsg ECHO CANNOT_COPY \
			"Cannot copy config.sys file.\n" config.sys
		return 1
	fi
	return 0
}

Determine_nlsfunc()
{
	# Contruct the nls command to put in autoexec.bat.
	# Put the command in a temporary file, and use the NLSFUNC_FN
	# variable to return the filename.  When not using nlsfunc,
	# then return with NLSFUNC_FN being null.
	#
	# The rules for when we are to use nlsfunc:
	# For all other types, use it when DOS_IMAGE_NLSFUNC is set to "on".

	NLSFUNC_FN=
	[ "x$DOS_NO_NLS_IMAGE" = "xon" ] && return
	[ "x$DOS_IMAGE_NLSFUNC" != "xon" ] && return

	# Contruct an nlsfunc command.  (The parameter is the country file.)

	NLSFUNC_FN=${TMP_DIR}/nls$$
	rm -f $NLSFUNC_FN

	# HARDWIRED J DRIVE !!!!!
	echo 'nlsfunc J:\\country.sys' > $NLSFUNC_FN
}

Determine_modecmd()
{
	MODECMD=
	[ "$1" != "vga" ] && return
	[ "x$DOS_NO_NLS_IMAGE" = "xon" ] && return

	[ "x$DOS_VGA_SOFT_CODEPAGE" = "x" ] && return
	MODECMD="mode con codepage prepare=((${DOS_VGA_SOFT_CODEPAGE})ega.cpi)"
}

Make_AE_File()
{
	# Put together a autoexec.bat file to put on the snap disk.
	# Create the autoexec.bat by concatinating "autoexec.mki"
	# with an "autoexec.bat".
	# First look for "autoexec.mki" in the image subdirectory.
	# If it is not in the subdirectory then use the file from
	# the IMAGE_ROOT ("$VARMERGE/image").  The "autoexec.bat"
	# file always comes from the image subdirectory.
	# Note: The first parameter to this function is the image type.

	Determine_nlsfunc $1
	Determine_modecmd $1

	AUTOE_MKI=$DIRECTORY/$1/autoexec.mki
	if [ ! -f $AUTOE_MKI -o ! -r $AUTOE_MKI -o ! -s $AUTOE_MKI ]
	then
		AUTOE_MKI=$IMAGE_ROOT/autoexec.mki
		if [ ! -f $AUTOE_MKI -o ! -r $AUTOE_MKI -o ! -s $AUTOE_MKI ]
		then
			AUTOE_MKI=
		fi
	fi

	AUTOE_BAT=$DIRECTORY/$1/autoexec.bat
	if [ ! -f $AUTOE_BAT -o ! -r $AUTOE_BAT -o ! -s $AUTOE_BAT ]
	then
		AUTOE_BAT=
	fi

	rm -f $TMPAUTOE

	# Now construct the autoexec file.

	# Do DOS to UNIX conversion to get rid of any trailing ^Z.
	if [ -n "$AUTOE_MKI" ]
	then	
		$STATICMERGE/charconv -d -x -z $AUTOE_MKI > $TMPFILE 2>>$NULL
	fi
	if [ "x$NLSFUNC_FN" != "x" ]
	then
		cat $NLSFUNC_FN >> $TMPFILE 2>>$NULL
		rm -f $NLSFUNC_FN
	fi
	if [ "x${MODECMD}" != "x" ]
	then
		echo "${MODECMD}" >> $TMPFILE
	fi
	if [ -n "$AUTOE_BAT" ]
	then	
		$STATICMERGE/charconv -d -x -z $AUTOE_BAT >> $TMPFILE 2>>$NULL
	fi
	if [ ! -s $TMPFILE ]
	then
		# Nothing to do.
		return 0
	fi

	# Do a UNIX to DOS conversion.
	$STATICMERGE/charconv -p -x $TMPFILE $TMPAUTOE > /dev/null 2>&1
	rm -f $TMPFILE

	Copy_To_Vsnap "$TMPAUTOE" autoexec.bat
	RETVAL=$?
	rm -f $TMPAUTOE
	if [ $RETVAL -ne 0 ]
	then
		usrMsg ECHO CANNOT_COPY \
			"Cannot copy autoexec.bat file.\n" autoexec.bat
		return 1
	fi
	return 0
}

Get_devattach()
{
	if [ -n "$NO_DOSDEV" ]
	then
		# When "no_dosdev" was specified (NO_DOSDEV=1),
		# then don't look for devices in the administration.
		return
	fi
	if [ -n "$DEV_LOW" ]
	then
		# When "devicelow" was specified (DEV_LOW=1), then no
		# need to look in the administration for memory
		# ranges to reserve from having devices put there.
		return
	fi
	# Since the attach list was not specified (with +aXXX), look
	# through /etc/dosdev for direct attach devices, and put the
	# them in the attach list.  With the "exclude_umb_only" flag,
	# $DOSWINBINDIR/dos will not actually attach them, but will only use
	# the memory map range info (if any) to tell the xms driver
	# to avoid those ranges.
	UMB_OPTION=-i
}

Format_Vflop()
{
	# Create virtual snap disk
	rm -f $VFLOP
	cp $DOSROOTDOS/boot.dsk $VFLOP
}

Create_Vsnap()
{
	# What the parameters are:
	# 1: the filename of the virtual floppy.
	# 2: the directory where the base Merge files are.
	# 3: the directory where the DOS files are.
	# 4: the directory where the image specific file are.

	VSnap=$1
	MergeDir=$2
	DosDir=$3
	ImageDir=$4

	> $TMPFILE
	# Copy files from MergeDir/lib
	for i in init.com snap.com bridge.sys hicon.sys part.sys ulink.sys \
		mrgxms.sys mrghmxms.sys drdosset.com merge_ae.exe merge.set \
		mrgtsr.exe mergecd1.sys
	do
		echo "copyto $i $MergeDir/lib/$i" >> $TMPFILE
	done
	# Copy files from MergeDir
	for i in quit.com merge.exe
	do
		echo "copyto $i $MergeDir/$i" >> $TMPFILE
	done
	# Copy files from DosDir
	for i in keyb.com  keyboard.sys subst.exe
	do
		if [ -f $DosDir/$i ]
		then
			echo "copyto $i $DosDir/$i" >> $TMPFILE
		fi
	done
	# Copy NLS only files from DosDir
	if [ "x$DOS_NO_NLS_IMAGE" != "xon" ]
	then
		for i in display.sys  ega.cpi  mode.com  nlsfunc.exe 
		do
			if [ -f $DosDir/$i ]
			then
				echo "copyto $i $DosDir/$i" >> $TMPFILE
			fi
		done
	fi
	if [ "z$DOSBRAND" = "zDRDOS" ]
	then
		# DR-DOS 6.0 only, copy hidos.sys to the snap disk.
		for i in hidos.sys
		do
			if [ -f $DosDir/$i ]
			then
				echo "copyto $i $DosDir/$i" >> $TMPFILE
			fi
		done
	fi
	for i in `ls $ImageDir`
	do
		# Don't put existing image files on the disk. (*.img)
		# Don't put saved image files on the disk.    (*.img.sv)
		# Don't put rom files on the disk.            (*.rom)
		# Don't put saved rom files on the disk.      (*.rom.sv)
		# Don't put config or autoexec files on the
		# list now. They are done later.  Note that we reject
		# any file name with "autoexec.bat" in it, not only those
		# that end with it.  This is because if for example,
		# "autoexec.bat.save" is copied to the snap disk, the
		# name is truncated to "autoexec.bat", thus conflicting
		# with the real autoexec.bat file.
		TEST=accept
		for j in  '\.img$' '\.img\.sv$' '\.rom$' '\.rom\.sv$' \
			'^autoexec.bat' '^autoexec.mki$' '^config.sys$' \
			'^config.mki$' '^core$'
		do
			echo $i | grep $j >> $NULL 2>&1
			if [ $? -eq 0 ]
			then
				# Don't copy it to the snap disk.
				TEST=reject
				continue
			fi
		done
		if [ "$TEST" = "accept" ]
		then
			# If a directory, then don't try to copy it.
			[ -d $ImageDir/$i ] && TEST=reject
		fi
		if [ "$TEST" = "accept" ]
		then
			echo "copyto $i $ImageDir/$i" >> $TMPFILE
		fi
	done
	# At this point $TMPFILE has all the "dosdisk" commands needed
	# to copy all the needed files to the vsnap disk,
	# (except for the config.sys file).

	# Copy the files to the vsnap disk.
	cat $TMPFILE | $STATICMERGE/dosdisk -s $VSNAP

	rm -f $TMPFILE
	return 0
}

# Determine display type
DISPLAY=`$STATICMERGE/display $CONSOLE 2>> $NULL`
TTYDSP=`$STATICMERGE/display /dev/tty 2>> $NULL`

	[ "$TTYDSP" = "vga" ] && DISPLAY=vga
	if [ -r "$STD_VGAROM" ]
	then
		VIDEO_ROM=$STD_VGAROM
		if [ "$DISPLAY" = "vga" -a  -r /dev/kmem ]
		then
			# Can use native vga rom ONLY when on a vga console AND
			# have root permissions.
			VGAOK=vgaok			# Native or standard
		else
			# When regular user OR the console is not a vga,
			# then can only use the standard ROM.
			NONATIVE=nonative
			VGAOK=nonative			#  standard ROM only
		fi
	elif [ "$DISPLAY" = "vga" -a  -r /dev/kmem ]
	then
		# Can use native vga rom ONLY when on a vga console AND
		# have root permissions.
		VGAOK=onlynative
		NATIVE=native			# Force native
	fi

USE1=
USE2=
USE3=
usrMsg STRING NATIVE_TOKEN "native"
NATIVE_TOKEN="$USRMSG_STRING"

if [ -n "$VGAOK" ]
then
	# When can use either generic or native video rom, give useage
	# of option that makes native ROM to be used: "native"
	if [ "$VGAOK" = "vgaok" ]
	then
		USE1="[ [$NATIVE_TOKEN] "
		USE3="] "
	fi
	if [ -n "$VGAOK" ]
	then
		USE2="[vga] "
	fi
fi

# For backwards compatability, all the options can use a leading dash.
# Also "tty", "mono" and "hercules" can be used for "mda".

while [ -n "$1" ]
do

	case $1 in

	d)	### custom directory for installing DOS image files
		shift
		DIRECTORY=$1
		;;
	-d)	### custom directory for installing DOS image files
		shift
		DIRECTORY=$1
		;;
	vga)	VGAFLAG=1
		;;
	-vga)	VGAFLAG=1
		;;
	cga)	CGAFLAG=1
		;;
	-cga)	CGAFLAG=1
		;;
	mda)	MDAFLAG=1
		;;
	-mda)	MDAFLAG=1
		;;
	mono)	MDAFLAG=1
		;;
	-mono)	MDAFLAG=1
		;;
	hercules)
		MDAFLAG=1
		;;
	-hercules)
		MDAFLAG=1
		;;
	tty)	MDAFLAG=1
		;;
	-tty)	MDAFLAG=1
		;;
	$NATIVE_TOKEN) NATIVE=native
		;;
	-$NATIVE_TOKEN) NATIVE=native
		;;
	visual)		VISUALMKIMG=1
		;;
	visual2)	VISUAL2=1
		;;
	devicelow)	DEV_LOW=1
		;;
	no_dosdev)	NO_DOSDEV=1
		;;
	*)
		AAA="$THISPROG ${USE1}${USE2}${USE3}"
		usrMsg ECHO USAGE_LINE \
	"USAGE: $AAA[cga] [mda] [ d directory ] [devicelow] [+aXXX]\n" "$AAA"
		Exit_Clean 1
		;;
	esac
	shift
done

# Default directory is DEFAULT_IMAGEDIR
[ -z "$DIRECTORY" ] && DIRECTORY=$DEFAULT_IMAGEDIR
if [ ! -d $DIRECTORY ]
then
	mkdir -p $DIRECTORY >> $NULL 2>&1 
	chmod 775 $DIRECTORY
	chgrp bin $DIRECTORY
	chown bin $DIRECTORY
fi
if [ ! -d $DIRECTORY ]
then
	usrMsg ECHO NO_DIRECTORY \
		"The directory $DIRECTORY does not exist.\n" $DIRECTORY
	usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi

if [ -n "$NATIVE" -a -n "$NONATIVE" ]
then
	usrMsg ERROR OPT_NA "$NATIVE_TOKEN option not allowed.\n" $NATIVE_TOKEN
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi

if [ -z "$VGAFLAG" -a -z "$CGAFLAG" -a -z "$MDAFLAG" ]
then
        ALLOPTIONS=1
	# Since image type was not specified, make the default image files:
	[ -n "$VGAOK" -a "$VGAOK" != "onlynative" ] && VGAFLAG=1
	CGAFLAG=1
	MDAFLAG=1
fi

Writeable ${TMP_DIR}/dos.img
if [ $? -ne 0 ]
then
	usrMsg ERROR NO_PERMIS_TMP \
		"Error: No permission to create temporary file ${TMP_DIR}/dos.img\n" \
		${TMP_DIR}/dos.img
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi
> ${TMP_DIR}/dos.img
chmod 664 ${TMP_DIR}/dos.img

Writeable $VFLOP
if [ $? -ne 0 ]
then
	usrMsg ERROR NO_PERMIS_TMP \
		"Error: No permission to create temporary file $VFLOP\n" \
		$VFLOP
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi
	
for i in ${TMP_DIR}/mem_????.img ${TMP_DIR}/umb_????.img ${TMP_DIR}/hma.img
do
	[ ! -f $i ] && continue
	rm -f $i
	[ ! -f $i ] && continue
	usrMsg ERROR NO_PERMIS_TMP \
		"Error: No permission to create temporary file $i\n" \
		$i
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
done

if [ -n "$MDAFLAG" ]
then
	if [ ! -d $DIRECTORY/mono ]
	then
		mkdir $DIRECTORY/mono >> $NULL 2>&1 
	fi
	if [ ! -d $DIRECTORY/mono ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make MDA image.\n" MDA
		usrMsg ECHO NO_PERMIS_WRITE \
			"Do not have permission to write $DIRECTORY\n" \
			$DIRECTORY
	fi
	Writeable $DIRECTORY/mono/mono.img
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make MDA image.\n" MDA
		usrMsg ECHO NO_PERMIS_WRITE \
		    "Do not have permission to write $DIRECTORY/mono.img\n" \
		    $DIRECTORY/mono.img
		MDAFLAG=""
		PROBLEMS=1
	fi
fi
if [ -n "$CGAFLAG" ]
then
	Writeable $DIRECTORY/cga.img
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make CGA image.\n" CGA
		usrMsg ECHO NO_PERMIS_WRITE \
		    "Do not have permission to write $DIRECTORY/cga.img\n" \
		    $DIRECTORY/cga.img
		CGAFLAG=""
		PROBLEMS=1
	fi
fi

if [ -n "$VGAFLAG" ]
then
	if [ -z "$VGAOK" ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make VGA image.\n" VGA
		usrMsg ECHO WRONG_DISP \
			"Cannot make VGA image on $DISPLAY type display.\n" \
			VGA $DISPLAY
		VGAFLAG=""
		PROBLEMS=1
	elif [ -n "$NATIVE" -a ! -r /dev/kmem ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make VGA image.\n" VGA
		usrMsg ECHO MUST_SU \
			"Must be root or su to make VGA image.\n" VGA
		VGAFLAG=""
		PROBLEMS=1
	fi
fi
if [ -n "$VGAFLAG" ]
then
	Writeable $DIRECTORY/vga.img
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE "Cannot make VGA image.\n" VGA
		usrMsg ECHO NO_PERMIS_WRITE \
		    "Do not have permission to write $DIRECTORY/vga.img\n" \
		    $DIRECTORY/vga.img
		VGAFLAG=""
		PROBLEMS=1
	fi
fi

if [ -z "$VGAFLAG" -a -z "$MDAFLAG" -a -z "$CGAFLAG" ]
then
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi
Check_Room $VFLP_DIR 650
if [ $? -ne 0 ]
then
	usrMsg ERROR NO_ROOM_TMP \
		"Not enough room to create temporary file $VFLOP\n" $VFLOP
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi
##############################################################################
# Here is where we actually start the image making work.
########################################################

usrMsg ECHO CONFIG_MSG "Configuring DOS image files.\n"

# Give this script three minutes (180 seconds) to complete.
BG_ID=`$STATICMERGE/bin/waitkill 180 $$`

if [ ! -r $DOSROOTDOS/boot.dsk ]
then
	if [ ! -f $DOSROOTDOS/boot.dsk ]
	then
		usrMsg ERROR MISSING \
			"Missing file $DOSROOTDOS/boot.dsk\n" $DOSROOTDOS/boot.dsk
		if [ ! -f $DOSROOTDOS/command.com ]
		then
			usrMsg ERROR DOS_NI "DOS is not installed\n"
		fi
	else
		usrMsg ERROR CANNOT_READ \
			"Cannot read file $DOSROOTDOS/boot.dsk\n" \
			$DOSROOTDOS/boot.dsk
	fi
	usrMsg ECHO CANNOT_CONTINUE "Cannot continue.\n"
	usrMsg ECHO NO_IMG_MADE "No image files were made.\n"
	Exit_Clean 1
fi
if [ -f $DOSROOTDOS/version.dos ]
then
	. $DOSROOTDOS/version.dos
fi
Make_config_mki

# Create the $VFLOP virtual floppy.
Format_Vflop

# Set up UMB_OPTION flag
Get_devattach
echo "UMB_OPTION=$UMB_OPTION" >> $NULL

# Function "Put_Back" cleans up after a failed "Do_Img" or "Do_vga".
# Remove the temporary files and restore the original img files.
Put_Back()
{
	rm -f $VSNAP
	rm -f ${TMP_DIR}/dos.img
	rm -f ${TMP_DIR}/${IMG_TYPE}.img
	rm -f ${TMP_DIR}/mem_????.img
	rm -f ${TMP_DIR}/umb_????.img
	rm -f ${TMP_DIR}/hma.img
	for p_b in ${IMD}/*.img.sv
	do
		[ ! -f $p_b ]  && continue
		rm -f  ${IMD}/`basename $p_b .sv`
		mv $p_b ${IMD}/`basename $p_b .sv`
	done
	if [ -n "$NEW_ROM" -a -n "$SAVE_ROM" -a -f "$SAVE_ROM" ]
	then
		rm -f $ROMFILE
		mv $SAVE_ROM $ROMFILE >> $NULL 2>&1
	fi
}

################# "Do_Img" - Function that makes mda/cga image files. #########
################# (image files that don't use video roms) #####################
Do_Img ()
{
	IMG_TAG=$1
	IMG_TYPE=$2
	IMD=${DIRECTORY}/${IMG_TYPE}
	NEW_ROM=
	if [ ! -d ${IMD} ]
	then
		mkdir ${IMD} >> $NULL 2>&1
	fi
	if [ ! -d ${IMD} ]
	then
		usrMsg ECHO CANNOT_MKDIR \
			"Cannot make directory: ${IMD}" \
			"${IMD}"
		return 1	# problems
	fi
	if [ -f $DOSROOTDOS/dblspace.ini -a ! -f ${IMD}/dblspace.ini ]
	then
		cp $DOSROOTDOS/dblspace.ini ${IMD}/dblspace.ini
		chmod 664 ${IMD}/dblspace.ini
		chgrp bin ${IMD}/dblspace.ini
		chown bin ${IMD}/dblspace.ini
	fi
	# Remove any existing temporary files.
	for i in ${TMP_DIR}/dos.img ${TMP_DIR}/${IMG_TYPE}.img \
		${TMP_DIR}/mem_????.img ${TMP_DIR}/umb_????.img \
		${TMP_DIR}/hma.img $VSNAP
	do
		rm -f $i
		if [ -f $i ]
		then
			usrMsg WARN REMOVE_FAIL "Could not remove file $i\n" $i
			return 1	# problems
		fi
	done

	cp $VFLOP $VSNAP
	cmp -s $VFLOP $VSNAP >> $NULL 2>&1
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_WRITE \
			"Could not write $VSNAP\n" $VSNAP
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		return 1
	fi
	# Parameters to "Create_Vsnap":
	# 1: the filename of the virtual floppy.
	# 2: the directory where the base Merge files are.
	# 3: the directory where the DOS files are.
	# 4: the directory where the image specific files are.
	Create_Vsnap $VSNAP ${DOS_ROOT}/merge $DOSROOTDOS ${IMD}
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1
	fi
	Make_Config_File $IMG_TYPE
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1	# problems
	fi
	Make_AE_File $IMG_TYPE
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1	# problems
	fi

	usrMsg ECHO MAKING "Making $IMG_TAG Image.\n" $IMG_TAG

	> ${TMP_DIR}/dos.img
	chmod 664 ${TMP_DIR}/dos.img

	Check_Room ${TMP_DIR} 400
	if [ $? -ne 0 ]
	then
		usrMsg WARN NOT_MADE "$IMG_TAG image was not made.\n" \
			$IMG_TAG
		usrMsg ECHO CREATE_NOROOM \
			"Not enough room to create file ${TMP_DIR}/dos.img\n" \
			${TMP_DIR}/dos.img
		return 1	# problems
	fi
	if [ -n "$SAVE_VSNAP" ]
	then
		cp $VSNAP ${TMP_DIR}/vsnap.$IMG_TYPE
	fi
	> $VARMERGE/tmp/save_msg.dos 2> /dev/null
	rm -f $VARMERGE/tmp/save_msg.dos
	if [ ! -s $VARMERGE/tmp/save_msg.dos ]
	then
		MSG_SAVE=$VARMERGE/tmp/save_msg.dos
	else
		MSG_SAVE=
	fi
	if [ -z "$VISUALMKIMG" ]
	then
		$DOSWINBINDIR/dos $KOPT +a$IMG_TYPE +aa:=$VSNAP $UMB_OPTION \
			+b +c -e -l -p ver >> $NULL
	else
		# Allow the screen to be seen during boot and snap.
		$DOSWINBINDIR/dos $KOPT +a$IMG_TYPE +aa:=$VSNAP $UMB_OPTION \
			-b +c -e -l -p ver
	fi
	EXITVAL=$?
	echo "EXITVAL=$EXITVAL" >> $NULL
	if [ $EXITVAL -ne 0 ]
	then
		usrMsg WARN NOT_MADE "$IMG_TAG image was not made.\n" \
			$IMG_TAG
		echo "($EXITVAL)" | tee -a $NULL
		if [ -n "$MSG_SAVE" -a -s "$MSG_SAVE" ]
		then
			cat $MSG_SAVE | tee -a $NULL
		fi
		Put_Back
		return 1	# problems
	fi
	
	SIZE=`wc -c < ${TMP_DIR}/dos.img`
	if [ $SIZE -lt 10000 ]
	then
		echo "SIZE=$SIZE" >> $NULL
		usrMsg WARN NOT_MADE "$IMG_TAG image was not made.\n" \
			$IMG_TAG
		if [ $SIZE -ne 0 ]
		then
			usrMsg ECHO NO_ROOM "(No more room?)\n"
		fi
		if [ -n "$MSG_SAVE" -a -s "$MSG_SAVE" ]
		then
			cat $MSG_SAVE | tee -a $NULL
		fi
		Put_Back
		return 1	# problems
	fi
	# Rename the current image files to ".sv" names, so that can
	# restore them if needed.
	for i in $IMD/${IMG_TYPE}.img
	do
		rm -f $i.sv
		if [ -f $i.sv ]
		then
			usrMsg WARN REMOVE_FAIL \
				"Could not remove file $i.sv\n" $i.sv
			Put_Back
			return 1	# problems
		fi
		[ -f $i ] && mv $i $i.sv
	done

	rm -f ${TMP_DIR}/${IMG_TYPE}.img
	if [ -f ${TMP_DIR}/${IMG_TYPE}.img ]
	then
		usrMsg WARN REMOVE_FAIL \
			"Could not remove file ${TMP_DIR}/${IMG_TYPE}.img\n" \
			${TMP_DIR}/${IMG_TYPE}.img
		Put_Back
		return 1	# problems
	fi
	mv ${TMP_DIR}/dos.img ${TMP_DIR}/${IMG_TYPE}.img
	
	if [ -f $IMD/mem_????.img ]
	then
		rm -f $IMD/mem_????.img >> $NULL
	fi

	if [ -f $IMD/umb_????.img ]
	then
		rm -f $IMD/umb_????.img >> $NULL
	fi

	if [ -f $IMD/hma.img ]
	then
		rm -f $IMD/hma.img >> $NULL
	fi

	for i in ${TMP_DIR}/${IMG_TYPE}.img \
		${TMP_DIR}/mem_????.img ${TMP_DIR}/umb_????.img ${TMP_DIR}/hma.img
	do
		[ ! -f $i ] && continue
		chmod 664 $i
		chgrp bin $i
		chown bin $i
		mv $i $IMD
		BN=`basename $i`
		if [ ! -f $IMD/$BN ]
		then
			usrMsg WARN CANNOT_WRITE \
				"Could not write $IMD/$BN\n" \
				$IMD/$BN
			usrMsg ECHO NO_ROOM "(No more room?)\n"
			Put_Back
			return 1	# problems

		fi
	done
	rm -f ${TMP_DIR}/dos.img
	rm -f ${TMP_DIR}/mem_????.img
	rm -f ${TMP_DIR}/umb_????.img
	rm -f ${TMP_DIR}/hma.img
	rm -f $IMD/*.img.sv
	rm -f $VSNAP
	$STATICMERGE/patchimg $IMG_TYPE $IMD/${IMG_TYPE}.img >> $NULL 2>&1
	return 0	# Success
}


################# "Do_vga" - Function that makes vga image files ##########
Do_vga()
{
	IMG_TAG=$1
	IMG_TYPE=$2
	IMD=${DIRECTORY}/${IMG_TYPE}
	NEW_ROM=

	if [ ! -d ${IMD} ]
	then
		mkdir ${IMD} >> $NULL 2>&1
	fi
	if [ ! -d ${IMD} ]
	then
		usrMsg ECHO CANNOT_MKDIR \
			"Cannot make directory: ${IMD}" \
			"${IMD}"
		return 1	# problems
	fi

	ROMFILE="${IMD}/${IMG_TYPE}.rom"
	SAVE_ROM=$ROMFILE.sv

	# Set up vga.rom
	if [ -n "$NATIVE" ]
	then
		# Use ROM on the VGA card.
		Check_Room $RS_DIR 274
		if [ $? -ne 0 ]
		then
			usrMsg WARN CANNOT_MAKE \
				"Cannot make $IMG_TAG image.\n" $IMG_TAG
			usrMsg ECHO CREATE_NOROOM \
				"Not enough room to create file $RS_FILE\n" \
				$RS_FILE
			return 1	# problems
		fi
		grep CTMERGE $VARMERGE/Version >>$NULL 2>&1
		if [ $? = 0 ]
		then	# do CT/UNISYS way
			$STATICMERGE/romsnap d60c0000 $RS_FILE -k
		else
			$STATICMERGE/romsnap c0000 $RS_FILE
		fi
		SIZE=`wc -c < $RS_FILE`
		if [ $SIZE -lt 10000 ]
		then
			usrMsg WARN CANNOT_MAKE \
				"Cannot make $IMG_TAG image." $IMG_TAG
			usrMsg WARN CANNOT_WRITE \
				"Could not write $RS_FILE\n" $RS_FILE
			rm -f $RS_FILE
			return 1	# problems
		fi
		cmp -s $ROMFILE $RS_FILE
		if [ $? -ne 0 ]
		then
			# The ROM is different.  Need to use the fresh snap.
			usrMsg ECHO USING_ONCARD \
				"Using $IMG_TAG ROM on the display card.\n" \
				$IMG_TAG
			NEW_ROM=$RS_FILE
		fi
	else
		# Not native, so use the generic ROM
		cmp -s $ROMFILE $VIDEO_ROM
		if [ $? -ne 0 ]
		then
			# Need to copy in the generic ROM.
			NEW_ROM=$VIDEO_ROM
		fi
	fi
	if [ -n "$NEW_ROM" ]
	then
		Writeable $ROMFILE
		if [ $? -ne 0 ]
		then
			usrMsg WARN CANNOT_MAKE \
				"Cannot make $IMG_TAG image." $IMG_TAG
			usrMsg ECHO NO_PERMIS_WRITE \
				"Do not have permission to write $ROMFILE\n" \
				$ROMFILE
			return 1	# problems
		fi
		rm -f $SAVE_ROM
		mv $ROMFILE $SAVE_ROM >> $NULL 2>&1
		cp $NEW_ROM $ROMFILE >> $NULL 2>&1
		if [ $? -ne 0 ]
		then
			usrMsg WARN CANNOT_MAKE \
				"Cannot make $IMG_TAG image." $IMG_TAG
			usrMsg WARN CANNOT_WRITE \
				"Could not write $ROMFILE\n" $ROMFILE
			usrMsg ECHO NO_ROOM "(No more room?)\n"
			rm -f $ROMFILE
			mv $SAVE_ROM $ROMFILE >> $NULL 2>&1
			return 1	# problems
		fi
		chmod 664 $ROMFILE
		chgrp bin $ROMFILE
		chown bin $ROMFILE
	fi
	rm -f $RS_FILE

# do not make VGA images for Japanese version
        if [ $JDOS -eq 1 ]
        then
              if [ $ALLOPTIONS -ne 1 ]
              then
                    usrMsg WARN CANNOT_MAKE \
                    "Cannot make $IMG_TAG image.\n" \
                     $IMG_TAG
              fi
              return 0
        fi

	if [ -f $DOSROOTDOS/dblspace.ini -a ! -f ${IMD}/dblspace.ini ]
	then
		cp $DOSROOTDOS/dblspace.ini ${IMD}/dblspace.ini
		chmod 664 ${IMD}/dblspace.ini
		chgrp bin ${IMD}/dblspace.ini
		chown bin ${IMD}/dblspace.ini
	fi
	# Remove any existing temporary files.
	for i in ${TMP_DIR}/dos.img ${TMP_DIR}/${IMG_TYPE}.img \
		${TMP_DIR}/mem_????.img ${TMP_DIR}/umb_????.img ${TMP_DIR}/hma.img $VSNAP
	do
		rm -f $i
		if [ -f $i ]
		then
			usrMsg WARN REMOVE_FAIL "Could not remove file $i\n" $i
			return 1	# problems
		fi
	done

	cp $VFLOP $VSNAP
	cmp -s $VFLOP $VSNAP >> $NULL 2>&1
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_WRITE \
			"Could not write $VSNAP\n" $VSNAP
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		return 1
	fi
	# Parameters to "Create_Vsnap":
	# 1: the filename of the virtual floppy.
	# 2: the directory where the base Merge files are.
	# 3: the directory where the DOS files are.
	# 4: the directory where the image specific files are.
	Create_Vsnap $VSNAP ${DOS_ROOT}/merge $DOSROOTDOS ${IMD}
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1
	fi
	Make_Config_File $IMG_TYPE
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1	# problems
	fi

	Make_AE_File $IMG_TYPE
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image.\n" $IMG_TAG
		rm -f $VSNAP
		return 1	# problems
	fi

	> ${TMP_DIR}/dos.img
	chmod 664 ${TMP_DIR}/dos.img

	if [ -n "$NEW_ROM" ]
	then
		# When changing roms, then move the old image aside.
		# Do this because if the image make crashes, we don't want
		# the old image there, because the rom will be mismatched.
		mv ${IMD}/${IMG_TYPE}.img ${IMD}/${IMG_TYPE}.img.sv >>$NULL 2>&1
	fi
	sync
	Check_Room ${TMP_DIR} 400
	if [ $? -ne 0 ]
	then
		usrMsg WARN CANNOT_MAKE \
			"Cannot make $IMG_TAG image." $IMG_TAG
		usrMsg ECHO CREATE_NOROOM \
			"Not enough room to create file ${TMP_DIR}/dos.img\n" \
			${TMP_DIR}/dos.img
		return 1	# problems
	fi
	if [ -n "$SAVE_VSNAP" ]
	then
		cp $VSNAP ${TMP_DIR}/vsnap.$IMG_TYPE
	fi
	usrMsg ECHO MAKING "Making $IMG_TAG Image.\n" $IMG_TAG
	> $VARMERGE/tmp/save_msg.dos 2> /dev/null
	rm -f $VARMERGE/tmp/save_msg.dos
	if [ ! -s $VARMERGE/tmp/save_msg.dos ]
	then
		MSG_SAVE=$VARMERGE/tmp/save_msg.dos
	else
		MSG_SAVE=
	fi
	if [ -n "$NATIVE" ]
	then
		if [ -z "$VISUALMKIMG" ]
		then
			$DOSWINBINDIR/dos $KOPT +a${IMG_TYPE} $UMB_OPTION \
			  +a.40-44,3b0-3df,7c6,b66,fc6..a0000-bffff.,i,no_umb \
			  +aa:=$VSNAP +b +c -e -l$DIRECTORY -p \
			  ver < $CONSOLE >$CONSOLE 2>$CONSOLE
		else
			# Allow the screen to be seen during boot and snap.
			$DOSWINBINDIR/dos $KOPT +a${IMG_TYPE} $UMB_OPTION \
			  +a.40-44,3b0-3df,7c6,b66,fc6..a0000-bffff.,i,no_umb \
			  +aa:=$VSNAP -b +c -e -l$DIRECTORY -p \
			  ver < $CONSOLE >$CONSOLE 2>$CONSOLE
		fi
	else
		if [ -z "$VISUALMKIMG" ]
		then
			$DOSWINBINDIR/dos $KOPT +a${IMG_TYPE} $UMB_OPTION \
				+aa:=$VSNAP +b +c -e -l$DIRECTORY -p \
				ver >$ERR_FILE 2>&1
		else
			# Allow the screen to be seen during boot and snap.
			$DOSWINBINDIR/dos $KOPT +a${IMG_TYPE} $UMB_OPTION \
				+aa:=$VSNAP -b +c -e -l$DIRECTORY -p \
				ver
		fi
	fi
	EXITVAL=$?
	if [ $EXITVAL -ne 0 ]
	then
		usrMsg WARN NOT_MADE "$IMG_TAG image was not made.\n" \
			$IMG_TAG
		usrMsg ECHO INCOMPATABLE \
			"(Incompatable $IMG_TAG card?)\n" $IMG_TAG
		echo "($EXITVAL)" | tee -a $NULL
		if [ -n "$MSG_SAVE" -a -s "$MSG_SAVE" ]
		then
			cat $MSG_SAVE | tee -a $NULL
		fi
		cat $ERR_FILE 2>> $NULL
		Put_Back
		return 1	# problems
	fi
	if [ -n "$NATIVE" ]
	then
		usrMsg ECHO IMG_MADE "$IMG_TAG image made.\n" $IMG_TAG
	fi

	# Move old image back.
	[ -f ${IMD}/${IMG_TYPE}.img.sv ] && \
		mv ${IMD}/${IMG_TYPE}.img.sv ${IMD}/${IMG_TYPE}.img >>$NULL 2>&1

	rm -f $ERR_FILE

	SIZE=`wc -c < ${TMP_DIR}/dos.img`
	if [ $SIZE -lt 10000 ]
	then
		# Image is much too small.
		usrMsg WARN NOT_MADE "$IMG_TAG image was not made.\n" \
			$IMG_TAG
		if [ $SIZE -ne 0 ]
		then
			usrMsg ECHO NO_ROOM "(No more room?)\n"
		fi
		Put_Back
		return 1	# problems
	fi

	# Rename the current image files to ".sv" names, so that can
	# restore them if needed.
	for i in $IMD/${IMG_TYPE}.img
	do
		rm -f $i.sv
		if [ -f $i.sv ]
		then
			usrMsg WARN REMOVE_FAIL \
				"Could not remove file $i.sv\n" $i.sv
			Put_Back
			return 1	# problems
		fi
		[ -f $i ] && mv $i $i.sv
	done

	rm -f ${TMP_DIR}/${IMG_TYPE}.img
	if [ -f ${TMP_DIR}/${IMG_TYPE}.img ]
	then
		usrMsg WARN REMOVE_FAIL \
			"Could not remove file ${TMP_DIR}/${IMG_TYPE}.img\n" \
			${TMP_DIR}/${IMG_TYPE}.img
		Put_Back
		return 1	# problems
	fi
	mv ${TMP_DIR}/dos.img ${TMP_DIR}/${IMG_TYPE}.img
	
	if [ -f $IMD/mem_????.img ]
	then
		rm -f $IMD/mem_????.img >> $NULL
	fi

	if [ -f $IMD/umb_????.img ]
	then
		rm -f $IMD/umb_????.img >> $NULL
	fi

	if [ -f $IMD/hma.img ]
	then
		rm -f $IMD/hma.img >> $NULL
	fi

	for i in ${TMP_DIR}/${IMG_TYPE}.img \
		${TMP_DIR}/mem_????.img ${TMP_DIR}/umb_????.img \
		${TMP_DIR}/hma.img
	do
		[ ! -f $i ] && continue
		mv $i $IMD
		BN=`basename $i`
		if [ ! -f $IMD/$BN ]
		then
			usrMsg WARN CANNOT_WRITE \
				"Could not write $IMD/$BN\n" \
				$IMD/$BN
			usrMsg ECHO NO_ROOM "(No more room?)\n"
			Put_Back
			return 1	# problems

		fi
		chmod 664 $IMD/$BN
		chgrp bin $IMD/$BN
		chown bin $IMD/$BN
	done

	rm -f $SAVE_ROM
	rm -f ${TMP_DIR}/dos.img
	rm -f ${TMP_DIR}/mem_????.img
	rm -f ${TMP_DIR}/umb_????.img
	rm -f ${TMP_DIR}/hma.img
	rm -f $IMD/*.img.sv
	rm -f $VSNAP
	$STATICMERGE/patchimg $IMG_TYPE $IMD/${IMG_TYPE}.img >> $NULL 2>&1
	return 0	# Success
}

# (NOTE: Do NOT nls-ize the copyright lines.  They must be exactly like this.)
# Print ROM copyrights taken from rom/atbase.asm
echo "BIOS V2.09 Copyright 1984, 1985 Award Software Inc."
# Print EMS copyright taken from ems40/locus/oeminit.asm
echo "Quadtel Expanded Memory Manager Copyright(c) 1989, 1990 Quadtel Corp."
if [ -n "$VGAFLAG" -a  -z "$NATIVE" ]
then
	# Print VGA ROM copyrights taken from vga/chip451/oem/oem.asm
	echo "Quadtel 82C451 VGA BIOS Copyright(c) 1987, 1988 Quadtel Corp."
fi
# (NOTE: Do NOT nls-ize this string.  It must be exactly like this.)
echo "All Rights Reserved"

if [ -s $MERGEFILE ]
then
	rm -f $TMPFILE
	[ "x" = "x$DOS_IMAGE_NLSFUNC" ] && \
	    grep "^DOS_IMAGE_NLSFUNC="          $MERGEFILE >> $TMPFILE 2>> $NULL
	[ "x" = "x$DOS_VGA_HARDWARE_CODEPAGE" ] && \
	    grep "^DOS_VGA_HARDWARE_CODEPAGE="  $MERGEFILE >> $TMPFILE 2>> $NULL
	[ "x" = "x$DOS_VGA_SOFT_CODEPAGE" ] && \
	    grep "^DOS_VGA_SOFT_CODEPAGE="      $MERGEFILE >> $TMPFILE 2>> $NULL
	[ "x" = "x$DOS_NO_NLS_IMAGE" ] && \
	    grep "^DOS_NO_NLS_IMAGE="           $MERGEFILE >> $TMPFILE 2>> $NULL
	if [ -s $TMPFILE ]
	then
		. $TMPFILE
	fi
	rm -f $TMPFILE
fi

if [ -n "$MDAFLAG" ]
then
	Do_Img MDA mono
	if [ $? -ne 0 ]
	then
		MDAFLAG=""
		PROBLEMS=1
	fi
fi
if [ -n "$CGAFLAG" ]
then
	Do_Img CGA cga
	if [ $? -ne 0 ]
	then
		CGAFLAG=""
		PROBLEMS=1
	fi
fi
if [ -n "$VGAFLAG" ]
then
	Do_vga VGA vga
	if [ $? -ne 0 ]
	then
		VGAFLAG=""
		PROBLEMS=1
	fi
fi

if [ $PROBLEMS -ne 0 ]
then
	if [ -z "$VGAFLAG" -a -z "$CGAFLAG" -a -z "$MDAFLAG" ]
	then
		usrMsg WARN NO_IMG_MADE "No image files were made.\n"
	else
		usrMsg WARN SOME_NOT_MADE "Some image files were not made.\n"
	fi
	Exit_Clean 1
fi
Exit_Clean 0
