#!/bin/ksh
#
# ident "@(#)utrepair_services.ksh	1.2 02/11/19 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
#

#
# This script replaces utrepair for the /etc/inet/services file.
# The difference is that this script provides provides the capability
# of cleaning up duplicated SunRay entries in the file and updating
# the utds port.
#

ETC_SERVICES=/etc/inet/services
PROTO_SERVICES=/opt/SUNWut/lib/prototype/services.SUNWut.prototype
GREP_STRING="^ut[^	 ]*[	 ].*# SUNWut"

#
# First check to see if we need to repair the file.
# We are no longer keying on the start and end tags.
# We now check the number of entries currently defined in the
# services file against the prototype file.  If they are the same,
# we assume that all required entries are there.  Otherwise, we
# will have to repair the services file.
#
LINE_FROM_SERVICE=$(grep "$GREP_STRING" \
	$ETC_SERVICES | wc -l)
LINE_FROM_TEMPLATE=$(grep "$GREP_STRING" \
	$PROTO_SERVICES | wc -l)

if [[ $LINE_FROM_SERVICE == $LINE_FROM_TEMPLATE ]]; then
	# /etc/services file OK - nothing needs to be done
	exit 0
fi

PROGRAM_ID=$(basename $0)
TMPDIR="/var/opt/SUNWut/tmp"
TMP_FILE="${TMPDIR}/${PROGRAM_ID}.$$.tmp"

# cleanup services file file
grep -v "$GREP_STRING" $ETC_SERVICES | \
	sed -e "/^# Start SUNWut/,/^# End SUNWut/d" \
	> $TMP_FILE

print "# Start SUNWut" >> $TMP_FILE

#
# copies the SunRay entries from the services.SUNWut.prototype file.
# For now, we will handle the LDAP port manually based on the info from
# the utdsd.ini file.
#
# Future: the prototype file should use macros for the port numbers
# so that we can substitutue the port number based on the info from a central
# registry where all the port numbers are registered.
#
LDAP_PORT=$(nawk -F= '/^LdapPort/ {val =  $2} END {print val}' \
	/etc/opt/SUNWut/srds/current/utdsd.ini)
LDAP_PORT=${LDAP_PORT:-7012}
sed -e "s/^\(utdsd[ 	][ 	]*\)[0-9][0-9]*\(\/.*\)$/\1${LDAP_PORT}\2/" \
	$PROTO_SERVICES >> $TMP_FILE

print "# End SUNWut" >> $TMP_FILE

cp $ETC_SERVICES ${ETC_SERVICES}_$$
cp $TMP_FILE $ETC_SERVICES
/bin/rm -f $TMP_FILE ${ETC_SERVICES}_$$
exit 0
