#!/bin/sh
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)s8_postclone.sh	1.1	08/05/06 SMI"

PATH=/sbin:/usr/bin:/usr/sbin; export PATH

ZONENAME=$1
ZONEROOT=$2
err=0

# Don't re-sysunconfig if we've already done so
if [ ! -f $ZONEROOT/etc/.UNCONFIGURED ]; then

	chmod 700 $ZONEROOT

	#
	# Boot the zone.  The zone is still in the INCOMPLETE state, so we have
	# to -f(orce) boot it.
	#
	# We use the old 'boot -f -s' syntax to work around an arg parsing bug
	# in S10.
	#
	zoneadm -z $ZONENAME boot -f -s
	if [ $? -ne 0 ]; then
		echo `gettext "Could not boot zone for sys-unconfig"`
		exit 1
	fi

	#
	# The sys-unconfig can hang if the zone is still in the process of
	# booting when we try to run sys-unconfig.  Wait until the startup
	# rc scripts are done, which we do by checking for sulogin, or waiting
	# 20 seconds, whichever comes first.
	#
	for i in 0 1 2 3 4 5 6 7 8 9
	do
		pgrep -z $ZONENAME sulogin >/dev/null 2>&1 && break
		sleep 2
	done

	if [ $i -eq 9 ]; then
		echo `gettext "WARNING: zone did not finish booting."`
	fi

	#
	# Log into the zone and sys-unconfig it.
	#
	echo "yes" | zlogin -S $ZONENAME sys-unconfig >/dev/null 2>&1
	err=$?
	if [ $err -ne 0 ]; then
		echo `gettext "sys-unconfig failed"`
	fi
fi

if [ $err -eq 0 ]; then
	/usr/lib/brand/solaris8/s8_servicetag -a $ZONENAME
fi

exit $err
