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

# =============================================================================
# =============================================================================
# post_boot_archive_pkg_image_mod
#
# Customizations to the package image area after boot archive construction completes
# =============================================================================
# =============================================================================

builtin cd
builtin cp
builtin cut
builtin grep
builtin mkdir
builtin mv
builtin rm

# Define a few commands.
ECHO=/usr/bin/echo
LOFIADM=/usr/sbin/lofiadm
MKISOFS=/usr/bin/mkisofs
TIME=/usr/bin/time

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

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Customizations to the package image area after boot archive construction
# completes.
# 
# 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
#
#   BA_BUILD: Area where boot archive is put together
#
#   MEDIA_DIR: Area where the media is put. (Not used)
# 
#   USER_ZLIB_KEY=USER_ZLIB_ALG: The compression algorithm used for the usr.zlib.
#		USER_ZLIB_KEY is required to be the string "usr_zlib_compression"
#		USER_ZLIB_ALG is an algorithm that's accepted by the lofiadm command.
#
# Note: This assumes a populated pkg_image area exists at the location
#		${PKG_IMG_PATH} and that the boot archive has been built.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if [ "$#" != "6" ] ; then
	print -u2 -f "Usage: %s: Requires 6 args:\n" "$0"
	print -u2 "    Reader socket, pkg_image area, tmp_dir,"
	print -u2 "    boot archive build area, media area, usr zlib compression algorithm"
	exit 1
fi

MFEST_SOCK=$1

PKG_IMG_PATH=$2
if [ ! -d $PKG_IMG_PATH ] ; then
	print -u2 -f "%s: Image package area %s is not valid\n" \
	    "$0" "$PKG_IMG_PATH"
	exit 1
fi

TMP_DIR=$3
if [ ! -d $TMP_DIR ] ; then
	print -u2 -f "%s: Temporary area %s is not valid\n" "$0" "$TMP_DIR"
	exit 1
fi

BA_BUILD=$4
if [ ! -d ${BA_BUILD} ] ; then
	print -u2 -f "%s: Boot archive build area %s is not valid\n" \
	    "$0" "$BA_BUILD"
	exit 1
fi

# Copying volsetid to live CD root		
cp ${BA_BUILD}/.volsetid ${PKG_IMG_PATH}

USER_ZLIB_KEY=$($ECHO $6 | cut -s -d'=' -f1)
if [ "XX${USER_ZLIB_KEY}" != "XXusr_zlib_compression" ] ; then
	print -u2 "Key to specify usr zlib compression algorithm is not valid."
	exit 1
fi

USER_ZLIB_ALG=$($ECHO $6 | cut -s -d'=' -f2)
#
# Just make sure the algorithm is not an empty string.  We will not
# check to make sure whether it is a valid algorithm or not here.
# This way, we can accomodate any algorithm in the future.
# If the algorithm is not valid, the error will be reported when
# it is actually being used by lofiadm
#
if [ "XX${USER_ZLIB_ALG}" = "XX" ] ; then
	print -u2 "Algorithm for usr zlib compression is not specified."
	exit 1
fi

COMPRESSION_TYPE=$($MANIFEST_READ $MFEST_SOCK \
    "img_params/live_img_compression/type")
if [ "XX${COMPRESSION_TYPE}" = "XX" ] ; then
	COMPRESSION_TYPE="gzip"
fi

# Note that DIST_ISO_SORT may or may not exist, given the type of image.
DIST_ISO_SORT=$($MANIFEST_READ -k $MFEST_SOCK "iso_sort")

# Remove password lock file left around from user actions during
# package installation; if left in place it becomes a symlink
# into /mnt/misc which will cause the installer's attempt to
# create a user account to fail
rm ${PKG_IMG_PATH}/etc/.pwd.lock

if [ ! -d $PKG_IMG_PATH ] ; then
	print -u2 -f "%s: Image package area %s is not valid\n" \
	    "$0" "$PKG_IMG_PATH"
	exit 1
