#!/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 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# =============================================================================
# =============================================================================
# create_usb - Create a USB image based on an ISO image.
# =============================================================================
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a USB image based on an ISO image.
# 
# Args:
#   MFEST_SOCKET: Socket needed to get manifest data via ManifestRead object
#
#   PKG_IMG_PATH: Package image area (not used)
#
#   TMP_DIR: Temporary directory to contain the boot archive file
#
#   BA_BUILD: Area where boot archive is put together (not used)
#
#   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 "$0: Requires 5 args: Reader socket, pkg_image area,"
	print -u2 "    tmp dir, boot archive build area, media area."
        exit 1
fi

MFEST_SOCKET=$1

TMP_DIR=$3
if [ ! -d ${TMP_DIR} ] ; then
        print -u2 "$0: $TMP_DIR is not valid"
        exit 1
fi

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

# Define a few commands.
MKDIR=/usr/bin/mkdir
RM=/usr/bin/rm

# Define non-core-OS commands.
MANIFEST_READ=/usr/bin/ManifestRead
USBGEN=/usr/bin/usbgen

DISTRO_NAME=`$MANIFEST_READ $MFEST_SOCKET "name"`
DIST_ISO=${MEDIA_DIR}/${DISTRO_NAME}.iso
if [ ! -f "$DIST_ISO" ] ; then
	print -u2 "$0: Input $DIST_ISO not found, can not generate USB image"
	exit 1
fi

DIST_USB=${MEDIA_DIR}/${DISTRO_NAME}.usb

TMP_MNT=${TMP_DIR}/usb_mnt
$MKDIR $TMP_MNT
if [ $? -ne 0 ] ; then
	print -u2 "$0: Unable to create temporary area $TMP_MNT"
	exit 1
fi

$RM -f "$DIST_USB"
$USBGEN "$DIST_ISO" "$DIST_USB" $TMP_MNT
if [ $? -ne 0 ] ; then
	print -u2 "FAILURE: Generating $DIST_USB failed"
	$RM -rf $TMP_MNT
	exit 1	
fi
$RM -rf $TMP_MNT
exit 0
