#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)merge_name_service.sh	1.1	04/11/19 SMI"
#
# This is the mergescript used to create the /var/svc/profile/name_service.xml
# on an installed system that is installed using Live Upgrade with a flash
# archive.
#

if [ $# != 1 ]; then
	exit 1
fi

BASENAME=/usr/bin/basename
CPIO=/usr/bin/cpio
FIND=/usr/bin/find
GREP=/usr/bin/grep
LN=/usr/bin/ln

INSTALLROOTDIR=$1
NAME_SERVICE_PROFILE_DIR=/var/svc/profile
NAME_SERVICE_PROFILE=/var/svc/profile/name_service.xml

# If /var/svc/profile/name_service.xml doesn't exist in the installed
# area, then exit success.  We don't want to create it if its not there.
# This simulates the REPLACE type for the transfer_list.
if [ ! -f ${INSTALLROOTDIR}/${NAME_SERVICE_PROFILE} ]; then
	exit 0
fi

# If /var/svc/profile/name_service.xml exists in the miniroot/PBE,
# then just copy it over to the installed area.
if [ -f ${NAME_SERVICE_PROFILE} ]; then
	rm -f ${INSTALLROOTDIR}/${NAME_SERVICE_PROFILE} >/dev/null 2>&1
	cd ${NAME_SERVICE_PROFILE_DIR}
	$FIND `$BASENAME ${NAME_SERVICE_PROFILE}` -print | $CPIO -pdumv \
		${INSTALLROOTDIR}/${NAME_SERVICE_PROFILE_DIR} \
		>/dev/null 2>&1
	exit $?
fi

# If we are here, this means that /var/svc/profile/name_service.xml does
# not exist in the miniroot/PBE and we still need to copy it over the
# installed area.  We have to figure out what name service is being used
# in the installed area and create the file based on the name service.
# NOTE: the following code is modelled after SUNWcsr's postinstall
# script which also figures out how to create name_service.xml by
# probing the installed system to determine what name service is used.
$GREP -v '^#' < /etc/nsswitch.conf | $GREP -w ldap >/dev/null 2>&1
is_ldap=$?
$GREP -v '^#' < /etc/nsswitch.conf | $GREP -w nisplus >/dev/null 2>&1
is_nisplus=$?
$GREP -v '^#' < /etc/nsswitch.conf | $GREP -w nis >/dev/null 2>&1
is_nis=$?
$GREP -v '^#' < /etc/nsswitch.conf | $GREP -w dns >/dev/null 2>&1
is_dns=$?

if [ $is_ldap = 0 ]; then
	ns_profile=ns_ldap.xml
elif [ $is_nisplus = 0	]; then
	ns_profile=ns_nisplus.xml
elif [ $is_nis = 0 ]; then
	ns_profile=ns_nis.xml
elif [ $is_dns = 0 ]; then
	ns_profile=ns_dns.xml
else
	ns_profile=ns_files.xml
fi

rm -f ${INSTALLROOTDIR}/${NAME_SERVICE_PROFILE} >/dev/null 2>&1
$LN -s $ns_profile ${INSTALLROOTDIR}/${NAME_SERVICE_PROFILE} >/dev/null 2>&1

# If dns wasn't symlinked above as the name service AND dns
# was found as well, then enable it by adding it to the profile/upgrade
# script.
if [ "$ns_profile" != "ns_dns.xml" -a $is_dns = 0 ]; then
	echo "/usr/sbin/svcadm enable network/dns/client" >> \
	${INSTALLROOTDIR}/var/svc/profile/upgrade
fi

exit 0
