#!/bin/sh
#
# Copyright (c) 2002-2004 by Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
#ident "@(#)postpatch 1.0 - 02/10/23"

debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

#########################################
# make_link
#
# Remove existing link at dest if necessary
# Create the link
# Set the owner id and group of the new link
# source: $1
# dest: $2
# owner id of the link: $3 and
# owner group of the link:$4
#########################################
make_link () {

	rc=0
	src=$1
	dest=$2
	USER=$3
	GROUP=$4

	# if the dest link already exists: remove it
	if [ -h $dest ]; then
		rm -f $dest
		if [ $? -ne 0 ];then
			rc=1
			echo "Removal of $dest link failed"
		fi
	fi

	# create the link
	ln -s $src $dest
	
	if [ $? -ne 0 ];then
		rc=1
		echo "Creation of $dest link failed"
	else
		if [ "x${USER}" != "x" ] || [ "x${GROUP}" != "x" ]; then
			# set the owner id and group of the link.
			chown -h $USER:$GROUP $dest
			if [ $? -ne 0 ];then
				rc=1
				echo "Update of $dest link owner failed"
			fi
		fi
	fi
	
	return $rc
}

#########################################
# create_security_links
#	For both 32 and 64 bits
#	For 32 bits, check wether AS patch
#	has been applied
#########################################
create_security_links() 
{
	rc=0
	opt=$1
	if [ $opt = "64" ]; then
		ARCH=sparcv9
	else
		ARCH=
	fi
	SERVER_ROOT_LIB=${SERVER_ROOT}/lib/${ARCH}

	get_basedir SUNWtls
	TLS_LIBDIR=${LOCAL_BASEDIR}/usr/lib/mps/secv1/${ARCH}

	get_basedir SUNWpr
	NSPR_LIBDIR=${LOCAL_BASEDIR}/usr/lib/mps/secv1/${ARCH}

	get_basedir SUNWjss
	JSS_LIBDIR=${LOCAL_BASEDIR}/usr/lib/mps/secv1/${ARCH}
	JSS_SHARED_DIR=${LOCAL_BASEDIR}/usr/share/lib/mps/secv1/${ARCH}

	if [ $opt = "32" ]; then
		if [ -h ${SERVER_ROOT}/shared/bin/sync-version ] ; then
			#
			# It is not necessary to update the security symbolic links. AS is responsible for this update
			#
			return 0 
		fi
	fi

	sslLibs="
		libssl3.so
		libnss3.so
		libsmime3.so
		"
	sslLibsSparc="
		libfreebl_hybrid_3.so
		libfreebl_pure32_3.so
		"

	nsprLibs="
		libnspr4.so
		libplc4.so
		libplds4.so
		"

	for fileName in ${sslLibs}; do
		make_link ${TLS_LIBDIR}/${fileName} ${SERVER_ROOT_LIB}/${fileName} $SYSTEMUSER $SYSTEMGROUP
		if [ $? -ne 0 ]; then
			rc=1
		fi
	done

	for fileName in ${nsprLibs}; do
		make_link ${NSPR_LIBDIR}/${fileName} ${SERVER_ROOT_LIB}/${fileName} $SYSTEMUSER $SYSTEMGROUP
		if [ $? -ne 0 ]; then
			rc=1
		fi
	done

	if [ $opt = "32" ]; then
		make_link ${TLS_LIBDIR}/libnssckbi.so ${SERVER_ROOT}/alias/libnssckbi.so $SYSTEMUSER $SYSTEMGROUP
		if [ $? -ne 0 ]; then
			rc=1
		fi
		# Update the JSS links
		make_link ${JSS_LIBDIR}/libjss3.so ${SERVER_ROOT}/lib/jss/libjss3.so $SYSTEMUSER $SYSTEMGROUP
		if [ $? -ne 0 ]; then
			rc=1
		fi

		make_link ${JSS_SHARED_DIR}/jss3.jar ${SERVER_ROOT}/java/jss3.jar $SYSTEMUSER $SYSTEMGROUP
		if [ $? -ne 0 ]; then
			rc=1
		fi
	fi

	if [ `uname -p` = "sparc" ] && [ $opt = "32" ] ; then
		for fileName in ${sslLibsSparc}; do
			make_link ${TLS_LIBDIR}/${fileName} ${SERVER_ROOT_LIB}/${fileName} $SYSTEMUSER $SYSTEMGROUP
			if [ $? -ne 0 ]; then
				rc=1
			fi
		done
	fi
	return $rc
}

