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

# =============================================================================
# =============================================================================
# ai_pre_boot_archive_pkg_image_mod
#
# pkg_image area modifications specific for automated installer to be made
# before the boot archive is built (possibly to prepare the pkg_image area for
# boot archive build)
# =============================================================================
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Perform pkg_image area modifications specific for automated installer before
# the boot archive is built.  This finalizer script is to be called before the
# boot archive build is started.
# 
# Args:
#   MFEST_SOCKET: Socket needed to get manifest data via ManifestRead object
#	(not used)
#
#   PKG_IMG_PATH: Package image area
#
#   TMP_DIR: Temporary directory (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.
#
# Note: This assumes pkg_image area is intact.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

builtin cat
builtin cd
builtin fgrep

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

typeset -r 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

typeset -r TMP_DIR="$3" 
if [[ ! -d $TMP_DIR ]] ; then
        print -u2 "\t${PROG}: Unable to access temporary directory $TMP_DIR"
	exit 1
fi

# Define used commands
CPIO=/usr/bin/cpio
FIND=/usr/bin/find
LN=/usr/bin/ln
MKDIR=/usr/bin/mkdir

# Create link for usr/has/bin/vi
$LN -s ../has/bin/vi  ${PKG_IMG_PATH}/usr/bin

# Create the .release file
cat ${PKG_IMG_PATH}/etc/release > ${PKG_IMG_PATH}/.release

# Create a directory to store auto_install specific files for
# use on the install server.
# NOTE: VMC uses this location (and the default.xml manifest contained
# therein) to be able to customize virtual machine installations.
AI_DIR=${PKG_IMG_PATH}/auto_install
$MKDIR -m 755 ${AI_DIR}

# Copy the /usr/share/auto_install directory
cd ${PKG_IMG_PATH}/usr/share/auto_install
$FIND . -depth -print | $CPIO -pdum ${AI_DIR}

# Store the auto-install package version information for use by
# finalizer checkpoint ai-publish-pkg.
typeset ai_pkg_fmri=$(/usr/bin/pkg -R ${PKG_IMG_PATH} info auto-install | fgrep FMRI:)
if [[ $? != 0 ]] ; then
        print -u2 "\t${PROG}: Error accessing the auto-install package information"
	exit 1
fi

typeset ai_pkg_ver=${ai_pkg_fmri#*auto-install@*,}
ai_pkg_ver=${ai_pkg_ver%:*}
print "${ai_pkg_ver}" >  ${TMP_DIR}/ai_pkg_version

exit 0
