#!/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
#

# =============================================================================
# =============================================================================
# create_iso - Create an ISO image from a prepared package image area
# =============================================================================
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create an ISO image from a prepared package image area
# 
# Args:
#   MFEST_SOCKET: Socket needed to get manifest data via ManifestRead object
#
#   PKG_IMG_PATH: Package image area
#
#   TMP_DIR: Temporary directory to contain the boot archive file (not used)
#
#   BA_BUILD: Area where boot archive is put together
#
#   MEDIA_DIR: Area where the media is put
#
# Note: This assumes a completely prepared package image area and boot archive
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if [ "$#" != "5" ] ; then
	print -u2 -f "%s: Requires 5 args:\n" "$0"
	print -u2 "    Reader socket, pkg_image area, tmp dir,"
	print -u2 "    boot archive build area, media area."
	exit 1
fi

MFEST_SOCKET=$1
PKG_IMG_PATH=$2
if [ ! -d $PKG_IMG_PATH ] ; then
	print -u2 -f "%s: Unable to access pkg_image area %s\n" \
	    "$0" "$PKG_IMG_PATH"
        exit 1
fi

BA_BUILD=$4
if [ ! -d $BA_BUILD ] ; then
	print -u2 -f "%s: Unable to access bootroot directory %s\n" \
	    "$0" "$BA_BUILD"
        exit 1
fi

MEDIA_DIR=$5
if [ ! -d $MEDIA_DIR ] ; then
	print -u2 -f "%s: Unable to access media directory %s\n" \
	    "$0" "$MEDIA_DIR"
	exit 1
fi

builtin rm

# Define a few commands.
MKISOFS=/usr/bin/mkisofs

# Non core-OS commands.
MANIFEST_READ=/usr/bin/ManifestRead

DISTRO_NAME=$($MANIFEST_READ $MFEST_SOCKET "name")

# The maximum volumeid length is restricted to 32 characters
if [ "${#DISTRO_NAME}" -gt 32 ]; then
	print -u2 -f "%s: Volume ID string \"%s\" is too long\n" \
	    "$0" "$DISTRO_NAME"
	exit 1
fi
		
VOLSETID=$( < "$BA_BUILD/.volsetid" )

DIST_ISO=${MEDIA_DIR}/${DISTRO_NAME}.iso

print "Making final ISO image"

rm -f "$DIST_ISO"

PLATFORM=$(uname -m)

if [[ "${PLATFORM}" == "i86pc" ]] ; then
	$MKISOFS -o "$DIST_ISO" -b boot/grub/stage2_eltorito -c .catalog \
	    -no-emul-boot -boot-load-size 4 -boot-info-table -N -l -R -U \
	    -allow-multidot -no-iso-translate -cache-inodes -d -D -volset \
	    "$VOLSETID" -V "$DISTRO_NAME" "$PKG_IMG_PATH"
else
	# 
	# First create the hsfs bootblock
	# The first 16 sectors (or 8K) of the media contain
	# the bootblock. The hsfs bootblock starts at offset
	# 512.
	#
	/usr/bin/dd if="${BA_BUILD}/platform/${PLATFORM}/lib/fs/hsfs/bootblk" \
	    of="${PKG_IMG_PATH}/boot/hsfs.bootblock" \
	    bs=1b oseek=1 count=15 conv=sync 2> /dev/null
	if [ "$?" != "0" ] ; then
		print -u2 -f "%s: hsfs.bootblock creation failed\n" "$0"
		exit 1
	fi
	$MKISOFS -o "$DIST_ISO" -G "${PKG_IMG_PATH}/boot/hsfs.bootblock" \
	    -B ... -N -l -ldots -R -D -volset "$VOLSETID" -V \
	    "$DISTRO_NAME" "$PKG_IMG_PATH"
fi

if [ "$?" != "0" ] ; then
	print -u2 -f "%s: mkisofs of %s failed\n" "$0" "$DIST_ISO"
	exit 1	
fi

exit 0
