#!/usr/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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
#

# =============================================================================
# =============================================================================
# slimcd_boot_archive_configure - Additional boot archive configuration for slim_cd
# =============================================================================
# =============================================================================

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Perform special boot archive configuration for slim_cd images.
# 
# 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
#
#   BA_BUILD: Area where boot archive is put together.
#
#   MEDIA_DIR: Area where the media is put. (Not used)
#
# Note: This assumes a populated boot archive area exists at BA_BUILD.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

# Get commandline args.  First 5 are passed to all finalizer scripts.
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

BA_BUILD=$4
if [ ! -d $BA_BUILD ] ; then
	print -u2 "$0: Boot archive build area $BA_BUILD is not valid"
	exit 1
fi

# A few commands.
MKDIR=/usr/bin/mkdir
CP=/usr/bin/cp
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed

print "Making slim_cd specific boot archive configurations..."

# Note: the following replaces /etc/gdm/custom.conf and /etc/user_attr
# It's OK that symbolic links have been created to these files already, as
# the files can change without affecting the links to them.

# Enable auto-login in gdm, and save original to replace after installation
$MKDIR -p ${PKG_IMG_PATH}/save/etc/gdm
$CP -p ${BA_BUILD}/etc/gdm/custom.conf ${PKG_IMG_PATH}/save/etc/gdm
$NAWK '/^\[daemon\]/ { print $0;
    print "AutomaticLoginEnable=true";
    print "AutomaticLogin=jack";
    print "GdmXserverTimeout=30";
    continue }  { print $0 }' ${BA_BUILD}/etc/gdm/custom.conf \
    > ${TMP_DIR}/custom.conf
$CP ${TMP_DIR}/custom.conf ${BA_BUILD}/etc/gdm
$RM ${TMP_DIR}/custom.conf

# Set the default X cursor theme
if [ -f ${BA_BUILD}/etc/X11/Xdefaults ] ; then
	$CP ${BA_BUILD}/etc/X11/Xdefaults ${TMP_DIR}/Xdefaults
fi
echo "Xcursor.theme: DMZ-White" \
    >> ${TMP_DIR}/Xdefaults
$CP ${TMP_DIR}/Xdefaults ${BA_BUILD}/etc/X11
$RM ${TMP_DIR}/Xdefaults

# Enable suspend/resume
$CP -p ${BA_BUILD}/etc/power.conf ${TMP_DIR}
echo "S3-support\t\tenable" \
    >> ${TMP_DIR}/power.conf
$CP ${TMP_DIR}/power.conf ${BA_BUILD}/etc


# Give jack System Administrator profile, and convert root to a role
$SED -e's/^root::::/root::::type=role;/' ${BA_BUILD}/etc/user_attr \
    >${TMP_DIR}/user_attr
echo "jack::::profiles=System Administrator;roles=root" \
    >>${TMP_DIR}/user_attr
$CP ${TMP_DIR}/user_attr ${BA_BUILD}/etc
$RM ${TMP_DIR}/user_attr

# Add parted and gparted to the File System Management profile
echo "File System Management:suser:cmd:::/usr/sbin/parted:euid=0" \
    >> ${TMP_DIR}/exec_attr.slim_cd
echo "File System Management:suser:cmd:::/usr/sbin/gparted:uid=0" \
    >> ${TMP_DIR}/exec_attr.slim_cd
$CP ${TMP_DIR}/exec_attr.slim_cd ${BA_BUILD}/etc/security/exec_attr.d/slim_cd
$RM ${TMP_DIR}/exec_attr.slim_cd

# Give jack full sudo rights, saving sudoers for restoration during install
$CP ${BA_BUILD}/etc/sudoers ${PKG_IMG_PATH}/save/etc
echo "jack ALL=(ALL) NOPASSWD: ALL " >>${BA_BUILD}/etc/sudoers

# Remove /dev/zcons
rm -rf ${BA_BUILD}/dev/zcons

exit 0
