#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/mkcd/mkcd.sh 1.58.19.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2013 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#  @(#)00  1.58.19.1  src/bos/usr/sbin/mkcd/mkcd.sh, cmdbsys, bos720 10/16/13 23:28:37
#############################################################
## The following functions are included in this file and
## are listed in alphabetical order:
##
##   build_boot_images
##   calculate_vg_space
##   change_cd
##   check_cd_fs
##   check_cd_ready
##   check_file_links
##   check_memory_size
##   check_metadata_backup
##   check_user
##   cleanup
##   compare_options
##   copy_backup
##   copy_bundle_file
##   copy_custom_script
##   copy_data_files
##   copy_devices_packages
##   copy_lang_support
##   copy_map_files
##   create_boot_cd
##   create_cd_fs
##   create_cd_images_fs
##   create_mkcd_data_file
##   create_efi_partition
##   create_empty_efi_partition
##   create_just_file_cdfs
##   create_oslevel_file
##   create_rockridge_fs
##   debug_switch
##   determine_conflicting_options
##   file_system_cleanup
##   get_backup_file_number
##   get_fs_free_space
##   get_fs_size
##   get_stanza_data
##   get_vg_name
##   go_straight_to_rockridge
##   go_straight_to_udf
##   initialize_log
##   initialize_variables
##   install_device_support
##   kill_pid
##   log_print
##   make_dos_dirs
##   manage_backup
##   manage_mksysb
##   manage_multivolume
##   manage_savevg
##   manage_savewpar
##   populate_cd_fs
##   print_usage
##   remove_file_system
##   setup_data_files
##   setup_exclude_file
##   size_input_file_list
##   validate_argument
##   verify_parm
##   verify_image
##   write_to_cd
##
##
## The main script is the last routine of this file.
#############################################################


######################### build_boot_images ############################
##
## Name:  build_boot_images
##
## Function:  The build_boot_images function is responsible for building
##            the boot image.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function build_boot_images
{
typeset boot_image_dir
typeset imgsize
typeset mesg
typeset -i needed_space
typeset -i tmpfree
typeset kernel=
typeset cd_fs_size
typeset cd_fs_size_required

##########################################################
## No boot images should be created for a nonbootable CD
##########################################################

if [ "$PLATFORM" != "ia64" ]; then
	if [ $NONBOOT_FLAG -eq $ON ]; then
		return

##########################################################
## If a generic CD is requested, all three platform type
## boot images will be created.
##########################################################

	else  ## bootable CD
		kernel="-k /usr/lib/boot/unix_64"

##########################################################
## Ensure the CD file system directory exists, and then
## recreate the proper bootinfo.txt file in the CD file
## system structure.
##
## NOTE:  The Bull firmware does not look at the
#         bootinfo.txt file.  Therefore, the file remains
##        the same for a Bull CD.
##########################################################

		if [ ! -d "${CD_FS_DIR}/ppc/${PLATFORM}" ]; then
			/usr/bin/mkdir -p "${CD_FS_DIR}/ppc/${PLATFORM}" 2>$MSGBUF
			if [ $? -ne 0 ]; then
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

		/usr/bin/sed 's/^<boot-script>.*<\/boot-script>$/<boot-script>boot \&device;:\\ppc\\chrp\\bootfile.exe<\/boot-script>/' \
			/usr/lib/boot/bootinfo.txt >/tmp/bootinfo.txt 2>$MSGBUF
			cp /tmp/bootinfo.txt ${CD_FS_DIR}/ppc/bootinfo.txt
#			/usr/lib/boot/bootinfo.txt >${CD_FS_DIR}/ppc/bootinfo.txt 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## sed command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		boot_image_dir="${CD_FS_DIR}/ppc/${PLATFORM}/bootfile.exe"

##########################################################
## Determine the size of the boot image and ensure the
## temporary file system has enough space to support
## the image.  If not, create more temporary file system
## space.
##########################################################

		imgsize=`LANG=C /usr/sbin/bosboot -q -a -d cd -T $PLATFORM \
			-b $boot_image_dir $kernel 2>$MSGBUF`
		if [ $? -ne 0 ]; then
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		cd_fs_size_required=`echo $imgsize | awk '{print$7}'`     ## in KB
		if [ -z "$cd_fs_size_required" ]; then
                        cd_fs_size_required=`echo $imgsize | awk '{print$5}'`     ## in KB
                fi
		(( cd_fs_size_required = cd_fs_size_required * 2 ))       ## 512 byte blocks
		imgsize=`echo $imgsize | awk '{print$5}'`     ## in KB
		(( imgsize = imgsize * 2 ))     ## 512 byte blocks
		tmpfree=`get_fs_free_space /tmp`
		(( needed_space = imgsize - (tmpfree/512) ))  ## 512 byte blocks
		if (( needed_space > 0 )); then
			/usr/sbin/chfs -a size=+${needed_space} /tmp 2>$MSGBUF
			if [ $? -ne 0 ]; then
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

		cd_fs_size=`/usr/bin/df $CD_FS_DIR/ | tail -1 | awk '{print$3}'`
		(( needed_space = cd_fs_size_required - cd_fs_size ))
		if (( needed_space > 0 )); then
			if [ $MKSYSB_IMAGE_FLAG -eq $OFF ] && [ $ALLOW_INCREASE_CD_FS -eq $ON ]; then
				CD_FS_CHANGED=${needed_space}
				/usr/sbin/chfs -a size=+${needed_space} $CD_FS
				if [ $? -ne 0 ]; then
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi
			fi
		fi

##########################################################
## Create the boot image for the CD
## Create using current system or use bootpkg to create
## from an existing backup image so that the boot image
## will be sure to match the backup image.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 44 \
			"Building $PLATFORM boot image...\n" $PLATFORM`
		log_print "$mesg"
		if [ $MKSYSB_IMAGE_FLAG -eq $OFF ]; then
			LANG=C /usr/sbin/bosboot -a -d cd -T $PLATFORM \
			-b $boot_image_dir $kernel >$MSGBUF 2>&1
		else
			bootpkg -m $MKSYSB_IMAGE -d cd -l $CD_FS_DIR -O
			mv $CD_FS_DIR/cd.bi $boot_image_dir
		fi
		if [ $? -ne 0 ]; then
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi
fi
} ## end of build_boot_images


######################### calculate_vg_space ############################
##
## Name:  calculate_vg_space
##
## Function:  Calculate space ${VG} takes up excluding files if asked
##
## Notes:
##       SOURCE = $1  name of volume group or workload partition
##       EF = $2      0  => do not exclude files (default)
##                    >0 => exclude files
##
## Returns: (int)
##       0                           = success
##
## Calling Routine:
##
########################################################################

function calculate_vg_space {
typeset SOURCE=$1 EF=${2:-0}
typeset lv fs LVLIST
typeset BUF                 # command output buffer
typeset EXCLUDEFILE         # exclude file
typeset TEF=/tmp/ef.mkcd.$$ # temporary exclude file
typeset LSVG=/usr/sbin/lsvg
typeset LSFS=/usr/sbin/lsfs
typeset LSWPAR=/usr/sbin/lswpar

	cd /
        if [ $WPAR_FLAG -eq $OFF ]; then
		BUF=$(${LSVG} -l ${SOURCE} | awk '$6 ~ "^open" && ($2 == "jfs2" || $2 == "jfs") {print $1,$7}')
        else
		BUF=$(${LSWPAR} -d' ' -qMa vfs,mountpoint ${SOURCE} | \
			egrep "^jfs[2]" | while read vfs fs 
			do
			   ${LSFS} -c $fs | awk -F':' 'NR>1 {print $2, $1}'
			done) 
	fi

	if [[ ${EF} == 0 ]] ; then
		echo "${BUF}" | while read lv fs; do
			typeset LVLIST="/dev/${lv} ${LVLIST}"
		done
		usr/bin/df -kIM ${LVLIST} | awk 'NR > 1 {tot = tot + $4} END {printf "%d",tot+1}'
	else
		echo "${BUF}" | while read lv fs; do
		# this seems ugly but we really need to match how mksysb/savevg
		# works, so ... the long pipeline follows the logic:
		# - find command to get the files and sizes per FS
		# - awk to reorder in a manner egrep and we can use
		#   (eg, $11=filename first, $2=size)
		# - egrep out based on the exclude file
		# - awk to calculate total and buffer by 512K for the header

		# setup a temporary exclude_file
		> ${TEF}

		EXCLUDEFILE=/etc/exclude.${SOURCE}

		# process the same as savevg
		cat $EXCLUDEFILE | sed 's/       //g;s/^  *//g;s/  *$//g;/^$/d' > ${TEF}

		find .$fs ! -type s \( -fstype jfs -o -fstype jfs2 \) -xdev -ls |
		awk '{print $11,$2}' | egrep -v -f ${TEF} |
		awk -v fs=".$fs" '{tot=tot+$2} END {printf ("%s\t%d\n",fs,tot)}'
		done  | awk '{tot=tot+$2} END {printf ("%.0d",tot+512)}'

		# cleanup the temporary exclude file
		[[ -r ${TEF} ]] && rm -f ${TEF}
	fi

	return 0

} ## end of calculate_vg_space


######################### change_cd ####################################
##
## Name:  change_cd
##
## Function:  The change_cd function is responsible for prompting the
##            user to change CDs, when multi-volume CDs are being
##            created.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_multivolume & copy_devices_packages
##
########################################################################

function change_cd
{
typeset cd_ready
typeset mesg
typeset -i next_vol

##########################################################
## Prompt the user to change CDs
##########################################################

	if [ $MULTI_VOLUME_FLAG -eq $ON ]; then
		if [[ $UDF_FLAG -eq $ON ]]; then
			/usr/sbin/umount $CD_FS_DIR
		fi
		(( next_vol = VOLUME_ID + 1 ))
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 65 \
			"The backup will require an additional CD or DVD.\n\
Remove the current writable CD or DVD (volume $VOLUME_ID) from the\n\
CD or DVD device and place a new writable CD or DVD (volume $next_vol),\n\
in device $DEVICE.\n\
Press the <enter> key when ready...\n" $VOLUME_ID $next_vol $DEVICE`
		echo "$mesg"
	else
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 67 \
			"Remove the source CD or DVD from the CD or DVD device and place a\n\
writable CD or DVD in device $DEVICE.\n\
Press the <enter> key when ready...\n" $DEVICE`
		echo "$mesg"
	fi

##########################################################
## Wait for the CD device to be ready.  Check ready status
## after the user has hit the enter key.
##########################################################

	cd_ready=$OFF
	while [ $cd_ready -eq $OFF ]; do

		read dummy
		if [ $? -ne 0 ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 133 \
				"0512-334:Error reading from the controlling terminal.\n"`
			log_print "$mesg"
			cleanup 1
		fi

##########################################################
## Next check to see if the CD drive contains a disc
## and is in a working state.  If the device is not ready,
## then wait 30 seconds and try again.
##########################################################

		>$MSGBUF 2>&1 <$DEVICE
		if [ $? -ne 0 ]; then
			sleep 30

			>$MSGBUF 2>&1 <$DEVICE
			if [ $? -eq 0 ]; then
				cd_ready=$ON
			else
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 93 \
					"0512-332 $NAME: Device $DEVICE does not appear to be ready.  For
information about possible causes, see /usr/lpp/bos.sysmgt/mkcd.README.txt
  Continuing...\n" $NAME $DEVICE`
				echo "$mesg"
				sleep 5
				cd_ready=$ON
			fi
		else
			cd_ready=$ON
		fi
	done

} ## end of change_cd


######################### check_cd_fs ##################################
##
## Name:  check_cd_fs
##
## Function:  The check_cd_fs function is responsible for ensuring
##            the CD file system has enough disk space to create the
##            data necessary to build the image.  The file system size
##            is obtained by the get_fs_free_space routine.
##
## Parameters:  CD file system/directory
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function check_cd_fs
{
typeset check_fs=$1
typeset check_fs_size=$2

typeset mesg
typeset -i fs_free

##########################################################
## Determine the file system size and ensure it exceeds
## the minimum size requirement for the CD file system.
##########################################################

	fs_free=`get_fs_free_space $check_fs $check_fs_size`
	if (( fs_free < check_fs_size )); then
		(( ADDITIONAL_NEEDED_SPACE = ( $check_fs_size * 100 / 94 - fs_free ) * 100 / 96 + 1 ))
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 \
			"0512-329 $NAME: User specified CD or DVD file system: ${check_fs}\n\
needs an additional $ADDITIONAL_NEEDED_SPACE bytes.\n" $NAME $check_fs $ADDITIONAL_NEEDED_SPACE`
		log_print "$mesg"
		cleanup 1
	fi

} ## end of check_cd_fs


######################### check_cd_ready ###############################
##
## Name:  check_cd_ready
##
## Function:  The check_cd_ready function is responsible for ensuring
##            the CD drive is available and is ready.
##
## Parameters:  CD device
##
## Returns:  None
##
## Calling Routine:  verify_parm
##
########################################################################

function check_cd_ready
{
typeset cd_device=$1

typeset cdrom
typeset chk_avail
typeset mesg

##########################################################
## First check to see if the device is a CD
##########################################################

	cdrom=`basename $cd_device`
	chk_avail=`LANG=C /usr/sbin/lsdev -Cc cdrom | grep -w $cdrom | \
		awk '{print$2}'`
	if [ -z "$chk_avail" ]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 111 \
			"0512-301 $NAME: $cd_device is not a CD or DVD device.\n" $NAME $cd_device`
		log_print "$mesg"
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

##########################################################
## Then check to see if the CD device is available
##########################################################

	if [ "$chk_avail" != "Available" ]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 95 \
			"0512-302 $NAME: Device $cd_device is not Available.\n" $NAME $cd_device`
		log_print "$mesg"
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

##########################################################
## Next check to see if the CD drive contains a disc
## and is in a working state.
##########################################################

	>$MSGBUF 2>&1 <$cd_device
	if [ $? -ne 0 ]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 93 \
			"0512-332 $NAME: Device $DEVICE does not appear to be ready.  For
information about possible causes, see /usr/lpp/bos.sysmgt/mkcd.README.txt
  Continuing...\n" $NAME $DEVICE`
		log_print "$mesg"
		sleep 5
	fi

} ## end of check_cd_ready


######################### check_file_links #############################
##
## Name:  check_file_links
##
## Function:  The check_file_links function is responsible for ensuring
##            that links exist for the ISO file system creations routine,
##            as well as the CD write routine.  If a CD is not being
##            burned, then the CD write routine link is not a mandate.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function check_file_links
{
typeset each_link
typeset mesg

##########################################################
## Check each mandatory link
##########################################################

	for each_link in $FILE_LINKS ; do
		if [ ! -L "$each_link" ]; then

##########################################################
## If the user has requested a stop before the CD is
## burned, then the burn_cd link is not required.
##########################################################

			if [ "$each_link" = "/usr/sbin/burn_cd" ] && \
				[ $STOP_FLAG -eq $ON ]; then
				continue
			else
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 96 \
					"0512-305 $NAME: $each_link \
must be a link to the \
appropriate OEM routine.\n" $NAME $each_link`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi
	done

} ## end of check_file_links


######################### check_memory_size ############################
##
## Name:  check_memory_size
##
## Function:  The check_memory_size function is responsible for checking
##            the system memory, necessary for creating a backup on CD-R.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function check_memory_size
{
typeset memsize
typeset mesg

##########################################################
## Determine the memory size in Kbytes and check to see
## if it's larger than the minimum needed requirement.
##########################################################

	memsize=`LANG=C bootinfo -r`
	(( memsize = memsize * 1024 ))

	if (( memsize < MIN_MEM_SIZE )); then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 112 \
			"0512-306 $NAME: 32 megabytes of memory is required\n\
to create a mksysb on a CD or DVD device.\n\
Current memory size is: $memsize bytes.\n" $NAME $memsize`
		log_print "$mesg"
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

} ## end of check_memory_size

######################### check_metadata_backup ############################
##
## Name:  check_metadata_backup
##
## Function:  Checks to see if the user supplied savevg image is a metadata
##            backup or not.  If it is then we proceed, if not exit.
##            This only matters when the -n flag is used for a no data backup.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
############################################################################

function check_metadata_backup
{
  ## First try getting colon separated output from savevg image.
  IS_METADATA=`LANG=C /usr/bin/listvgbackup -f $SAVEVG_IMAGE \
                               -lc | awk -F: '{print $15}' | tail -1`

  if [[ "$IS_METADATA" = "VG Data Only" ]]
  then
    ## The user supplied backup contains no logical volumes.
    ## Try getting the information from the general output.
    IS_METADATA=`LANG=C /usr/bin/listvgbackup -f $SAVEVG_IMAGE \
    -l | grep "VG DATA" | sed -e "s/[ 	]//g" | awk -F: '{print $2}'`
  fi

  if [[ "$IS_METADATA" != "yes" ]]
  then
    ## User supplied savevg image is not a metadata backup!
    print_usage
    rm -f $FS_FREE_SPACE 2>/dev/null
    exit 1
  fi
} ## end of check_metadata_backup

######################### check_user ###################################
##
## Name:  check_user
##
## Function:  The check_user function is responsible for checking the
##            user ID of the person calling mkcd.  If the user is not
##            root, the command will fail.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function check_user
{
typeset mesg

	if [[ $(whoami) != root ]]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 97 \
			"0512-307 $NAME: You must be a root user to run \
this command.\n" $NAME`
		log_print "$mesg"
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

} ## end of check_user


######################### cleanup ######################################
##
## Name:  cleanup
##
## Function:  The cleanup function is responsible for the cleanup after
##            an mkcd failure or completion.  Flags are used,
##            within mkcd, to determine where in the process
##            of the command, did the failure occur.  Depending upon the
##            location of the failure, determines the cleanup technique.
##
## Parameters:  0 or 1
##              0 - indicates a cleanup after a successful command
##              1 - indicates a cleanup after an unsuccessful command
##
## Returns:  None
##
## Calling Routine:  Hangup, interrupt, or kill trap set in main
##                   Also a variety of functions within the script.
##
########################################################################

function cleanup
{
typeset -i cleanup_flag=$1

typeset mesg

##########################################################
## Clear the trap for the final exit
##########################################################

	trap '' 1 2 15

	if (( cleanup_flag == 1 )); then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 10 "Cleaning up...\n"`
		log_print "$mesg"
	fi
	if [ -n "$SLEEP_PID" ]
	then
		kill_pid "$SLEEP_PID"
		echo ""
	fi

##########################################################
## Remove any command created file and file systems.
##########################################################

	rm -f $MSGBUF /tmp/bosboot.cd.* 2>/dev/null
	rm -f $FS_FREE_SPACE 2>/dev/null

	file_system_cleanup   ## remove mkcd created file systems

	if [ $UDF_FLAG -eq $ON ];then
		if [ $MK_ROCKRIDGE_FLAG -eq $OFF ];then
			/usr/sbin/umount $CD_FS_DIR
                        rmdir $CD_FS_DIR 2>/dev/null
                        rmdir /mkcd/cd_fs 2>/dev/null
		else
			/usr/sbin/umount $TMP_CD_MOUNT
		fi
	fi
##########################################################
## If the data files were created or moved to the system
## / directory, then remove them.
##########################################################

	if [ $REMOVE_IMAGE_DATA -eq $ON ]; then
		rm -f /image.data 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## rm command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi
	fi

	if [ $REMOVE_BOSINST_DATA -eq $ON ]; then
		rm -f /bosinst.data 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## rm command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi
	fi

##########################################################
## If the package source is a CD device, then unmount
## the device and remove the mount point.
##########################################################

	if [ -n "$CD_DEV" ]; then
		/usr/sbin/umount $TMP_CD_MOUNT 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## umount command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi

		rmdir $TMP_CD_MOUNT 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## rmdir command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi
	fi

##########################################################
## If original data files were saved, replace the new
## files with the original.
##########################################################

	/usr/bin/mv /bosinst.data.save.mkcd /bosinst.data 2>/dev/null
	/usr/bin/mv /image.data.save.mkcd /image.data 2>/dev/null

##########################################################
## If the exclude file has been processed, then move any
## saved exclude file to it's original location.
##########################################################

	if [ -n "$VG_NAME" ] && [ -f /etc/exclude.${VG_NAME}.save.mkcd ]; then
		rm -f /etc/exclude.${VG_NAME} 2>/dev/null
		/usr/bin/mv /etc/exclude.${VG_NAME}.save.mkcd \
			/etc/exclude.${VG_NAME} 2>/dev/null
	fi
	if [ "$AUTORESUME" = "yes" ]; then
                /usr/sbin/cdutil -r $DEVICE > /dev/null 2>&1
	fi
	[[ $MKCD_DIR_RM -eq $ON ]] && rmdir /mkcd 2>/dev/null
	ODMDIR=$ODMDIR_OLD
	export ODMDIR
	sync;sync;sync

##########################################################
## If  any filesystem was mounted for backup purposes
## now is the time to unmount them.
##########################################################
if [ $NNFLAG = $ON ]
then
        unmount_wpar_fs
fi

##########################################################
## If cleanup was called as a clean up to an unsuccessful
## command, then exit with an error return code.
## Otherwise, return to main.
##########################################################

	if (( cleanup_flag == 1 )); then
		exit 1
	fi

} ## end of cleanup


######################### compare_options ##############################
##
## Name:  compare_options
##
## Function:  The compare_options function is responsible for comparing
##            mkcd options.  There are two types of comparisons, when
##            one option must be set with another and when one option
##            cannot be set with another.  The type parameter is used to
##            determine the comparison.  The option parameters are used
##            for messaging.  The flag parameters are used to determine
##            if an option has been used and the associated flag set.
##
## Parameters:  type, 1st option, first flag, 2nd option, 2nd flag
##    type:  0 - IN_CONJUNCTION - If the 1st option is set, the 2nd
##                                option must be set also.
##           1 - MUTUALLY_EXCLUSIVE - If the 1st option is set, the 2nd
##                                option cannot be set.
##
## Returns:  None
##
## Calling Routine:  determine_conflicting_options
##
########################################################################

