:
#!/bin/sh
#
#    NAME
#	relink
#
#    DESCRIPTION 
#	performs manual relinking of Oracle product executables based
#	on what has been installed in the ORACLE_HOME.  
# 	script calls appropriate makefile targets for the following 
# 	accepted script parameters: 
#		all -- everything which has been installed
#		oracle -- oracle database executable only
#		network -- net_client, net_server, cman, cnames
#		client -- net_client, otrace, plsql
#		client_sharedlib
#		interMedia -- ctx
#		precomp -- all precompilers which have been installed
#		utilities -- utilities
#		oemagent -- oemagent, odg
#
#    PRECONDITIONS
#	if ORACLE_HOME is not set, doesn't exist, or points to an
#	invalid location, script exits.
#
#	if makefiles do not exist for the entered parameter, script
#	displays appropriate message and exits execution.
#
#   MODIFIED   (MM/DD/YY)
#      dschrein 11/02/99 - emending utilities and adding hs_odbc for 8.1.6
#      dschrein 02/11/99 - remove nau stuff, add profor
#      dschrein	01/20/99 - fix implementation of "all", add 
#				parameter "oemagent"
#      dschrein 01/14/99 - Creation

##-------------------------------------#
## INITIATION AND PARAMETER VALIDATION #
##-------------------------------------#

#-----------------------------
# platform specific variables
RM="/bin/rm"
FIND="/bin/find"
AWK="/bin/awk"
CAT="/bin/cat"
TEMPDIR="/tmp/"
SCRIPTHOME=`pwd`
MAKE="/usr/ccs/bin/make"   # full path to make
PRECOMPS="proc ott procob procob18 rtsora modada profor"

if [ x${ORACLE_HOME} = x ] -o [ ${ORACLE_HOME} = "" ] ; then
	echo "ORACLE_HOME is either unset or empty."
	echo "Exiting relink script..."
	exit 1
fi

#-----------------------------------------------------
# makefile lists per parameter -- N.B. ins_<value>.mk

oracleFILES="rdbms"
networkFILES="net_client net_server cman cnames"
clientFILES="net_client otrace plsql"
client_sharedlibFILES="net_client"
#interMediaFILES="ctx"
#precompFILES="precomp"
utilitiesFILES="rdbms sqlplus"
#oemagentFILES="oemagent odg"
allFILES="$clientFILES $oracleFILES $interMediaFILES $networkFILES $utilitiesFILES $oemagentFILES hsodbc"
allFILES=`echo $allFILES | $AWK '{for (i = 1; i <= NF; ++i) print $i}' | sort -u | $AWK '{ORS = " "; print $0}'`

#--------------
# help message

SCRIPTNAME=relink
ARGUMENTS="<parameter>"
USAGE="usage: $SCRIPTNAME $ARGUMENTS"
HELPMESG="accepted values for parameter: all, oracle, network, client, client_sharedlib
\n\tinterMedia, precomp, utilities, oemagent"
if [ $# -eq 0 ] ; then
	echo
	echo $USAGE
	echo $HELPMESG
	echo $ARGS
	exit 2
fi

#---------------------------
# check for valid parameter

if [ $1 != "all" -a $1 != "oracle" -a $1 != "client" -a $1 != "client_sharedlib" -a $1 != "network" -a $1 != "interMedia" -a $1 != "precomp" -a $1 != "utilities" -a $1 != "oemagent" ] ; then
	echo no valid parameter
	echo $HELPMESG
	exit 1
fi

##-----------#
## FUNCTIONS #
##-----------#

find_files () {
# locate installed products
# parameters: $1 = original script parameter, $2 = name of file with list of make files

	libs="`$FIND . -name lib`"
	case $1 in
	 oracle)	FILES=$oracleFILES ;;
	network)	FILES=$networkFILES ;;
	 client)	FILES=$clientFILES ;;
client_sharedlib)	FILES=$client_sharedlibFILES ;;
     interMedia)	FILES=$interMediaFILES ;;
	precomp)	FILES=$precompFILES ;;
      utilities)	FILES=$utilitiesFILES ;;
       oemagent)	FILES=$oemagentFILES ;;
	    all)	FILES=$allFILES ;;
	esac

	for f in $libs; do
  		for g in $FILES; do
			$FIND $f -name "ins_${g}.mk" >> ${TEMPDIR}tempmakes
		done	
	done

# write filenames with full path to temporary file $2

	for l in `cut -c2- ${TEMPDIR}tempmakes` ; do
		echo ${ORACLE_HOME}$l >> ${TEMPDIR}$2
	done
	$RM ${TEMPDIR}tempmakes
#	more $2
}

##----------#
## CLEAN UP #
##----------#

cleanup () {
# remove files in parameter list if any
	if [ $# -gt 0 ] ; then
		for p in $@ ; do
			$RM ${TEMPDIR}$p
		done
	fi
	if [ -r ${TEMPDIR}mfiles ] ; then
		$RM ${TEMPDIR}mfiles
	fi
	cd $SCRIPTHOME
}

#--------------------------------
# functions per script parameter

call_make () {
# function parameters:
	if [ $# -eq 1 ] ; then
		target=$1
		fil="mfiles"
	else
		target=$1
		fil=$2
	fi

	if [ ! -r ${TEMPDIR}$fil ] ; then
		echo Makefile not found.
		return
	fi 
	for f in `$CAT ${TEMPDIR}$fil`; do
		cd `echo $f | $AWK -F/ '{s = ""; for (i = 1; i < NF; ++i) s = s $i "/"; print s}'`
		$MAKE -f $f $target
	done
}

call_precomp_make () {
	# find installed precompilers
	for f in $PRECOMPS; do
		$FIND bin -name $f >> ${TEMPDIR}pretemp
	done	

	$AWK -F/ '{print $NF}' ${TEMPDIR}pretemp > ${TEMPDIR}pretargets

	for f in `$CAT ${TEMPDIR}$1`; do
		for target in `cat ${TEMPDIR}pretargets`; do
			cd `echo $f | $AWK -F/ '{s = ""; for (i = 1; i < NF; ++i) s = s $i "/"; print s}'`
			$MAKE -f $f relink EXENAME=$target
		done
	done
}

all () {
	find_files "client_sharedlib" "cslfiles" 
	call_make "client_sharedlib" "cslfiles" 

#	find_files "precomp" "precompfiles"
#	call_precomp_make "precompfiles"	

	call_make "install"
#	cleanup "cslfiles" "precompfiles" "pretargets" "pretemp"
	cleanup "cslfiles" 
}

oracle () {
	call_make "ioracle"
	cleanup
}

network () {
	call_make "install"
	cleanup
}

client () {
	call_make "install"
	cleanup
}

client_sharedlib () {
	call_make "client_sharedlib"
	cleanup
}

interMedia () {
	call_make "install"
	cleanup
}

precomp () {
	call_precomp_make "mfiles"
	cleanup "pretargets" "pretemp"

}

utilities () {
	call_make "utilities"
	cleanup
}

oemagent () {
	call_make "install"
	cleanup
}

##-----------------------#
## MAIN SCRIPT EXECUTION #
##-----------------------#


cd $ORACLE_HOME
find_files $1 "mfiles"	# find the make files for this parameter
$1			# call the appropriate function (name same as parameter)





