#!/bin/sh
#
# $Id: relink 04-jan-2005.15:28:26 mdenney Exp $
#
# Copyright (c) 1999, 2005, Oracle. All rights reserved.  
#
#    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, names
#        client -- net_client, plsql
#        client_sharedlib
#        interMedia -- ctx
#        ctx --Oracle Text Utilities 
#        precomp -- all precompilers which have been installed
#        utilities -- utilities and sqlplus
#        oemagent -- oemagent
#        ldap -- ldap/oid executables
#
#    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)
#    mdenney     01/04/05 - fix linux syntax errors - see bug 4102441 
#    anataraj    06/10/04 - bug fix 3643280. adding ldap to all target 
#    mwidjaja    01/08/04 - bug 3357749: ldap relink target update for 10g
#    anusharm    11/24/03 - bug 2899601 
#    mdenney     03/14/02 - fix ctx renaming and backward compatibility bug # 22
#    dschrein    05/29/01 - rm obsolete stuff
#    dschrein    03/23/01 - fix logical or
#    dschrein    03/07/01 - search for executables rather than hardcode them
#    dschrein    01/04/01 - fixes for bugs 1397793, 1429271, 1429382 and simplif
#    dschrein    07/07/00 - fix typo: cnames -> names
#    dschrein    06/27/00 - fix for bug 1337908 - include utilities in all
#    dschrein    06/27/00 - fix for bug 1202311
#    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 #
#-------------------------------------#

#-----------------------------
# make sure ORACLE_HOME is set

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

if [ ! -d $ORACLE_HOME ];
then
    echo "ORACLE_HOME is not a directory"
    exit 1
fi

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

SCRIPTNAME=relink
ARGUMENTS="<parameter>"
USAGE="usage:\n\t$SCRIPTNAME $ARGUMENTS"
HELPMESG="\nparameters: all, oracle, network, client, client_sharedlib,\
          interMedia,\n\tctx, precomp, utilities, oemagent, ldap"

if [ $# -ne 1 ];
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 != "ctx" -a $1 != "precomp" -a $1 != "utilities" -a\
     $1 != "oemagent" -a $1 != "ldap" ] ; then
    echo "No valid parameter"
    echo $HELPMESG
    exit 1
fi

#-------
# MACROS

RM=/bin/rm
FIND=/usr/bin/find
DIRNAME=/usr/bin/dirname
AWK=/bin/awk
CAT=/bin/cat
TOUCH=/bin/touch

commands="$RM $FIND $DIRNAME $AWK $CAT $TOUCH"
fail=""

for cmd in $commands;
do
    if [ ! -x $cmd ];
    then
        fail="${fail}   $cmd"
    fi
done

if [ "$fail" != "" ];
then
    echo "relink: cannot execute the following commands:\n"
    echo $fail
    echo "\nplease change the appropriate MACROS in $ORACLE_HOME/bin/relink \
          to the correct location on your machine\n"
    exit 1
fi


#-----------------------------
# platform specific variables

TEMPDIR="/tmp/"
TEMPFILE="${TEMPDIR}mfile$$"
CALLEDFROM=`pwd`
MAKE="/usr/bin/make"   # full path to make

PRECOMPS="proc procob procob18 rtsora modada profor"


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

# 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 ${TEMPFILE} ];
    then
        $RM ${TEMPFILE}
    fi

    cd $CALLEDFROM
}

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

call_make () {
# parameters: $1 script parameter, $2 = make target
	
    prm=$1
    target=$2

    case $prm in
        oracle)
                 files="$ORACLE_HOME/rdbms/lib/ins_rdbms.mk" ;;
        network)
                 files="$ORACLE_HOME/network/lib/ins_net_client.mk \
                        $ORACLE_HOME/network/lib/ins_net_server.mk \
                        $ORACLE_HOME/network/lib/ins_cman.mk \
                        $ORACLE_HOME/network/lib/ins_names.mk" ;;
        client)
                 files="$ORACLE_HOME/network/lib/ins_net_client.mk \
                        $ORACLE_HOME/plsql/lib/ins_plsql.mk" ;;
        client_sharedlib)
                 files="$ORACLE_HOME/network/lib/ins_net_client.mk" ;;
        interMedia)
                 files="$ORACLE_HOME/ctx/lib/ins_ctx.mk" ;;
        ctx)
                 files="$ORACLE_HOME/ctx/lib/ins_ctx.mk" ;;
        utilities)
                 files="$ORACLE_HOME/rdbms/lib/ins_rdbms.mk" ;;
        sqlplus)
                 files="$ORACLE_HOME/sqlplus/lib/ins_sqlplus.mk" ;;
        oemagent)
                 files="$ORACLE_HOME/sysman/lib/ins_sysman.mk" ;;
        ldap)
                 files="$ORACLE_HOME/ldap/lib/ins_ldap.mk" ;;
        all)
                 files="`$FIND $ORACLE_HOME -follow -name 'ins_*.mk'`" ;;
    esac

    for l in `echo $files`;
    do
    # it must exist and must not be the precomp and ldap makefile
        if [ -f $l -a "$l" != "$ORACLE_HOME/precomp/lib/ins_precomp.mk" -a \
	     "$l" != "$ORACLE_HOME/ldap/lib/ins_ldap.mk" ];
        then
            echo $l >> ${TEMPFILE}
        fi
    done

    if [ ! -r ${TEMPFILE} ];
    then
        echo "$prm: No Makefiles found."
        return
    fi

    for f in `$CAT ${TEMPFILE}`;
    do
        cd `$DIRNAME $f`
        $MAKE -f $f $target
    done
}

call_precomp_make () {
    # makefile
    files="precomp/lib/ins_precomp.mk"

    # find installed precompilers
    $TOUCH ${TEMPDIR}pretargets
    for f in $PRECOMPS;
    do
        $FIND $ORACLE_HOME/bin -follow -name $f |\
            $AWK -F/ '{print $NF}' >> ${TEMPDIR}pretargets
    done

    for target in `$CAT ${TEMPDIR}pretargets`;
    do
        $MAKE -f $ORACLE_HOME/$files relink EXENAME=$target
    done
}

call_ldap_make () {
     files="ldap/lib/ins_ldap.mk"
     $MAKE -f $ORACLE_HOME/$files clientonlyinstall
}

oracle () {
    call_make $param "ioracle"
    cleanup
}

network () {
    call_make $param "install"
    cleanup
}

ldap () {
    call_ldap_make 
    cleanup
}

client () {
    call_make $param "install"
    cleanup
}

client_sharedlib () {
    call_make "client_sharedlib" "client_sharedlib"
    cleanup
}

interMedia () {
    call_make $param "install"
    cleanup
}

ctx () {
    call_make $param "install"
    cleanup
}

precomp () {
    call_precomp_make
    cleanup "pretargets"
}

utilities () {
    call_make "utilities" "utilities"
    cleanup
    call_make "sqlplus" "install"
    cleanup
}

oemagent () {
    call_make $param "install"
    cleanup
}

all () {
    client_sharedlib
    cleanup
    precomp
    cleanup
    call_ldap_make
    cleanup
    call_make "utilities" "utilities"
    cleanup
    call_make $param "install"
    cleanup
}

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

param=$1
$param		# call the appropriate function (name same as parameter)