function compare_options
{
typeset comparison_type=$1
typeset first_option=$2
typeset first_flag=$3
typeset second_option=$4
typeset second_flag=$5

typeset mesg

########################################################################
## If the first option is set, the second option must also be set
########################################################################

	if [ $comparison_type -eq $IN_CONJUNCTION ]; then
		if [ $first_flag -eq $ON -a $second_flag -eq $OFF ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 89 \
				"0512-330 $NAME: option -${first_option} must be used in conjunction\n\
with the -${second_option} option.\n" $first_option $second_option $NAME`
			log_print "$mesg"
			print_usage
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi

########################################################################
## The options are mutually exclusive and cannot be called together
########################################################################

	elif [ $comparison_type -eq $MUTUALLY_EXCLUSIVE ]; then
		if [ $first_flag -eq $ON -a $second_flag -eq $ON ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 90 \
				"0512-331 $NAME: option -${first_option} and -${second_option} are mutually exclusive.\n" \
				$first_option $second_option $NAME`
			log_print "$mesg"
			print_usage
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi
	fi

} ## end of compare_options


######################### copy_backup ##################################
##
## Name:  copy_backup
##
## Function:  The copy_backup function is responsible for copying the
##            backup to the CD file system.  This routine also
##            determines if the multi-volume CDs are necessary.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_backup
{
typeset mesg
typeset total_size
typeset vgid
typeset -i boot_image_size
typeset -i cd_fs_tmp_create_size_512
typeset -i dd_size1
typeset -i dd_size2
typeset -i increase_fs_flag
typeset -i fs_size
typeset -i ppbytes
typeset -i ppbytes_512

	mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 72 \
		"Copying backup to the CD or DVD file system...\n"`
	log_print "$mesg"
	sleep_count=1
	while :; do
		while [ "$sleep_count" -le 45 ]
		do
			sleep 10
			echo ".\c"
			let "sleep_count = sleep_count + 1"
		done
		echo ""
		sleep_count=1
	done &
	SLEEP_PID=$!
##########################################################
## Estimate the necessary space for boot image(s) and then
## determine the size of the CD file system before
## copying the backup to the file system.
##
## NOTE: The PADDING_128K is used to correct the firmware
## problem of not reading all of the boot image.
##########################################################

	boot_image_size=$BOOT_IMAGE_SIZE_OPT4     # 36MB
	if [ $SAVEWPAR_IMAGE_FLAG -eq $OFF ] && \
	   [ $WPAR_FLAG -eq $OFF ]; then
		bi_size=`bosboot -qad cd -b /b${PID} | grep "/	" | awk '{print $2}'`
		(( boot_image_size = $bi_size * 1024 ))  ## boot image in bytes
	fi

	fs_size=`get_fs_size $CD_FS_DIR`
	(( total_size = fs_size + BACKUP_SIZE + boot_image_size + EFI_PARTITION_SIZE + PADDING_128K ))

###########################################################
## If install images are also being copied, then we will
## need to adjust the size of the CD file system to
## account for the space they occupy.  Be sure that the
## extra space does not cause the entire file system to
## go beyond what can fit on the media.  NOTE: We will only
## change the size of the file system if we created it.
## This is determined by the ALLOW_INCREASE_CD_FS variable
## which is set in the create_cd_fs function when the
## file system is created.
###########################################################

	if [ $ALLOW_INCREASE_CD_FS -eq $ON ] && [ $PKG_SOURCE_DIR_FLAG -eq $ON ]; then
		vgid=`getlvodm -v $CDFS_VOLUME_GROUP`
		ppbytes=`lqueryvg -g $vgid -s 2>/dev/null| \
			awk '{print 2 ^ $1}'`        # Get Partition Size
		(( ppbytes_512 = ppbytes / 512 ))

###########################################################
## How much bigger is the data than the file system it will
## be placed in?  Make sure the resultant file system is
## not larger than what the media can hold.
###########################################################

		(( INCREASE_FS_512 = total_size / 512 - CD_FS_NEW_MIN_512 ))
		(( increase_fs_flag = CD_FS_MIN_SIZE / ppbytes_512 - CD_FS_CREATE_SIZE_512 / ppbytes_512 ))
		if [ $INCREASE_FS_512 -gt 0 ] && [ $increase_fs_flag -gt 0 ]; then
			(( cd_fs_tmp_create_size_512 = CD_FS_NEW_CREATE_SIZE_512 + INCREASE_FS_512 ))
			if (( cd_fs_tmp_create_size_512 < CD_FS_MAX_512 ));then
				chfs -a size=+${INCREASE_FS_512} $CD_FS
				(( CD_FS_NEW_CREATE_SIZE_512 = CD_FS_NEW_CREATE_SIZE_512 + INCREASE_FS_512 ))
			else
				chfs -a size=$CD_FS_MAX_512 $CD_FS
				CD_FS_NEW_CREATE_SIZE_512=$CD_FS_MAX_512
			fi
		fi
	fi

##########################################################
## If the entire backup will fit in the available space,
## then copy the entire backup image file.  If the backup
## will not completely fit in the space available, then
## dd only a portion that will fit on the file system.
##########################################################

	if [ $total_size -le $LESS_THAN_2GB ] && \
		[ $total_size -le $CD_FS_MIN_SIZE ]; then

##########################################################
## Copy the mksysb or savevg image to the appropriate
## location in the CD file system.  Less than CD size if
## creating CDs, less than 2GB if creating DVDs.
##########################################################

		if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || \
			[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
			/usr/bin/cp -p $MKSYSB_IMAGE \
				${CD_FS_DIR}/usr/sys/inst.images/mksysb_image 2>$MSGBUF
		elif [ $SAVEVG_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEVG_COMPLETE_FLAG -eq $ON ]; then
			/usr/bin/cp -p $SAVEVG_IMAGE \
				${CD_FS_DIR}/usr/sys/inst.images/savevg_image 2>$MSGBUF
		elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEWPAR_COMPLETE_FLAG -eq $ON ]; then
			/usr/bin/cp -p $SAVEWPAR_IMAGE \
				${CD_FS_DIR}/usr/sys/inst.images/savewpar_image 2>$MSGBUF
		fi
	elif [ $total_size -le $CD_FS_MIN_SIZE ]; then
##########################################################
## DVD only.
## Copy the mksysb or savevg image to the appropriate
## location in the CD file system.  Greater than 2GB
## but small enough to fit on a single DVD.
##########################################################
		if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || \
			[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$LESS_THAN_2GB_2048 if=$MKSYSB_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image 2>$MSGBUF
			dd bs=$BS_SIZE skip=$LESS_THAN_2GB_2048 if=$MKSYSB_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image2 2>$MSGBUF
		elif [ $SAVEVG_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEVG_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$LESS_THAN_2GB_2048 if=$SAVEVG_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image 2>$MSGBUF
			dd bs=$BS_SIZE skip=$LESS_THAN_2GB_2048 if=$SAVEVG_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image2 2>$MSGBUF
		elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEWPAR_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$LESS_THAN_2GB_2048 if=$SAVEWPAR_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image 2>$MSGBUF
			dd bs=$BS_SIZE skip=$LESS_THAN_2GB_2048 if=$SAVEWPAR_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image2 2>$MSGBUF
		fi

		if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

##########################################################
## If the mksysb did not completely fit in the CD file
## system, then determine how many blocks can be dd'ed to
## the file system, set the multi-volume flag for
## additional volume processing.
## NOTE:  dd in 2048 byte blocks
##        PADDING_128K for firmware problem
##########################################################

	else

		(( DD_SIZE = (CD_FS_MIN_SIZE - fs_size - boot_image_size - EFI_PARTITION_SIZE - PADDING_128K ) / 4096 ))
		dd_size2=0
		dd_size1=$DD_SIZE
		if (( DD_SIZE > LESS_THAN_2GB_2048 )); then
			(( dd_size2 = ( DD_SIZE - LESS_THAN_2GB_2048 ) ))
			dd_size1=$LESS_THAN_2GB_2048
		fi
		(( CD_FS_MIN_512 = CD_FS_MIN_512 - ( dd_size1 + dd_size2 ) * 8 ))
		if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || \
			[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$dd_size1 if=$MKSYSB_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image 2>$MSGBUF
			if (( dd_size2 != 0 )); then
				dd bs=$BS_SIZE count=$dd_size2 skip=$dd_size1 if=$MKSYSB_IMAGE \
					of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image2 2>$MSGBUF
			fi
		elif [ $SAVEVG_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEVG_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$dd_size1 if=$SAVEVG_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image 2>$MSGBUF
			if (( dd_size2 != 0 )); then
				dd bs=$BS_SIZE count=$dd_size2 skip=$dd_size1 if=$SAVEVG_IMAGE \
					of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image2 2>$MSGBUF
			fi
		elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] || \
			[ $SAVEWPAR_COMPLETE_FLAG -eq $ON ]; then
			dd bs=$BS_SIZE count=$dd_size1 if=$SAVEWPAR_IMAGE \
				of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image 2>$MSGBUF
			if (( dd_size2 != 0 )); then
				dd bs=$BS_SIZE count=$dd_size2 skip=$dd_size1 if=$SAVEWPAR_IMAGE \
					of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image2 2>$MSGBUF
			fi
		fi

		if [ $? -ne 0 ]; then     ##  dd command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		MULTI_VOLUME_FLAG=$ON
		MORE_CDS=$ON

	fi
	if [ -n "$SLEEP_PID" ]
	then
		kill_pid "$SLEEP_PID"
		echo ""
	fi

} ## end of copy_backup


######################### copy_bundle_file #############################
##
## Name:  copy_bundle_file
##
## Function:  The copy_bundle_file function is responsible for copying
##            any user specified bundle file to the CD file structure
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_bundle_file
{
typeset b_file
typeset mesg

##########################################################
## If a user supplies a bundle file, then copy the file
## to the appropriate file location in the CD file
## structure.  Ensure that the bundle file directory
## exists in the CD file structure, before copying
## the file.
##########################################################

	if [ $BUNDLE_FILE_FLAG -eq $ON ]; then

		if [ ! -d "${CD_FS_DIR}/usr/sys/inst.data/user_bundles" ]; then
			/usr/bin/mkdir -p ${CD_FS_DIR}/usr/sys/inst.data/user_bundles 2>$MSGBUF
			if [ $? -ne 0 ]; then     ##  mkdir command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

		b_file=`basename $BUNDLE_FILE`     ## eliminate the path

		/usr/bin/cp $BUNDLE_FILE \
			${CD_FS_DIR}/usr/sys/inst.data/user_bundles/${b_file} 2>$MSGBUF
		if [ $? -ne 0 ]; then     ##  cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

} ## end of copy_bundle_file


######################### copy_custom_script ###########################
##
## Name:  copy_custom_script
##
## Function:  The copy_custom_script function is responsible for copying
##            the customization file to the CD file system structure.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_custom_script
{
typeset mesg

##########################################################
## If a customization file is supplied by the user, then
## copy the file to the / directory of the CD file
## structure.
##########################################################

	if [ $CUSTOMIZATION_FILE_FLAG -eq $ON ]; then
		/usr/bin/cp $CUSTOMIZATION_FILE ${CD_FS_DIR}/ 2>$MSGBUF
		if [ $? -ne 0 ]; then     ##  cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

} ## end of copy_custom_script


######################### copy_data_files ##############################
##
## Name:  copy_data_files
##
## Function:  The copy_data_files function is responsible for copying
##            the data files to the CD file system structure.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_data_files
{
typeset mesg
typeset olddir

##########################################################
## If the user supplied a previously created mksysb or
## savewpar image or if a wpar image is being created...
##########################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || 
	     [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] ||
	     [ $WPAR_FLAG -eq $ON ] ; then

##########################################################
## If the user supplied an image.data file, then copy the
## file from the system root directory.  This file was
## previously copied to the root directory to exist on
## the mkcd created mksysb.
##########################################################

		if [ -n "$NEW_IMAGE_DATA" ]; then
			/usr/bin/cp $NEW_IMAGE_DATA ${CD_FS_DIR}/image.data 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## If the user has not supplied an image.data file, then
## restore the data file from the user supplied mksysb
## for mksysb images.  Just let restwpar get it out of
## the savewpar image.
##########################################################

		else
		    if [ $MKSYSB_IMAGE_FLAG -eq $ON  ]  
		    then
			olddir=`pwd`
			cd $CD_FS_DIR
			restore -xqf $MKSYSB_IMAGE ./image.data >/dev/null 2>$MSG_BUF
			if [ $? -ne 0 ]; then     ## restore command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
			cd $olddir
		    fi
		fi

##########################################################
## If the user supplied an bosinst.data file, then copy
## the file from the system root directory.  This file was
## previously copied to the root directory to exist on
## the mkcd created mksysb.
##########################################################

		if [ -n "$NEW_BOSINST_DATA" ]; then
			/usr/bin/cp $NEW_BOSINST_DATA ${CD_FS_DIR}/bosinst.data 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## If the user has not supplied an bosinst.data file, then
## restore the data file from the user supplied mksysb.
##########################################################

		elif [ $MKSYSB_IMAGE_FLAG -eq $ON ] ; then
			olddir=`pwd`
			cd $CD_FS_DIR
			restore -xqf $MKSYSB_IMAGE ./bosinst.data >/dev/null 2>$MSG_BUF
			if [ $? -ne 0 ]; then     ## restore command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
			cd $olddir
		fi

##########################################################
## If the user supplied a wpar specification file, then 
## copy the file into the cd file system.
##########################################################

		if [ "$WPAR_SPEC_FLAG" -eq "$ON" ]; then
			/usr/bin/cp $WPAR_SPEC_FILE ${CD_FS_DIR}/wpar.spec 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

#############################################################
## If the mkcd created a mksysb, then copy the data
## files from the system to the CD file structure.
#############################################################

	elif [ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then

		/usr/bin/cp /image.data ${CD_FS_DIR}/image.data 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		/usr/bin/cp /bosinst.data ${CD_FS_DIR}/bosinst.data 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

} ## end of copy_data_files


######################### copy_devices_packages ########################
##
## Name:  copy_devices_packages
##
## Function:  The copy_devices_packages function is responsible for
##            copying any necessary devices or filesets to the CD file
##            system.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_devices_packages
{
typeset orig_pkg_source_dir
typeset pkg_dev
typeset pkg_file
typeset ppc_or_ia64
typeset mesg
typeset -i error_rc

	error_rc=0
	ppc_or_ia64="ppc"

	if [ "$PLATFORM" = "ia64" ]; then
		ppc_or_ia64="ia64"
	fi
	/usr/bin/mkdir -p ${CD_FS_DIR}/installp/${ppc_or_ia64} 2>$MSGBUF
	/usr/bin/mkdir -p ${CD_FS_DIR}/RPMS/${ppc_or_ia64} 2>$MSGBUF
	/usr/bin/mkdir -p ${CD_FS_DIR}/udi/${ppc_or_ia64} 2>$MSGBUF
	/usr/bin/mkdir -p ${CD_FS_DIR}/ismp/${ppc_or_ia64} 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## mkdir command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		error_rc=1
	fi
##########################################################
## If the package source directory is not a directory
## then ensure the source directory is a CD device.
##########################################################

	if [ ! -d "$PKG_SOURCE_DIR" ] &&
		[ -n "$PKG_SOURCE_DIR" ]; then
		orig_pkg_source_dir=$PKG_SOURCE_DIR
		pkg_dev=`basename $PKG_SOURCE_DIR`
		PKG_SOURCE_DIR=/dev/${pkg_dev}
		CD_DEV=`LANG=C /usr/sbin/lsdev -Cc cdrom -l $pkg_dev -S available`

##########################################################
## If the package source directory is a CD device, then
## create a temporary mount point and mount the device.
##########################################################

		if [ -n "$CD_DEV" ]; then

			/usr/bin/mkdir -p $TMP_CD_MOUNT 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## mkdir command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				error_rc=1
				CD_DEV=
			else

				/usr/sbin/mount -v'cdrfs' -r $PKG_SOURCE_DIR \
					$TMP_CD_MOUNT 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mount command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					error_rc=1
				else

##########################################################
## Assign the package source directory to the mounted CD's
## installp/<arch> directory.
##########################################################

					PKG_SOURCE_DIR=$TMP_CD_MOUNT
				fi
			fi
		else
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 99 \
				"0512-311 $NAME: The device or directory: $orig_pkg_source_dir does \
not exist.\n" $NAME $orig_pkg_source_dir`
			log_print "$mesg"
			cleanup 1
		fi
	fi

##########################################################
## If the user has specified a package list to be added
## to the CD, then copy each package listed, from the
## package source directory.
##########################################################

	if [ $STACK_LIST_FLAG -eq $ON ] && \
		[ $error_rc -eq 0 ]; then

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 71 \
			"Copying package list to the CD or DVD file system...\n"`
			log_print "$mesg"

		/usr/bin/cat $STACK_LIST | while read pkg_file; do
			/usr/bin/cp -p ${PKG_SOURCE_DIR}/installp/${ppc_or_ia64}/${pkg_file} \
				${CD_FS_DIR}/installp/${ppc_or_ia64} 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cp -p ${PKG_SOURCE_DIR}/RPMS/${ppc_or_ia64}/${pkg_file} \
				${CD_FS_DIR}/RPMS/${ppc_or_ia64} 2>$MSGBUF
			fi
			if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cp -p ${PKG_SOURCE_DIR}/udi/${ppc_or_ia64}/${pkg_file} \
				${CD_FS_DIR}/udi/${ppc_or_ia64} 2>$MSGBUF
			fi
			if [ $? -ne 0 ]; then     ## cp command return code
			/usr/bin/cp -p ${PKG_SOURCE_DIR}/ismp/${ppc_or_ia64}/${pkg_file} \
				${CD_FS_DIR}/ismp/${ppc_or_ia64} 2>$MSGBUF
			fi
			if [ $? -ne 0 ]; then     ## cp command return code
				cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				error_rc=1
			fi
		done
	fi

##########################################################
## If an error occurs during this function (error_rc=1),
## then exit with an error return code.
##########################################################

	if (( error_rc == 1 )); then
		cleanup 1
	fi

##########################################################
## If the package source is a CD device, then unmount
## the device and remove the mount point.
##########################################################

	if [ -n "$CD_DEV" ]; then
		/usr/sbin/umount $TMP_CD_MOUNT 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## umount command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi

		rmdir $TMP_CD_MOUNT 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## rmdir command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		else
			CD_DEV=

##########################################################
## Check to if the package source device is the same
## device as the CD-R device and the user hasn't specified
## to stop the command before writing a CD.  If so, then
## prompt the user to change the CDs.
##########################################################

			if [ "/dev/${pkg_dev}" = "$DEVICE" ] && \
				[ $STOP_FLAG -eq $OFF ]; then
				change_cd
			fi
		fi
	fi

##########################################################
## Build a Table of Contents in the installp/<arch>
##########################################################

	/usr/sbin/inutoc ${CD_FS_DIR}/installp/${ppc_or_ia64} 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## inutoc command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
	fi

} ## end of copy_devices_packages


######################### copy_lang_support ############################
##
## Name:  copy_lang_support
##
## Function:  The copy_lang_support function is responsible for copying
##            the language support that is installed on the running
##            system.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_lang_support
{
typeset each_cat
typeset mesg

##########################################################
## The language support (BosMenus.cat) is only necessary
## for bootable CDs.
##########################################################

	if [ $NONBOOT_FLAG -eq $OFF ]; then

##########################################################
## If language support for BosMenus exists on the running
## system, then copy the catalog file to the CD file
## system.
##########################################################

		for each_cat in `/usr/bin/find /usr/lib/nls/msg -name BosMenus.cat`; do
			/usr/bin/mkdir -p ${CD_FS_DIR}`dirname $each_cat` 2>$MSGBUF
			if [ $? -ne 0 ]; then     ##  mkdir command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
			else
				/usr/bin/cp -p $each_cat ${CD_FS_DIR}${each_cat} 2>$MSGBUF
				if [ $? -ne 0 ]; then     ##  cp command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
				fi
			fi
		done
	fi

} ## end of copy_lang_support


######################### copy_map_files ###############################
##
## Name:  copy_map_files
##
## Function:  The copy_map_files function is responsible for copying
##            the map files to the CD file system structure, if
##            mapping is requested or included in the user supplied
##            mksysb.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function copy_map_files
{
typeset exact_fit
typeset mesg
typeset olddir

##########################################################
## If a mksysb was created by mkcd and mapping was
## requested, then create the map file directories in the
## CD file system structure and copy the map files.
##########################################################

	if [ $MKSYSB_COMPLETE_FLAG -eq $ON ] && \
		[ $MAPPING_FLAG -eq $ON ]; then

		/usr/bin/mkdir -p ${CD_FS_DIR}/tmp/vgdata/rootvg 2>$MSGBUF
		if [ $? -ne 0 ]; then     ##  mkdir command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		/usr/bin/cp -p -R /tmp/vgdata/rootvg/* \
			${CD_FS_DIR}/tmp/vgdata/rootvg 2>$MSGBUF
		if [ $? -ne 0 ]; then     ##  cp command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

##########################################################
## If a user supplied a previously created mksysb, then
## use the image.data from the backup to determine if
## mapping is needed.  If so, then restore the map files
## from the mksysb to the CD file system structure.
##########################################################

	elif [ $MKSYSB_IMAGE_FLAG -eq $ON ]; then

		exact_fit=`get_stanza_data "logical_volume_policy" "EXACT_FIT" "" \
			"EXACT_FIT" "${CD_FS_DIR}/image.data"`
		if [ "$exact_fit" = "yes" ]; then

			olddir=`pwd`
			cd $CD_FS_DIR
			restore -xdqf $MKSYSB_IMAGE ./tmp/vgdata/rootvg >/dev/null 2>$MSG_BUF
			if [ $? -ne 0 ]; then     ## restore command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
			cd $olddir
		fi
	fi

} ## end of copy_map_files


######################### create_boot_cd ###############################
##
## Name:  create_boot_cd
##
## Function:  The create_boot_cd function is responsible for making a
##            a bootable CD image.  It requires previously created
##            boot images from the build_boot_images routine.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function create_boot_cd
{
typeset boot_img_args
typeset mesg
typeset mesgtype

	if [ $NONBOOT_FLAG -eq $OFF ]; then

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 88 \
			"Making the CD or DVD image bootable...\n"`
		log_print "$mesg"

##########################################################
## If a personal CD is being created, there are cases
## where the system seems to only boot if additional
## padding is appended to the ISO image. Therefore, dummmy
## images are created and added to the CD image, while
## adding the actual boot image.
##########################################################

		if [ "$PLATFORM" = "ia64" ] || [ $CREATE_EFI_PARTITION -eq $ON ]; then
	####################################################################
	## For the IA64 platform, create an El Torito boot image (-e).
	## The cdboot.img file has already been placed in the Rock Ridge
	## file system.  The cdboot command will find it and set up the
	## appropriate pointer so the IA64 firmware will be able to find it.
	####################################################################
			/usr/lpp/bosinst/cdboot -e -d $CD_IMAGE_NAME 2>$MSGBUF

		elif [ "$PLATFORM" = "chrp" ]; then
			dd if=/dev/zero of=/tmp/bosboot.cd.dummy count=8 bs=512 2>$MSGBUF
			/usr/lpp/bosinst/cdboot -c -d $CD_IMAGE_NAME \
				-p /tmp/bosboot.cd.dummy -1 /tmp/bosboot.cd.dummy 2>$MSGBUF

		elif [ "$PLATFORM" = "BULL_chrp" ]; then
			dd if=/dev/zero of=/tmp/bosboot.cd.dummy count=8 bs=512 2>$MSGBUF
			/usr/lpp/bosinst/cdboot -c -d $CD_IMAGE_NAME \
				-p /tmp/bosboot.cd.dummy -3 /tmp/bosboot.cd.dummy 2>$MSGBUF

		fi
	fi

	if [ $? -ne 0 ]; then     ## cdboot command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

##########################################################
## If the cdboot command worked, then pad the final CD
## image with PADDING_128K (128K of zeros).  This fixes a
## firmware shortcoming, where the firmware is not
## reading all of the boot image.
## Skip this step if creating an El Torito image.
##########################################################

	if [[ "$PLATFORM" != "ia64" && $CREATE_EFI_PARTITION = $OFF ]]; then
		dd if=/dev/zero bs=4096 count=32 >> $CD_IMAGE_NAME 2>/dev/null
	fi

} ## end of create_boot_cd


######################### create_cd_fs #################################
##
## Name:  create_cd_fs
##
## Function:  The create_cd_fs function is responsible for creating
##            the default CD file system or verifying the existing
##            default CD file system has adequate space.  The the
##            routine will populate the file system structure, copy
##            the backup, and create the necessary files for the CD
##            type being created.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function create_cd_fs
{
typeset mesg
typeset -i cd_fs_size
typeset -i needed_space

##########################################################
## If the CD file system was not given by the user and
## the default CD file system does not exist, then
## create the file system.
##########################################################

	if [ $CD_FS_DIR_FLAG -eq $OFF ]; then
		if [[ UDF_FLAG -eq $OFF ]];then
			if [ ! -d "$CD_FS" ]; then

				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
					"Creating temporary file system: ${CD_FS}...\n" $CD_FS`
				log_print "$mesg"

##########################################################
## Check actual size of backup and create file system big
## enough to hold it.
##########################################################

				(( CD_FS_NEW_MIN_512 = ( BACKUP_SIZE + BOOT_IMAGE_SIZE_OPT4 + EFI_PARTITION_SIZE + PADDING_128K + PADDING_20MB ) / 512 ))
				(( CD_FS_NEW_CREATE_SIZE_512 = CD_FS_NEW_MIN_512 * 100 / 96 + 1 )) ## 672 MB
				if (( CD_FS_NEW_CREATE_SIZE_512 < CD_FS_CREATE_SIZE_512 )); then
					CD_FS_CREATE_SIZE_512=$CD_FS_NEW_CREATE_SIZE_512
				fi

##########################################################
## If we copy install images into the file system,
## we will want to be able to increase the size of the
## filesystem later, specifically in the copy_backup
## function.
##########################################################

				ALLOW_INCREASE_CD_FS=$ON
				crfs -g"$CDFS_VOLUME_GROUP" -a size="$CD_FS_CREATE_SIZE_512" \
					$BIGFILE \
					-m "$CD_FS" -p "rw" -v $FTYPE \
					-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
				if [ $? -ne 0 ]; then     ## crfs command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

##########################################################
## Once the file system is created, then mount it and
## create the CD file system directory
##########################################################

				/usr/sbin/mount $CD_FS
				if [ $? -ne 0 ]; then     ## mount command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

				/usr/bin/mkdir -p $CD_FS_DIR 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

##########################################################
## If the default file system does exist, then make the
## PID directory and ensure there is adequate space
## available.
##########################################################

			else

##########################################################
## If the CD file system was user supplied or
## existed, then save the location information so it will
## not be removed in the file_system_cleanup routine.
## Also, set the CD_FS_DIR_FLAG, as if it were user specified.
##########################################################

				EXISTING_FS="$CD_FS $EXISTING_FS"
				CD_FS_DIR_FLAG=$ON
				/usr/bin/mkdir -p $CD_FS_DIR 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

				cd_fs_size=`/usr/bin/df $CD_FS_DIR/ | tail -1 | awk '{print$3}'`
				(( needed_space = CD_FS_MIN_512 - cd_fs_size ))
				if (( needed_space > 0 )); then
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 "0512-329 $NAME: User specified CD or DVD file system: ${CD_FS}\n\ needs an additional ${needed_space} bytes.\n" $NAME $CD_FS $needed_space`
					log_print "$mesg"
					cleanup 1
				fi
			fi
		else
			mkdir -p $CD_FS_DIR
			/usr/sbin/mount -v udfs $DEVICE $CD_FS_DIR
			if [ $? -ne 0 ]; then ## mount command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi
	else
		EXISTING_FS="$CD_FS $EXISTING_FS"
	fi

##########################################################
## Ensure the CD file system directory for the backup
## has been created.
##########################################################

	if [ ! -d "${CD_FS_DIR}/usr/sys/inst.images" ]; then
		/usr/bin/mkdir -p ${CD_FS_DIR}/usr/sys/inst.images 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## mkdir command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

	copy_devices_packages     ## copy devices/packages to cdfs

##########################################################
## If the CD is a rootvg image, populate the CD file
## system according to the cdfs.required.list proto file.
## All files in the required list must exist and be
## placed in the CD FS or an error will occur and the
## command will fail.
##########################################################

	if [ $SAVEVG_VOLUME_GROUP_FLAG -eq $OFF ] && \
		[ $SAVEVG_IMAGE_FLAG -eq $OFF ] && \
		[ $SAVEWPAR_IMAGE_FLAG -eq $OFF ] && \
		[ $WPAR_FLAG -eq $OFF ]; then

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 68 \
			"Populating the CD or DVD file system...\n"`
		log_print "$mesg"

		populate_cd_fs $CD_FS_DIR $CDFS_REQUIRED_LIST $OFF
		if [ $? -ne 0 ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 115 \
				"0512-321 $NAME: Error populating the CD or DVD file system\n\
using the $CDFS_REQUIRED_LIST proto file.\n" $NAME $CDFS_REQUIRED_LIST`
			log_print "$mesg"
			cleanup 1
		fi

##########################################################
## Now, populate the CD file system according to the
## cdfs.optional.list proto file.
##########################################################

		if [ "$PLATFORM" = "ia64" ];then
			if [ -s $CDFS_DONT_ADD_LIST ];then
				lslpp -fc devices* | awk 'BEGIN {FS=":| "} /methods|drivers/{print $4 " " $4 " " $2}' | \
				egrep -v "/methods |/drivers |/usr/lib/drivers/pci |/usr/lib/drivers/isa " | \
				egrep -v -f $CDFS_DONT_ADD_LIST > $CDFS_OPTIONAL_LIST_TMP
			else
				lslpp -fc devices* | awk 'BEGIN {FS=":| "} /methods|drivers/{print $4 " " $4 " " $2}' | \
				egrep -v "/methods |/drivers |/usr/lib/drivers/pci |/usr/lib/drivers/isa "  > $CDFS_OPTIONAL_LIST_TMP
			fi
			CDFS_OPTIONAL_LIST=$CDFS_OPTIONAL_LIST_TMP
		fi

		populate_cd_fs $CD_FS_DIR $CDFS_OPTIONAL_LIST $ON
		if [ $? -ne 0 ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 115 \
				"0512-321 $NAME: Error populating the CD or DVD file system\n\
using the $CDFS_OPTIONAL_LIST proto file.\n" $NAME $CDFS_OPTIONAL_LIST`
			log_print "$mesg"
                        mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 122 \
				"0512-328 $NAME: The CD or DVD may not be able to boot some systems.\n" $NAME`
			log_print "$mesg"
		fi
		rm -f $CDFS_OPTIONAL_LIST_TMP 2>/dev/null

##########################################################
## If the CD is being created for a Bull machine, then we
## must populate the CD with Bull dependent files.
##########################################################

		if [ `echo $PLATFORM | grep -ci bull` -ne 0 ] || \
			[ "$CREATE_BULL_BOOTS" = "yes" ]; then

			populate_cd_fs $CD_FS_DIR $CDFS_BULL_LIST $ON
			if [ $? -ne 0 ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 115 \
					"0512-321 $NAME: Error populating the CD or DVD file system\n\
using the $CDFS_BULL_LIST proto file.\n" $NAME $CDFS_BULL_LIST`
				log_print "$mesg"
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 122 \
					"0512-328 $NAME: The CD or DVD may not be able to boot some systems.\n" $NAME`
				log_print "$mesg"
			fi
		fi
	fi

	copy_data_files     ## copy the image.data & bosinst.data files to cdfs

	create_oslevel_file     ## create OSLEVEL file

	copy_bundle_file     ## copy a bundle file to cdfs, if necessary

	copy_custom_script     ## copy the customization file to cdfs

	copy_lang_support     ## copy language support to cdfs

	copy_map_files     ### copy files for mapping to cdfs

	copy_backup     ## copy the mksysb or savevg to cdfs

	create_mkcd_data_file     ## create the mkcd.data file

} ## end of create_cd_fs


######################### create_cd_images_fs ##########################
##
## Name:  create_cd_images_fs
##
## Function:  The create_cd_images_fs function is responsible for
##            creating the default file system for the CD image location
##            or verifying the existing default location has adequate
##            space.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_rockridge_fs
##
########################################################################

function create_cd_images_fs
{
typeset mesg
typeset -i cd_fs_size
typeset -i needed_space
typeset -i num_cds

##########################################################
## If the CD image file system was not given by the user
## and the default CD file system does not exist, then
## create the file system.
##########################################################

	if [ $CD_IMAGE_DIR_FLAG -eq $OFF ]; then
		if [ ! -d "$CD_IMAGE_DIR" ]; then

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
				"Creating temporary file system: ${CD_IMAGE_DIR}...\n" \
				$CD_IMAGE_DIR`
			log_print "$mesg"
			if (( CD_IMAGE_CREATE_SIZE_512 == 0 )) &&
				(( $CD_FS_NEW_CREATE_SIZE_512 > 0 )); then
				CD_IMAGE_CREATE_SIZE_512=$CD_FS_NEW_CREATE_SIZE_512
			else
				(( num_cds = BACKUP_SIZE / CD_FS_MIN_SIZE + 1 ))
				(( CD_IMAGE_MIN_512 = ( BACKUP_SIZE + (POPULATE_CDFS_SIZE_USED * num_cds) + BOOT_IMAGE_SIZE_OPT4 + EFI_PARTITION_SIZE + PADDING_128K ) / 512 + INCREASE_FS_512 ))
				(( CD_IMAGE_CREATE_SIZE_512 = CD_IMAGE_MIN_512 * 100 / 96 + 1 )) ## 672 MB
			fi

			if [ $STOP_FLAG -eq $OFF ] &&
				[ $LEAVE_IMAGE_FLAG -eq $OFF ] &&
				[ $CD_IMAGE_CREATE_SIZE_512 -gt $CD_FS_MAX_512 ];then
				CD_IMAGE_CREATE_SIZE_512=$CD_FS_MAX_512
			fi

			crfs -g"$CDFS_VOLUME_GROUP" -a size="$CD_IMAGE_CREATE_SIZE_512" \
				$BIGFILE \
				-m "$CD_IMAGE_DIR" -p "rw" -v $FTYPE \
				-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
			if [ $? -ne 0 ]; then     ## crfs command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi

			if (( $CD_FS_CHANGED > 0 )); then
				/usr/sbin/chfs -a size=+$CD_FS_CHANGED $CD_IMAGE_DIR
				if [ $? -ne 0 ]; then
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi
			fi


##########################################################
## Once the file system is created, then mount it and
## create the CD file system directory
##########################################################

			/usr/sbin/mount $CD_IMAGE_DIR 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## mount command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## Set CD_IMAGE_DIR_SET = ON, so that if this is
## a multi-volume CD, the second time through,
## it will still be known that the fs was created
## and should be cleaned up at the end.
##########################################################

			[ $MULTI_VOLUME_FLAG -eq $ON ] && CD_IMAGE_DIR_SET=$ON

##########################################################
## If the default file system does exist, ensure there
## is adequate space available.
##########################################################

		else

##########################################################
## If the CD image directory/filesystem was user
## supplied or existed, then save the location
## information so it will not be removed in the
## file_system_cleanup routine.
## If multi-volume processing, this routine is executed
## multiple times, and after the first execution the
## CD_IMAGE_DIR will exist.  CD_IMAGE_DIR_SET is set
## to ON, if multivolume processing, so that the file
## system is known to have not existed originally.
##########################################################

			if [ $CD_IMAGE_DIR_SET -eq $OFF ]; then
				EXISTING_FS="$CD_IMAGE_DIR $EXISTING_FS"
				CD_IMAGE_DIR_FLAG=$ON
			fi

			if [ $LEAVE_IMAGE_FLAG -eq $ON ] || [ $STOP_FLAG -eq $ON ]; then
				cd_fs_size=`/usr/bin/df $CD_IMAGE_DIR/ | tail -1 | \
					awk '{print$3}'`
				(( needed_space = ( BACKUP_SIZE / 512 - DD_SIZE * 8 - cd_fs_size ) * 512 * 100 / 96 + 1 ))
				if (( needed_space > 0 )); then
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 \
					"0512-329 $NAME: User specified CD or DVD file system: ${CD_IMAGE_DIR} \n\
needs an additional ${needed_space} bytes.\n" $NAME $CD_IMAGE_DIR $needed_space`
					log_print "$mesg"
					cleanup 1
				fi
			fi
		fi
	else
		EXISTING_FS="$CD_IMAGE_DIR $EXISTING_FS"
	fi

} ## end of create_cd_images_fs


######################### create_mkcd_data_file #############################
##
## Name:  create_mkcd_data_file
##
## Function:  The create_mkcd_data_file function is responsible for creating
##            a mkcd.data file.  This file will contain a timestamp,
##            CD volume ID, and the number of files in the backup.  The
##            timestamp will be used to verify if the multi-volume CDs
##            belong to one another.  The volume ID will indicate the
##            volume number in a set,  If the volume ID is left blank,
##            then the CD is not multi-volume.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function create_mkcd_data_file
{
#typeset backup_file_number
typeset mkcd_data_file

##########################################################
## Create the mkcd.data, by adding the time stamp to the
## file.  This information will be used to verify that
## all the CD volumes are from the same creation.
##########################################################

	mkcd_data_file="/tmp/mkcd.data_${PID}"

	echo "DATE=$TIMESTAMP" >$mkcd_data_file

##########################################################
## Put the volume ID in the mkcd data file.  If the CD is
## the first of a one volume set or is the last CD in a
## multi-volume set, then put an annotation on the mkcd
## data file.
##########################################################

	echo "VOLUME=$VOLUME_ID" >>$mkcd_data_file
	if [ $MORE_CDS -eq $OFF ];then
		echo "LASTVOLUME" >>$mkcd_data_file
	fi

##########################################################
## Determine the number of files in the backup.  Then
## create an entry in the mkcd data file.  This
## information will be used by bi_main to display percent
## complete messages when restoring the backup.
##########################################################

#	backup_file_number=`get_backup_file_number`
#	echo "FILES=$backup_file_number" >>$mkcd_data_file

##########################################################
## Populate the mkcd data file with the CD type.
## When booting from the CD, bi_main will look for a
## TYPE=generic for special booting procedures.  The
## other listed TYPEs are for manual identification only.
##########################################################

	if [ $SAVEVG_VOLUME_GROUP_FLAG -eq $ON ] ||
			[ $SAVEVG_IMAGE_FLAG -eq $ON ] &&
			[ "$SAVEVG_VOLUME_GROUP" != "rootvg" ]; then
		echo "TYPE=savevg" >>$mkcd_data_file
	elif [ $SAVEWPAR_IMAGE_FLAG ] ||
			[ $WPAR_FLAG -eq $ON ]; then
		echo "TYPE=savewpar" >>$mkcd_data_file
	elif [ $NONBOOT_FLAG -eq $ON ];then
		echo "TYPE=rootvg(nonbootable)" >>$mkcd_data_file
	else
		echo "TYPE=rootvg(bootable)" >>$mkcd_data_file
	fi
	cp $mkcd_data_file ${CD_FS_DIR}/mkcd.data 2>$MSGBUF
	rm -f $mkcd_data_file 2>$MSGBUF
} ## end of create_mkcd_data_file

######################### create_efi_partition #################################
##
## Name:  create_efi_partition
##
## Function:  The create_efi_partition function is responsible for creating
##            an EFI partition and populating it with the list (efi_list) of
##            requested files.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function create_efi_partition
{
typeset efi_file
typeset mesg
typeset sys_file

##########################################################
## Populate the partition image.
##########################################################

	mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 81 \
			"Populating the EFI partition...\n"`
	log_print "$mesg"

##########################################################
# Update the mtools configuration
##########################################################
	export MTOOLSRC=${CD_FS_DIR}/mtools.conf
	echo "drive $DRIVE file=\"${CD_FS_DIR}/${CDBOOT_IMAGE}\"" >$MTOOLSRC

##########################################################
# Call function to create an empty EFI partition
##########################################################
	create_empty_efi_partition $DRIVE

##########################################################
## Copy each file in the efi partition list to it's
## specified location.
## Be careful to create all the necessary dos directories
## in the EFI partition
##########################################################


	/usr/bin/cat $EFI_PARTITION_PROTO | grep -v "^#" | \
	while read sys_file efi_file; do

		make_dos_dirs $efi_file
		${MBIN}/mcopy -o $sys_file ${DRIVE}${efi_file}

		if [ $? -ne 0 ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 82 \
				"0512-327 mkcd: Error populating the EFI partition \n\
using the $EFI_PARTITION_PROTO proto file.\n" $EFI_PARTITION_PROTO `
			log_print "$mesg"
		fi

	done
	make_dos_dirs /EFI/IA64/PRE_BOOT/
	if [ $? -ne 0 ]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 82 \
			"0512-327 mkcd: Error populating the EFI partition \n\
using the $EFI_PARTITION_PROTO proto file.\n" $EFI_PARTITION_PROTO `
		log_print "$mesg"
	fi

##########################################################
# Restore the mtools configuration
##########################################################
	rm $MTOOLSRC
	unset MTOOLSRC

} ## end of create_efi_partition

######################### create_empty_efi_partition ###################
##
## Name:  create_empty_efi_partition
##
## Function:  The create_empty_efi_partition function is responsible for
##            creating an empty EFI partition by copying one from a supplied
##            or default location.  This function also creates an empty
##            bootcat file.
##
## Parameters:  Input drive (example -- q:)
##
## Returns:  None
##
## Calling Routine:  create_efi_partition
##
########################################################################

function create_empty_efi_partition
{
typeset mesg

################################################################
## Copy the EFI partition
################################################################

	if [ "$PLATFORM" = "ia64" ]; then
		dd if=/dev/zero of=${CD_FS_DIR}/${CDBOOT_IMAGE} bs=1024 \
			count=32768 >/dev/null 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## dd command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
		LANG=C bosboot -ad/dev/cd0
		if [ $? -ne 0 ]; then
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
		/usr/sbin/eformat -D ${CD_FS_DIR}/${CDBOOT_IMAGE} -d
	elif [[ -n $EFI_PARTITION_LOCATION ]]; then
		cp $EFI_PARTITION_LOCATION ${CD_FS_DIR}/${CDBOOT_IMAGE} 2>$MSGBUF
	else
		cp /mkcd/cd_fs/cdboot.img.CD ${CD_FS_DIR}/${CDBOOT_IMAGE} 2>$MSGBUF
	fi
	if [ $? -ne 0 ]; then     ## cp command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

	/usr/bin/chmod +w ${CD_FS_DIR}/${CDBOOT_IMAGE} 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## chmod command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

##########################################################
## Check the sanity of the empty EFI partition.
##########################################################

	${MBIN}/mdir $DRIVE >/dev/null 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## mdir command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

##########################################################
## Create an empty bootcat file
##########################################################

	dd if=/dev/zero of=${CD_FS_DIR}/${BOOTCAT} bs=1024 count=2 \
		>/dev/null 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## dd command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

	/usr/bin/chmod +w ${CD_FS_DIR}/${BOOTCAT} 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## chmod command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi

} ## end of create_empty_efi_partition

######################## create_just_file_cdfs #################################
##
## Name:  create_just_file_cdfs
##
## Function:  The create_just_file_cdfs function is responsible for creating
##            the default CD file system or verifying the existing
##            default CD file system has adequate space.  Then the
##            routine will populate the file system structure specified in
##            "just_file" provided.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function create_just_file_cdfs
{
typeset mesg

##########################################################
## If the CD file system was not given by the user and
## the default CD file system does not exist, then
## create the file system.
##########################################################

	if [ $CD_FS_DIR_FLAG -eq $OFF ]; then
		if [[ $UDF_FLAG -eq $OFF ]]; then
			if [ ! -d "$CD_FS" ]; then

##########################################################
## Check input file to determine required size of
## filesystem.
##########################################################
				size_input_file_list $JUST_FILE

				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
				"Creating temporary file system: ${CD_FS}...\n" $CD_FS`
				log_print "$mesg"

				crfs -g"$CDFS_VOLUME_GROUP" -a size="$CD_FS_CREATE_SIZE_512" \
					$BIGFILE \
					-m "$CD_FS" -p "rw" -v $FTYPE \
					-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
				if [ $? -ne 0 ]; then     ## crfs command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

##########################################################
## Once the file system is created, then mount it and
## create the CD file system directory
##########################################################

				/usr/sbin/mount $CD_FS
				if [ $? -ne 0 ]; then     ## mount command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

				/usr/bin/mkdir -p $CD_FS_DIR 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

##########################################################
## If the default file system does exist, then make the
## PID directory and ensure there is adequate space
## available.
##########################################################

			else

##########################################################
## If the CD file system was user supplied or
## existed, then save the location information so it will
## not be removed in the file_system_cleanup routine.
##########################################################

				EXISTING_FS="$CD_FS $EXISTING_FS"
				CD_FS_DIR_FLAG=$ON
				/usr/bin/mkdir -p $CD_FS_DIR 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

				if ! size_input_file_list $JUST_FILE $CD_FS; then
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 106 \
						"0512-319 $NAME: Standard CD file system: ${CD_FS}\n\
does not have enough free space to hold the CD file structure.\n\
The minimum free space required is $CD_FS_MIN_SIZE bytes.\n" \
						$NAME $CD_FS $CD_FS_MIN_SIZE`
					log_print "$mesg"
					cleanup 1
				fi
			fi
		else
			mkdir -p $CD_FS_DIR
			/usr/sbin/mount -v udfs $DEVICE $CD_FS_DIR
			if [ $? -ne 0 ]; then  ## mount command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi
	else
		EXISTING_FS="$CD_FS $EXISTING_FS"

		if [ ! -d "$CD_FS_DIR" ]; then
			/usr/bin/mkdir -p $CD_FS_DIR 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## mkdir command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

##########################################################
## Ensure the CD file system has enough space
##########################################################

		if ! size_input_file_list $JUST_FILE $CD_FS_DIR; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 114 \
				"0512-319 $NAME: Standard CD or DVD file system: ${CD_FS_DIR}\n\
does not have enough free space to hold the CD or DVD file structure.\n\
The minimum free space required is $CD_FS_MIN_SIZE bytes.\n" \
						$NAME $CD_FS_DIR $CD_FS_MIN_SIZE`
			log_print "$mesg"
			cleanup 1
		fi

	fi

##########################################################
## Ensure the CD file system directory for the backup
## has been created.
##########################################################

	if [ ! -d "${CD_FS_DIR}/usr/sys/inst.images" ]; then
		/usr/bin/mkdir -p ${CD_FS_DIR}/usr/sys/inst.images 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## mkdir command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

##########################################################
## Populate the CD file system according to the just_file.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 68 \
			"Populating the CD or DVD file system...\n"`
		log_print "$mesg"

		populate_cd_fs $CD_FS_DIR $JUST_FILE $OFF
		if [ $? -ne 0 ]; then
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 115 \
				"0512-321 $NAME: Error populating the CD or DVD file system\n\
using the $JUST_FILE proto file.\n" $NAME $JUST_FILE`
			log_print "$mesg"
			cleanup 1
		fi

} ## end of create_just_file_cdfs

######################### create_oslevel_file ##########################
##
## Name:  create_oslevel_file
##
## Function:  The create_oslevel_file function is responsible for
##            creating an OSLEVEL file on the CD.  This file will
##            contain an OSLEVEL variable listing of the V.R.M.F
##            for the mksysb.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  create_cd_fs
##
########################################################################

function create_oslevel_file
{
typeset oslevel

##########################################################
## Only create the OSLEVEL file if the command is
## creating or using a mksysb backup.
##########################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || \
		[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then

##########################################################
## If this is a mksysb backup, then get the OSLEVEL from
## the image.data file, located in the / directory of the
## CD file system structure.
##########################################################

		oslevel=`get_stanza_data "image_data" "" "" "OSLEVEL" \
			${CD_FS_DIR}/image.data`

##########################################################
## Create the OSLEVEL file from the OSLEVEL information
## determined from the above logic.
##########################################################

		echo "OSLEVEL=$oslevel"> ${CD_FS_DIR}/OSLEVEL
	fi

} ## end of create_oslevel_file


######################### create_rockridge_fs ##########################
##
## Name:  create_rockridge_fs
##
## Function:  The create_rockridge_fs function is responsible for
##            not only naming the Rockridge CD image (volume included),
##            but also calling the mkrr_fs link to the script that calls
##            the OEM routines for creating the Rockridge file system
##            image.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_multivolume & main
##
########################################################################

function create_rockridge_fs
{
typeset mesg
typeset cmdname

##########################################################
## If the CD image will remain after the CD has been
## "burned", multi-volume CD will contain a volume
## suffix.  All CD images will conatin a PID number to
## remain unique.
##########################################################

	if [ $STOP_FLAG -eq $ON ] || \
		[ $LEAVE_IMAGE_FLAG -eq $ON ] && \
		[ $MULTI_VOLUME_FLAG -eq $ON ]; then
		CD_IMAGE_NAME="${CD_IMAGE_DIR}/cd_image_${PID}.vol${VOLUME_ID}"
	else
		CD_IMAGE_NAME="${CD_IMAGE_DIR}/cd_image_${PID}"
	fi

##########################################################
## Create Rockridge file system from the user supplied
## link to the sample script.  The script should expect
## to receive a target and source directory, in that
## order.
##########################################################

	create_cd_images_fs

	mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 80 \
		"Creating Rock Ridge format image: ${CD_IMAGE_NAME}\n" $CD_IMAGE_NAME`
	log_print "$mesg"

	sleep_count=1
	while :; do
		while [ "$sleep_count" -le 45 ]
		do
			sleep 10
			echo ".\c"
			let "sleep_count = sleep_count + 1"
		done
		echo ""
		sleep_count=1
	done &
	SLEEP_PID=$!
	/usr/sbin/mkrr_fs $DVD_FLAG $CD_IMAGE_NAME $CD_FS_DIR
	if [ $? -ne 0 ]; then     ## mkrr_fs command return code
		cmdname=mkrr_fs
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 107 \
			"0512-324 $NAME: The $cmdname command failed.  Check the linked\n\
OEM script for possible errors.\n" $NAME $cmdname`
		log_print "$mesg"
		cleanup 1
	fi
	if [ -n "$SLEEP_PID" ]
	then
		kill_pid "$SLEEP_PID"
		echo ""
	fi

} ## end of create_rockridge_fs


######################### debug_switch #################################
##
## Name:  debug_switch
##
## Function:  The debug_switch function is responsible for toggling the
##            debug functionality of mkcd.  If the debug switch is on,
##            then the shell debug capability will be used.  The
##            function was written to allow for debugging to be turned
##            off, mkcd does not use this portion of the function.
##
## Parameters:  ON or OFF
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function debug_switch
{
typeset debug_flag=$1

typeset i

	if [ $debug_flag -eq $ON ]; then

		set -x     ## turn on command and function tracing
		for i in $(typeset +f)
		do
			typeset -ft $i
		done

	else
		set +x     ## turn off command and function tracing
	fi

} ## end of debug_switch


######################### determine_conflicting_options ################
##
## Name:  determine_conflicting_options
##
## Function:  The determine_conflicting_options function is responsible
##            for calling the routine that determines if the command
##            options are compatible.  This function check each option
##            conflict and ensures that dependent options are set with
##            each called option.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function determine_conflicting_options
{

##########################################################
## Flags that cannot be used together
##########################################################

	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             M $MKSYSB_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             P $MAPPING_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             e $EXCLUDE_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             v $SAVEVG_VOLUME_GROUP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			m $MKSYSB_IMAGE_FLAG             W $WPAR_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             M $MKSYSB_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             P $MAPPING_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             b $BUNDLE_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             e $EXCLUDE_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             l $STACK_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             p $PKG_SOURCE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             v $SAVEVG_VOLUME_GROUP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             u $NEW_BOSINST_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             z $CUSTOMIZATION_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			s $SAVEVG_IMAGE_FLAG             W $WPAR_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      b $BUNDLE_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      l $STACK_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      p $PKG_SOURCE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      u $NEW_BOSINST_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      z $CUSTOMIZATION_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			v $SAVEVG_VOLUME_GROUP_FLAG      W $WPAR_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
			d $DEVICE_FLAG                   S $STOP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			B $NONBOOT_FLAG                  l $STACK_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			B $NONBOOT_FLAG                  p $PKG_SOURCE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			i $NEW_IMAGE_DATA_FLAG           P $MAPPING_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             m $MKSYSB_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             j $JUST_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             E $CREATE_EFI_PARTITION
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             v $SAVEVG_VOLUME_GROUP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             W $WPAR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             C $CD_FS_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             M $MKSYSB_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             V $CDFS_VOLUME_GROUP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             p $PKG_SOURCE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             B $NONBOOT_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             u $NEW_BOSINST_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             e $EXCLUDE_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             P $MAPPING_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             l $STACK_LIST_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             b $BUNDLE_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			r $MK_ROCKRIDGE_FLAG             z $CUSTOMIZATION_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			U $UDF_FLAG                   S $STOP_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			U $UDF_FLAG                   R $LEAVE_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			U $UDF_FLAG                   C $CD_FS_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			U $UDF_FLAG                   I $CD_IMAGE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			U $UDF_FLAG                   L $DVD_ISO9660_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               m $MKSYSB_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               W $WPAR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               p $PKG_SOURCE_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               u $NEW_BOSINST_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               j $JUST_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               r $MK_ROCKRIDGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			n $NO_DATA_FLAG               z $CUSTOMIZATION_FILE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			c $NO_PACK_FLAG               m $MKSYSB_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			c $NO_PACK_FLAG               s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			c $NO_PACK_FLAG               w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			c $NO_PACK_FLAG               r $MK_ROCKRIDGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			a $NO_EA_FLAG                 m $MKSYSB_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			a $NO_EA_FLAG                 s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			a $NO_EA_FLAG                 w $SAVEWPAR_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			a $NO_EA_FLAG                 r $MK_ROCKRIDGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			A $DMAPI_FLAG                 m $MKSYSB_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			A $DMAPI_FLAG                 s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			A $DMAPI_FLAG                 r $MK_ROCKRIDGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			A $DMAPI_FLAG                 w $SAVEWPAR_IMAGE_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
			w $SAVEWPAR_IMAGE_FLAG        W $WPAR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
			w $SAVEWPAR_IMAGE_FLAG        M $MKSYSB_DIR_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     v $SAVEVG_VOLUME_GROUP_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     m $MKSYSB_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     s $SAVEVG_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     W $WPAR_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     f $WPAR_SPEC_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     E $CREATE_EFI_PARTITION
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     r $MK_ROCKRIDGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     j $JUST_FILE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     w $SAVEWPAR_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     n $NO_DATA_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        N $NNFLAG                     G $NO_WPARS_FLAG

	compare_options $MUTUALLY_EXCLUSIVE \
                        N $NO_WPARS_FLAG              v $SAVEVG_VOLUME_GROUP_FLAG
        ## Same sets of excluded flags for -G as -N ##
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              m $MKSYSB_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              s $SAVEVG_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              W $WPAR_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              f $WPAR_SPEC_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              E $CREATE_EFI_PARTITION
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              r $MK_ROCKRIDGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              j $JUST_FILE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              w $SAVEWPAR_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        G $NO_WPARS_FLAG              n $NO_DATA_FLAG


        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              m $MKSYSB_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              s $SAVEVG_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              i $NEW_IMAGE_DATA_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              E $CREATE_EFI_PARTITION
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              r $MK_ROCKRIDGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              j $JUST_FILE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              w $SAVEWPAR_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        x $XFS_FLAG              n $NO_DATA_FLAG

        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              m $MKSYSB_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              s $SAVEVG_IMAGE_FLAG
	compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              E $CREATE_EFI_PARTITION
        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              r $MK_ROCKRIDGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              j $JUST_FILE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              w $SAVEWPAR_IMAGE_FLAG
        compare_options $MUTUALLY_EXCLUSIVE \
                        T $SNAP_FLAG              n $NO_DATA_FLAG

##########################################################
## Flags that must be used together
##########################################################

	compare_options $IN_CONJUNCTION \
			l $STACK_LIST_FLAG               p $PKG_SOURCE_DIR_FLAG
	compare_options $IN_CONJUNCTION \
			E $CREATE_EFI_PARTITION          j $JUST_FILE_FLAG
	compare_options $IN_CONJUNCTION \
			U $UDF_FLAG                      d $DEVICE_FLAG

	if [[ "$NO_DATA_FLAG" -eq "$ON" ]] && \
	   [[ "$SAVEVG_IMAGE_FLAG" -eq "$OFF" ]]
	then
		compare_options $IN_CONJUNCTION \
			n $NO_DATA_FLAG			v $SAVEVG_VOLUME_GROUP_FLAG
	fi

	if [[ "$WPAR_SPEC_FLAG" -eq "$ON" ]] && \
	   [[ "$SAVEWPAR_IMAGE_FLAG" -eq "$OFF" ]] && \
	   [[ "$WPAR_FLAG" -eq "$OFF" ]]
	then
Y		mesg=`/usr/bin/dspmsg -s 5 mksysb.cat 128 \
				"0512-333 $NAME: option -f must be used with either -w or -W.\n" \
				$NAME`
			NONBOOT_FLAG=$ON
			log_print "$mesg"
			print_usage
	fi
} ## end of determine_conflicting_options


######################### file_system_cleanup ##################################
##
## Name:  file_system_cleanup
##
## Function:  The file_system_cleanup function is responsible for
##            removing any file system that was created by the mkcd
##            command.  The only exception is the final CD image file
##            system that may be kept if the user specifically requests
##            to save the CD image by using the -S or -R flag of the
##            mkcd command.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  cleanup
##
########################################################################

function file_system_cleanup
{

##########################################################
## If the UDF DVD format is used, then we don't have to
## worry about temporary cd_image, or cd_fs filesystems
## cleanup.
##
## If the user has not specified to leave the final
## CD/DVD image and the CD/DVD image directory was not user
## specified, then remove the file system created by
## mkcd.
##
## Else if the user has not specified to leave the final
## CD/DVD images and the CD/DVD image directory WAS user specified,
## then remove the the image.
##########################################################

	if [ $UDF_FLAG -eq $OFF ]; then
		if [ $LEAVE_IMAGE_FLAG -eq $OFF ] && \
			[ $STOP_FLAG -eq $OFF ]; then

			if [ $CD_IMAGE_DIR_FLAG -eq $OFF ]; then
				remove_file_system $CD_IMAGE_DIR
			else
				rm -f $CD_IMAGE_DIR/cd_image_$PID 2>$MSGBUF
			fi
		fi

##########################################################
## If the CD/DVD file system was not user specified, then
## remove the file system created by mkcd.
##
## Else the CD/DVD file system WAS user specified, then
## remove just the files created by mkcd.
##########################################################

		if [ $MK_ROCKRIDGE_FLAG -eq $OFF ]; then
			if [ $CD_FS_DIR_FLAG -eq $OFF ]; then
				remove_file_system $CD_FS
			elif [ "$CD_FS_DIR" != "$CD_FS_DIR_OLD" ]; then
				rm -fr $CD_FS_DIR
			fi
		fi

	fi # Endif for UDF_FLAG check

##########################################################
## If the mksysb image directory was not user specified
## and a mksysb, savevg, or savewpar image was not specified, then
## remove the file system created by mkcd.
##
## Else if the mksysb file system WAS user specified,
## and a mksysb, savevg, or savewpar image is not specified, then
## remove just the files created by mkcd.
##########################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $OFF ] && \
	[ $SAVEVG_IMAGE_FLAG -eq $OFF ] && \
	[ $SAVEWPAR_IMAGE_FLAG -eq $OFF ] && \
	[ $JUST_FILE_FLAG -eq $OFF ] && \
	[ $MK_ROCKRIDGE_FLAG -eq $OFF ]; then

		if [ $MKSYSB_DIR_FLAG -eq $OFF ]; then
			remove_file_system $MKSYSB_DIR
			rm -fr $MKSYSB_DIR 2>/dev/null
		else
			if [ -f $MKSYSB_IMAGE ]; then
				rm -f $MKSYSB_IMAGE 2>/dev/null
			fi
			if [ -f $SAVEVG_IMAGE ]; then
				rm -f $SAVEVG_IMAGE 2>/dev/null
			fi
			if [ -f $SAVEWPAR_IMAGE ]; then
				rm -f $SAVEWPAR_IMAGE 2>/dev/null
			fi
		fi
	fi

	sync;sync;sync

} ## end of file_system_cleanup


######################### get_backup_file_number #######################
##
## Name:  get_backup_file_number
##
## Function:  The get_backup_file_number function is responsible for
##            determining the number of files in the backup.  This is
##            used by bi_main to produce percent complete messages when
##            restoring the backup.
##
## Parameters:  None
##
## Returns:  Number of files in the backup
##
## Calling Routine:  create_mkcd_data_file
##
########################################################################

function get_backup_file_number
{
typeset backup_image
typeset -i backup_number

##########################################################
## If the backup file number determination has not
## been previously run
##########################################################

	if [ -z "$backup_number" ]; then

##########################################################
## If the backup is a mksysb, then assign the mksysb image
## name to the backup variable.  Otherwise, assign the
## savevg image name to the backup variable.
##########################################################

		if [ $MKSYSB_IMAGE_FLAG -eq $ON ] || \
			[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
			backup_image=$MKSYSB_IMAGE
		elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] || \
			[ $WPAR_COMPLETE_FLAG -eq $ON ]; then
			backup_image=$SAVEWPAR_IMAGE
		else
			backup_image=$SAVEVG_IMAGE
		fi

##########################################################
## Determine the number of files in the backup and return
## the value to create_mkcd_data_file.
##########################################################

		backup_number=`restore -Tq -f $backup_image 2>&1 1>/dev/null | \
			tail -1 | awk -F: '{print $2}' | awk '{print$1}'`

	fi

	echo "$backup_number"

} ## end of get_backup_file_number

######################### get_backup_size ###############################
##
##
## Name:  get_backup_size
##
## Function:  The get_backup_size function is responsible for estimating
##            the amount space that will be needed for the CD filesystem
##            and CD image filesystem.
##
## Parameters:  none
##
## Returns:  Estimated size of backup.
##
## Calling Routine:  main
##
########################################################################

function get_backup_size
{
typeset each_fs
typeset -i fs_sz
typeset image_data="/image.data"
typeset exclude_size_file="/tmp/exclude_size${PID}"
typeset fsize=0
typeset new_exclude_file="/tmp/new_exclude_${PID}"
typeset olddir

	BACKUP_SIZE=0
	BACKUP_SIZE_512=0

##########################################################
## Call routine to manage the image.data and bosinst.data
## files.  This routine will also save existing files
## to a *.save.mkcd file.  This routine will handle the
## creation of a new image.data file, if necessary.
##
## For Rockridge, we don't need to do that, but we do need
## to figure some things out.
##########################################################

	if [[ $MK_ROCKRIDGE_FLAG = $ON ]] ; then
		BACKUP_SIZE=$(get_fs_size $CD_FS_DIR)
	else
		setup_data_files

##########################################################
## If a previously created mksysb is being used to
## create the CD, then get the size of the image.
##########################################################
		if [ $MKSYSB_IMAGE_FLAG -eq $ON ]; then
			BACKUP_SIZE=`ls -l $MKSYSB_IMAGE | awk '{print$5}'`  ## in bytes

##########################################################
## If a previously created savevg is being used to
## create the CD, then get the size of the image.
##########################################################
		elif [ $SAVEVG_IMAGE_FLAG -eq $ON ]; then
			BACKUP_SIZE=`ls -l $SAVEVG_IMAGE | awk '{print$5}'`  ## in bytes
##########################################################
## If a previously created savewpar is being used to
## create the CD, then get the size of the image.
##########################################################
		elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ]; then
			BACKUP_SIZE=`ls -l $SAVEWPAR_IMAGE | awk '{print$5}'`  ## in bytes

		## need to calculate the amount of data in volume group
		elif [ $SAVEVG_VOLUME_GROUP_FLAG -eq $ON ]; then

			if [ "$SAVEVG_VOLUME_GROUP" = "rootvg" ]; then
				for each_fs in `get_stanza_data "fs_data" "FS_NAME" "" "FS_NAME"`; do
					dmapi=`lsjfs2 $each_fs | tail +2l | awk 'BEGIN {FS=":"} {print $19}'`
					if [ "$dmapi" != "yes" ] || \
						[ $DMAPI_FLAG -eq $ON ]
					then
						fs_sz=`/usr/bin/df $each_fs/ | tail -1 | LANG=C awk '{printf "%.2f",$2-$3}'`
						(( BACKUP_SIZE_512 = BACKUP_SIZE_512 + fs_sz ))     ## 512 byte blocks
					else
						mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 130 \
							"DMAPI enabled file system detected, use -A to back up contents.\n\
Refer to /usr/lpp/bos.sysmgt/README.\n"`
						log_print "$mesg"
					fi
				done

			else ## calculate user volume group size
				for each_fs in `LANG=C lsvg -l $SAVEVG_VOLUME_GROUP \
					| egrep -w "jfs|jfs2" | awk '{print$7}'`; do

					if [ `mount | grep -wc $each_fs` -ne 0 ]; then
						dmapi=`lsjfs2 $each_fs | tail +2l | awk 'BEGIN {FS=":"} {print $19}'`
						if [ "$dmapi" != "yes" ] || \
							[ $DMAPI_FLAG -eq $ON ]
						then
							fs_sz=`/usr/bin/df $each_fs/ | tail -1 \
								| LANG=C awk '{printf "%.2f",$2-$3}'`
							(( BACKUP_SIZE_512 = BACKUP_SIZE_512 + fs_sz ))
							## 512 byte blocks
						else
							mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 130 \
								"DMAPI enabled file system detected, use -A to back up contents.\n\
Refer to /usr/lpp/bos.sysmgt/README.\n"`
							log_print "$mesg"
						fi
					fi
				done

			fi

			(( BACKUP_SIZE = BACKUP_SIZE_512 * 512 ))

		elif [ $WPAR_FLAG -eq $ON ]; then

			for each_fs in `LANG=C lswpar -d' ' -qMca vfs,mountpoint $WPAR \
				| egrep "^jfs[2]" | awk '{print $2}'`; do

				if [ `mount | grep -wc $each_fs` -ne 0 ]; then
					dmapi=`lsjfs2 $each_fs | tail +2l | awk 'BEGIN {FS=":"} {print $19}'`
					if [ "$dmapi" != "yes" ] || \
						[ $DMAPI_FLAG -eq $ON ]
					then
						fs_sz=`/usr/bin/df $each_fs/ | tail -1 \
							| LANG=C awk '{printf "%.2f",$2-$3}'`
						(( BACKUP_SIZE_512 = BACKUP_SIZE_512 + fs_sz ))
						## 512 byte blocks
					else
						mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 130 \
							"DMAPI enabled file system detected, use -A to back up contents.\n\
Refer to /usr/lpp/bos.sysmgt/README.\n"`
						log_print "$mesg"
					fi
				fi
			done

			(( BACKUP_SIZE = BACKUP_SIZE_512 * 512 ))

		else  ## calculate size of mksysb (rootvg)
			for each_fs in `get_stanza_data "fs_data" "FS_NAME" "" "FS_NAME"`; do
				dmapi=`lsjfs2 $each_fs | tail +2l | awk 'BEGIN {FS=":"} {print $19}'`
				if [ "$dmapi" != "yes" ] || \
					[ $DMAPI_FLAG -eq $ON ]
				then
					fs_sz=`/usr/bin/df $each_fs/ | tail -1 | LANG=C awk '{printf "%.2f",$2-$3}'`
					(( BACKUP_SIZE_512 = BACKUP_SIZE_512 + fs_sz ))
					## 512 byte blocks
				else
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 130 \
						"DMAPI enabled file system detected, use -A to back up contents.\n\
Refer to /usr/lpp/bos.sysmgt/README.\n"`
					log_print "$mesg"
				fi
			done

			(( BACKUP_SIZE = BACKUP_SIZE_512 * 512 ))
		fi

		if [ "$PLATFORM" = "ia64" ]; then
			EFI_PARTITION_SIZE=33554432
		fi
	fi
	if [ $EXCLUDE_LIST_FLAG -eq $ON ]; then
		if [ -n "$SAVEVG_VOLUME_GROUP" ]; then
			BACKUP_SIZE=$(calculate_vg_space $SAVEVG_VOLUME_GROUP ${EXCLUDE_LIST_FLAG})
		elif [ -n "$WPAR" ]; then
			BACKUP_SIZE=$(calculate_vg_space $WPAR ${EXCLUDE_LIST_FLAG})
		else
			BACKUP_SIZE=$(calculate_vg_space rootvg ${EXCLUDE_LIST_FLAG})
		fi
		(( BACKUP_SIZE = ( BACKUP_SIZE + 512 ) * 1024 ))
	fi
	(( BACKUP_SIZE = ( BACKUP_SIZE ) * 100 / 94 ))
	if (( BACKUP_SIZE < 10240000 )); then
		BACKUP_SIZE=10240000  # Set minimum size to 10mb to avoid
                                      # zero or negative values in calculations
	fi
	(( BACKUP_SIZE_512 = BACKUP_SIZE / 512 ))
	(( CD_FS_MIN_SIZE = BACKUP_SIZE + BOOT_IMAGE_SIZE_OPT4 + EFI_PARTITION_SIZE + PADDING_128K + PADDING_20MB ))
	CD_IMAGE_MIN_SIZE=$CD_FS_MIN_SIZE
#############################################
## Test to see if backup is larger than media
## can contain.
#############################################
	if [ $DVD_FLAG -eq $OFF ]; then
		if (( CD_FS_MIN_SIZE > CD_SIZE )); then
			CD_FS_MIN_SIZE=$CD_SIZE
		fi
	else
		if (( CD_FS_MIN_SIZE > DVD_SIZE )); then
			CD_FS_MIN_SIZE=$DVD_SIZE
		fi
	fi

} ## end of get_backup_size


######################### get_fs_free_space ############################
##
## Name:  get_fs_free_space
##
## Function:  The get_fs_free_space function is responsible for
##            determining the directory or file system free space,
##            in bytes.  If the running total is neede for the file
##            system, then the needed space is passed.
##
## Parameters:  1) file system,
##              2) (optional) If a running total is needed for the file
##                    system, then space needed is passed.
##
## Returns:  free space in bytes
##
## Calling Routine:  build_boot_images & check_cd_fs
##
########################################################################

function get_fs_free_space
{
typeset dir_fs_name=$1
typeset space_needed=$2     ## If non-null, running total is needed

typeset dir_fs_name
typeset -i fs_free
typeset fs_old_free
typeset fs_new_free
typeset fs_name

if [ $MK_ROCKRIDGE_FLAG -eq $ON ]; then
	space_needed=
fi
##########################################################
## First get the free space of the file system associated
## with the directory or specified file system.  Then
## covert the value to bytes and return the new value.
##########################################################

	fs_free=`/usr/bin/df $dir_fs_name/ | tail -1 | awk '{print$3}'`
	(( fs_free = fs_free * 512 ))     ## convert to bytes

##########################################################
## If a running total of the space needed for specific
## file system, then determine if a running total has
## already been started.  If so, then add the current
## needed space.  If not, create a running total for the
## the file system.
##########################################################

	if [ -n "$space_needed" ]; then
		fs_name=`/usr/bin/df $dir_fs_name/ | tail -1 | awk '{print$7}'`
		fs_old_free=`cat $FS_FREE_SPACE | grep -w "$fs_name " | awk '{print$2}'`
		if [ -z "$fs_old_free" ]; then
			(( fs_new_free = fs_free - space_needed ))
			echo "$fs_name     $fs_new_free" >>$FS_FREE_SPACE
		else
			fs_free=$fs_old_free
			(( fs_new_free = fs_old_free - space_needed ))
			grep -vw "^$fs_name " $FS_FREE_SPACE >${FS_FREE_SPACE}.tmp
			echo "$fs_name     $fs_new_free" >>${FS_FREE_SPACE}.tmp
			/usr/bin/mv ${FS_FREE_SPACE}.tmp $FS_FREE_SPACE
		fi
	fi

	echo "$fs_free"

} ## end of get_fs_free_space


######################### get_fs_size ##################################
##
## Name:  get_fs_size
##
## Function:  The get_fs_size function is responsible for determining
##            a file system or directory size, in bytes.
##
## Parameters:  file system/directory
##
## Returns:  file system in bytes
##
## Calling Routine: copy_backup & manage_multivolume
##
########################################################################

function get_fs_size
{
typeset given_directory=$1

typeset actual_directory
typeset -i fs_size

	## If the directory we were given is a symbolic link to
	## a directory, du will return 0.  We need to find out
	## where the actual directory is.

	if [[ -L $given_directory ]] ; then
		actual_directory=$(cd $given_directory ; /usr/bin/pwd)
	else
		actual_directory=$given_directory
	fi

##########################################################
## First get the file space size associated with the
## directory or specified file system.  Then covert the
## value to bytes and return the value.
##########################################################

	fs_size=`/usr/bin/du -s $actual_directory | tail -1 | awk '{print$1}'`
	(( fs_size = fs_size * 512 ))     ## convert to bytes

	echo "$fs_size"

} ## end of get_fs_size


######################### get_stanza_data ##############################
##
## Name:  get_stanza_data
##
## Function:  The get_stanza_data function is responsible for obtaining
##            stanza information from a stanza data file.  Using the
##            specified parameters, this function will determine the
##            appropriate record value.
##
##            NOTE: If no stanza_file value is given, the default value
##            is the image.data file.
##
## Parameters:  stanza, field name, field value, record, stanza file
##
## Returns:  0 - for completion
##
## Calling Routine:  create_oslevel_file
##
########################################################################

function get_stanza_data
{
typeset stanza=$1       ## Stanza Name
typeset field="$2"      ## Field Name=
typeset val=$3          ## Unique Field Name Value (may be null)
typeset record=$4       ## Field Name for stanza line you seek the data from.
typeset stanza_file=$5  ## (i.e. /image.data or /bosinst.data)

typeset val1

##########################################################
## If the stanza_file value is null, the set to the
## image.data file.
##########################################################

	stanza_file=${stanza_file:=/image.data}

	if [ "$val" = "" ]; then
		val1=""
	else
		val1=${val}"$"
	fi

##########################################################
## Ensure that the stanza data file contains blank line
## delineations between stanzas and then query for the
## record value from the specified parameters.
##########################################################

	sed '/:$/i\
' $stanza_file | grep -p "^"${stanza}: | \
		grep -p "${field}[ 	]*=[ 	]*${val1}" | grep $record | \
		awk -F"=" '{print $2}' | sed 's/^[ 	+]//g' | sed 's/[ 	+]$//g'

} ## end of get_stanza_data


######################### get_vg_name ##################################
##
## Name:  get_vg_name
##
## Function:  The get_vg_name function is responsible for returning the
##            volume group name from a given directory.
##
## Parameters:  directory
##
## Returns:  volume group
##
## Calling Routine:  setup_exclude_file
##
########################################################################

function get_vg_name
{
typeset directory=$1

typeset each_vg
typeset fs_name
typeset chk_vg

##########################################################
## Get the file system name from the directory.
##########################################################

	fs_name=`/usr/bin/df ${directory}/ |tail -1|awk '{print$7}'`

##########################################################
## Check each volume group on the system and determine if
## the file system name resides in the volume group.  If
## so, the return the volume group name.
##########################################################

	for each_vg in `lsvg -o`; do
		chk_vg=`lsvg -l $each_vg | grep -w $fs_name`
		if [ -n "$chk_vg" ]; then
			echo $each_vg
			return 0
		fi
	done

} ## end of get_vg_name


######################### go_straight_to_rockridge ##########################
##
## Name:  go_straight_to_rockridge
##
## Function:  The go_straight_to_rockridge function is responsible for
##            calling the mkrr_fs link to the script that calls
##            the OEM routines for creating the Rockridge file system
##            image.  The customer has already set up the file system
##            directory structure, so the command is just being used
##            to call the mkrr_fs script.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function go_straight_to_rockridge
{
typeset mesg
typeset -i fs_size

##########################################################
## Create Rockridge file system from the user supplied
## link to the sample script.  The script should expect
## to receive a target and source directory, in that
## order.
##########################################################

	fs_size=$(get_fs_size $CD_FS_DIR)
	if (( fs_size > CD_FS_MIN_SIZE )); then
		mesg=`/usr/bin/dspmsg -s 5 mksysb.cat 110 \
			"0512-326 $NAME: $CD_FS_DIR is too large for specified media.\n" $NAME $CD_FS_DIR`
		log_print "$mesg"
		cleanup 1
	fi
	if [ $CD_IMAGE_DIR_FLAG -eq $ON ] || [ -d $CD_IMAGE_DIR ]; then
	   check_cd_fs $CD_IMAGE_DIR $fs_size
	fi

	create_rockridge_fs

} ## end of go_straight_to_rockridge


######################### go_straight_to_udf ##########################
##
## Name:  go_straight_to_udf
##
## Function:  The go_straight_to_udf function is responsible for
##            copying the customer specified directory structure and
##            its contents to the UDF media.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function go_straight_to_udf
{
typeset mesg
typeset -i fs_size

	fs_size=$(get_fs_size $CD_FS_DIR)
	if (( fs_size > CD_FS_MIN_SIZE )); then
		mesg=`/usr/bin/dspmsg -s 5 mksysb.cat 110 \
			"0512-326 $NAME: $CD_FS_DIR is too large for specified media.\n" $NAME $CD_FS_DIR`
		log_print "$mesg"
		cleanup 1
	fi
	/usr/bin/mkdir -p $TMP_CD_MOUNT 2>$MSGBUF
	/usr/sbin/mount -v udfs $DEVICE $TMP_CD_MOUNT
	if [ $? -ne 0 ]; then ## mount command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
		cleanup 1
	fi
	cd $CD_FS_DIR
	if [ $DEBUG_FLAG -eq $ON ]; then
		( find ./ -print | backup -i -qf - ) | \
		( cd $TMP_CD_MOUNT; restore -v -qf - )
	else
		( find ./ -print | backup -i -qf - ) | \
		( cd $TMP_CD_MOUNT; restore -v -qf - ) > /dev/null
	fi
}


######################### initialize_log ###############################
##
## Name:  initialize_log
##
## Function:  The initialize_log function is responsible for creating
##            the initial entry for the mkcd command log.  The inital
##            entry includes the date/time of execution, as well as the
##            exact command executed.
##
## Parameters:  command line string (optional)
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function initialize_log
{
typeset mesg
	mesg=`/usr/bin/dspmsg -s 5 mksysb.cat 35 \
			"Initializing $NAME log: ${LOGFILE}...\n" $NAME $LOGFILE`

	echo "##################################################" > $LOGFILE
	echo $(date) >> $LOGFILE
	echo "cmd: $NAME $*" >> $LOGFILE

	log_print "$mesg"

} ## end of initialize_log


######################### initialize_variables #########################
##
## Name:  initialize_variables
##
## Function:  The initialize_variables function is responsible for
##            setting or initializing the mkcd global variables.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function initialize_variables
{

##########################################################
## Initial variable settings the are necessary for the
## asignment of other global variables.
##########################################################

	OFF=0
	ON=1
	PID=$$

##########################################################
## These variables retain a constant value throughout
## the execution of the program.
##########################################################

	ODMDIR_OLD=$ODMDIR
	ODMDIR=/etc/objrepos
	export ODMDIR
	AUTORESUME=no
	BIGFILE=""                           ## jfs big file enablement
	BOOTCAT=bootcat.bin
	BOOT_IMAGE_SIZE_OPT4=37200000     ## 36MB in bytes
	BS_SIZE=4096                      ## block size for dd
	CD_FS=/mkcd/cd_fs
	CD_SIZE=676331520
	CDBOOT_IMAGE=cdboot.img
	CDFS_BULL_LIST=/usr/lpp/bosinst/cdfs.optional.B.list
	CDFS_DONT_ADD_LIST=/usr/lpp/bosinst/cdfs.dont_add.list
	CDFS_OPTIONAL_LIST_TMP=/tmp/.cdfs.optional.list.tmp
	DEVICE_FILESET_LIST_FILE=/tmp/.mkcd.devices.list
	DRIVE="q:"
	DVD_SIZE=4293918720             ## Less than 4.095 GB DVD-R limitation
	FILE_LINKS="/usr/sbin/mkrr_fs /usr/sbin/burn_cd"
	FS_FREE_SPACE="/tmp/.mkcd.fs_space.list";>$FS_FREE_SPACE
	FTYPE="jfs2"                 ## mkcd temp space type
	IN_CONJUNCTION=0; MUTUALLY_EXCLUSIVE=1
	IS_BUNDLE=5; IS_DEVICE=6; IS_VG=7; IS_WPAR=9
	IS_MKSYSB=0; IS_SAVEVG=1; IS_SAVEWPAR=2
	LESS_THAN_2GB=2146959360
	LESS_THAN_2GB_2048=524160    ## Less than 2GB in 4096 byte blocks
	LOGFILE=/var/adm/ras/mkcd.log
	[[ -s /usr/bin/mdir ]] && MBIN=/usr/bin	|| MBIN=/usr/local/bin ## directory containing mtools cmds
	MIN_MEM_SIZE=33554432     ## 32 MB in bytes
	MSGBUF=/tmp/.mkcd.tmp.msg; /usr/bin/rm -f $MSGBUF
	PADDING_128K=131072         ## 128K padding for firmware problem
	PADDING_20MB=40000000	    ## 40MB of Padding to fix 2 vol creation problem and growing boot/install files needs.
	PLATFORM=`LANG=C /usr/sbin/bootinfo -p`     ### ??? is -p flag the best flag? ###
	READABLE=1; WRITABLE=2; EXECUTABLE=3; RWX_DIRECTORY=4; RX_DIRECTORY=8
	TIMESTAMP=`/usr/bin/date +"%y %m %d %H %M %S" | sed 's/ //g'`
	TMP_CD_MOUNT=/tmpcdmt

##########################################################
## These variables are initialized here and modified
## only in the main program.  No other functions will
## change the value of these variables.
##########################################################

	ACCEPT_LICENSE_AGMT_FLAG=$OFF
	BIG_FILE_ENABLED="false"          ## For DVD sized file systems
	BUNDLE_FILE_FLAG=$OFF
	[[ -d "/mkcd/cd_fs" ]] && CD_FS_DIR=$CD_FS || CD_FS_DIR=${CD_FS}/${PID}
	CD_FS_DIR_FLAG=$OFF
	CD_IMAGE_DIR=/mkcd/cd_images
	CD_IMAGE_DIR_FLAG=$OFF
	CDFS_REQUIRED_LIST=/usr/lpp/bosinst/cdfs.required.list
	CDFS_VOLUME_GROUP=rootvg
	CDFS_VOLUME_GROUP_FLAG=$OFF
	CREATE_EFI_PARTITION=$OFF
	CUSTOMIZATION_FILE=
	CUSTOMIZATION_FILE_FLAG=$OFF
	DEBUG_FLAG=$OFF
	DEVICE=
	DEVICE_FLAG=$OFF
	DMAPI_FLAG=$OFF
	DVD_FLAG=$OFF
	DVD_ISO9660_FLAG=$OFF
	EXCLUDE_LIST_FLAG=$OFF

	# JUST_FILE
	### This is the proto file provided with the -j flag which
	### contains the list of files to be copied to CD or DVD.

	JUST_FILE_FLAG=$OFF
	LEAVE_IMAGE_FLAG=$OFF
	MAPPING_FLAG=$OFF
	MK_ROCKRIDGE_FLAG=$OFF
	MKSYSB_DIR="/mkcd/mksysb_image"
	MKSYSB_DIR_FLAG=$OFF
	MKSYSB_IMAGE="${MKSYSB_DIR}/mksysb_${PID}"
	MKSYSB_IMAGE_FLAG=$OFF
	NEW_BOSINST_DATA=
	NEW_BOSINST_DATA_FLAG=$OFF
	NEW_IMAGE_DATA=
	NEW_IMAGE_DATA_FLAG=$OFF
	NONBOOT_FLAG=$OFF
	NO_EA_FLAG=$OFF
	NO_PACK_FLAG=$OFF
	PKG_SOURCE_DIR_FLAG=$OFF
	SAVEVG_IMAGE="${MKSYSB_DIR}/savevg_${PID}"
	SAVEVG_IMAGE_FLAG=$OFF
	SAVEVG_VOLUME_GROUP=
	SAVEVG_VOLUME_GROUP_FLAG=$OFF
	WPAR=
	WPAR_FLAG=$OFF
	SAVEWPAR_IMAGE="${MKSYSB_DIR}/savewpar_${PID}"
	SAVEWPAR_IMAGE_FLAG=$OFF
	WPAR_SPEC_FILE=
	WPAR_SPEC_FLAG=$OFF
	STACK_LIST=
	STACK_LIST_FLAG=$OFF
	STOP_FLAG=$OFF
	UDF_FLAG=$OFF
	NO_DATA_FLAG=$OFF
	Z_FLAG=$OFF
	NNFLAG=$OFF
	NO_WPARS_FLAG=$OFF
	XFS_FLAG=$OFF
	SNAP_FLAG=$OFF

##########################################################
## Global variable assignments, in alphabetical order.
##########################################################

	ALLOW_INCREASE_CD_FS=$OFF
	### Set in the create_cd_fs function to be used in the
	### copy_backup function as an indicator that since we
	### created the CD file system, we are allowed to expand
	### it, if necessary.

	# BACKUP_SIZE
	### Contains the size of the item to be copied to CD or DVD in bytes.

	# BACKUP_SIZE_512
	### This is the size of the item to be copied to CD or DVD in 512 byte
	### blocks.  It is not set the value is set in get_backup_size for
	### several backup types, which may be used in manage_mksysb or
	### manage_savevg, but appear to be unused after that and in all other
	### cases.

	BUNDLE_FILE=
	### BUNDLE_FILE is altered only in the main program and in the
	### verify_parm function, which simply determines the location
	### of the file and prepends the absolute path name to the
	### parameter given to the -b flag.  Its value remains constant
	### throughout the rest of the program.

	# CD_DEV
	### Used as a flag to determine whether we have a CD mounted or
	### not.  A non-NULL value indicates a CD is mounted.

	CD_FS_CHANGED=0

	# CD_FS_CREATE_SIZE_512
	### Ususally set to CD_FS_MIN_512 * 100 / 96 + 1, except in create_cd_fs.
	### This is the size that we will use to create the filesystem that will
	### contain the files that will eventually make up the CD image.  We
	### start with the number of blocks the data will occupy and add
	### 4% (100/96 = 1.04) for the inodes we will require and add 1 to
	### counteract rounding.

	CD_FS_MAX_512=0

	# CD_FS_MIN_512
	### CD_FS_MIN_SIZE / 512

	# CD_FS_MIN_SIZE
	### This is the number of bytes that we figure the data in the backup
	### will require including the backup data, the boot image, the EFI
	### partition, and a little padding to compensate for rounding errors.

	CD_FS_NEW_CREATE_SIZE_512=0
	# CD_FS_NEW_CREATE_SIZE_512
	###

	# CD_FS_NEW_MIN_512
	###

	CD_IMAGE_CREATE_SIZE_512=0
	### This is the number of blocks we will use to store the CD image.
	### This value is comparable to CD_FS_CREATE_SIZE_512 in that it
	### will include enough space to account for data, rounding, and
	### inode usage.

	CD_IMAGE_DIR_SET=$OFF
	### Set to $ON when we create the CD image file system and
	### a multi-volume CD is being created.  Apparently never
	### looked at again, though.

	# CD_IMAGE_MIN_512
	### CD_IMAGE_MIN_SIZE / 512

	# CD_IMAGE_MIN_SIZE
	### This is the size of the image we will create and burn to CD in
	### bytes.

	# CD_IMAGE_NAME
	### Used for the name of the boot image or the Rock Ridge image
	### to be burned to CD or DVD.

	CDFS_OPTIONAL_LIST=/usr/lpp/bosinst/cdfs.optional.list
	### Value could be modified in the create_cd_fs function for IA64.

	DD_SIZE=0
	### This is where we store the total number of dd blocks (2048 bytes)
	### that we want to copy into the CD image.

	# EFI_PARTITION_PROTO
	### Used in the create_efi_partition function.  The value is set
	### by using the -E flag.

	EFI_PARTITION_SIZE=0
	### Set to 33554432 in the get_backup_size function on IA64 platforms
	### and used in various size calculations.

	EXISTING_FS=
	[[ -d "/mkcd/cd_fs" ]] && EXISTING_FS="/mkcd/cd_fs $EXISTING_FS"
	[[ -d "/mkcd/cd_images" ]] && EXISTING_FS="/mkcd/cd_images $EXISTING_FS"
	[[ -d "/mkcd/mksysb_image" ]] && EXISTING_FS="/mkcd/mksysb_image $EXISTING_FS"
	### This is a list of file systems that existed prior to run time.
	### It's used by the remove_file_system function to determine whether
	### a file system should be removed or not upon clean up.  Only file
	### systems NOT in this list will be removed.

	INCREASE_FS_512=0
	### If we need to copy install images and we created the CD_FS
	### file system, this is how many additional blocks we will need
	### to allocate to accomodate those files.

	INCREASE_FS_FLAG=$OFF

	### Did /mkcd previously exist, if so, do not remove on cleanup
	[[ -d /mkcd ]] && MKCD_DIR_RM=$OFF || MKCD_DIR_RM=$ON

	MKSYSB_COMPLETE_FLAG=$OFF
	### Set to $ON in the manage_mksysb function after we have successfully
	### created a mksysb image.

	MORE_CDS=$OFF
	### Turned $ON in the copy_backup function if the CD image is too
	### large to fit on a single CD.  Turned $OFF again in manage_multivolume
	### when enough of the CD image has been copied to CD so that the
	### portion remaining can be written to a single CD.

	MULTI_VOLUME_FLAG=$OFF
	### Turned $ON in the copy_backup function if the CD image if too
	### large to fit on a single CD.

	PKG_SOURCE_DIR=
	### This is the directory or device that devices packages are
	### copied from in the copy_devices_packages and install_devices_support
	### functions.

	POPULATE_CDFS_SIZE_USED=0
	### Total size of files copied to the cd_fs in populate_cd_fs.
	### Most of these files are included in each image created. If
	### multiple images are needed and those images are not to be
	### removed, we need to account for these files in each
	### image when we create in the cd_images filesystem.
	### Note this is updated for UDF but not used since UDF does not
	### create the cd_images fs.
	
	REMOVE_BOSINST_DATA=$OFF
	REMOVE_IMAGE_DATA=$OFF
	### Both of these variables are set in setup_data_files if the user
	### provides a bosinst.data and/or an image.data file using the -u
	### and/or -i flags, respectively.  They are then used in the cleanup
	### function to remove the copy this program makes.

	SAVEVG_COMPLETE_FLAG=$OFF
	### Set to $ON in manage_savevg as soon as the savevg operation
	### completes successfully.

	VG_NAME=rootvg
	### Used when creating an exclude file in the setup_exclude_file
	### function.

	VOLUME_ID=1
	### Set in manage_multivolume with the number of volumes the CD
	### image will require.

	WPAR_COMPLETE_FLAG=$OFF
	### Set to $ON in manage_savewpar as soon as the savewpar operation
	### completes successfully.

} ## end of initialize_variables


######################### install_device_support #######################
##
## Name:  install_device_support
##
## Function:  The install_device_support function is responsible for
##            installing the necessary device support for creating
##            a generic CD.  This devide support needs to be installed
##            to create the necessary boot images.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  copy_devices_packages
##
########################################################################

function install_device_support
{
typeset mesg

##########################################################
## Create a list of devices that should be installed on
## the system prior to creating a generic CD.  These
## devices are necessary for creating the boot images.
##########################################################

	/usr/sbin/geninstall -L -d $PKG_SOURCE_DIR | grep "^devices\." | \
		awk -F: '{print $1}' | grep -v "devices\.msg" >$DEVICE_FILESET_LIST_FILE
	echo "bos.mp" >> $DEVICE_FILESET_LIST_FILE

##########################################################
## Using geninstall, install all the device support except
## the device messages.
##########################################################

	mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 69 \
		"Installing device support for generic CD or DVD...\n"`
	log_print "$mesg"
        if [ $ACCEPT_LICENSE_AGMT_FLAG = $ON ]; then
		/usr/sbin/geninstall -I"abgXYQq" -Y -d $PKG_SOURCE_DIR -f \
                  $DEVICE_FILESET_LIST_FILE 2>&1 | tee -a $LOGFILE
        else
		/usr/sbin/geninstall -I"abgXQq" -d $PKG_SOURCE_DIR -f \
                  $DEVICE_FILESET_LIST_FILE 2>&1 | tee -a $LOGFILE
        fi

	rm -f $DEVICE_FILESET_LIST_FILE 2>$MSGBUF
	if [ $? -ne 0 ]; then     ## rm command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
			"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
		log_print "$mesg"
	fi

} ## end of install_device_support


######################### kill_pid ####################################
##
## Name:  kill_pid
##
## Function:  The kill_pid function is responsible for stopping
##            the progress indicator pid.
##
## Parameters:  The background progress indicator pid.
##
## Returns:  None
##
## Calling Routine:  Throughout mkcd main and associated functions
##
########################################################################

function kill_pid
{
typeset PID=$1
typeset cnt=0
typeset PIDTEST=0
	while [ -n "$PID" -a $PIDTEST -eq 0 -a $cnt -lt 10 ]
	do
		kill $PID >/dev/null 2>&1
		sleep 2
		(( cnt = $cnt + 1 ))
		ps $PID >/dev/null 2>&1
		PIDTEST=$?
	done
} ## end of kill_pid


######################### log_print ####################################
##
## Name:  log_print
##
## Function:  The log_print function is responsible for printing
##            information to the mkcd log, as well as the standard
##            output.
##
## Parameters:  print string
##
## Returns:  None
##
## Calling Routine:  Throughout mkcd main and associated functions
##
########################################################################

function log_print
{

##########################################################
## This routine will be used for printing progress
## information to the standard output and the log file.
##########################################################

	echo "$*" | /usr/bin/tee -a $LOGFILE

} ## end of log_print

######################### make_dos_dirs #################################
##
## Name:  make_dos_dirs
##
## Function:  The make_dos_dirs function is responsible for creating
##            any dos directories required to contain files copied to
##            the EFI partition
##
## Parameters:  File path
##
## Returns:  None
##
## Calling Routine:  create_efi_partition
##
########################################################################
function make_dos_dirs
{
typeset ddir="$1"

typeset i=""
typeset dir=""
typeset mesg
typeset oldifs

########################################################################
## If the input path is not a directory, chop off the last element to
## make it a directory
########################################################################
	[[ ! -d $ddir ]] && ddir=${ddir%/*}

########################################################################
## Diddle the field separator to create the list of directories
########################################################################
	oldifs="$IFS"
	IFS='/'
	set -- ${ddir}
	IFS="${oldifs}"

##########################################################
## Loop thru the directories in the file path
##########################################################

	for i
	do
		[[ "${dir}" != / ]] && dir=${dir}/${i} || dir=${dir}${i}

##########################################################
## If the directory does not exist, create it.
##########################################################

		${MBIN}/mdir ${DRIVE}${dir} >/dev/null 2>&1
		if [ $? -ne 0 ]; then		## mdir command return code
			${MBIN}/mmd ${DRIVE}${dir} 2>$MSGBUF
			if [ $? -ne 0 ]; then	## mmd command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi
	done

} ## end of make_dos_dirs

######################### manage_backup ################################
##
## Name:  manage_backup
##
## Function:  The manage_backup function is responsible for determining
##            the proper routine to call for managing the backup type
##            (mksysb, savevg, or savewpar).
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function manage_backup
{
##########################################################
## If processing a savewpar backup, then
## call manage_savewpar
##########################################################

	if [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] || [ -n "$WPAR" ];then
		manage_savewpar


##########################################################
## If a previously created mksysb is being used to
## create the CD, then call manage_mksysb
##########################################################

	elif [ $MKSYSB_IMAGE_FLAG -eq $ON ]; then
		manage_mksysb

	else  ## Create a mksysb or handle/create a savevg

##########################################################
## If a previously created savevg is being used to
## create the CD, then call manage_savevg
##########################################################

		if [ $SAVEVG_IMAGE_FLAG -eq $ON ]; then
			manage_savevg

		else  ## mksysb or savevg will be created

##########################################################
## If a volume group has been specified for the savevg
## backup and the user specifies the rootvg as the volume
## group, then create a non-bootable mksysb, rather than a
## savevg image.  Otherwise call manage_savevg to create
## the savevg for the specified volume group.
##########################################################

			if [ $SAVEVG_VOLUME_GROUP_FLAG -eq $ON ]; then

				if [ "$SAVEVG_VOLUME_GROUP" = "rootvg" ]; then
					manage_mksysb
				else
					manage_savevg
				fi

##########################################################
##  Create a system mksysb for the backup CD
##########################################################

			else
				manage_mksysb
			fi
		fi
	fi

} ## end of manage_backup


######################### manage_mksysb ################################
##
## Name:  manage_mksysb
##
## Function:  The manage_mksysb function is responsible for managing a
##            previously created mksysb or creating a new system mksysb
##            to include in the CD image(s).
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_backup
##
########################################################################

function manage_mksysb
{
typeset mesg
typeset mksysb_flags
typeset -i dir_fs
typeset -i needed_space

##########################################################
## Start with flag for automatic /tmp expansion and
## using the exclude list file.
##########################################################

	mksysb_flags="-X -e"

##########################################################
## If excluding WPAR file systems, add that flag to 
## the mksysb flags
##########################################################

	if [ $NO_WPARS_FLAG -eq $ON ]; then
		mksysb_flags="$mksysb_flags -G"
	fi

##########################################################
## If using snapshots, add that flag to 
## the mksysb flags
##########################################################

	if [ $SNAP_FLAG -eq $ON ]; then
		mksysb_flags="$mksysb_flags -T"
	fi

##########################################################
## If excluding file systems, add that flag to 
## the mksysb flags
##########################################################

	if [ $XFS_FLAG -eq $ON ]; then
		mksysb_flags="$mksysb_flags -x $XFSLIST"
	fi

##########################################################
## If the user hasn't specified a mksysb image then one
## will be created and used for the new mksysb.
##########################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $OFF ]; then

##########################################################
## If the user has specified a directory location for
## mksysb placement, then ensure there is enough space
## available in the directory.
##########################################################

		if [ $MKSYSB_DIR_FLAG -eq $ON ] || [ -d "$MKSYSB_DIR" ];then

##########################################################
## Add the mksysb directory to the existing file system
## variable so it will not be remove during system
## cleanup. Also if the file system/directory already exists
## but wasn't user specified, treat it like it was
## by setting the MKSYSB_DIR_FLAG variable to on.
##########################################################

			MKSYSB_DIR_FLAG=$ON
			EXISTING_FS="$MKSYSB_DIR $EXISTING_FS"
			MKSYSB_IMAGE="${MKSYSB_DIR}/mksysb_${PID}"
			dir_fs=`get_fs_free_space $MKSYSB_DIR $BACKUP_SIZE`
			(( dir_fs = dir_fs / 512 ))     ## 512 byte blocks
			(( needed_space = ( BACKUP_SIZE_512 - dir_fs ) * 512 * 100 / 96 + 1 ))
			if (( needed_space > 0 )); then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 \
					"0512-329 $NAME: User specified CD or DVD file system: ${MKSYSB_DIR}\n\
needs an additional ${needed_space} bytes.\n" $NAME $MKSYSB_DIR $needed_space`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## If no mksysb directory location has been specified,
## then create a new file system with the adequate space.
##########################################################

		else

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
				"Creating temporary file system: ${MKSYSB_DIR}...\n" \
				$MKSYSB_DIR`
			log_print "$mesg"

##########################################################
## Save the old /etc/filesystems, so that it can be used
## when the mksysb is created, thus not having the
## MKSYSB_DIR filesystem in the mksysb.
##########################################################

			cp -p /etc/filesystems /etc/filesystems.${$}.orig
                        if [[ $BIG_FILE_ENABLED = "true" || \
                              $BACKUP_SIZE_512*512 -gt $LESS_THAN_2GB ]] ;then
				if [[ "$FTYPE" = jfs ]]; then
					BIGFILE="-a bf=true"
				else
					BIGFILE=""
				fi
                                crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
                                     $BIGFILE \
                                     -m $MKSYSB_DIR -p "rw" -v $FTYPE \
                                     -A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
                        else
			        crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
				     $BIGFILE \
				     -m $MKSYSB_DIR -p "rw" -v $FTYPE \
				     -A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
                        fi
			if [ $? -ne 0 ]; then     ## crfs command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				rm /etc/filesystems.${$}.orig
				cleanup 1
			fi
			/usr/sbin/mount $MKSYSB_DIR
		fi


##########################################################
## Setup the exclude list for mksysb.  If the user
## specifies an exclude file, then we add mkcd exclusions
## to the list.  Otherwise, we create a new list for
## mkcd entries.
##########################################################

		setup_exclude_file rootvg

##########################################################
## If the user has specified the debug flag, then set
## the mksysb flag for verbose output.
##########################################################

		if [ $DEBUG_FLAG -eq $ON ]; then
			mksysb_flags="$mksysb_flags -v"
		fi

##########################################################
## If the user has specified the no pack flag, then set
## the mksysb flag for no packing.
##########################################################

		if [ $NO_PACK_FLAG -eq $ON ]; then
			mksysb_flags="$mksysb_flags -p"
		fi

##########################################################
## If the user has specified the no EA flag, then set
## the mksysb flag for no EAs.
##########################################################

		if [ $NO_EA_FLAG -eq $ON ]; then
			mksysb_flags="$mksysb_flags -a"
		fi

##########################################################
## If the user has specified back up DMAPI filesystems,
## then set the mksysb flag for DMAPI.
##########################################################

		if [ $DMAPI_FLAG -eq $ON ]; then
			mksysb_flags="$mksysb_flags -A"
		fi

##########################################################
## If the user has specified to decrypt root files and
## not backup other users encrypted files.
##########################################################

		if [ $Z_FLAG -eq $ON ]; then
			mksysb_flags="$mksysb_flags -Z"
		fi

##########################################################
## Call mksysb with the appropriate flags.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 25 \
			"Creating mksysb image...\n"`
		log_print "$mesg"

##########################################################
## if /etc/filesystems.${$}.orig exists and is not empty
## save the new /etc/filesystems, and use the original one
## during the mksysb creation
##########################################################

		if [ -s /etc/filesystems.${$}.orig ]
		then
			cp -p /etc/filesystems /etc/filesystems.${$}.new
			mv  /etc/filesystems.${$}.orig /etc/filesystems
		fi
		/usr/bin/mksysb $mksysb_flags $MKSYSB_IMAGE 2>$MSGBUF
                /usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		if [ $? -ne 0 ]; then     ## mksysb command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			mv /etc/filesystems.${$}.new /etc/filesystems
			cleanup 1
		fi

		MKSYSB_COMPLETE_FLAG=$ON     ## set the flag for create_cd_fs

#########################################################
## Reset /etc/filesystems
#########################################################

		if [ -s /etc/filesystems.${$}.new ]
		then
			mv /etc/filesystems.${$}.new /etc/filesystems
		fi
	fi

##########################################################
## Determine the mksysb size.
##########################################################

	BACKUP_SIZE=`ls -l $MKSYSB_IMAGE | awk '{print$5}'`     ## in bytes

} ## end of manage_mksysb


######################### manage_multivolume ###########################
##
## Name:  manage_multivolume
##
## Function:  The manage_multivolume function is responsible for
##            determining if the created backup will require multiple
##            volume CDs.  If so, it will prompt the user for a new
##            CD and continue to populate the CD file system structure
##            and create new CD images per volume.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function manage_multivolume
{
typeset mesg
typeset total_size
typeset -i fs_size
typeset -i dd_size1
typeset -i dd_size2
typeset -i old_dd_size
typeset -i skip_size

##########################################################
## If the backup will require multi-volume CDs, then
## process each volume until there are no more CDs to
## process.  Otherwise, exit the routine.
##########################################################

	if [ $MULTI_VOLUME_FLAG -eq $ON ]; then

##########################################################
## If there are more CDs to create, then prompt the user
## for a new CD.
##########################################################

		while [ $MORE_CDS -eq $ON ]; do

##########################################################
## If the CD will be created now, then the multi-volume
## CDs will need to be changed.
##########################################################

			if [ $STOP_FLAG -eq $OFF ]; then
				change_cd
			fi
			if [[ $UDF_FLAG -eq $ON ]]; then
				udfcreate -d $DEVICE 2>/dev/null
				if [ $? -ne 0 ]; then ## udfcreate command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 121 \
						"0512-399 $NAME: Unable to create UDF media.\n" $NAME`
					log_print "$mesg"
					cleanup 1
				fi
				/usr/sbin/mount -v udfs $DEVICE $CD_FS_DIR
					populate_cd_fs $CD_FS_DIR $CDFS_REQUIRED_LIST $OFF
					populate_cd_fs $CD_FS_DIR $CDFS_OPTIONAL_LIST $ON

##########################################################
## If the user has not specified to leave the final CD
## images and then remove the image.
##########################################################

			else
				if [ $LEAVE_IMAGE_FLAG -eq $OFF ] && \
					[ $STOP_FLAG -eq $OFF ]; then
					rm -f $CD_IMAGE_NAME 2>$MSGBUF
					if [ $? -ne 0 ]; then     ## rm command return code
						/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
						mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
							"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
						log_print "$mesg"
					fi
				fi

##########################################################
## Remove the CD file system directory structure and
## recreate the only directory structure needed for the
## follow on CD(s).
##########################################################

				rm -f ${CD_FS_DIR}/bosinst.data ${CD_FS_DIR}/image.data 2>/dev/null
				rm -fr ${CD_FS_DIR}/usr/!(*bin) ${CD_FS_DIR}/ppc ${CD_FS_DIR}/root ${CD_FS_DIR}/installp 2>/dev/null
			fi
			/usr/bin/mkdir -p ${CD_FS_DIR}/usr/sys/inst.images 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## mkdir command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
			fi

##########################################################
## Determine the size of CD file system structure and
## verify the size can accomodate the remainder of the
## backup.
## NOTE: The dd size is in 2048 byte blocks.
##########################################################

			fs_size=`get_fs_size $CD_FS_DIR`
			(( total_size = fs_size + (BACKUP_SIZE - (DD_SIZE * 4096)) ))

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 75 \
				"Copying the remainder of the backup to the CD \
or DVD file system...\n"`
			log_print "$mesg"

##########################################################
## If the file system will accomodate the remainder of the
## backup, skip the number of blocks that was copied
## previously and copy the remainder.
##########################################################

			if [ $total_size -le $CD_FS_MIN_SIZE ]; then
				dd_size2=0
				(( dd_size1 = total_size / 4096 ))
				if [ $dd_size1 -gt $LESS_THAN_2GB_2048 ]; then
					(( dd_size2 = ( dd_size1 - LESS_THAN_2GB_2048 ) ))
					dd_size1=$LESS_THAN_2GB_2048
				fi
				if [ $MKSYSB_IMAGE_FLAG -eq $ON ] ||
					[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$DD_SIZE count=$dd_size1 if=$MKSYSB_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( DD_SIZE + dd_size1 ) ))
						dd bs=$BS_SIZE count=$dd_size2 skip=$skip_size if=$MKSYSB_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image2 2>$MSGBUF
					fi
				elif [ $SAVEVG_IMAGE_FLAG -eq $ON ] ||
					[ $SAVEVG_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$DD_SIZE count=$dd_size1 if=$SAVEVG_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( DD_SIZE + dd_size1 ) ))
						dd bs=$BS_SIZE count=$dd_size2 skip=$skip_size if=$SAVEVG_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image2 2>$MSGBUF
					fi
				elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] ||
					[ $SAVEWPAR_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$DD_SIZE count=$dd_size1 if=$SAVEWPAR_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( DD_SIZE + dd_size1 ) ))
						dd bs=$BS_SIZE count=$dd_size2 skip=$skip_size if=$SAVEVG_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image2 2>$MSGBUF
					fi
				fi

				if [ $? -ne 0 ]; then     ## dd command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

				(( DD_SIZE = DD_SIZE + dd_size1 + dd_size2 ))

