#! /bin/ksh
#
# Licensed Materials - Property of IBM
#
#  5724-A60
#  5765-F36
#  (C) Copyright IBM Corporation 2001, 2004
#
# All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# This script tries to locate the library ORALIB and
# creates a symbolic link in /usr/opt/IMinerX/V8.2/lib (AIX)
# or /opt/IMinerX/V8.2/lib (SunOS, Linux)
# The script must be executed as root !
#

# User who is owner of oracle DBMS files, normally: oracle
ORAUSER=oracle

# The library needed by IM Scoring
ORALIB=libclntsh

# The (32-bit) lib directory of Oracle
ORALIBDIR32=lib

######################################################

# First we must find out the operating system
case "$(uname -s)" in
*AIX*)   LIBEXT=a
         LIBVERSION=
         LIBDIR=/usr/opt/IMinerX/V8.2/lib
         IDCOM=id ;;
*SunOS*) LIBEXT=so
         LIBVERSION=8.0
         LIBDIR=/opt/IMinerX/V8.2/lib
         IDCOM=/usr/xpg4/bin/id ;;
*Linux*) LIBEXT=so
         LIBVERSION=8.0
         LIBDIR=/opt/IMinerX/V8.2/lib
         IDCOM=id ;;
    *) echo Error: Operating system found: $(uname -s). Supported are: AIX, SunOS, Linux
       exit ;;
esac

ORALIB=$ORALIB.$LIBEXT

if [ $($IDCOM -u) != 0 ]; then
 echo "This script must be executed as root."
 exit 1
fi

$IDCOM -u $ORAUSER 2>/dev/null >/dev/null
if [ $? != 0 ]; then
  echo "Cannot find user \"$ORAUSER\". Please modify this script:"
  echo "Change variable ORAUSER to the correct value."
  exit 1
fi

ORAHOME=$(su - $ORAUSER -c 'echo & echo $ORACLE_HOME' </dev/null | tail -1)

if [ "x$ORAHOME" = x ]; then
  echo "Environment Variable ORACLE_HOME of user \"$ORAUSER\" is not defined."
  echo "\"$ORAUSER\" must be a user where ORACLE_HOME is defined."
  exit 1
fi

if [ ! -d $ORAHOME ]; then
  echo "Could not find directory \"$ORAHOME\". Check the value of"
  echo "environment variable ORACLE_HOME of user \"$ORAUSER\"."
  exit 1
fi

# If lib32 exists, we are one a 64-bit machine and have to use lib32
if [ -d $ORAHOME/lib32 ]; then
  ORALIBDIR32=lib32

  # Write a warning if 64-bit extproc is configured

  ORALISTENER=$(su - $ORAUSER -c 'echo & echo $TNS_ADMIN' </dev/null | tail -1)
  if [ "x$ORALISTENER" = x ]; then  
    ORALISTENER=$ORAHOME/network/admin
  fi
  ORALISTENER=$ORALISTENER/listener.ora

  if [ -f $ORALISTENER ]; then
    grep extproc32 $ORALISTENER 2>/dev/null >/dev/null
    if [ $? != 0 ]; then
      echo "Note: Oracle is not configured for 32-bit external procedures."
      echo "      IM Scoring is a 32-bit application. You may have to modify"
      echo "      the listener configuration file (listener.ora) to call"
      echo "      extproc32 instead of extproc."
      echo ""
    fi
  fi

fi

if [ ! -f $ORAHOME/$ORALIBDIR32/$ORALIB ]; then
 echo "File \"$ORAHOME/$ORALIBDIR32/$ORALIB\" does not exist."
 echo "This Oracle library file is needed by IM Scoring."
 echo "Try to find it on your system and copy/move it"
 echo "to the expected location."
 exit 1
fi

ln -s -f $ORAHOME/$ORALIBDIR32/$ORALIB $LIBDIR/$ORALIB
chown -h bin:bin $LIBDIR/$ORALIB

if [ "x$LIBVERSION" != x ]; then
  ln -s -f $ORAHOME/$ORALIBDIR32/$ORALIB $LIBDIR/$ORALIB.$LIBVERSION
  chown -h bin:bin $LIBDIR/$ORALIB.$LIBVERSION
fi

echo "Link(s) to Oracle library successfully created."



