#!/bin/sh
#
# $Id: genclntsh.sh 10-apr-2007.12:42:47 bemeng Exp $
# Copyright (c) 1996, 2007, Oracle. All rights reserved.  
#
# genclntsh.sh	(for Linux)
# Script to create single shared client library for OCI, Pro*C, and XA
#
#  MODIFIED   (MM/DD/YY)
#  bemeng      04/10/07 - add aio library for asynchronous I/O
#  jboyce      02/13/07 - 10->11
#  rpingte     03/01/07 - export everything from CORE and NLS
#  tnallath    08/31/06 - add gssapi library
#  tnallath    03/15/06 - Remove libnsslb 
#  tnallath    12/22/05 - new library libzt10 
#  jboyce      06/30/04 - Just remove soft-link to library.
#  jboyce      02/26/04 - Use shrept.lst from XDK
#  mkrohan     01/22/04 - Honour exit codes 
#  mkrohan     10/28/03 - Do not auto generate static client library 
#  ajgupta     07/24/03 - 
#  amckinst    08/25/03 - merge fwd RDBMS_MAIN_SOLARIS_030822
#  jboyce      06/27/03 - remove xsd dependency (3024453)
#  ekarichk    02/05/03 - arg list too long in qa environment
#  kecallan    12/02/02 - Include libcore10.a 
#  jboyce      11/20/02 - remove reference to $(SRCHOME)/lib/libcore9.a
#  mkrohan     11/14/02 - Use 10 libraries where available
#  mkrohan     11/21/02 - Remove wtc
#  tkarren     11/15/02 - Assign LNZ to libnnz9.a
#  kecallan    11/07/02 - Use /usr/bin/ar instead of /usr/ccs/bin/ar which doens't exist
#  jciminsk    10/09/02 - refine native net linking
#  ycao        09/12/02 - Add LSNLSRTL into the shared lib
#  skalyana    08/06/02 - Add NZ shared lib to libclntsh link line
#  vgoel       06/28/02 - 10i merges
#  madivi      05/10/02 - Removed -lrt option and added stubs to link line.
#  jciminsk    04/03/02 - add native net/skgxp
#  bkhaladk    11/18/01 - add XSD dependency.
#  sichandr    07/11/01 - add XDK
#  mkrohan     03/05/01 - Remove echo
#  mkrohan     03/05/01 - Drop libnent9
#  mkrohan     12/13/00 - Fix symlinking
#  mkrohan     12/07/00 - Final cleanup
#  mkrohan     11/27/00 - Add support static library map file
#  mkrohan     11/08/00 - Search for product 9 libraries
#  mkrohan     10/26/00 - Update to version 9
#  rachacos    11/25/98 - use naming adapter libraries
#  rachacos    07/30/98 - use GS LDAP package
#  hpiao       11/12/97 - Make libcommon$$.a writable
#  jweisz      06/30/97 - ADE support: if lib is symlink, copy it
#  mluong      05/30/97 - Read entry points from $PRODUCT/admin/shrept.lst
#  hpiao       04/04/97 - Modify for 803
#  dkozlows    05/01/95 - File created
#

#
# Utility functions

#
# Error
#
# Print an error message and exit
#

Error () {
	Status=$1
	Msg="$2"
	echo "genclntsh: $Msg" 1>&2
	exit $Status
}

#
# Check_Status
#
# Check an exit status, if non-zero, raise an error
#

Check_Status () {
	Status=$1
	Msg="$2"
	[ $Status -ne 0 ] && Error $Status "$Msg"
}

#
# Remove_File
#
# Remove a file verifying it has been removed
#

Remove_File () {
	File=$1
	/bin/rm -f $File
	[ ! -r $File ]
	Check_Status $? "Failed to remove $File"
}

#
# List the required symbols from a product's shrept.lst file
listf () {
	product=$1
	entryList=$ORACLE_HOME/$product/admin/shrept.lst
	grep -v '#' $entryList | \
		grep $product | \
		awk '{ printf (" -u %s", $3); }'
}

#
# Main line starts here
#

# Print archive table of contents
ARPRINT="/usr/bin/ar t"

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

