#!/bin/sh 

#
# Shell script to call all of the Sun emulated PC disk creatation tools
# for building a new DOS emulated hard disk
#
# This utility is called via the disk editor menu "Create Disk" button
#
# usage: create_disk new_disk_name size
#
#     Copyright (c) 1995, Sun Microsystems, Inc.  All Rights Reserved
#     Sun considers its source code as an unpublished, proprietary
#     trade secret, and it is available only under strict license
#     provisions.  This copyright notice is placed here only to protect
#     Sun in the event the source is deemed a published work.  Dissassembly,
#     decompilation, or other means of reducing the object code to human
#     readable form is prohibited by the license agreement under which
#     this code is provided to the user or company in possession of this
#     copy.
 
#     RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the
#     Government is subject to restrictions as set forth in subparagraph
#     (c)(1)(ii) of the Rights in Technical Data and Computer Software
#     clause at DFARS 52.227-7013 and in similar clauses in the FAR and
#     NASA FAR Supplement.

# This script DOES rely on external environment variables. 
# It assumes that:
#
#	1) SUNPCHOME has been set
#	2) The following utility programs can be found in $SUNPCHOME/bin:
#
#         	makedisk - create, format, fdisk and install s/w on the disk
#		dirname - so that we don't depend on sys 5 stuff (vpc936)
#
# SUNPCHOME is normally set by the sunpc startup script.
#
# In addition it assumes:
#	 that the specified disk name does not exist.
#
#        that $SUNPCHOME/defaults/C.$DOS_VERSION.sunpc3 does exist and is readable,
#	      bootable, properly formatted disk
#
#        that $SUNPCHOME/defaults/$DOS_VERSION contains only legal MS-DOS filenames and
#             files and that they will all fit on a minimum 2Mb disk file 
#	      NOTE: MS DOS 5.0 will require a minimum 3.0 Mb unless you setup the disk 
#                   and autoexec.bat files as the SunPC MS-DOS 4.01 disk does; with 
#                   none essential C: files and drivers accessed via the redirector.
#                   This will save lots of disk space for every user!
#
#	 if $DOS_VERSION != 4.01 we are running a user customized version, so we add error
#             messages that can not (should not) occur with a the factory version.
#
#
# This is how it works:
# 1: use makedisk to create a file of the specified size and insert the disk signature
#    at the appropriate locations so that the disk validation routines will regonize this
#    new file as a virtual SunPC disk. Using the size we calculate the number of sectors, etc.
#    using a variable geometry. This geometry uses either 4, 8, or 16 heads then calculates
#    the cylinder value based on the size and humber of heads.
#
#    makedisk also grabs the appropriate information off of an already existing
#    bootable and formatted disk. It then formats the disk and copy the bootable
#    information IO.SYS, etc, from the existing, bootable and formatted disk.
#
#    Finally, makedisk copies an MS-DOS directory and file structure to the new disk.
#
# errors:
#    If we get an error anywhere along the way we will always try to remove the disk that
#    we were trying to create. If this is custom job (i.e. DOS_VERSION != 4.01) then we try
#    to add as much error information as possible to help you debug your problems.
#
#    The most common errors will probably be that the DOS file layout will not fit on a 2Mb minimum
#    disk size, or that you have  set up the default MS-DOS directory structure incorrectly. Always
#    debug the existing disk first, and then layout the file system using XCOPY or something
#    similar. More subtle errors will be that the installed autoexec.bat file does not contain
#    the proper internationalization stuff (note the SunPC over writes the autoexec.bat file
#    on your first boot - see $SUNPCHOME/defaults/4.01/autoexec.bat for more details), or that
#    the autoexec.bat file that you use does not contain the proper path setup for most users.
#    Try not to let a user specific setup get put into $SUNPCHOME.
#
#    As a final note, the existing disk does not really need to include anything other than IO.SYS,
#    MSDOS.SYS & COMMAND.COM. If you are really tight on disk space, this can also help you reduce
#    the SUNPCHOME disk space requirements.
#
# okay lets parse off the arguments, 
# and make sure we have everything we need
#
# Set the search path.
PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc

disk_name=$1
disk_size=$2

if test $# = 2 
then
	:
else
	echo "usage: create_disk new_disk_file size"
	echo "       new_disk_file - new SunOS file name"
	echo "       size          - requested disk size between 2 and 128 Mb"
	exit 1
fi

if test -x $SUNPCHOME/bin/dirname
then
	:
else
	echo "create_disk: error: $SUNPCHOME/bin/dirname: does not exist or is not executable"
	echo "             SUNPCHOME may be set incorrectly or"
	echo "             SUNPCHOME it may be installed incorrectly"
	exit 1
fi
dir_path=`$SUNPCHOME/bin/dirname $disk_name`

if test -d $dir_path
then
	:
else
	echo "create_disk: error: '$dir_path': directory does not exist"
	exit 1
fi

if test $2 -ge 2 -a $2 -le 128
then 
	:
else
	echo "create_disk: error: disk size must be between 2 & 128 Mb"
	exit 1
fi

if test -r $disk_name
then
	echo "create_disk: error: '$disk_name': already exists"
	echo "             you must specify a new SunOS file name"
	exit 1
else
	:
fi

# Set DOS_VERSION=4.01 if you already had a version of SunPC prior to 4.1
# and still want the ability to create DOS 4.01 emulated hard drives.
#
DOS_VERSION=${DOS_VERSION:=7.01}

#
# if DOS_VERSION is 7.x or high, assume OpenDOS. makedisk accepts the
# -c flag to alternatively copy the OpenDOS DOS files (rather than
# IO.SYS, etc.
#

if [ "`echo $DOS_VERSION|sed -e 's/\..*//'`" -ge "7" ]
then
	ADD_ARGS="-c"
else
	ADD_ARGS=
fi

existing_disk=$SUNPCHOME/defaults/C.$DOS_VERSION.sunpc3
DOS_DIR=$SUNPCHOME/defaults/$DOS_VERSION

if test -r $existing_disk
then
	:
else
	echo "create_disk: error: '$existing_disk': does not exist"
	echo "             SUNPCHOME may be set incorrectly or"
	echo "             SUNPCHOME it may be installed incorrectly"
	if [ $DOS_VERSION -eq "4.01" ]
	then
		:
	else
		echo "             or you have not installed your custom, bootable disk"
	fi
	exit 1
fi


if test -x $SUNPCHOME/bin/makedisk
then
	:
else
	echo "create_disk: error: $SUNPCHOME/bin/makedisk: does not exist or is not executable"
	echo "             SUNPCHOME may be set incorrectly or"
	echo "             SUNPCHOME it may be installed incorrectly"
	exit 1
fi

#
# make sure we can cleanup on any error
# or a user abort
#
#trap "exit 1" 0 1 2 13 15

#
# Make a new hard disk file of the specified name. the -b makes it bootable -l tells
#  makedisk to label the disk. We do NOT recommend making drives non-bootable.
#
$SUNPCHOME/bin/makedisk   -o $disk_name -i $existing_disk -s $disk_size -l -b -d $DOS_DIR $ADD_ARGS

RETURN=$?
if [ $RETURN -eq 0 ]
then
	:
else
	echo "create_disk: an error occurred while building the disk file $disk_name"
	echo "             you may not have the proper permissions to create the new disk file"
	echo "             or you may not have enough disk space"
	rm -f $disk_name
	exit 1
fi

#
# okay we are all set
#
exit 0
