#!/bin/sh
#
# $Id: genclntst.sh /aix64/1 2008/12/05 20:29:20 spattnay Exp $
#
# genclntst.sh
#
# Copyright (c) 2000, 2008, Oracle and/or its affiliates.All rights reserved. 
#
#    NAME
#      genclntst.sh - Generate the client static library 
#
#    DESCRIPTION
#      This script generate the library libclntst10.a: a single static
#      library clients can use to link OCI, Pro*C, and XA applications
#
#    NOTES
#      This script assumes the client shared library has already been
#      generated and the linker map file for it has been saved to the
#      file ${LIB_DIR}/clntsh.map.  The map file is scanned to determine
#      the list of objects to include in the static library.
#
#    MODIFIED   (MM/DD/YY)
#    spattnay    12/05/08 - Clean up 32bit reference
#    fjlee       09/19/08 - 
#    jboyce      02/13/07 - 10->11
#    jtao        02/15/04 - 
#    jtao        02/06/04 - 
#    jboyce      01/07/04 - workaround for linker bug (3344378)
#    jtao        02/27/03 - 
#    mkrohan     11/21/02 - Remove wtc
#    skalyana    08/06/02 - Add NZ symbols to libclntst
#    mkrohan     12/11/00 - Macro-ize 9
#    mkrohan     12/11/00 - Remove WRKDIR
#    mkrohan     08/02/00 - Less verbose output
#    mkrohan     07/18/00 - Static client library generation script
#    mkrohan     07/12/00 - Verifying
#    mkrohan     05/29/00 - Creation
#

#Below has been commented to clean up 32bit reference
#if [ "$1" = "-32" ]; then
#    LIB=lib32
#    LOOP="DONE"
#    OBJECT_MODE=32
#else
#    LIB=lib
#    # Set object mode in customer environment
#    [ -z "${SRCHOME}" ] && OBJECT_MODE=64
#fi

LIB=lib
# Set object mode in customer environment
[ -z "${SRCHOME}" ] && OBJECT_MODE=64
export OBJECT_MODE

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

#
# Define a default TMPDIR location
[ -z "$TMPDIR" ] && TMPDIR=/tmp

#
# Library names and locations
CLNT_NAM=clntst					# (short) library name
CLNT_VER=11					# library version number

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

#
# List of libraries explicitly merged into the static library
EXTRA_STATIC_LIBS=
if [ -r ${ORACLE_HOME}/${LIB}/libnnz${CLNT_VER}.a ]
then
EXTRA_STATIC_LIBS=" ${EXTRA_STATIC_LIBS} \
                    ${ORACLE_HOME}/${LIB}/libnnz${CLNT_VER}.a"
fi

#
# Define the name of the map and list file
MAPFILE=${LIB_DIR}/clntsh.map
LISFILE=${LIB_DIR}/${CLNT_NAM}.lis
LIBNAME=${LIB_DIR}/lib${CLNT_NAM}${CLNT_VER}.a

if [ ! -f ${MAPFILE} ]
then
	echo "genclntst: error: could not locate ${MAPFILE}" 1>&2
	echo "           please generate the client shared library first" 1>&2
	exit 1
fi

#
# Format the linker map file into a list of objs and library members
/bin/cat ${MAPFILE} | \
        sed -e "s/\[/\(/g" |sed -e "s/\]/\)/g" | \
	sort | \
	uniq > ${LISFILE}

#
# Create the static client library using the object list from the
# shared client library
WRKDIR=$TMPDIR/clntst${CLNT_VER}.$$
OBJDIR=$WRKDIR/objs
mkdir -p $OBJDIR
cd $OBJDIR
exec < ${LISFILE}
while read objname
do
	case "$objname" in
	*\(*\))
		library=`expr $objname : '\(.*\)(.*)'`
		object=`expr $objname : '.*(\(.*\))'`
		libname=`basename $library`
		echo $library > $WRKDIR/$libname.nm
		echo $object >> $WRKDIR/$libname.objs
		;;
	*)
		object=$objname
		cp $object `basename $object`
		;;
	esac
done
#
# bug in some version of solaris linker causes files with only .bss section
# not to be mentioned in .map file.  (When this is fixed, we should add .bss
# to the list of sections we care about.)
echo slxl.o   >> $WRKDIR/libnls$CLNT_VER.a.objs
echo ncrfg.o  >> $WRKDIR/libnro$CLNT_VER.a.objs

#
# Extract the objects for the libraries
for lib in $WRKDIR/*.nm
do
	library=`cat $lib`
	objects=`basename $lib .nm`.objs
	ar x $library `sort $WRKDIR/$objects | uniq`
done

#
# Extra the objects from the extra libraries
for lib in $EXTRA_STATIC_LIBS
do
	ar x $lib
done

#
# Create the library
rm -f ${LIBNAME}
# AIX has a arglist limit of 24K
echo *.o | xargs -n 100 ar r ${LIBNAME}

# archive in rdbms/lib/xaondy.o
ar r ${LIBNAME} ${ORACLE_HOME}/rdbms/${LIB}/xaondy.o

echo "Created ${LIBNAME}"

cd $TMPDIR
/bin/rm -rf $WRKDIR

#Below has been commented to clean up 32bit reference
# Create 32bit static library
#if [ -z "${SRCHOME}" -a -d ${ORACLE_HOME}/lib32 -a "$LOOP" != "DONE" ]
#then
#        ${ORACLE_HOME}/bin/genclntst -32
#fi

exit 0