##########################################################
## Set the flag for no more CD multivolume processing
##########################################################

				MORE_CDS=$OFF

##########################################################
## If the file system will not accomodate the remainder of
## the backup, skip the number of blocks that was copied
## previously, but then copy the number of blocks that the
## file system can accomodate.
## NOTE: dd in 2048 byte blocks
##########################################################

			else
				old_dd_size=$DD_SIZE
				(( DD_SIZE = (CD_FS_MIN_SIZE - fs_size) / 4096 ))
				dd_size2=0
				dd_size1=$DD_SIZE
				if (( DD_SIZE > LESS_THAN_2GB_2048 )); then
					(( dd_size2 = ( DD_SIZE - LESS_THAN_2GB_2048 ) ))
					dd_size1=$LESS_THAN_2GB_2048
				fi

				if [ $MKSYSB_IMAGE_FLAG -eq $ON ] ||
					[ $MKSYSB_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$old_dd_size count=$dd_size1 if=$MKSYSB_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( old_dd_size + dd_size1 ) ))
						dd bs=$BS_SIZE skip=$skip_size count=$dd_size2 if=$MKSYSB_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/mksysb_image2 2>$MSGBUF
					fi

				elif [ $SAVEVG_IMAGE_FLAG -eq $ON ] ||
					[ $SAVEVG_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$old_dd_size count=$dd_size1 if=$SAVEVG_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( old_dd_size + dd_size1 ) ))
						dd bs=$BS_SIZE skip=$skip_size count=$dd_size2 if=$SAVEVG_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/savevg_image2 2>$MSGBUF
					fi
				elif [ $SAVEWPAR_IMAGE_FLAG -eq $ON ] ||
					[ $SAVEWPAR_COMPLETE_FLAG -eq $ON ]; then
					dd bs=$BS_SIZE skip=$old_dd_size count=$dd_size1 if=$SAVEWPAR_IMAGE \
						of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image 2>$MSGBUF
					if (( dd_size2 != 0 )); then
						(( skip_size = ( old_dd_size + dd_size1 ) ))
						dd bs=$BS_SIZE skip=$skip_size count=$dd_size2 if=$SAVEWPAR_IMAGE \
							of=${CD_FS_DIR}/usr/sys/inst.images/savewpar_image2 2>$MSGBUF
					fi
				fi

				if [ $? -ne 0 ]; then     ## dd command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi

