#!/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.
#

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# gen_cd_content
#
# Generate the list of files that are part of the (LiveCD/Text Install) 
# media image. The result will be stored in the file .livecd-cdrom-content 
# in the root of the image.
#
# The content of the .livecd-cdrom-content file will be used by the
# Transfer Module to determine what files are part of the media image.
# The transfer module will ignore all other files on the media that
# are not in the .livecd-cdrom-content file
#
# This finalizer script must be placed immediately before the finalizer
# script to create the ISO file for the (LiveCD/Text Install) media, 
# otherwise, content of the cd might get modified further, and the 
# content list might not be accurate.
#
# The following files will NOT be included in the .livecd-cdrom-content file
#
# 1) *.zlib
# 2) .livecd-cdrom-content
# 3) .image_info
# 
# ==========================================================================
# Args:
#   MFEST_SOCKET: Socket needed to get manifest data via ManifestRead object
#	(not used)
#
#   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 (not used)
#
#   MEDIA_DIR: Area where the media is put (not used)
#
# Of these automatically-passed variables, only the PKG_IMG_PATH is actually
# used.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

PKG_IMG_PATH=$2
if [ ! -d ${PKG_IMG_PATH} ] ; then
	print -u2 "$0: Image package area $PKG_IMG_PATH is not valid"
	exit 1
fi

FIND=/usr/bin/find
CD=cd	#use the "cd" built-in to the shell

IMG_CONTENT_FILE=".livecd-cdrom-content"
IMG_INFO_FILE=".image_info"
BOOT_ARCHIVE_BASE="boot_archive"

#
# The package image area is the "root" of the live CD
#
${CD} ${PKG_IMG_PATH}
if [ "$?" != "0" ] ; then
	print -u2 "$0: unable to change into ${PKG_IMG_PATH}."
	exit 1
fi

${FIND} . ! \( -name '*.zlib' -o -name ${IMG_INFO_FILE} \
	-o -name ${IMG_CONTENT_FILE} -o -name ${BOOT_ARCHIVE_BASE} \) -print \
	> ${IMG_CONTENT_FILE}

if [ "$?" != "0" ] ; then
	print -u2 "$0:  there's an error generating the image content list."
	exit 1
fi

exit 0
