#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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.
#

# 0.  Initialization.

[ $# != 2 ] && echo "Usage: mkrepo <rootdir> <pkg_img_path>" \
&& exit 1

ROOTDIR=$1
SVCCFG_DTD=${ROOTDIR}/usr/share/lib/xml/dtd/service_bundle.dtd.1
SVCCFG_REPOSITORY=${ROOTDIR}/etc/svc/repository.db

export SVCCFG_DTD SVCCFG_REPOSITORY

PKG_IMG_PATH=$2
SVCCFG_CMD=$PKG_IMG_PATH/usr/sbin/svccfg
if [ ! -x $PKG_IMG_PATH/usr/sbin/svccfg ] ; then
	echo "$PKG_IMG_PATH/usr/sbin/svccfg is not executable" >& 2
	exit 1
fi
LD_LIBRARY_PATH=$PKG_IMG_PATH/lib:$PKG_IMG_PATH/usr/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

[ -f /lib/svc/share/smf_include.sh ] || exit 1

svccfg_import () {
	$SVCCFG_CMD import $1 2>>/tmp/manifest_import.$$
	if [ $? -ne 0 ]; then
		echo > /dev/msglog
		echo "WARNING: svccfg import $1 failed" | tee /dev/msglog
	fi
}

import_manifests_pre_emi () {
	#
	# 2.  Manifest import.  Application directories first, then
	# site-specific manifests.
	#

	nonsite_dirs=`/usr/bin/find ${ROOTDIR}/var/svc/manifest/* -name site -prune \
	    -o -type d -print -prune`

	nonsite_manifests=`${ROOTDIR}/lib/svc/bin/mfstscan $nonsite_dirs`
	site_manifests=`${ROOTDIR}/lib/svc/bin/mfstscan ${ROOTDIR}/var/svc/manifest/site`

	manifests="$nonsite_manifests $site_manifests"

	[ -n "$_MFST_DEBUG" ] && {
		echo "Changed manifests to import:"
		for m in $manifests; do echo "  $m"; done
	}

	#
	# 2b.  Import the manifests while giving a running display of imports on
	# console, and a final count in the logfile.
	#
	if [ -n "$nonsite_manifests" -o -n "$site_manifests" ]; then
		rm -f /tmp/manifest_import.$$

		set -- $manifests
		backup=`echo "$#/$#" | sed 's/.//g'`
		fwidth=`echo "$#\c" | wc -c`

		echo "Loading smf(5) service descriptions: \c"

		i=1; n=$#
		while [ $# -gt 0 ]; do
			printf "%${fwidth}s/%${fwidth}s" $i $n
			svccfg_import $1
			i=`expr $i + 1`
			shift
			echo "$backup\c"
		done

		echo
		echo "Loaded $n smf(5) service descriptions"
		activity=true

		if [ -s /tmp/manifest_import.$$ ]; then
			echo "svccfg warnings:"
			cat /tmp/manifest_import.$$

			msg="svccfg import warnings.  See"
			msg="$msg ${ROOTDIR}/var/svc/log/system-manifest-import:default.log ."
			echo $msg
		fi
		rm -f /tmp/manifest_import.$$
	fi
}

import_manifests () {
	basedir=$1
	logf="/tmp/manifest_import.$$"

	rm -f $logf

	nonsite_dirs=`/usr/bin/find $basedir/svc/manifest/* -name site \
	    -prune -o -type d -print -prune`

	dirs="$nonsite_dirs $basedir/svc/manifest/site"
	$SVCCFG_CMD import -p /dev/msglog $dirs > $logf 2>&1

	if [ -s $logf ]; then
		grep "smf(5) service descriptions failed to load" $logf > /dev/null 2>&1
		failures=$?
		if [ $failures -eq 0 ]; then
			echo "svccfg warnings:"
		fi
		cat $logf

		if [ $failures -eq 0 ]; then
                        msg="svccfg import warnings.  See"
			msg="$msg ${ROOTDIR}/var/svc/log/system-manifest-import:default.log ."
			echo $msg
		fi
	fi
	rm -f $logf
}

if [ -f ${ROOTDIR}/lib/svc/manifest/system/early-manifest-import.xml ]; then
	import_manifests ${ROOTDIR}/lib
	import_manifests ${ROOTDIR}/var
else
	import_manifests_pre_emi
fi

exit 0