##########################################################
## Reset the number of blocks processed.
##########################################################

				(( DD_SIZE = DD_SIZE + old_dd_size ))
			fi

##########################################################
## Change the volume ID for the next CD processing
##########################################################

			(( VOLUME_ID = VOLUME_ID + 1 ))

			create_mkcd_data_file     ## create the mkcd.data file

			if [[ $UDF_FLAG -eq $OFF ]]; then
				create_rockridge_fs  ## Create the Rockridge file system image

				write_to_cd          ## write the new volume CD
			fi
		done
	fi

} ## end of manage_multivolume


######################### manage_savevg ################################
##
## Name:  manage_savevg
##
## Function:  The manage_savevg function is responsible for managing a
##            previously created savevg or creating a new system savevg
##            to include in the CD image(s).
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_backup
##
########################################################################

function manage_savevg
{
typeset mesg
typeset savevg_flags
typeset -i dir_fs
typeset -i needed_space

##########################################################
## Start with flags for auto /tmp expansion, data file
## creation, and using exclude list file.
##########################################################

	savevg_flags="-X -e"

##########################################################
## If the $NO_DATA_FLAG is set to $ON, then we will pass
## the -r flag to savevg, to just create a metadata
## backup. We will also create a fifo to run the backup to,
## and then read from the fifo to get the size of the metadata
## backup. (Dividing of course the frag size of the mksysb
## temporary filesystem we will create later.
##########################################################

	if [[ "$NO_DATA_FLAG" = "$ON" ]] && \
	   [[ "$SAVEVG_IMAGE_FLAG" = "$OFF" ]]; then
		savevg_flags="$savevg_flags -r"
		SAVEVG_FIFO="/tmp/savevg_cd$$.FIFO"
                /usr/bin/mkfifo $SAVEVG_FIFO
		/usr/bin/savevg $savevg_flags -f $SAVEVG_FIFO \
			$SAVEVG_VOLUME_GROUP > /dev/null 2>&1 &
                SVG_SIZE_IN_BYTES=`/usr/bin/cat $SAVEVG_FIFO | /usr/bin/wc -c`
		/usr/bin/rm $SAVEVG_FIFO
		(( SVG_SIZE_IN_BLOCKS = $SVG_SIZE_IN_BYTES / 512 )) ## blocks
		(( SVG_BACKUP_SIZE = $SVG_SIZE_IN_BLOCKS + 4000 ))
	fi
##########################################################
## If the user hasn't specified a savevg image then one
## will be created and used for the new savevg.
##########################################################

	if [ $SAVEVG_IMAGE_FLAG -eq $OFF ]; then

##########################################################
## If the user has specified a directory location for
## savevg placement, then ensure there is enough space
## available in the directory.
##########################################################

		if [ $MKSYSB_DIR_FLAG -eq $ON ] || [ -d "$MKSYSB_DIR" ];then

##########################################################
## Add the mksysb directory to the existing file system
## variable so it will not be remove during system
## cleanup. Also treat the dir/fs as if were specified in
## case it wasn't but exists already.
##########################################################

			EXISTING_FS="$MKSYSB_DIR $EXISTING_FS"
			MKSYSB_DIR_FLAG=$ON

			SAVEVG_IMAGE="${MKSYSB_DIR}/savevg_${PID}"
			dir_fs=`/usr/bin/df $MKSYSB_DIR/ | tail -1 | awk '{print$3}'`
			if [ $NO_DATA_FLAG -eq $OFF ]; then
				(( needed_space = ( BACKUP_SIZE_512 - dir_fs ) * 512 * 100 / 96 + 1 ))
			else
				(( needed_space = ( SVG_BACKUP_SIZE - dir_fs ) * 512 * 100 / 96 + 1 ))
			fi
			if (( needed_space > 0 )); then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 \
				"0512-329 $NAME: User specified CD or DVD file system: ${MKSYSB_DIR}\n\
needs an additional ${needed_space} bytes.\n" $NAME $MKSYSB_DIR $needed_space`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## If no savevg directory location has been specified,
## then create a new file system with the adequate space.
##########################################################

		else

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
				"Creating temporary file system: ${MKSYSB_DIR}...\n" \
				$MKSYSB_DIR`
			log_print "$mesg"
			##########################################################
			## If NO_DATA_FLAG is set, then just create the smaller
			## filesystem, needed to hold a metadata backup.
			##########################################################
			if [[ $NO_DATA_FLAG -eq $ON ]]; then
				crfs -g"$CDFS_VOLUME_GROUP" -a size="$SVG_BACKUP_SIZE" \
					$BIGFILE \
					-m $MKSYSB_DIR -p "rw" -v $FTYPE \
					-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
			else
				if [[ $BIG_FILE_ENABLED = "true" || \
					$BACKUP_SIZE_512*512 -gt $LESS_THAN_2GB ]] ;then
					if [[ "$FTYPE" = jfs ]]; then
						BIGFILE="-a bf=true"
					else
						BIGFILE=""
					fi
					crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
						$BIGFILE \
						-m $MKSYSB_DIR -p "rw" -v $FTYPE \
						-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
				else
					crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
						$BIGFILE \
						-m $MKSYSB_DIR -p "rw" -v $FTYPE \
						-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
				fi
			fi

			if [ $? -ne 0 ]; then     ## crfs command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
			/usr/sbin/mount $MKSYSB_DIR
		fi

##########################################################
## Setup the exclude list for savevg.  If the user
## specifies an exclude file, then we add mkcd exclusions
## to the list.  Otherwise, we create a new list for
## mkcd entries.
##########################################################

		setup_exclude_file $SAVEVG_VOLUME_GROUP

##########################################################
## If the user has specified the debug flag, then set
## the savevg flag for verbose output.
##########################################################

		if [ $DEBUG_FLAG -eq $ON ]; then
			savevg_flags="$savevg_flags -v"
		fi

##########################################################
## If the user has specified the no pack flag, then set
## the savevg flag for no packing.
##########################################################

		if [ $NO_PACK_FLAG -eq $ON ]; then
			savevg_flags="$savevg_flags -p"
		fi

##########################################################
## If the user has specified the no EA flag, then set
## the savevg flag for no EAs.
##########################################################

		if [ $NO_EA_FLAG -eq $ON ]; then
			savevg_flags="$savevg_flags -a"
		fi

##########################################################
## If using snapshots, add that flag to 
## the mksysb flags
##########################################################

	if [ $SNAP_FLAG -eq $ON ]; then
		savevg_flags="$savevg_flags -T"
	fi


##########################################################
## Call savevg with the appropriate flags.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 26 \
			"Creating savevg image...\n"`
		log_print "$mesg"

		savevg_flags="$savevg_flags -f $SAVEVG_IMAGE"
		/usr/bin/savevg $savevg_flags $SAVEVG_VOLUME_GROUP 2>$MSGBUF
                /usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		if [ $? -ne 0 ]; then     ## savevg command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		SAVEVG_COMPLETE_FLAG=$ON     ## set the flag for create_cd_fs

	fi

##########################################################
## Determine the savevg size.
##########################################################

	BACKUP_SIZE=`ls -l $SAVEVG_IMAGE | awk '{print$5}'`  ## in bytes

} ## end of manage_savevg

######################## manage_savewpar ################################
##
## Name:  manage_savewpar
##
## Function:  The manage_savewpar function is responsible for managing a
##            previously created savewpar image or creating a new wpar
##            image to include in the CD image(s).
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_backup
##
########################################################################

function manage_savewpar
{
typeset mesg
typeset savewpar_flags
typeset -i dir_fs
typeset -i needed_space

##########################################################
## Start with flags for auto /tmp expansion, data file
## creation, and using exclude list file.
##########################################################

	savewpar_flags="-X -e"

##########################################################
## If the user hasn't specified a savewpar image then one
## will be created and used for the new savewpar.
##########################################################

	if [ $SAVEWPAR_IMAGE_FLAG -eq $OFF ]; then

##########################################################
## If the user has specified a directory location for
## savewpar placement, then ensure there is enough space
## available in the directory.
##########################################################

		if [ $MKSYSB_DIR_FLAG -eq $ON ] || [ -d "$MKSYSB_DIR" ];then

##########################################################
## Add the mksysb directory to the existing file system
## variable so it will not be remove during system
## cleanup. Also treat the dir/fs as if were specified in
## case it wasn't but exists already.
##########################################################

			EXISTING_FS="$MKSYSB_DIR $EXISTING_FS"
			MKSYSB_DIR_FLAG=$ON

			SAVEWPAR_IMAGE="${MKSYSB_DIR}/savewpar_${PID}"
			dir_fs=`/usr/bin/df $MKSYSB_DIR/ | tail -1 | awk '{print$3}'`
			(( needed_space = ( BACKUP_SIZE_512 - dir_fs ) * 512 * 100 / 96 + 1 ))
			if (( needed_space > 0 )); then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 118 \
				"0512-329 $NAME: User specified CD or DVD file system: ${MKSYSB_DIR}\n\
needs an additional ${needed_space} bytes.\n" $NAME $MKSYSB_DIR $needed_space`
				log_print "$mesg"
				cleanup 1
			fi

##########################################################
## If no savewpar directory location has been specified,
## then create a new file system with the adequate space.
##########################################################

		else

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 38 \
				"Creating temporary file system: ${MKSYSB_DIR}...\n" \
				$MKSYSB_DIR`
			log_print "$mesg"
			##########################################################
			## Create a filesystem needed to hold a metadata backup.
			##########################################################
			if [[ $BIG_FILE_ENABLED = "true" || \
				$BACKUP_SIZE_512*512 -gt $LESS_THAN_2GB ]] ;then
				if [[ "$FTYPE" = jfs ]]; then
					BIGFILE="-a bf=true"
				else
					BIGFILE=""
				fi
				crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
					$BIGFILE \
					-m $MKSYSB_DIR -p "rw" -v $FTYPE \
					-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
			else
				crfs -g"$CDFS_VOLUME_GROUP" -a size="$BACKUP_SIZE_512" \
					$BIGFILE \
					-m $MKSYSB_DIR -p "rw" -v $FTYPE \
					-A "`locale nostr | awk -F: '{print$1}'`" >$MSGBUF 2>&1
			fi

			if [ $? -ne 0 ]; then     ## crfs command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
			/usr/sbin/mount $MKSYSB_DIR
		fi

##########################################################
## Setup the exclude list for savewpar.  If the user
## specifies an exclude file, then we add mkcd exclusions
## to the list.  Otherwise, we create a new list for
## mkcd entries.
##########################################################

		setup_exclude_file $WPAR

##########################################################
## If the user has specified the debug flag, then set
## the savewpar flag for verbose output.
##########################################################

		if [ $DEBUG_FLAG -eq $ON ]; then
			savewpar_flags="$savewpar_flags -v"
		fi

##########################################################
## If the user has specified the no pack flag, then set
## the savewpar flag for no packing.
##########################################################

		if [ $NO_PACK_FLAG -eq $ON ]; then
			savewpar_flags="$savewpar_flags -p"
		fi

##########################################################
## If the user has specified the no EA flag, then set
## the savewpar flag for no EAs.
##########################################################

		if [ $NO_EA_FLAG -eq $ON ]; then
			savewpar_flags="$savewpar_flags -a"
		fi

##########################################################
## Call savewpar with the appropriate flags.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5 mksysb.cat 129 \
			"Creating savewpar image...\n"`
		log_print "$mesg"

		savewpar_flags="$savewpar_flags -f $SAVEWPAR_IMAGE"
		/usr/bin/savewpar $savewpar_flags $WPAR 2>$MSGBUF
                /usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		if [ $? -ne 0 ]; then     ## savewpar command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi

		SAVEWPAR_COMPLETE_FLAG=$ON     ## set the flag for create_cd_fs

	fi

##########################################################
## Determine the savewpar size.
##########################################################

	BACKUP_SIZE=`ls -l $SAVEWPAR_IMAGE | awk '{print$5}'`  ## in bytes

} ## end of manage_savewpar