fi
cd $PKG_IMG_PATH

# sbin, kernel and lib are all in the root archive
print "Removing sbin, kernel and lib from package image area"
rm -rf sbin kernel lib

print "Generating usr filesystem image"
if [ ! -d $PKG_IMG_PATH ] ; then
	print -u2 -f "%s: Image package area %s is not valid\n" \
	    "$0" "$PKG_IMG_PATH"
	exit 1
fi
cd $PKG_IMG_PATH

if [[ "X${DIST_ISO_SORT}" != "X" && -s "${DIST_ISO_SORT}" ]]; then
	SORT_OPTION="-sort $DIST_ISO_SORT"
	print "Sorting according to $DIST_ISO_SORT"
else
	SORT_OPTION=""
fi

$MKISOFS -o solaris.zlib $SORT_OPTION -quiet -N -l -R \
    -U -allow-multidot -no-iso-translate -cache-inodes \
    -d -D -V "compress" usr

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

print "Confirm lofiadm is available in image..."
if [ ! -f ${PKG_IMG_PATH}${LOFIADM} ] ; then
	print -u2 -f "%s: %s%s NOT FOUND\n" "$0" "${PKG_IMG_PATH}" "${LOFIADM}"
	exit 1
fi

LOFI_OUT_STR=${TMP_DIR}/lofi_out_str.$$

print "Compressing usr filesystem image using compression algorithm: ${USER_ZLIB_ALG}"
LD_LIBRARY_PATH=${PKG_IMG_PATH}/usr/lib $TIME ${PKG_IMG_PATH}/${LOFIADM} \
    -C ${USER_ZLIB_ALG} ${PKG_IMG_PATH}/solaris.zlib >/dev/null \
    2>$LOFI_OUT_STR
if [ $? -ne 0 ] ; then
	grep "invalid algorithm name" $LOFI_OUT_STR
	if [ $? -eq 0 ] ; then
		print -u2 -f "%s: %s is an invalid lofiadm algorithm\n." \
		    "$0" "${USER_ZLIB_ALG}"
		print -u2 "Please modify your USER_ZLIB_ALG parameter."
		rm $LOFI_OUT_STR
		exit 1
	fi
	rm $LOFI_OUT_STR
	print -u2 -f "%s: compression of usr filesystem failed\n" "$0"
	exit 1	
fi
rm $LOFI_OUT_STR

print "Generating misc filesystem image"
if [ ! -d $PKG_IMG_PATH ] ; then
	print -u2 -f "%s: Image package area %s is not valid\n" \
	    "$0" "$PKG_IMG_PATH"
	exit 1
fi
cd $PKG_IMG_PATH
mkdir miscdirs
mv opt miscdirs
mv etc miscdirs
mv var miscdirs
$MKISOFS -o solarismisc.zlib -N -l -R -U -allow-multidot \
    -no-iso-translate \
    -quiet -cache-inodes -d -D -V "compress" miscdirs
if [ "$?" != "0" ] ; then
	print -u2 -f "%s: mkisofs of solarismisc failed\n" "$0"
	exit 1	
fi
rm -rf miscdirs

print "Compressing misc filesystem image using compression algorithm: ${COMPRESSION_TYPE}"
LD_LIBRARY_PATH=${PKG_IMG_PATH}/usr/lib $TIME ${PKG_IMG_PATH}${LOFIADM} \
    -C $COMPRESSION_TYPE ${PKG_IMG_PATH}/solarismisc.zlib >/dev/null 2>&1
if [ "$?" != "0" ] ; then
	print -u2 -f "%s: compression of solarismisc failed\n" "$0"
	exit 1	
fi

#
# Delay rm of usr because lofiadm is used from usr to compress
# solaris, pkg, and solarismisc
#
rm -rf ${PKG_IMG_PATH}/usr

exit 0
