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

# =============================================================================
# =============================================================================
# slimcd_pre_boot_archive_pkg_image_mod
#
# pkg_image area modifications specific for slim cd, 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 slim cd 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
#
#   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

TMP_DIR=$3
if [ ! -d $TMP_DIR ]; then
	print -u2 "$0: Temporary area $TMP_DIR 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

# Remove gnome-power-manager, vp-sysmon and updatemanagernotifier
# from the liveCD and restore after installation
$MKDIR -p ${SAVE_PATH}/usr/share/dbus-1/services
$MKDIR -p ${SAVE_PATH}/usr/share/gnome/autostart
$MKDIR -p ${SAVE_PATH}/etc/xdg/autostart
$CP -p ${PKG_IMG_PATH}/etc/xdg/autostart/updatemanagernotifier.desktop \
    ${SAVE_PATH}/etc/xdg/autostart
$RM ${PKG_IMG_PATH}/etc/xdg/autostart/updatemanagernotifier.desktop
$CP -p ${PKG_IMG_PATH}/usr/share/dbus-1/services/gnome-power-manager.service \
    ${SAVE_PATH}/usr/share/dbus-1/services
$RM ${PKG_IMG_PATH}/usr/share/dbus-1/services/gnome-power-manager.service
$CP -p ${PKG_IMG_PATH}/usr/share/gnome/autostart/gnome-power-manager.desktop \
    ${SAVE_PATH}/usr/share/gnome/autostart
$RM ${PKG_IMG_PATH}/usr/share/gnome/autostart/gnome-power-manager.desktop
$CP -p ${PKG_IMG_PATH}/usr/share/gnome/autostart/vp-sysmon.desktop \
    ${SAVE_PATH}/usr/share/gnome/autostart
$RM ${PKG_IMG_PATH}/usr/share/gnome/autostart/vp-sysmon.desktop

# Reconfigure panel to:
# - remove Gnome netstatus-applet
# - use theme background rather than image
# on live CD and restore after installation
$MKDIR -p ${SAVE_PATH}/etc/gconf/schemas
$CP -p ${PKG_IMG_PATH}/etc/gconf/schemas/panel-default-setup.entries \
    ${SAVE_PATH}/etc/gconf/schemas
$SED -e '/.*<string>netstatus_applet<\/string>/{$!N;N;d;}' \
    -e '/.*<string>image<\/string>/s:image:gtk:' \
    ${PKG_IMG_PATH}/etc/gconf/schemas/panel-default-setup.entries \
    > ${TMP_DIR}/panel-default-setup.entries
$CP ${TMP_DIR}/panel-default-setup.entries \
    ${PKG_IMG_PATH}/etc/gconf/schemas/panel-default-setup.entries
$RM ${TMP_DIR}/panel-default-setup.entries

# 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

# Pre-configure Gnome databases
#

print "Configuring Gnome in package image area"

#
#Create a temporary /dev/null since many GNOME scripts
#redirect their output to /dev/null.  The /dev/null here will
#not have the same link structure as the one in a regular system,
#it is just a character type special file. 
#

echo "dev/null" | ($CD / ; $CPIO -Lp $PKG_IMG_PATH)

#
# The GNOME scripts decide whether to regenerate its caches
# by looking at the timestamps of certain files delivered by GNOME packages.
# When IPS installs packages, there's no guarantee that
# they are installed in a certain order.  So, looking
# at the timestamp is not a valid method.  Since we
# want to make sure that all the caches are regenerated,
# all 4 caches that have to be regenerated will be removed.
# Note that this is just a workaround until we figure out how
# to solve the problem of forcing the cache to regenerate.
#
# With this workaround, also make sure that the icon-cache SMF service
# comes after the pixbuf-loaders-installer SMF service, because the
# icon-cache service depends on the existence of the gdk-pixbuf.loaders cache.
#
$RM -f ${PKG_IMG_PATH}/usr/share/applications/mimeinfo.cache \
	${PKG_IMG_PATH}/etc/gtk-2.0/gtk.immodules \
	${PKG_IMG_PATH}/etc/amd64/gtk-2.0/gtk.immodules \
	${PKG_IMG_PATH}/etc/gtk-2.0/gdk-pixbuf.loaders \
	${PKG_IMG_PATH}/etc/amd64/gtk-2.0/gdk-pixbuf.loaders \
	${PKG_IMG_PATH}/usr/share/mime/mimeinfo.cache

$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/desktop-mime-cache start
$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/gconf-cache start
$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/input-method-cache start
$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/mime-types-cache start
$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/pixbuf-loaders-installer start
$CHROOT $PKG_IMG_PATH $SH /lib/svc/method/icon-cache start

# We disabled gnome-netstatus-applet for the liveCD but we want it
# to be active when the default user logs in after installation.
# By giving the saved copy of panel-default-setup.entries a later
# timestamp than the global gconf cache we'll end up enabling the
# applet on first reboot when the desktop-cache/gconf-cache service
# starts.
$TOUCH ${SAVE_PATH}/etc/gconf/schemas/panel-default-setup.entries

#Remove the temp /dev/null
$RM ${PKG_IMG_PATH}/dev/null

print "Creating font cache"
if [ -x ${PKG_IMG_PATH}/usr/bin/fc-cache ] ; then
	$CHROOT $PKG_IMG_PATH /usr/bin/fc-cache --force
fi

exit 0