######################### populate_cd_fs ###############################
##
## Name:  populate_cd_fs
##
## Function:  The populate_cd_fs function is responsible for populating
##            the CD file structure with the data specified in the
##            required and optional prototype files.
##
## Parameters:  destination directory, proto file
##
## Returns:  0 for success or 1 for failure
##
## Calling Routine:  create_cd_fs
##
########################################################################

function populate_cd_fs
{
typeset cd_file
typeset err_file_list
typeset fset
typeset files_not_found
typeset separator
typeset sys_file
typeset title1
typeset title2

##########################################################
## Assign the passed parameters and assign necessary
## local variables.
##########################################################

typeset dest_dir=$1    ## destination directory for population
typeset cdfs_list=$2   ## the CD file system proto file
typeset optional_file=$3
typeset mesg
typeset -i initial_size ending_size
typeset olddir
typeset tmpsys_file="/tmp/.sys_file$$"
typeset tmpcd_file="/tmp/.cd_file$$"
typeset boot_image_dir
	err_file_list=
	files_not_found=${dest_dir}/.files_not_found
	rm -f $files_not_found 2>/dev/null

##########################################################
## Get the initial size of the destination directory. 
## We'll get the size again after we've copied the
## files listed in $cdfs_list and add the difference
## to POPULATE_CDFS_SIZE_USED.
##########################################################
	initial_size=$(get_fs_size $dest_dir)

##########################################################
## Read each line in the CD prototype file list and
## copy the file from the system to the target directory
##########################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $ON ]; then
		olddir=`pwd`
		cd $dest_dir
		>$tmpsys_file
		>$tmpcd_file
		restore -xqf $MKSYSB_IMAGE .$cdfs_list >/dev/null
		/usr/bin/cat .$cdfs_list | grep -v "^#" | \
			while read sys_file cd_file fset; do
				echo ".$sys_file" >> $tmpsys_file
				if [ $sys_file != $cd_file ]; then
					echo ".$sys_file .$cd_file" >> $tmpcd_file
				fi
			done
		xargs restore -xqf $MKSYSB_IMAGE < $tmpsys_file >/dev/null
		if [ $? -ne 0 ]; then     ## restore command return code
			if [ $optional_file -eq $ON ]; then
				MESG=`/usr/bin/dspmsg -s 5  mksysb.cat 108 \
					"Warning: $NAME was unable to copy the optional \
file: $NAME $sys_file\n\
and did not include this file in the backup.\n" $sys_file`
				log_print "$MESG"

##########################################################
## If the file is not optional and the copy attempt failed
## then log the error and return unsuccessfully.
##########################################################

			else
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cd ${olddir}
				return 1
			fi
		fi
		/usr/bin/cat $tmpcd_file | grep -v "^#" | \
		while read sys_file cd_file; do
			if [ ! -d `dirname $cd_file` ]; then
				/usr/bin/mkdir -p `dirname $cd_file`
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cd ${olddir}
					return 1
				fi
			fi
			/usr/bin/cp -p -R $sys_file $cd_file 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
				if [ $optional_file -eq $ON ]; then
					MESG=`/usr/bin/dspmsg -s 5  mksysb.cat 108 \
						"Warning: $NAME was unable to copy the optional \
file: $NAME $sys_file\n\
and did not include this file in the backup.\n" $sys_file`
					log_print "$MESG"

##########################################################
## If the file is not optional and the copy attempt failed
## then log the error and return unsuccessfully.
##########################################################

				else
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cd ${olddir}	
					return 1
				fi
			fi
		done
		rm -f .$cdfs_list $tmpsys_file $tmpcd_file 2>/dev/null
		cd ${olddir}

		ending_size=$(get_fs_size $dest_dir)
		(( POPULATE_CDFS_SIZE_USED = POPULATE_CDFS_SIZE_USED + ending_size - initial_size ))

##########################################################
## If using an existing mksysb image call
## build_boot_images now to create the boot image.
## This is done at this point to use the space that will
## eventually be taken up by the mksysb image so that
## extra working space is not needed for bootpkg to
## create the boot image.
##########################################################

		if [ $optional_file -eq $OFF ]; then
			build_boot_images
		fi
		return
	fi
	/usr/bin/cat $cdfs_list | grep -v "^#" | \
		while read sys_file cd_file fset; do

##########################################################
## The next loop is used to process non existent files on
## the source system.  This loop will also be used to
## compile a list of missing required files.
##########################################################

			if [ ! -r "$sys_file" ]; then
				if [ $optional_file -eq $OFF ]; then
					mesg="${sys_file}\t${fset}\n"
					err_file_list="${err_file_list}${mesg}"
				fi

				continue
			fi

#########################################################
## Ensure the directory path exists before copying each
## file.  If the directory does not exist, create it.
##########################################################

			if [ ! -d `dirname ${dest_dir}${cd_file}` ]; then
				/usr/bin/mkdir -p `dirname ${dest_dir}${cd_file}` 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## mkdir command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					return 1
				fi
			fi

##########################################################
## Copy the system file to the CD path.  If an error
## occurs while copying the file and the OPTIONAL flag
## is set, then log the file as not included.
##########################################################

			/usr/bin/cp -p -R $sys_file ${dest_dir}${cd_file} 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## cp command return code
				if [ $optional_file -eq $ON ]; then
					MESG=`/usr/bin/dspmsg -s 5  mksysb.cat 108 \
						"Warning: $NAME was unable to copy the optional \
file: $NAME $sys_file\n\
and did not include this file in the backup.\n" $sys_file`
					log_print "$MESG"

##########################################################
## If the file is not optional and the copy attempt failed
## then log the error and return unsuccessfully.
##########################################################

				else
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					return 1
				fi
			fi
			/usr/bin/chmod +r ${dest_dir}${cd_file} > /dev/null 2>&1
	done

	ending_size=$(get_fs_size $dest_dir)
	(( POPULATE_CDFS_SIZE_USED = POPULATE_CDFS_SIZE_USED + ending_size - initial_size ))

##########################################################
## If a list of missing required files exists, then
## display an error message that contains all the missing
## files and the associated filesets.  Then return from
## the routine unsuccessfully.
##########################################################

	if [ -n "$err_file_list" ]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 117 \
			"0512-323 $NAME: The following files are required for the\n\
creation of the CD or DVD image and are not available on the source system:\n\
${err_file_list}\n\
The files can be installed from the listed filesets.\n" $NAME $err_file_list`
		log_print "$mesg"
		return 1
	fi

} ## end of populate_cd_fs


######################### print_usage ##################################
##
## Name:  print_usage
##
## Function:  The print_usage function is responsible for printing a
##            usage message to the mkcd log and the standard output.
##
## Parameters:  None.
##
## Returns:  0 - for successful completion
##
## Calling Routine:  check_cd_ready, compare_options, validate_argument,
##                   & main
##
########################################################################

function print_usage
{
typeset usage
typeset usage1
typeset usage2

rm -f $FS_FREE_SPACE 2>/dev/null
if [ $NAME = "mkcd" ]
then
	usage=`/usr/bin/dspmsg -s 5  mksysb.cat 137 \
"\n\
Usage: mkcd {-d <cd_device | dvd_device> | -S}\n\
            [-m <mksysb_image> | -M <mksysb_target> |\n\
             -s <savevg_image> | -v <savevg_volume_group> |\n\
             -w <savewpar_image> | -W <wparName>]\n\
            [-C <cd_fs>] [-I <cd_image_dir>] [-V <cdfs_volume_group>]\n\
            [-B] [-p <pkg_source_dir>] [-R]\n\
            [-i <image.data>] [-u <bosinst.data>]\n\
            [-f <wparSpecificationFile>]\n\
            [-e] [-P] [-l <package_list>] [-b <bundle_file>]\n\
            [-x filename] [-T]\n\
            [-z <custom_file>] [-D] [-L | -U] [-n] [-c] [-a] [-A] [-Z] [-G | -N]\n"`
	log_print "$usage"

	usage1=`/usr/bin/dspmsg -s 5 mksysb.cat 91 \
"\n\
To make a Rock Ridge CD or DVD from an existing directory structure:\n\
      Usage: mkcd {-d <cd_device | dvd_device> | -S}\n\
                  [-r <directory>] [-R]\n\
                  [-I <cd_image_dir>] [-D] [-L | -U]\n"`

	log_print "$usage1"

	return 0
else
	usage=`/usr/bin/dspmsg -s 5  mksysb.cat 138 \
"\n\
Usage: mkdvd {-d <dvd_device> | -S}\n\
            [-m <mksysb_image> | -M <mksysb_target> |\n\
             -s <savevg_image> | -v <savevg_volume_group> |\n\
             -w <savewpar_image> | -W <wparName>]\n\
            [-C <cd_fs>] [-I <cd_image_dir>] [-V <cdfs_volume_group>]\n\
            [-B] [-p <pkg_source_dir>] [-R]\n\
            [-i <image.data>] [-u <bosinst.data>]\n\
            [-f <wparSpecificationFile>]\n\
            [-e] [-P] [-l <package_list>] [-b <bundle_file>]\n\
            [-x filename] [-T]\n\
            [-z <custom_file>] [-D] [-U] [-n] [-c] [-a] [-A] [-Z] [-G | -N]\n"`
	log_print "$usage"

	usage1=`/usr/bin/dspmsg -s 5 mksysb.cat 119 \
"\n\
To make a Rock Ridge DVD from an existing directory structure:\n\
      Usage: mkdvd {-d <dvd_device> | -S}\n\
                  [-r <directory>] [-R]\n\
                  [-I <cd_image_dir>] [-D] [-U]\n"`

	log_print "$usage1"
	return 0
fi

} ## end of print_usage


######################### remove_file_system ###########################
##
## Name:  remove_file_system
##
## Function:  The remove_file_system function is responsible for
##            unmounting and removing a mkcd created file system, as
##            well as removing the mount point.
##
## Parameters:  file system
##
## Returns:  None
##
## Calling Routine:  file_system_cleanup
##
########################################################################

function remove_file_system
{
typeset fs_name=$1

typeset mesg

#############################################################
## If the file system exists and was not an existing
## directory/file system, then remove it.
#############################################################

	if [ -n "$fs_name" ] && [ -d "$fs_name" ] &&
		[ `echo $EXISTING_FS | grep -c $fs_name` -eq 0 ]; then

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 49 \
			"Removing temporary file system: ${fs_name}...\n" $fs_name`
		log_print "$mesg"

#############################################################
## First, unmount the file system.  Then remove the
## file system and the mount point.
#############################################################

		/usr/sbin/umount $fs_name 2>$MSGBUF
		if [ $? -ne 0 ]; then
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi

		rmfs -r $fs_name >$MSGBUF 2>&1
		if [ $? -ne 0 ]; then
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
		fi
	fi

} ## end of remove_file_system


