#!/bin/sh
#
# 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 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)postinstall	1.4	07/04/30 SMI"
#

# Contracted interface.
UPGRADE=${PKG_INSTALL_ROOT}/var/svc/profile/upgrade

#
# Delete obsolete property groups from the repository.
#
smf_obs_pg() {
	service=$1
	property_group=$2

	cat >> $UPGRADE <<-EOF

	if svcprop -q -p $property_group $service; then
		svccfg -s $service delpg $property_group
	fi
	EOF
}

# BIND 9.3.4 integration introduces a smf_method(5) start script,
# option properties and removes the configuration file dependency.
# Each instance has to be upgraded to use the script instead of
# executing named(1M) directly.
cat >> $UPGRADE <<-\_UPDATE_START_METHOD
	svc="svc:network/dns/server"
	if [ -z "$TMP" ]; then
	    TMP="/tmp"
	fi

	# fupdate is a dynamically created script which is used to
	# update the repository after the manifest is imported.
	fupdate="$TMP/BIND_UPDATE"
	rm -f $fupdate

	# fdefault is a svccfg(1m) input file that adds new properties
	# to each instance.
	fdefault=$TMP/BIND_OPTIONS
	echo "addpg options application" > $fdefault
	echo 'setprop options/chroot_dir = astring: ("")' >> $fdefault
	echo 'setprop options/configuration_file = astring: ("")' >> $fdefault
	echo 'setprop options/debug_level = integer: ("0")' >> $fdefault
	echo 'setprop options/ip_interfaces = astring: ("all")' >> $fdefault
	echo 'setprop options/listen_on_port = integer: ("0")' >> $fdefault
	echo 'setprop options/server = astring: ("")' >> $fdefault
	echo 'setprop options/threads = integer: ("0")' >> $fdefault

	oset=$@ # Remember current options if any.
	privileges='basic,!proc_session,!proc_info,!file_link_any,net_privaddr'
	privileges="$privileges,file_dac_read,file_dac_search,sys_resource,proc_chroot"

	# For each instance.
	for inst in `svcs -H -o INST $svc`
	do
	    # Config may default, be set from config_data, or from command line option.
	    unset config

	    # After import check if this instance needs updating with new properties.
	    echo "svcadm refresh $svc:$inst" >> $fupdate
	    echo "if svcprop -q -p options $svc:$inst; then" >> $fupdate
	    echo '    :' >> $fupdate
	    echo 'else' >> $fupdate
	    echo "    svccfg -s $svc:$inst -f $fdefault" >> $fupdate
	    echo "    svcadm refresh $svc:$inst" >> $fupdate
	    echo 'fi' >> $fupdate

	    # fopt is a dynamically created instance specific input
	    # file for svccfg(1M).
	    fopt=$TMP/BIND_UPDATE.$inst
	    rm -f $fopt

	    # Retrieve current exec string for this instance, removing
	    # quote characters.
	    cmd=`svcprop -p start/exec $svc:$inst | sed -e 's/\\\\//g'`
	    if [ $? -eq 0 -a -n "$cmd" ]; then
		set -- $cmd

		if [ "$1" = "/lib/svc/method/dns-server" ]; then
		    # No need to convert the converted.
		    continue
		elif [ "$1" != "/usr/sbin/named" ]; then
		    echo "setprop options/server=$1" >> $fopt
		fi

		# Update exec and privilileges.
		echo 'setprop start/exec="/lib/svc/method/dns-server %m %i"' >> $fopt
		echo "setprop start/privileges=\"$privileges\"" >> $fopt
		shift # dispose of command ready to check for options.

		# Retrieve current configuration file.
		if fmri=`svcprop -p config_data/entities $svc:$inst`; then
		    # Remove leading string,  file://localhost.
		    config=`echo $fmri | sed -e 's.^file://localhost..'`
		    # These should no longer be identical if sed worked.
		    if [ "$config" = "$fmri" ]; then
			# fmri is not of supported type, so ignore it.
			unset config
		    fi
		fi

		# Remove configuration file dependency.
		if svcprop -q -p config_data $svc:$inst; then
		    svccfg -s $svc:$inst delpg config_data
		fi

		# Convert command line options to supported properties.
		# Other named(1M) options are inappropriate for bind within SMF.
		while getopts :c:t:n:d:p:46 arg; do
		    case $arg in
		    c) config="$OPTARG";; # cmd line opt overrides.
		    t) echo "setprop options/chroot_dir=$OPTARG" >> $fopt;;
		    n) echo "setprop options/threads=$OPTARG" >> $fopt;;
		    d) echo "setprop options/debug_level=$OPTARG" >> $fopt;;
		    p) echo "setprop options/listen_on_port=$OPTARG" >> $fopt;;
		    4) echo "setprop options/ip_interfaces=IPv4" >> $fopt;;
		    6) echo "setprop options/ip_interfaces=IPv6" >> $fopt;;
		    esac
		done
		if [ -n "$config" -a "$config" != "/etc/named.conf"  ]; then
		    echo "setprop options/configuration_file=$config" >> $fopt
		fi
	    fi # start/exec prop

	    # Apply changed properties using svccfg(1M) command after import.
	    if [ -f $fopt ]; then
		echo "svccfg -s $svc:$inst -f $fopt" >> $fupdate
		# Must refresh for changes to take effect
		echo "svcadm refresh $svc:$inst" >> $fupdate
		echo "rm $fopt" >> $fupdate
	    fi
	done
	# Restore previous command line args.
	if [ -n "$oset" ]; then
	    set -- $oset
	fi
	echo "rm -f $fdefault" >> $fupdate
_UPDATE_START_METHOD

# Remove historical dependencies if they exist.
smf_obs_pg network/dns/server usr
smf_obs_pg network/dns/server physical
smf_obs_pg network/dns/server start

cat >> $UPGRADE <<-\EOF

	# Delete the stale single_instance restriction.
	if svcprop -q -p general/single_instance $svc; then
	    svccfg -s network/dns/server delprop general/single_instance
	fi

	# Delete the common_name.
	if svcprop -q -p tm_common_name/C $svc; then
	    svccfg -s network/dns/server delprop tm_common_name/C
	fi

	# Import service manifest into the repository.
	svccfg import /var/svc/manifest/network/dns/server.xml

	# Apply BIND 9.3.4 conversion if applicable.
	if [ -f $fupdate ]; then
	    . $fupdate
	    rm $fupdate
	fi

EOF

#
# Enable svc:/network/dns/server if requested.
#
BASEPREFIX=`echo ${PKG_INSTALL_ROOT} | sed "s/\//_/g"`
FILENAME=`echo sunwbindr_dns"$BASEPREFIX" | cut -c 1-256`
TMPFILE=/tmp/$FILENAME

if [ -f $TMPFILE ]; then
	echo "svcadm enable svc:network/dns/server:default" >> $UPGRADE
	rm $TMPFILE
fi

#
# Create rndc(1m) configuration if one does not exist.
#
if [ ! -f ${PKG_INSTALL_ROOT}/etc/rndc.key ] &&
	[ ! -f ${PKG_INSTALL_ROOT}/etc/rndc.conf ]; then
	echo "rndc-confgen -a"  >> $UPGRADE
fi
exit 0
