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

# =============================================================================
# =============================================================================
# tm_pre_boot_archive_pkg_image_mod
#
# pkg_image area modifications specific for text 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 text install before the
# boot archive is built.  This finalizer script is to be called before the
# boot archive build is complete.
# 
# 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.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

# Define a few commands
CAT=/usr/bin/cat
CD=cd			# Built into the shell
CHROOT=/usr/sbin/chroot
CPIO=/usr/bin/cpio
RM=/usr/bin/rm
SH=/usr/bin/sh
MKDIR=/usr/bin/mkdir
CP=/usr/bin/cp
SED=/usr/bin/sed
TOUCH=/usr/bin/touch

# Path under which to save original files for restoration by ICT module
SAVE_PATH=${PKG_IMG_PATH}/save

# Modify /etc/system to make ZFS less mem hungry
$MKDIR -p ${SAVE_PATH}/etc
$CP -p ${PKG_IMG_PATH}/etc/system ${SAVE_PATH}/etc
$CAT <<EOF >>${PKG_IMG_PATH}/etc/system
set zfs:zfs_arc_max=0x4002000
set zfs:zfs_vdev_cache_size=0
EOF

exit 0