######################### setup_data_files #############################
##
## Name:  setup_data_files
##
## Function:  The setup_data_files function is responsible for saving
##            existing data files, copying, and creating user supplied
##            data files.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_mksysb & manage_savevg
##
########################################################################

function setup_data_files
{
typeset mesg
typeset mkszfile_flag
typeset mkvgdata_flag
typeset mkwpardata_flag

#############################################################
## If the mkcd is creating a mksysb...
#############################################################

	if [ $MKSYSB_IMAGE_FLAG -eq $OFF ] && \
		[ $SAVEWPAR_IMAGE_FLAG -eq $OFF ] && \
		[ $WPAR_FLAG -eq $OFF ] && \
		[ $SAVEVG_IMAGE_FLAG -eq $OFF ] && \
		[ "$SAVEVG_VOLUME_GROUP" = "rootvg" -o \
		$SAVEVG_VOLUME_GROUP_FLAG -eq $OFF ]; then

#############################################################
## If a user has supplied a new image.data, then copy the
## file to the / location on the system.  Then set a flag to
## clean up the file upon completion of the command.
#############################################################

		if [ -n "$NEW_IMAGE_DATA" ]; then
			if [ "$NEW_IMAGE_DATA" != "/image.data" ]; then

				/usr/bin/cp /image.data /image.data.save.mkcd 2>/dev/null
				/usr/bin/cp $NEW_IMAGE_DATA /image.data 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## cp command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi
				REMOVE_IMAGE_DATA=$ON
			fi

#############################################################
## If the user hasn't supplied a new image.data, then
## call mkszfile to create a new image.data file.
#############################################################

		else

##########################################################
## If the user has specified the mapping flag, then set
## the mkszfile flag for mapping
##########################################################

			 ## Mounting unmounted filesystems for WPARS ##
                        if  [ "$NNFLAG" -eq $ON ]
                        then
                                mount_fs_wpar
                        fi

			if [ $MAPPING_FLAG -eq $ON ]; then
				mkszfile_flag="-m"
			fi

			if [ $NO_WPARS_FLAG -eq $ON ]; then
				mkszfile_flag="$mkszfile_flag -G"
			fi

			if [ $XFS_FLAG -eq $ON ]; then
				mkszfile_flag="$mkszfile_flag -x $XFSLIST"
			fi

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 37 \
				"Creating image.data file...\n"`
			log_print "$mesg"

			/usr/bin/mkszfile -X $mkszfile_flag 2>$MSGBUF
			if [ $? -ne 0 ]; then     ## mkszfile command return code
				/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
					"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
				log_print "$mesg"
				cleanup 1
			fi
		fi

#############################################################
## If a user has supplied a new bosinst.data, then copy the
## file to the / location on the system.  Then create the
## save_bosinst.data_file to ensure that mksysb will not
## modify the user supplied bosinst.data file.  Then set a
## flag to clean up the file upon completion of the command.
#############################################################

		if [ -n "$NEW_BOSINST_DATA" ]; then
			if [ "$NEW_BOSINST_DATA" != "/bosinst.data" ]; then

				/usr/bin/cp /bosinst.data /bosinst.data.save.mkcd 2>/dev/null
				/usr/bin/cp $NEW_BOSINST_DATA /bosinst.data 2>$MSGBUF
				if [ $? -ne 0 ]; then     ## cp command return code
					/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
					mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
						"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
					log_print "$mesg"
					cleanup 1
				fi
				touch /save_bosinst.data_file
				REMOVE_BOSINST_DATA=$ON
			fi
		fi

##########################################################
## If mkcd is creating a savevg...
##########################################################

	elif [ $SAVEVG_VOLUME_GROUP_FLAG -eq $ON ]; then

##########################################################
## If the user has specified the mapping flag, then set
## the mkvgdata flag for mapping.
##########################################################

		if [ $MAPPING_FLAG -eq $ON ]; then
			mkvgdata_flag="-m"
		fi

		mesg=`/usr/bin/dspmsg -s 2  mksysb.cat 25 \
			"Creating information file for volume group ${SAVEVG_VOLUME_GROUP}." \
			$SAVEVG_VOLUME_GROUP`
		log_print "$mesg"

		/usr/bin/mkvgdata -X $mkvgdata_flag $SAVEVG_VOLUME_GROUP 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## mkvgdata command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
##########################################################
## If mkcd is creating a savewpar...
##########################################################

	elif [ $WPAR_FLAG -eq $ON ]; then

##########################################################
## If the user has specified the mapping flag, then set
## the mkvgdata flag for mapping.
##########################################################

		if [ $MAPPING_FLAG -eq $ON ]; then
			mkwpardata_flag="-m"
		fi

		mesg=`/usr/bin/dspmsg -s 2  mksysb.cat 33 \
			"Creating information file for workload partition group ${WPAR}." \
			$WPAR`
		log_print "$mesg"

		/usr/bin/mkwpardata -X $mkwpardata_flag $WPAR 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## mkwpardata command return code
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 94 \
				"0512-300 $NAME: Error in routine: $0.\n" $NAME $0`
			log_print "$mesg"
			cleanup 1
		fi
	fi

} ## end of setup_data_files


