#!/bin/sh
#
# $Header: genagtsh.sh 23-aug-99.19:36:54 mluong Exp $
#
# genagtsh.sh
#
#  Copyright (c) Oracle Corporation 1997, 1998, 1999. All Rights Reserved.
#
#    NAME
#      genagtsh.sh - Generate an agent shared library
#
#    DESCRIPTION
#
#  Invocation:
#
#	genagtsh.sh  lib_name agent_version hs_driver
#
#	lib_name:	Filename of library to generate;
#			typically, $(LIBAGTSH) as used in the
#			RDBMS Makefile.  E.g., "libagtsh.so"
#
#	lib_version:	Version of the shared library
#
#			On UNIX ".agent_version" is appended to
#			the shared-library filename.  E.g., "1.0"
#
#
#    NOTES
#	This is a remote descendent of the script used to generate
#	the client shared library, libclntsh.so.
#
#    MODIFIED   (MM/DD/YY)
#    mluong      08/23/99 - write to a uniq temp file
#    inetwork    11/26/98 - Adding additional Network libraries
#    mluong      08/28/98 -
#    mluong      08/27/98 - change -lextp8 to -lpls8
#    mluong      06/22/98 -
#    mluong      06/09/98 -
#    mluong      06/05/98 - agent to agent8
#    mluong      06/04/98 - change core and nlsrtl name
#    mluong      06/02/98 - CDM 2 - plsql
#    mluong      05/29/98 - CDM 2 - network
#    pravelin    03/17/98 - Eliminate directory path from ld -h operand
#    pravelin    02/05/98 - If in ADE replace .so symlink with actual file
#    pravelin    02/05/98 - Fix typo causing ADE build problems
#    jdoskow     01/28/98 -
#    pravelin    10/30/97 - Script to build an agent shared library
#    pravelin    10/30/97 - Creation
#


#  Set shell variables from script parameters

LIB_NAME=$1			# Library name
LIB_VER=$2			# Library version number



#  Related variables

AGENT_LIB=${LIB_NAME}.${LIB_VER}		# actual library file name
AGENT_LIBNAME=`basename ${LIB_NAME}`            # base lib name without version
AGENT_SYMS="${ORACLE_HOME}/rdbms/admin/agtept.lst" # list of symbols to force



#  Function to list symbols required in shared library

listf_agent() {
	entryList=${AGENT_SYMS}
	grep -v '#' $entryList | grep rdbms | awk '{print $3}'
}


# Function used to convert lists of symbols to proper format for linker

fmt_syms() { awk '/[ \t]*#/	{next}\
				{printf " -u %s", $1}' ; }



#  Explicit path to ensure that we're using the correct commands
PATH=/usr/bin:/usr/ccs/bin	export PATH



if [ "$SRCHOME" != "" ]; then
	LIB_DIR=${SRCHOME}/rdbms/lib		# lib. destination directory
else
	LIB_DIR=${ORACLE_HOME}/lib		# lib. destination directory
fi




# Create lists of symbols to pass to ld

SYMS_AGENT="`listf_agent | fmt_syms`"
TEMP_FILE=/tmp/agent_syms.$$
echo $SYMS_AGENT > $TEMP_FILE


# If in ADE, the library may be a symlink to the label server.
# If so, pull it over...

AGENT_LIBFILE=`basename ${AGENT_LIB}`		# lib filename without path
AGENT_LIBPATH=${LIB_DIR}/${AGENT_LIBFILE}       # lib path for ADE link

if [ -h ${AGENT_LIBPATH} ]; then
        echo "copying ${AGENT_LIBFILE} to local disk"
	cp -p ${AGENT_LIBPATH} ${AGENT_LIBPATH}.tmp
	rm -f ${AGENT_LIPATH}
	mv ${AGENT_LIBPATH}.tmp ${AGENT_LIBPATH}
	chmod +w ${AGENT_LIBPATH}
fi


# Linker command and options

LD="ld -G -B dynamic -L${ORACLE_HOME}/lib"	# shared library link command
LD_OPT="-h ${AGENT_LIBNAME}"			# name inserted into library
LD_OUT="-o ${LIB_NAME}"				# output specification


# Create library

${LD} ${LD_OPT} ${LD_OUT} ${SYMS_AGENT} \
-lagent8 -lpls8 -ldl -ln8 -lntns8 -lnoname8 -lnhost8 -ln8 -lnro8 -lnls8 -lcore8 -lcommon8 -lcore8 -lnls8
/bin/rm -f $TEMP_FILE

exit 0