#########################################
# create_help_links
#
#########################################

create_help_links() {

  USER=$1
  GROUP=$2
  mkdir $VAR_ADM/manual/help
  chown $USER:$GROUP $VAR_ADM/manual/help

  list_help_files=`(cd $USR_ADM/manual/help/; ls 2> /dev/null)`

  if [ x"$list_help_files" != x ] ; then
     for helpFile in ${list_help_files} ; do
        make_link $LOCAL_USR_ADM/manual/help/${helpFile} $VAR_ADM/manual/help/${helpFile} $USER $GROUP
     done
  fi

}

#########################################
# create_locale_product_link
#
#########################################

create_locale_product_link() {

    locale=$1
    product=$2
    USER=$3
    GROUP=$4
    ret=0

    if [ -d $USR_ADM/manual/${locale}/${product} ] ; then
	if [ ! -d $VAR_ADM/manual/${locale} ]; then
	   mkdir -p $VAR_ADM/manual/${locale}
	   chown $USER:$GROUP $VAR_ADM/manual/${locale}
	   if [ $? -ne 0 ]; then
	      ret=1
           fi  
        fi
        if [ $ret -eq 0 ] ; then
              make_link $LOCAL_USR_ADM/manual/${locale}/${product} $VAR_ADM/manual/${locale}/${product} $USER $GROUP
	      if [ $? -ne 0 ]; then
	        ret=1
              fi
        fi
    fi

 return $ret
}

#########################################
# create_links
#
#########################################
create_links() 
{
	rc=0
	ssusersConfFile=${SERVER_ROOT}/shared/config/ssusers.conf
	if [ -f $ssusersConfFile ]; then
		SYSTEMUSER=`cat $ssusersConfFile | awk '/^SuiteSpotUser/ { print $2; }' `
		SYSTEMGROUP=`cat $ssusersConfFile | awk '/^SuiteSpotGroup/ { print $2; }' `
	fi

	# updating links for NSS3.3.4 => NSS3.3.6 for 32  bits
	create_security_links 32
	if [ $? -ne 0 ]; then
		rc=1
	fi

	# Regarding 64 bits, it makes sense to check wether SUNWdsvx has been installed
	# before doing anything ...
	if [ `uname -p` = "sparc" ]; then
		get_pkgname SUNWdsvx
		if [ $? -eq 0 ]; then
			create_security_links 64
			if [ $? -ne 0 ]; then
				rc=1
			fi
		fi
	fi


	# Create a symbolic link to sync-directory in <SR>/shared/bin
	make_link ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin/sync-directory ${SERVER_ROOT}/shared/bin/sync-directory $SYSTEMUSER $SYSTEMGROUP
	if [ $? -ne 0 ]; then
		rc=1
	fi

	# Add links under <server root>/java/jars/ for the new jar files ds522.jar, ds522_en.jar ...
	get_basedir SUNWdsvcp
	DSVCP_BASEDIR=${PKGBASEDIR}
	DSVCP_LOCAL_BASEDIR=${LOCAL_BASEDIR}
	JAR_DIR=${DSVCP_BASEDIR}/usr/sadm/mps/admin/v5.2/java/jars
	JAR_LOCAL_DIR=${DSVCP_LOCAL_BASEDIR}/usr/sadm/mps/admin/v5.2/java/jars

	jar_list=`(cd ${JAR_DIR}; ls ./ds522*.jar 2> /dev/null)`
	if [ "x$jar_list" != "x" ]; then
		for fileName in ${jar_list}; do
			make_link ${JAR_LOCAL_DIR}/${fileName} ${SERVER_ROOT}/java/jars/${fileName} $SYSTEMUSER $SYSTEMGROUP
			if [ $? -ne 0 ];then
				rc=1
				echo "Creation of ${SERVER_ROOT}/java/jars/${fileName} link failed"
			fi	
		done
	fi

	# adds the links concerning to on-line help's

	ETC_ADM=$AS_BASEDIR/etc/mps/admin/v5.2
	VAR_ADM=$AS_BASEDIR/var/mps/serverroot
	if [ -f $ETC_ADM/shared/config/serverroot.conf ]; then
		VAR_ADM=$ROOTDIR/`cat $ETC_ADM/shared/config/serverroot.conf`
	fi

	LOCAL_USR_ADM=$AS_LOCAL_BASEDIR/usr/sadm/mps/admin/v5.2
	USR_ADM=$AS_BASEDIR/usr/sadm/mps/admin/v5.2
	locale_list=`(cd $USR_ADM/manual/ ; ls 2> /dev/null)`

	if [ -h $VAR_ADM/manual ]; then

	     # if manual is a link
 
	     rm -f $VAR_ADM/manual
	     mkdir -p $VAR_ADM/manual
	     chown $SYSTEMUSER:$SYSTEMGROUP $VAR_ADM/manual

             if [ x"$locale_list" != x ] ; then

		for localeName in ${locale_list}; do
		   if [ $localeName = "help" ] ; then
		     create_help_links $SYSTEMUSER $SYSTEMGROUP
		   else
		      product_list=`(cd $USR_ADM/manual/$localeName ; ls 2> /dev/null)`
		      if [ x"$product_list" != x ] ; then
		         for productName in ${product_list} ; do
			     create_locale_product_link ${localeName} ${productName} $SYSTEMUSER $SYSTEMGROUP
		             if [ $? -ne 0 ]; then
		               rc=1
		             fi
		         done
		      fi
		   fi
		done
             fi
	else 
          # manual not a link
          if [ x"$locale_list" != x ] ; then
             for localeName in ${locale_list}; do
		create_locale_product_link ${localeName} slapd $SYSTEMUSER $SYSTEMGROUP
                rc=$?
             done
          fi
	fi
}