######################### setup_exclude_file ###########################
##
## Name:  setup_exclude_file
##
## Function:  The setup_exclude_file function is responsible for
##            setting up the exclude file for the backup.  If the user
##            doesn't specify an exclude list, then one is created to
##            omit the mkcd created file systems.
##
## Parameters:  volume group
##
## Returns:  None
##
## Calling Routine:  manage_mksysb & manage_savevg
##
########################################################################

function setup_exclude_file
{
typeset mksysb_vg
typeset savevg_vg

	VG_NAME=$1

#############################################################
## If the user has specified an exclude list will be used,
## then copy the original and add the mkcd file systems to
## the file.
#############################################################

	if [ $EXCLUDE_LIST_FLAG -eq $ON ]; then
		/usr/bin/cp /etc/exclude.${VG_NAME} /etc/exclude.${VG_NAME}.save.mkcd \
			2>/dev/null

#############################################################
## If the user did not specify an exclude list, then save
## an existing file, if it exists.
#############################################################

	else
		/usr/bin/mv /etc/exclude.${VG_NAME} /etc/exclude.${VG_NAME}.save.mkcd \
			2>/dev/null
	fi

#############################################################
## Add the standard mkcd file systems to the exclude list
## file.  There is an assumption that the user would not
## want to include the mkcd file systems in the mksysb.
#############################################################

	echo "./mkcd/mksysb_image" >>/etc/exclude.${VG_NAME}
	echo "./mkcd/cd_images" >>/etc/exclude.${VG_NAME}
	echo "./mkcd/cd_fs" >>/etc/exclude.${VG_NAME}

} ## end of setup_exclude_file


######################### size_input_file_list #########################
##
## Name: size_input_file_list
##
## Function:  The size_input_file_list function is responsible for
##            comparing the total size of the files from a list of files
##            to the free space in a filesystem.
##				  If the destination directory is not specified, the
##				  the total size of the files is added to the global
##				  variables used to create the filesystems.
##
## Parameters:  list file, destination directory
##
## Returns:  0 for success or 1 if the files won't fit
##
## Calling Routine:  create_just_file_cd_fs
##
########################################################################

function size_input_file_list
{
##########################################################
## Assign the passed parameters and assign necessary
## local variables.
##########################################################

typeset cdfs_list=$1   ## the CD file system proto file
typeset dest_dir=$2    ## destination directory for population

typeset cd_file
typeset fset
typeset mesg
typeset sys_file
typeset total_size
typeset -i fs_free
typeset -i file_size

	total_size=0

##########################################################
## Get free space in directory
##########################################################

	[[ -z $dest_dir ]] && fs_free=0 || \
	fs_free=`/usr/bin/df $dest_dir/ | tail -1 | awk '{print $3}'`

##########################################################
## Read each line in the CD prototype file list and
## add up the total size of the files
## Check for symlink and append slash to get size of
## the real file.
##########################################################

	/usr/bin/cat $cdfs_list | grep -v "^#" | \
	while read sys_file cd_file fset; do
		if [[ -L $sys_file && -f $sys_file/ ]]; then
			file_size=$( /usr/bin/du $sys_file/ 2>/dev/null | awk '{print $1}' )
			(( total_size = total_size + file_size ))
		else
			if [[ -f $sys_file ]]; then
				file_size=$( /usr/bin/du $sys_file 2>/dev/null | awk '{print $1}' )
				(( total_size = total_size + file_size ))
			fi
		fi
	done

##########################################################
## If no destination directory was specified, add the
## total size of the requested files to the global
## variables for the required size of the filesystem.
##########################################################
	if [[ -z $dest_dir ]]; then
		(( CD_FS_MIN_SIZE = CD_FS_MIN_SIZE + total_size * 512 ))
		(( CD_FS_MIN_512 = CD_FS_MIN_512 + total_size ))
		(( CD_FS_CREATE_SIZE_512 = CD_FS_MIN_512 * 100 / 96 + 1 ))

		if [[ $DVD_FLAG = $ON ]]; then
			if (( CD_FS_MIN_SIZE > DVD_SIZE )); then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 114 \
					"0512-319 $NAME: Standard CD or DVD file system: ${CD_FS}\n\
does not have enough free space to hold the CD or DVD file structure.\n\
The minimum free space required is $CD_FS_MIN_SIZE bytes.\n" \
				$NAME $CD_FS $CD_FS_MIN_SIZE`
				log_print "$mesg"
				cleanup 1
			fi
		else
			if (( CD_FS_MIN_SIZE > CD_SIZE )); then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 114 \
					"0512-319 $NAME: Standard CD or DVD file system: ${CD_FS}\n\
does not have enough free space to hold the CD or DVD file structure.\n\
The minimum free space required is $CD_FS_MIN_SIZE bytes.\n" \
				$NAME $CD_FS $CD_FS_MIN_SIZE`
				log_print "$mesg"
				cleanup 1
			fi
		fi
	else
		##########################################################
		## Check space.  Return non-zero if the files won't fit
		##########################################################
		(( total_size > fs_free )) && return 1
		return 0
	fi

} ## end of size_input_file_list

######################### validate_argument ############################
##
## Name:  validate_argument
##
## Function:  The validate_argument function is responsible for ensuring
##            that if an option requires a following argument, the
##            argument does not begin with a dash "-".  This is really
##            a check for a missing argument.  Since the getopts shell
##            function doesn't care what the following argument is, we
##            check for a dash, which is really the next command option.
##
## Parameters:  option, argument
##              option - the option flag passed to the mkcd
##                       command.
##              argument - The required argument for the specified option.
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function validate_argument
{
typeset option=$1
typeset argument=$2

typeset mesg

	if [[ $argument = -* ]]; then
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 98 \
			"0512-310 $NAME: Bad argument: $argument for option -${option}.\n" $NAME $argument $option`
		log_print "$mesg"
		print_usage
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

} ## end of validate_argument


######################### verify_parm ##################################
##
## Name:  verify_parm
##
## Function:  The verify_parm function is responsible for checking the
##            parameter type and existence of a the parameter.
##
## Parameters:  type, file/directory
##              type - IS_DEVICE - a device file
##                   - IS_BUNDLE - a bundle file
##                   - IS_VG - a volume group
##                   - IS_WPAR - a workload partition
##                   - READABLE -  file with read access
##                   - WRITABLE - file with write access
##                   - EXECUTABLE - file with execute permission
##                   - RWX_DIRECTORY - a read/write/execute directory
##                   - RX_DIRECTORY - a read/execute directory
##              file - the path/name of the file
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function verify_parm
{
typeset check_type=$1
typeset file=$2

typeset mesg
typeset chk_avail
typeset bfile
typeset tape_dev
typeset automountcd

#############################################################
## If verifying a CD device, call check_cd_ready to ensure
## the CD is available and ready.
#############################################################

	if [ $check_type -eq $IS_DEVICE ]; then     ## file is a special file (device)
		if [ -b "$file" ]; then     ## if a device (cd)
			if [ -s "/usr/sbin/cdcheck" ]; then
				automountcd="`/usr/sbin/cdcheck -a $file 2>/dev/null`"
			fi
			if [ -n "$automountcd" ]; then
				/usr/sbin/cdutil -s -k $file > /dev/null 2>&1
				if [ $? -eq 0 ]; then   ## cdutil command return code
					AUTORESUME=yes
				fi
			fi
			check_cd_ready $file

#############################################################
## If the CD device is defined as a tape device
## (Young Minds, Inc.)
#############################################################

		elif [ -c "$file" ]; then

			tape_dev=`basename $file`
			chk_avail=`LANG=C /usr/sbin/lsdev -Cc tape | grep -w $tape_dev | \
				awk '{print$2}'`
			if [ -z "$chk_avail" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 111 \
					"0512-301 $NAME: $file is not a CD or DVD device.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi

##########################################################
## Then check to see if the tape device is available
##########################################################

			if [ "$chk_avail" != "Available" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 95 \
					"0512-302 $NAME: Device $file is not Available.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi

		else

#############################################################
## If the source directory is not a CDROM device, then
## ensure the directory exists.
#############################################################

			if [ ! -d "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 99 \
					"0512-311 $NAME: The device or directory: $file does \
not exist.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi

#############################################################
## Check to see if the bundle file exists, either from the
## file path given or from the bundle file location on the
## source system.
#############################################################

	elif [ $check_type -eq $IS_BUNDLE ]; then
		if [ ! -s "$file" ]; then
			bfile=${file%%.bnd}     ## remove the .bnd suffix if added
			if [ -s "/usr/sys/inst.data/sys_bundles/${bfile}.bnd" ]; then
				BUNDLE_FILE=/usr/sys/inst.data/sys_bundles/${bfile}.bnd
			elif [ -s "/usr/sys/inst.data/user_bundles/${bfile}.bnd" ]; then
				BUNDLE_FILE=/usr/sys/inst.data/user_bundles/${bfile}.bnd
			else
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 100 \
					"0512-312 $NAME: Bundle file $file does not exist or is \
zero length.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi

#############################################################
## Check to see if the user specified volume group exists
#############################################################

	elif [ $check_type -eq $IS_VG ]; then
		lsvg -o | grep -wq $file
		if [ $? -ne 0 ]; then     ## lsvg command return code
			mesg=`/usr/bin/dspmsg -s 1  mksysb.cat 6 \
				"0512-009 $NAME: Invalid or missing Volume Group Name.\n" \
				$NAME`
			log_print "$mesg"
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi

#############################################################
## Check to see if the user-specified workload partition exists
#############################################################

	elif [ $check_type -eq $IS_WPAR ]; then
		if [[ `lswpar -qca type $WPAR  2>/dev/null` != 'S' ]]
		then
			mesg=`/usr/bin/dspmsg -s 1  mksysb.cat 72 \
				"0512-009 $NAME: Invalid or missing Workload Partition.\n" \
				$NAME`
			log_print "$mesg"
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi


#############################################################
## If the file is a file/directory, then check to see if
## it exists and begins with a "/".  This command mandates
## that files be given with their absolute path.
#############################################################

	else     ## file is a file/directory

#############################################################
## If the file is a readable/writable/executable directory,
## then verify it exists and check to see if it has full
## permissions.
#############################################################

		if [ $check_type -eq $RWX_DIRECTORY ]; then
			if [ ! -d "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 101 \
					"0512-313 $NAME: $file directory does not exist.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			else
				verify_parm $READABLE $file
				verify_parm $WRITABLE $file
				verify_parm $EXECUTABLE $file
			fi

#############################################################
## If the file is a readable/executable directory, then
## verify it exists and check to see that it has read and
## execute permissions.
#############################################################

		elif [ $check_type -eq $RX_DIRECTORY ]; then
			if [ ! -d "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 101 \
					"0512-313 $NAME: $file directory does not exist.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			else
				verify_parm $READABLE $file
				verify_parm $EXECUTABLE $file
			fi

#############################################################
## Check to see if the file exists
#############################################################

		else
			if [ ! -s "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 1 mksysb.cat 42 \
					"0512-054 $NAME: File $file does not exist or is empty.\n" \
					$NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi

#############################################################
## Check to see if the file has read permission
#############################################################

		if [ $check_type -eq $READABLE ]; then
			if [ ! -r "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 102 \
					"0512-314 $NAME: $file does not have read permission.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi

#############################################################
## Check to see if the file has write permission
#############################################################

		if [ $check_type -eq $WRITABLE ]; then
			if [ ! -w "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 103 \
					"0512-315 $NAME: $file does not have write permission.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi

#############################################################
## Check to see if the file has execute permission
#############################################################

		if [ $check_type -eq $EXECUTABLE ]; then
			if [ ! -x "$file" ]; then
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 104 \
					"0512-316 $NAME: $file does not have execute permission.\n" $NAME $file`
				log_print "$mesg"
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
		fi
	fi

} ## end of verify_parm


