#!/usr/bin/ksh
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
#Use is subject to license terms.
#
#

iroot=${PKG_INSTALL_ROOT:-$BASEDIR}
SMSOPT=${iroot}/opt/SUNWSMS

#############################################################################
#
# create_sms_startup_links()
#
# Create links in the appropriate run control directories to the sms startup
# script. The sms startup script starts and stops the ssd which in turn
# starts and stops all the SMS daemons and servers. These links are only
# created if they are not already present i.e. a version is already installed
#
#############################################################################

INITD_DIR=${iroot}/etc/init.d
RC0_DIR=${iroot}/etc/rc0.d
RC1_DIR=${iroot}/etc/rc1.d
RC2_DIR=${iroot}/etc/rc2.d

create_sms_startup_links()
{
	# Set the initial return code
	return_code=0

	# create the appropriate run control links
	if [ ! -f ${INITD_DIR}/sms ]
	then
		cp -p ${iroot}/etc/opt/SUNWSMS/SMS/startup/sms ${INITD_DIR}/sms
		if [ $? -ne 0 ]; then
			echo "ERROR: \"SMS\" run control script link creation failed."
			return_code=1
			return $return_code
		fi
	fi
	if [ ! -f ${RC2_DIR}/S99sms ]
	then
		ln -f ${INITD_DIR}/sms ${RC2_DIR}/S99sms >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "ERROR: \"SMS\" run control script link creation failed."
			return_code=1
		fi
	fi
	if [ ! -f ${RC1_DIR}/K20sms ]
	then
		ln -f ${INITD_DIR}/sms ${RC1_DIR}/K20sms >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "ERROR: \"SMS\" run control script link creation failed."
			return_code=1
		fi
	fi
	if [ ! -f ${RC0_DIR}/K20sms ]
	then
		ln -f ${INITD_DIR}/sms ${RC0_DIR}/K20sms >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "ERROR: \"SMS\" run control script link creation failed."
			return_code=1
		fi
	fi
	return $return_code
}


############################################################
# MAIN - execution starts here!
############################################################

if [[ -d ${SMSOPT}/SMS ]]; then
	# trim target pathname to primary component e.g. "SMS1.0"
	smsopt_version=`ls -l ${SMSOPT}/SMS |cut -f2 -d'>'`
	smsopt_version=${smsopt_version##*/}
	if [[ $smsopt_version = "SMS1.2" ]]; then
		rm -f ${INITD_DIR}/sms
		rm -f ${RC2_DIR}/S99sms
		rm -f ${RC1_DIR}/K20sms
		rm -f ${RC0_DIR}/K20sms		
		create_sms_startup_links
		if [ $? -ne 0 ]; then
			exit $?
		fi
	fi
fi
exit 0