get_pkgname() {
    debug "get_pkgname $1"
    PKG=$1
    pkginfo -R $ROOTDIR $PKG.\* 1>/dev/null 2>&1
    if [ $? -ne 0 ]; then
	debug "No package $1 installed"
	return 1
    else
	PKGNAME=`pkginfo -R ${ROOTDIR} $1.\* | awk '{print $2}'`
    fi
    debug "get_pkgname returns $PKGNAME"
    return 0
}

get_basedir() {
    debug "get_basedir $1"    
    get_pkgname $1
    if [ $? -ne 0 ]; then
	return 1
    else
	LOCAL_BASEDIR=`pkginfo -R ${ROOTDIR} -r $PKGNAME`
	if [ ${ROOTDIR} != '/' ] ; then
	    PKGBASEDIR=${ROOTDIR}/${LOCAL_BASEDIR}
	else
	    PKGBASEDIR=${LOCAL_BASEDIR}
	fi
    fi
    debug "get_basedir returns $PKGBASEDIR"
    return 0
}

get_basedir SUNWdsvu
ds52_basedir=$PKGBASEDIR
ds52_local_basedir=${LOCAL_BASEDIR}

CMD51_PATH=${LOCAL_BASEDIR}/usr/iplanet/ds5/sbin/directoryserver
CMD52_PATH=${LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver

REGISTRY_PATH=$ds52_basedir/etc/ds
REGISTRY=$REGISTRY_PATH/versions
LOCAL_REGISTRY_PATH=$ds52_local_basedir/etc/ds
LOCAL_REGISTRY=$LOCAL_REGISTRY_PATH/versions

add_in_registry() {
    debug "add_in_registry $1 $2 $3 $4" 
    VERSION=$1
    CMD_PATH=$2
    INSTALLED=$3
    DEFAULT=$4
    if [ ! -d $REGISTRY_PATH ] ; then
	mkdir -p $REGISTRY_PATH
	chown root:sys $REGISTRY_PATH
	chmod 755 $REGISTRY_PATH	
    fi
    if [ ! -f $REGISTRY ] ; then
	echo "#version|command path|installed?|default?" > $REGISTRY
	chown root:sys $REGISTRY
	chmod 644 $REGISTRY
    fi
    grep "^$VERSION|" $REGISTRY 1>/dev/null 2>&1
    if [ $? -eq 0 ] ; then
	grep -v "^$VERSION|" $REGISTRY > $REGISTRY.tmp
	mv -f $REGISTRY.tmp $REGISTRY
	add_in_registry $VERSION $CMD_PATH $INSTALLED $DEFAULT
	return 
    fi
    echo "$VERSION|$CMD_PATH|$INSTALLED|$DEFAULT" >> $REGISTRY
}

over52() {
    debug "over52"
    if [ -f $REGISTRY ] ; then
	grep -v "^5.2|" $REGISTRY | grep -v "^#" 1>/dev/null 2>&1
	if [ $? -eq 0 ] ; then
	    # Further versions installed
	    add_in_registry 5.2 $CMD52_PATH YES NO
	else
	    add_in_registry 5.2 $CMD52_PATH YES YES
	fi
    else
	add_in_registry 5.2 $CMD52_PATH YES YES
    fi
    get_pkgname SUNWdsvr
    if [ $? -ne 0 ] ; then
	exit 1
    fi
    debug "Adding $LOCAL_REGISTRY in $PKGNAME"
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
    installf -R $ROOTDIR -f $PKGNAME || exit 2
}

over51() {
    debug "over51"
    if [ ! -d $ds52_basedir/usr/iplanet/ds5/sbin ] ; then
	mkdir $ds52_basedir/usr/iplanet/ds5/sbin
    fi
    if [ -f $ds52_basedir/usr/sbin/directoryserver.51bak ] ; then 
	debug "Moving $ds52_basedir/usr/sbin/directoryserver.51bak in DS 5.1 tree"
	mv $ds52_basedir/usr/sbin/directoryserver.51bak $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver    
    fi
    if [ -f $ds52_basedir/usr/sbin/directoryserver ] ; then 
	debug "Adding $ds52_local_basedir/usr/sbin/directoryserver in $IPLTDSU_PKG"
	installf -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/sbin/directoryserver f 0755 root bin
	installf -R $ROOTDIR -f $IPLTDSU_PKG || exit 2	    
    fi
    if [ -f $ds52_basedir/usr/iplanet/ds5/sbin/directoryserver ] ; then 
	debug "Adding $ds52_local_basedir/usr/iplanet/ds5/sbin/directoryserver in $PKGNAME"
	installf -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/iplanet/ds5/sbin d 0755 root bin
	installf -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/iplanet/ds5/sbin/directoryserver f 0755 root bin
	installf -R $ROOTDIR -f $IPLTDSU_PKG || exit 2
    fi

    add_in_registry 5.1 $CMD51_PATH YES YES
    add_in_registry 5.2 $CMD52_PATH YES NO
    get_pkgname SUNWdsvr
    if [ $? -ne 0 ] ; then
	exit 2
    fi
    debug "Adding $LOCAL_REGISTRY in $PKGNAME"
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
    installf -R $ROOTDIR -f $PKGNAME || exit 2
    get_pkgname IPLTdsr
    if [ $? -ne 0 ] ; then
	exit 2
    fi
    debug "Adding $LOCAL_REGISTRY in $PKGNAME"
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
    installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
    installf -R $ROOTDIR -f $PKGNAME || exit 2
}

over51p(){
    debug "over51p"
    if [ -f $ds52_basedir/usr/sbin/directoryserver.51bak ] ; then
	# DS 5.2 has be installed after DS 5.1 patch 4
	# In this case, $ds52_basedir/usr/sbin/directoryserver.51bak 
	# is the wrapper installed by DS 5.1 patch 4 => no need to keep it
	# But the registry does not contains DS 5.2 information => add them
	# IPLTdsu no more contains /usr/sbin/directoryserver => add it
	debug "Removing $ds52_basedir/usr/sbin/directoryserver.51bak"
	rm -f $ds52_basedir/usr/sbin/directoryserver.51bak
	add_in_registry 5.2 $CMD52_PATH YES NO
	get_pkgname SUNWdsvr
	if [ $? -ne 0 ] ; then
	    exit 2
	fi
	debug "Adding $LOCAL_REGISTRY in $PKGNAME"
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY_PATH d 0755 root sys
	installf -R $ROOTDIR $PKGNAME $LOCAL_REGISTRY f 0644 root sys
	installf -R $ROOTDIR -f $PKGNAME || exit 2
	debug "Adding $ds52_local_basedir/usr/sbin/directoryserver in $IPLTDSU_PKG"
	installf -R $ROOTDIR $IPLTDSU_PKG $ds52_local_basedir/usr/sbin/directoryserver f 0755 root bin
	installf -R $ROOTDIR -f $IPLTDSU_PKG || exit 2
    fi
}

if [ `uname -i` = "i86pc" ] ; then
    DS51_PATCHID="114273"
    DS51_MIN_PATCHREV="04"
else
    DS51_PATCHID="113859"
    DS51_MIN_PATCHREV="04"
fi

is_51_patch(){
    debug "is_51_patch"
    current_patch=`pkgparam -R $ROOTDIR $IPLTDSU_PKG PATCHLIST`
    if [ "x$current_patch" != "x" ]; then
	for i in `echo $current_patch`
	  do
	  check_ret=`echo "$i" | \
                     awk '{ FS="-" ; \
                     if (($1 == '$DS51_PATCHID') && ($2 >= '$DS51_MIN_PATCHREV'))
                         print 0 ; \
                     else print 1;}'`
	  if [ $check_ret -eq 0 ]; then
	      break
	  fi
	done
	if [ $check_ret -eq 1 ]; then
	    debug "is_51_patch returns 1"
	    return 1
	fi
    else
	debug "is_51_patch returns 1"
	return 1
    fi    
    debug "is_51_patch returns 0"
    return 0
}

get_basedir IPLTdsu
if [ $? -eq 0 ]; then
    # Existing installed DS 5.1
    IPLTDSU_PKG=$PKGNAME
    ds51_basedir=$PKGBASEDIR
    if [ $ds51_basedir = $ds52_basedir ]; then
	# Existing installed DS 5.1 in the same basedir
	is_51_patch
	if [ $? -eq 0 ] ; then
	    # DS 5.1 has been patched (case 1.3.4.3)
	    over51p
	else
	    # DS 5.1 has not been patched (case 1.3.4.2)
	    over51
	fi
    else
	# No existing installed DS 5.1 in the same basedir (case 1.3.4.1)
	over52
    fi
else
    # No DS 5.1 (case 1.3.4.1)
    over52 
fi

#####################################
#
# Setup the variables for AS and DS 
#
#####################################

# For AS

get_basedir SUNWasvu
AS_BASEDIR=${PKGBASEDIR}
AS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

# For DS

get_basedir SUNWdsvu
DS_BASEDIR=${PKGBASEDIR}
DS_LOCAL_BASEDIR=${LOCAL_BASEDIR}

####################################
#
# If DS is not configured, then there
# is nothing to do. Just exit 0 
#
####################################

AS_is_configured=no
DS_is_configured=no
if [ -f ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf ] ; then
    SERVER_ROOT=${ROOTDIR}/`cat ${AS_BASEDIR}/etc/mps/admin/v5.2/shared/config/serverroot.conf`
    if [ -f ${SERVER_ROOT}/admin-serv/config/adm.conf ];then
	AS_is_configured=yes
    fi
    instance_list=`ls -d ${SERVER_ROOT}/slapd-* 2>/dev/null`
    if [ ! "x${instance_list}" = "x" ]
    then
	DS_is_configured=yes
    fi
fi

if [ "${DS_is_configured}" = "no" ]; then
    exit 0
fi

#####################################
#
# Create the symbolic links
#
#####################################

create_links

#####################################
#
# Restart the ldap instances
#
#####################################

PATHNUM=`env | grep PatchNum`
start_request="no"
cmd_torun_ds=
instance_list=`ls -d ${SERVER_ROOT}/slapd-* 2>/dev/null`
if [ ! "x${instance_list}" = "x" ]
then
    for i in `ls -d ${SERVER_ROOT}/slapd-*`
    do
	if [ ! -f ${i}/logs/pid ]
	then
	    servername=`/bin/basename $i | /bin/awk '{ print substr($0,7,length($0)-6) }'` 
            if [ "x${ROOTDIR}" = "x" ] || [ "x${ROOTDIR}" = "x/" ]
    	    then
		if [ -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM} ]; then
           		echo "Postpatch script is starting Directory Server instance ${i}..."
			eval ${DS_LOCAL_BASEDIR}/usr/ds/v5.2/sbin/directoryserver -s ${servername} start
			if [ $? -eq 1 ]; then
				echo "Directory Server instance could not be restarted"
			else
				echo "Directory Server instance successfully restarted"
			fi
			rm -f /var/tmp/.shouldRestart_DS_${servername}_${PATHNUM}
		fi
            else
		start_request="yes"
		cmd_torun_ds="${cmd_torun_ds} ${DS_LOCAL_BASEDIR}/usr/sbin/directoryserver -u 5.2 -s ${servername} start\n"
    	    fi
	fi
    done
fi

if [ ${start_request} = "yes" ]
then
	echo "You must restart the servers after applying this patch."
	echo "Log in to the system where this patch is "
	echo "installed and run the following commands:"
	echo "${cmd_torun_admin}"
	echo "${cmd_torun_ds}"
fi

if [ "${AS_is_configured}" = "yes" ]; then
    echo "CAUTION"
    echo "        Upgrade of remote configuration data is not complete."
    echo "        Log in to the system where this patch is "
    echo "        installed and run the following command:"
    echo "        ${DS_LOCAL_BASEDIR}/usr/sbin/directoryserver -u 5.2 sync-cds"
    echo ""
fi