######################### verify_image #################################
##
## Name:  verify_image
##
## Function:  The verify_image function is responsible for verifying
##            the image is the type of the parameter.
##
## Parameters:  type, image
##              type - IS_MKSYSB - a mksysb image
##                   - IS_SAVEVG - a savevg image
##              image - the path/name of the image
##
## Returns:  None
##
## Calling Routine:  main
##
########################################################################

function verify_image
{
typeset type=$1
typeset img_name=$2

typeset mesg
typeset img_type

#############################################################
## If the image is suppose to be a mksysb, then check the
## image for the existance of a bosinst.data.
#############################################################

	if [ $type -eq $IS_MKSYSB ]; then
		if [ `dd if=$img_name bs=1k count=128 2>/dev/null \
			| restbyname -TqdSf - 2>/dev/null \
			| grep -cw '^\.\/bosinst\.data'` -eq 0 ]; then
			img_type=mksysb
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 109 \
				"0512-325 $NAME: The user supplied $img_type image: $img_name
is not a valid $img_type image.\n" $NAME $img_type $img_name $img_type`
			log_print "$mesg"
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi

#############################################################
## If the image is suppose to be a savevg, then check the
## image for the existance of a vgdata directory
#############################################################

	elif [ $type -eq $IS_SAVEVG ]; then
		if [ `dd if=$img_name bs=1k count=128 2>/dev/null \
			| restbyname -TqdSf - 2>/dev/null \
			| grep -cw '^\.\/tmp\/vgdata'` -eq 0 ]; then
			img_type=savevg
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 109 \
				"0512-325 $NAME: The user supplied $img_type image: $img_name
is not a valid $img_type image.\n" $NAME $img_type $img_name $img_type`
			log_print "$mesg"
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi
#############################################################
## If the image is suppose to be a savevg, then check the
## image for the existance of a vgdata directory
#############################################################

	elif [ $type -eq $IS_SAVEWPAR ]; then
		if [ `dd if=$img_name bs=1k count=128 2>/dev/null \
			| restbyname -TqdSf - 2>/dev/null \
			| grep -cw '^\.\/.savewpar_dir\/'` -eq 0 ]; then
			img_type=savewpar
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 109 \
				"0512-325 $NAME: The user supplied $img_type image: $img_name
is not a valid $img_type image.\n" $NAME $img_type $img_name $img_type`
			log_print "$mesg"
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
		fi
	fi

} ## end of verify_image


######################### write_to_cd ##################################
##
## Name:  write_to_cd
##
## Function:  The write_to_cd function is responsible for writing the CD
##            image to the CD-R device.
##
## Parameters:  None
##
## Returns:  None
##
## Calling Routine:  manage_multivolume & main
##
########################################################################

function write_to_cd
{
typeset mesg
typeset answer
typeset -i rc

	sleep_count=1
	while :; do
		while [ "$sleep_count" -le 45 ]
		do
			sleep 10
			echo ".\c"
			let "sleep_count = sleep_count + 1"
		done
		echo ""
		sleep_count=1
	done &
	SLEEP_PID=$!
#############################################################
## If the stop flag (-S) is set, then keep the final CD
## image, but don't burn the CD.  Otherwise, the CD is
## burned by default.
#############################################################

	if [ $STOP_FLAG -eq $OFF ]; then

##########################################################
## Write the CD image to the CD-R device using the user
## supplied link to a CD writing script.  The script
## should expect to receive a CD-R device and CD image,
## as parameters in that order.
##########################################################

		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 74 \
			"Writing the CD or DVD image to device: ${DEVICE}...\n" $DEVICE`
		log_print "$mesg"

		/usr/sbin/burn_cd $DVD_FLAG $DEVICE $CD_IMAGE_NAME 2>$MSGBUF
		if [ $? -ne 0 ]; then     ## burn_cd command return code
			if [ -n "$SLEEP_PID" ]
			then
				kill_pid "$SLEEP_PID"
				echo ""
			fi
			/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 116 \
				"0512-322 $NAME: An error has occurred\n\
writing the CD or DVD image to the CD or DVD device.\n" $NAME`
			log_print "$mesg"

##########################################################
## If an error occurs while burning the CD, the user
## is given the opportunity to attempt another burn or
## quit. The loop will ensure that the device is ready
## before beginning an another write to the CD device.
##########################################################

			rc=1
			while (( rc != 0 )); do

				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 76 \
					"\nIf you would like to attempt to write to another CD or DVD, replace
the current CD or DVD with a new writable CD or DVD.  Press the <enter> key when ready...
If you would like to stop the command and cleanup, Press 'q' to quit...\n"`
				log_print "$mesg"

				read answer
				if [ "$answer" = "q" ] || [ "$answer" = "Q" ]; then
					cleanup 1
				else

##########################################################
## If the user has replaced the CD and is attempting
## another burn, then check to see if the CD drive is
## ready.
##########################################################

					>$MSGBUF 2>&1 <$DEVICE
					rc=$?
					if (( rc != 0 )); then
						mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 93 \
							"0512-332 $NAME: Device $DEVICE does not appear to be ready.  For
information about possible causes, see /usr/lpp/bos.sysmgt/mkcd.README.txt
  Continuing...\n" $NAME $DEVICE`
						log_print "$mesg"
						rc=0
						sleep 5

					fi
				fi
			done

##########################################################
## If the previous burn encountered an error, then begin
## the routine again, for another attempt.
##########################################################

			write_to_cd
		fi
	fi
	if [ -n "$SLEEP_PID" ]
	then
		kill_pid "$SLEEP_PID"
		echo ""
	fi

} ## end of write_to_cd


######################### main #########################################

typeset mesg
typeset option
typeset left_over
#############################################################
## Set the PATH variable for command execution
#############################################################

export PATH="/usr/bin:/usr/sbin:/etc:/usr/ucb:/usr/bin/X11:/sbin:$PATH"

#############################################################
## Trap any hangup, interrupt, and termination for
## command cleanup.
#############################################################

trap 'cleanup 1'  1 2 15

check_user     ## validate the user is root

NAME=`/usr/bin/basename $0`
initialize_variables     ## initialize command global variables

initialize_log $*     ## setup the command log file

#############################################################
## Command Line Option Handling
#############################################################

mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 36 \
	"Verifying command parameters...\n"`
log_print "$mesg"
if [ $NAME = "mkdvd" ]
then
	DVD_ISO9660_FLAG=$ON
	DVD_FLAG=$ON
	BIG_FILE_ENABLED="true"
fi

while getopts ":d:E:m:j:s:v:C:M:I:V:p:r:BRSi:u:ePl:b:z:w:W:f:x:DLYUncaAZGNT" option
do
	if [[ $option = ':' ]]; then
		mesg=$(/usr/bin/dspmsg -s 5  mksysb.cat 79 \
			"Missing argument for option -${OPTARG}\n" $OPTARG)
		log_print "$mesg"
		print_usage
		rm -f $FS_FREE_SPACE 2>/dev/null
		exit 1
	fi

	case $option in
		d)	DEVICE="/dev/`basename $OPTARG`"
			validate_argument $option $DEVICE
			verify_parm $IS_DEVICE $DEVICE
			DEVICE_FLAG=$ON;;

		m)	MKSYSB_IMAGE=$OPTARG
			validate_argument $option $MKSYSB_IMAGE
			verify_parm $READABLE $MKSYSB_IMAGE
			verify_image $IS_MKSYSB $MKSYSB_IMAGE
			MKSYSB_IMAGE_FLAG=$ON;;

		j)	JUST_FILE=$OPTARG
			CD_FS_MIN_SIZE=41943040
			validate_argument $option $JUST_FILE
			JUST_FILE_FLAG=$ON;;

		r)	CD_FS_DIR=$OPTARG
			validate_argument $option $CD_FS_DIR
			verify_parm $RX_DIRECTORY $CD_FS_DIR
			MK_ROCKRIDGE_FLAG=$ON;;

		E)	EFI_PARTITION_PROTO=$OPTARG
			validate_argument $option $EFI_PARTITION_PROTO
			CREATE_EFI_PARTITION=$ON;;

		s)	SAVEVG_IMAGE=$OPTARG
			validate_argument $option $SAVEVG_IMAGE
			verify_parm $READABLE $SAVEVG_IMAGE
			verify_image $IS_SAVEVG $SAVEVG_IMAGE
			SAVEVG_IMAGE_FLAG=$ON
			NONBOOT_FLAG=$ON;;

		v)	SAVEVG_VOLUME_GROUP=$OPTARG
			validate_argument $option $SAVEVG_VOLUME_GROUP
			verify_parm $IS_VG $SAVEVG_VOLUME_GROUP
			SAVEVG_VOLUME_GROUP_FLAG=$ON
			NONBOOT_FLAG=$ON
			VG_NAME=$SAVEVG_VOLUME_GROUP;;

		C)	CD_FS_DIR=$OPTARG
			validate_argument $option $CD_FS_DIR
			verify_parm $RWX_DIRECTORY $CD_FS_DIR
			CD_FS_DIR_OLD=$CD_FS_DIR
			CD_FS_DIR_FLAG=$ON;;

		M)	MKSYSB_DIR=$OPTARG
			validate_argument $option $MKSYSB_DIR
			verify_parm $RWX_DIRECTORY $MKSYSB_DIR
			MKSYSB_DIR_FLAG=$ON;;

		I)	CD_IMAGE_DIR=$OPTARG
			validate_argument $option $CD_IMAGE_DIR
			verify_parm $RWX_DIRECTORY $CD_IMAGE_DIR
			CD_IMAGE_DIR_FLAG=$ON;;

		V)	CDFS_VOLUME_GROUP=$OPTARG
			validate_argument $option $CDFS_VOLUME_GROUP
			verify_parm $IS_VG $CDFS_VOLUME_GROUP
			CDFS_VOLUME_GROUP_FLAG=$ON;;

		p)	PKG_SOURCE_DIR=$OPTARG
			validate_argument $option $PKG_SOURCE_DIR
			verify_parm $IS_DEVICE $PKG_SOURCE_DIR
			PKG_SOURCE_DIR_FLAG=$ON;;

		B)	NONBOOT_FLAG=$ON;;

		R)	LEAVE_IMAGE_FLAG=$ON;;

		S)	STOP_FLAG=$ON;;

		i)	NEW_IMAGE_DATA=$OPTARG
			validate_argument $option $NEW_IMAGE_DATA
			verify_parm $READABLE $NEW_IMAGE_DATA
			NEW_IMAGE_DATA_FLAG=$ON;;

		u)	NEW_BOSINST_DATA=$OPTARG
			validate_argument $option $NEW_BOSINST_DATA
			verify_parm $READABLE $NEW_BOSINST_DATA
			NEW_BOSINST_DATA_FLAG=$ON;;

		e)	EXCLUDE_LIST_FLAG=$ON;;

		P)	MAPPING_FLAG=$ON;;

		l)	STACK_LIST=$OPTARG
			validate_argument $option $STACK_LIST
			verify_parm $READABLE $STACK_LIST
			STACK_LIST_FLAG=$ON;;

		b)	BUNDLE_FILE=$OPTARG
			validate_argument $option $BUNDLE_FILE
			verify_parm $IS_BUNDLE $BUNDLE_FILE
			BUNDLE_FILE_FLAG=$ON;;

		z)	CUSTOMIZATION_FILE=$OPTARG
			validate_argument $option $CUSTOMIZATION_FILE
			verify_parm $READABLE $CUSTOMIZATION_FILE
			CUSTOMIZATION_FILE_FLAG=$ON;;

		D)	set -x
			debug_switch $ON
			DEBUG_FLAG=$ON;;

		L)	if [ $NAME = "mkdvd" ]
			then
				LFLAG="-L"
				mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 105 \
					"0512-317 $NAME: Unknown command line option $LFLAG.\n" \
				$NAME $LFLAG`
				log_print "$mesg"
				print_usage
				rm -f $FS_FREE_SPACE 2>/dev/null
				exit 1
			fi
			DVD_ISO9660_FLAG=$ON
			DVD_FLAG=$ON
			BIG_FILE_ENABLED="true";;

		Y)	ACCEPT_LICENSE_AGMT_FLAG=$ON;;

		U)	DVD_FLAG=$ON
			DVD_ISO9660_FLAG=$OFF
			UDF_FLAG=$ON;;

		n)	NO_DATA_FLAG=$ON;;	## Only backup the vgdata files.
                            ## No data is backed up.

		c)	NO_PACK_FLAG=$ON;;	## Don't compress or pack files.

		a)	NO_EA_FLAG=$ON;;	## Don't backup extended attributes.

		A)	DMAPI_FLAG=$ON;;	## Back up DMAPI filesystems files.

		Z)	Z_FLAG=$ON;;	        ## Don't call backup with -Z

		w)	SAVEWPAR_IMAGE=$OPTARG
			validate_argument $option $SAVEWPAR_IMAGE
			verify_parm $READABLE $SAVEWPAR_IMAGE
			verify_image $IS_WPAR $SAVEWPAR_IMAGE
			NONBOOT_FLAG=$ON
			SAVEWPAR_IMAGE_FLAG=$ON;;

		W)	WPAR=$OPTARG
			validate_argument $option $WPAR
			verify_parm $IS_WPAR $WPAR
			NONBOOT_FLAG=$ON
			WPAR_FLAG=$ON;;

		f)	WPAR_SPEC_FILE=$OPTARG
			validate_argument $option $WPAR_SPEC_FILE
			verify_parm $READABLE $WPAR_SPEC_FILE
			WPAR_SPEC_FLAG=$ON;;


		N)	NNFLAG=$ON;;

		G)	NO_WPARS_FLAG=$ON;;

		x)	XFSLIST=$OPTARG
			validate_argument $option $XFSLIST
			verify_parm $READABLE $XFSLIST
			XFS_FLAG=$ON;;


		T)	SNAP_FLAG=$ON;;
		\?)	shift $(($OPTIND -2))

			mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 105 \
				"0512-317 $NAME: Unknown command line option $1.\n" \
				$NAME $1`
			log_print "$mesg"
			print_usage
			rm -f $FS_FREE_SPACE 2>/dev/null
			exit 1
	esac
done
shift $(($OPTIND - 1))
left_over=$*     ## dummy variable to handle leftover parameters

#############################################################
# if user specifies the directory for CD filesystem structure
# or if default directory exists, check that it is not mounted
# with cio.
#############################################################
if [ $CD_FS_DIR_FLAG -eq $ON ] || [ -d $CD_FS ]; then
    CD_FS_DIR_MOUNT_OPTION=`/usr/sbin/mount | \
        awk -v DIR=$CD_FS_DIR '$2 == DIR {print $(NF)}'`
    if [ `echo $CD_FS_DIR_MOUNT_OPTION | grep -c cio` -gt 0 ]; then
        mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 139 \
                "0512-335 $CD_FS_DIR cannot be in a file system mounted with the cio option.\n" $CD_FS_DIR`
            log_print "$mesg"
            rm -f $FS_FREE_SPACE 2>/dev/null
            exit 1
        fi
fi

#############################################################
## Load cmdbsys_lib functions.
#############################################################
if [ $NNFLAG -eq $ON ]
then
   . /usr/lpp/bosinst/cmdbsys_lib
fi

#############################################################
## Ensure that the -d flag has been set, unless the stop
## flag is set.  In which case the device is not needed.
#############################################################

if [ $STOP_FLAG -eq $OFF ] && [ $DEVICE_FLAG -eq $OFF ]; then
	mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 113 \
		"0512-318 $NAME: option -d is required if performing a CD or DVD write.\n" $NAME`
	log_print "$mesg"
	print_usage
	rm -f $FS_FREE_SPACE 2>/dev/null
	exit 1
fi

#############################################################
## Is DVD environment variable set to create DVD sized images
#############################################################
if [[ $MKCD_DVD_RAM = "yes" || $MKCD_DVD_RAM = "YES" ]]; then
	DVD_FLAG=$ON
fi

determine_conflicting_options  ## check conflicting and associated options

#############################################################
## If no data flag (-n) is set, and the savevg volume group
## specified is rootvg, then error out.
#############################################################
if [[ "$SAVEVG_VOLUME_GROUP" = "rootvg" ]] && \
   [[ "$NO_DATA_FLAG" -eq "$ON" ]]; then
	mesg=`/usr/bin/dspmsg -s 1 mksysb.cat 6 \
	"0512-009 $NAME: Invalid or missing Volume Group Name.\n" $NAME`
	log_print "$mesg"
	rm -f $FS_FREE_SPACE 2>/dev/null
	exit 1
fi

#############################################################
## If the system has a jfslog in the volume group, then set
## so mkcd creates jfs temp space, otherwise jfs2 temp space
#############################################################
if [ `lsvg -l $CDFS_VOLUME_GROUP | grep -c jfslog` -gt 0 ]; then
	FTYPE="jfs"
	BIGFILE="-a bf=$BIG_FILE_ENABLED"
fi

#############################################################
## If the user has specified a savevg image for use with the
## no data flag (-n), check it to verify that it is metadata.
#############################################################
if [[ "$SAVEVG_IMAGE_FLAG" -eq "$ON" ]] && \
   [[ "$NO_DATA_FLAG" -eq "$ON" ]]; then
	check_metadata_backup
fi

#######################################################################
## If making a backup without adding any additional packages, then
## estimate the amount of data in the volume group and then use that
## amount to determine sizes needed for CD filesystem and CD images.
## Result is minimum sized CD and CD images filesystems.
#######################################################################
if [ $JUST_FILE_FLAG -eq $OFF ] && [ $PKG_SOURCE_DIR_FLAG -eq $OFF ]; then
	get_backup_size
	if [ $STOP_FLAG -eq $ON ] || [ $LEAVE_IMAGE_FLAG -eq $ON ]; then
		(( CD_IMAGE_MIN_512 = CD_IMAGE_MIN_SIZE / 512 )) ## 645 MB in 512 byte blocks
		(( CD_IMAGE_CREATE_SIZE_512 = CD_IMAGE_MIN_512 * 100 / 96 + 1 )) ## 672 MB
	fi
else
#######################################################################
## check environment variable and flag to see if this is a dvd or cd
## and set size accordingly, either DVD sized or CD sized.
## Result is maximum sized CD and CD images filesystems.
#######################################################################
	get_backup_size
	if [ $STOP_FLAG -eq $ON ] || [ $LEAVE_IMAGE_FLAG -eq $ON ]; then
		(( CD_IMAGE_MIN_512 = CD_IMAGE_MIN_SIZE / 512 )) ## 645 MB in 512 byte blocks
		(( CD_IMAGE_CREATE_SIZE_512 = CD_IMAGE_MIN_512 * 100 / 96 + 1 )) ## 672 MB
	fi
	if [[ $DVD_FLAG = $ON ]]; then
		CD_FS_MIN_SIZE=$DVD_SIZE    ## 4.7 billion bytes (not 4.7 GB!)
	else
		CD_FS_MIN_SIZE=$CD_SIZE     ## 645 MB in bytes
	fi
fi

(( CD_FS_MIN_512 = CD_FS_MIN_SIZE / 512 )) ## 645 MB in 512 byte blocks
(( CD_FS_CREATE_SIZE_512 = CD_FS_MIN_512 * 100 / 96 + 1 )) ## 672 MB
(( CD_FS_MAX_512 = CD_FS_CREATE_SIZE_512 ))
#############################################################
## If the user specified the directories,
## check for required space for CD file system structure and
## the CD image.  Do not need to do this if UDF.
#############################################################
if [[ $UDF_FLAG -eq $OFF ]]; then
	if [ $CD_FS_DIR_FLAG -eq $ON ] || [ -d $CD_FS ] && \
		 [ $MK_ROCKRIDGE_FLAG -eq $OFF ]; then
		check_cd_fs $CD_FS_DIR $CD_FS_MIN_SIZE
		CD_FS_DIR=${CD_FS_DIR}/${PID}
	fi
	if [ $LEAVE_IMAGE_FLAG -eq $ON ] || [ $STOP_FLAG -eq $ON ] \
	|| [ $CD_IMAGE_DIR_FLAG -eq $ON ] || [ -d $CD_IMAGE_DIR ] \
        && [ $MK_ROCKRIDGE_FLAG -eq $OFF ]; then
	  if [[ $CD_IMAGE_DIR_FLAG -eq $ON || -d $CD_IMAGE_DIR ]] \
	  && [[ $LEAVE_IMAGE_FLAG -eq $ON || $STOP_FLAG -eq $ON ]]; then
		check_cd_fs $CD_IMAGE_DIR $CD_IMAGE_MIN_SIZE
	  elif [ $LEAVE_IMAGE_FLAG -eq $OFF ] && [ $STOP_FLAG -eq $OFF ]; then
		check_cd_fs $CD_IMAGE_DIR $CD_FS_MIN_SIZE
	  fi
	fi

	check_file_links     ## check OEM routine links


else
	udfcreate -d $DEVICE 2>/dev/null
	if [ $? -ne 0 ]; then ## udfcreate command return code
		/usr/bin/cat $MSGBUF | /usr/bin/tee -a $LOGFILE
		mesg=`/usr/bin/dspmsg -s 5  mksysb.cat 121 \
			"0512-399 $NAME: Unable to create UDF media.\n" $NAME`
		log_print "$mesg"
		cleanup 1
	fi
fi

check_memory_size     ## ensure the running system has the necessary memory

#############################################################
## If the user has already populated a directory/file system
## with the files, then just create the rockridge image
## and burn the CD (if applicable).  This will only work for
## non-bootable CD's.
#############################################################

if [[ $MK_ROCKRIDGE_FLAG -eq $ON ]]; then
	if [[ $UDF_FLAG -eq $ON ]]; then
		go_straight_to_udf
		cleanup 0
		exit 0
	else
		go_straight_to_rockridge ## Create the Rockridge Image
		write_to_cd     ## Write the CD image to the CD-R device, if specified
		cleanup 0
		exit 0
	fi
fi

ulimit -f unlimited

if [ $JUST_FILE_FLAG -eq $ON ]; then
#############################################################
## Building third-party CD, hack the minimum size
## requirement to 40MB
#############################################################

	CD_FS_MIN_SIZE=41943040
	(( CD_FS_MIN_512 = CD_FS_MIN_SIZE / 512 )) ## Convert size to 512-byte blocks
	(( CD_FS_CREATE_SIZE_512 = CD_FS_MIN_512 * 100 / 96 + 1 )) ## Add 4%

	create_just_file_cdfs  ## Create the file system with just the file listed

	if [ $CREATE_EFI_PARTITION -eq $ON ]; then

		create_efi_partition ## Create an EFI partition with specified files

	fi

	create_rockridge_fs     ## Create the Rockridge file system image

	if [ $CREATE_EFI_PARTITION -eq $ON ]; then

		create_boot_cd     ## Make the CD image bootable

	fi

	write_to_cd     ## Write the CD image to the CD-R device, if specified

else

	manage_backup	## handle previously created mksysb or savevg or
			## create new ones

	if [ "$PLATFORM" = "ia64" ]; then
		EFI_PARTITION_PROTO=/usr/lpp/bosinst/cdfs.efi.required.list
		CDFS_REQUIRED_LIST=/usr/lpp/bosinst/cdfs.ia64.required.list
	fi

	create_cd_fs     ## create and populate the CD file system structure

	if [ "$PLATFORM" = "ia64" ]; then
		create_efi_partition ## Create an EFI partition with specified files
	fi

	if [ $MKSYSB_IMAGE_FLAG -eq $OFF ]; then
		build_boot_images     ## create the boot images, if necessary
	fi
	if [[ $UDF_FLAG -eq $OFF ]];then

		create_rockridge_fs     ## Create the Rockridge file system image

		create_boot_cd     ## Make the CD image bootable, if specified

		write_to_cd     ## Write the CD image to the CD-R device, if specified

	fi
	manage_multivolume     ## Handle multi-volume CDs, if necessary
fi

cleanup 0    ## Clean up any command created files & file systems

exit 0

######################### end of main ##################################