#
# To avoid arg list too long errors, unset not essential environment variables
unset T_SOURCE T_SYSTEM T_LOG T_SQL T_DATA T_SRC T_COM T_DATA T_SOSD
unset MIGRATE_T_SOURCE MIGRATE_T_SQL CLASSES_12 CLASSPATH JDK_CLASSES
unset CLASSES_111 EJBCLASSES_11 ORDIMCLASSPATH ORDIMCLASSPATHSHIP CLASSES_102

#
# each product _must_ provide a $PRODUCT/admin/shrept.lst
ErrFiles=
for product in rdbms precomp plsql ldap network
do
	SHREPT=$ORACLE_HOME/$product/admin/shrept.lst
	if [ ! -f $SHREPT ]
	then
		ErrFiles="$ErrFiles $SHREPT"
	fi
done
[ ! -z "$ErrFiles" ] && Error 1 "genclntsh: Could not locate $ErrFiles"

#
# library definitions
OLIB=${ORACLE_HOME}/lib
LIBVER="11"

# Precomp
LSQL="-lsql$LIBVER"

# PL/SQL
LPLSQL="-lpls$LIBVER"

# RDBMS
LMM="-lmm"
LCLIENT="-lclient$LIBVER"
LCOMMON="-lcommon$LIBVER"
LGENERIC="-lgeneric$LIBVER"

# NLSRTL & CORE
LNLSRTL="-lnls$LIBVER"
LUNLSRTL="-lunls$LIBVER"
LSNLSRTL="-lsnls$LIBVER"
LCORE="-lcore$LIBVER"
LXDK="-lxml$LIBVER"

# LDAP
LLDAP="-lldapclnt$LIBVER"

# Native Net
LNNET_ON="-lnnet$LIBVER"
LNNET_OFF="-lnnetd$LIBVER"
LSKGXP="-lskgxp$LIBVER"

# Network
LDFLAGS="`cat ${ORACLE_HOME}/lib/ldflags`"
LNENT=
[ -r ${OLIB}/libnent$LIBVER.a ] && LNENT="-lnent$LIBVER"
# NZ
LNZ=
[ -r ${OLIB}/libnnz$LIBVER.so ] && LNZ="-lnnz$LIBVER -lzt$LIBVER"
LZTGSS="-lztkg$LIBVER"
LNCRYPT="-lncrypt$LIBVER -ln$LIBVER -lnl$LIBVER -lnro$LIBVER"
LNETWORK="${LDFLAGS} ${LNENT} ${LNZ} ${LNCRYPT} ${LZTGSS} ${LDFLAGS}"

# Asynchronous I/O library (port specific)
LAIO="-laio"

#
# Create lists of symbols to pass to ld
SYMS_NLS="-Wl,--whole-archive $LNLSRTL $LUNLSRTL $LSNLSRTL -Wl,--no-whole-archive"
SYMS_CORE="-Wl,--whole-archive $LCORE -Wl,--no-whole-archive"
SYMS_OCIC="`listf rdbms` ${LCLIENT}"
SYMS_SQL="`listf precomp` ${LSQL}"
SYMS_PLSQL="`listf plsql` ${LPLSQL}"
SYMS_LDAP="`listf ldap` ${LLDAP}"
SYMS_NETWORK="`listf network` ${LNETWORK}"
SYMS_XDK="`listf xdk` ${LXDK}"
#
# Library names and locations
CLNT_NAM=clntsh					# (short) library name
CLNT_VER=$LIBVER.1					# library version number
CLNT_LNK=lib${CLNT_NAM}.so			# name of symlink to library
CLNT_LIB=${CLNT_LNK}.${CLNT_VER}		# actual library file name
LIB_DIR=${ORACLE_HOME}/lib			# lib. destination directory

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

#
# If in ADE, the library may be a symlink to the label server.  If so,
# delete it
if [ -h ${LIB_DIR}/${CLNT_LIB} ]
then
    echo "removing soft link ${CLNT_LIB}"
    Remove_File ${LIB_DIR}/${CLNT_LIB}
fi

#
# Define the name of the map file
MAPFILE=${LIB_DIR}/${CLNT_NAM}.map

#
# Linker command and options

LD="gcc -shared -Wl,-relax -L${ORACLE_HOME}/lib" # shared library link command
LD_RUNTIME="-Wl,-R${ORACLE_HOME}/lib"
LD_OPT="-Wl,-Map ${MAPFILE} -Wl,-h${CLNT_LIB}"	 # name inserted into library
LD_OUT="-o ${LIB_DIR}/${CLNT_LIB}"		 # output specification
LD_SELF_CONTAINED=

