#!/bin/ksh -p
#
# Shell script to call all of the Sun emulated PC disk creatation tools
# for building a new DOS emulated hard disk
#
# usage: create_disk new_disk_name size
#
#     Copyright (c) 1998, 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) SUNPCIIHOME has been set
#	2) The following utility programs can be found in $SUNPCIIHOME/bin:
#
#         	makedisk - create, format, fdisk and install s/w on the disk
#		dirname - so that we don't depend on sys 5 stuff (vpc936)
#
# SUNPCIIHOME is normally set by the sunpc startup script.
#
# In addition it assumes:
#	 that the specified disk name does not exist.
#
#        that $SUNPCIIHOME/defaults/C.$DOS_VERSION.template does exist and is readable,
#	      bootable, properly formatted disk
#
#        that $SUNPCIIHOME/defaults/$DOS_VERSION contains only legal OpenDOS filenames and
#             files and that they will all fit on the minimum disk file 
#
# 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 recognize 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 OpenDOS directory and file structure to the new disk.
#
# Set the search path.
PATH=/bin:/usr/bin:/usr/ucb:/etc:/usr/etc

#
# I18N stuff
#
TEXTDOMAIN=create_disk
TEXTDOMAINDIR=$SUNPCIIHOME/lib/locale
export TEXTDOMAIN TEXTDOMAINDIR

if [ "$SLOW_CREATE_DISK" != "" ]
then
        echo Disk creation artificially slowed done
        sleep 30
fi


disk_name="`eval echo $1`"
disk_size=$2

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

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

if test -d $dir_path
then
	:
else
	echo "$dir_path: \c"
	echo `gettext "directory does not exist"`
	exit 1
fi

if test $2 -ge 10 -a $2 -le 2000
then 
	:
else
	echo `gettext "disk size must be between 2 & 2000 Mb"`
	exit 1
fi

if test -r $disk_name
then
	echo "$disk_name: \c"
	echo `gettext "already exists"`
	echo `gettext "you must specify a new SunOS file name"`
	exit 1
else
	:
fi

#
DOS_VERSION=${DOS_VERSION:=7.01}

#
# if DOS_VERSION is 7.x or higher, 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=$SUNPCIIHOME/defaults/C.$DOS_VERSION.template
DOS_DIR=$SUNPCIIHOME/defaults/$DOS_VERSION

if test -r $existing_disk
then
	:
else
	echo "$existing_disk: \c"
	echo `gettext "does not exist"`
	echo `gettext "SUNPCIIHOME may be set incorrectly or"`
	echo `gettext "SUNWspci may not have been installed correctly"`
	exit 1
fi

if test -x $SUNPCIIHOME/bin/makedisk
then
	:
else
	echo "$SUNPCIHOME/\c"
	echo `gettext "bin/makedisk: does not exist or is not executable"`
	echo `gettext "SUNPCIIHOME may be set incorrectly or"`
	echo `gettext "SUNWspci may not have been installed correctly"`
	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.
#

$SUNPCIIHOME/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 `gettext "an error occurred while building the disk file"` " $disk_name"
	echo `gettext "you may not have the proper permissions to create the new disk file"`
	echo `gettext "or you may not have enough disk space"`
	rm -f $disk_name
	exit 1
fi

#
# okay we are all set
#
exit 0