#
# Oracle library dependencies
NAUTAB="${ORACLE_HOME}/lib/nautab.o"
NAETAB="${ORACLE_HOME}/lib/naeet.o ${ORACLE_HOME}/lib/naect.o"
NAEDHS="${ORACLE_HOME}/lib/naedhs.o" 
XAONDY="${ORACLE_HOME}/rdbms/lib/xaondy.o"

OLIBS="${LCLIENT}"
OLIBS="${OLIBS} ${LSQL}"
OLIBS="${OLIBS} ${LNETWORK}"
OLIBS="${OLIBS} ${LCLIENT}"
OLIBS="${OLIBS} ${LCOMMON}"
OLIBS="${OLIBS} ${LGENERIC}"
OLIBS="${OLIBS} ${LMM}"
OLIBS="${OLIBS} ${XAONDY}"
OLIBS="${OLIBS} ${LNETWORK}"
OLIBS="${OLIBS} ${LCLIENT}"
OLIBS="${OLIBS} ${LCOMMON}"
OLIBS="${OLIBS} ${LGENERIC}"
OLIBS="${OLIBS} ${LTRACE}"
OLIBS="${OLIBS} ${LXDK}"

# 
# Native net.  During a from-scratch build, when genclntsh.sh is first called,
# (before p2icd is linked) neither libnnet10.a nor libskgxp10.a
# exist.  Therefore pull in libnnetd10.a (native net disabled).
# Also, if called in a customer install (in which case libnnete10.a does not
# exist), pull in libnnetd10.a.
# Otherwise, pull in libnnet10.a.  This file is just a copy of either 
# libnnete10.a or libnnetd10.a, depending on whether native net is enabled
# or disabled.  If it's the disabled version, don't pull in libskgxp, otherwise
# do pull it in.
#
if [ ! -r ${OLIB}/libnnet$LIBVER.a ] || [ ! -r ${OLIB}/libnnete$LIBVER.a ]
then
    OLIBS="${OLIBS} ${LNNET_OFF}"
else
    OLIBS="${OLIBS} ${LNNET_ON}"
    if ${ARPRINT} ${OLIB}/libnnet$LIBVER.a|grep kpf_stubs > /dev/null 2>&1
    then
       :
    else
       [ -r ${OLIB}/libskgxp$LIBVER.so ] && OLIBS="${OLIBS} ${LSKGXP}"
    fi
fi

    

# LD does not seem to recognize -Wl,-relax, which CC needs
# SYSLIBS="-Wl,-relax -ldl -lnsl -lpthread -lrt -lm -lc"
# Removed -lrt option 
SYSLIBS="-Wl,-relax `cat ${ORACLE_HOME}/lib/sysliblist` -lc ${LAIO}"

# Introducing stubs for compatibility with higher version of glibc
STUBS=-L${ORACLE_HOME}/lib/stubs

# Create library
Remove_File ${LIB_DIR}/${CLNT_LIB}
Remove_File ${MAPFILE}
${LD} ${LD_RUNTIME} ${STUBS} ${LD_OPT} ${LD_OUT} ${LD_SELF_CONTAINED} \
	${NAUTAB} ${NAETAB} ${NAEDHS} ${SYMS_XDK} ${SYMS_SQL} \
	${SYMS_PLSQL} ${SYMS_OCIC} ${SYMS_LDAP} ${SYMS_NETWORK} \
	${SYMS_NLS} ${SYMS_CORE} ${OLIBS} ${SYSLIBS} ${USRLIBS} > ${MAPFILE}
Check_Status $? "Failed to link ${CLNT_LIB}"

# Create ".so" link to library in a non-development environment only
# and generate the client static library
if [ -z "${SRCHOME}" ]
then
	if [ -s ${LIB_DIR}/${CLNT_LIB} ]
	then
		cd ${LIB_DIR}
		Remove_File ${CLNT_LNK}
		ln -s ${CLNT_LIB} ${CLNT_LNK}
		Check_Status $? "Failed to create ${CLNT_LNK}"
	fi
fi

exit 0
