#!/bin/sh
#############################################################################
#  Copyright (c) 2002-2008, Oracle. All rights reserved.
#  Shell Script Wrapper for perl
#
#  $Id: rda.sh,v 1.22 2008/02/25 17:41:50 mschenke Exp $
#  ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/bin/rda.sh,v 1.22 2008/02/25 17:41:50 mschenke Exp $
#############################################################################

if [ -z "$RDA_CWD" ]
then
  RDA_CWD=`pwd`
  export RDA_CWD
fi
RDA_DIR=`dirname "$0"`
cd "$RDA_DIR" 

# Try to local perl if it is not in the path
if [ -z "$RDA_NO_NATIVE" ]
then
  PERL_CMD=`type perl 2>&1`
  PERL_EXE=`expr "$PERL_CMD" : "perl is \(.*\)"`
fi

# Validate the local perl
if [ -n "$PERL_EXE" ]
then
  PERL5OLD="${PERL5LIB:-.}"
  PERL5LIB=.
  export PERL5LIB
  ( ulimit -c 0
    "$PERL_EXE" -e "die 'too old' if $] < 5.005; use strict"
  ) >/dev/null 2>/dev/null
  if [ $? -ne 0 ]
  then
    PERL_EXE=''
    PERL5LIB="$PERL5OLD"
  fi
fi

# Validate Applications perl
if [ -z "$PERL_EXE" -a -n "$ADPERLPRG" ]
then
  ( ulimit -c 0
    "$ADPERLPRG" -e "die 'too old' if $] < 5.005; use strict"
  ) >/dev/null 2>/dev/null
  if [ $? -eq 0 ]
  then
    PERL_EXE="$ADPERLPRG"
  fi
fi

# Locate perl
ORA_HOME="${IAS_ORACLE_HOME:-$ORACLE_HOME}"
if [ -z "$PERL_EXE" ]
then
  if [ -r .config ]
  then
    . ./.config
  elif [ -z "$ORA_HOME" ]
  then
    echo "Error: ORACLE_HOME is not set."
    echo "Please set your ORACLE_HOME."
    exit 1
  elif [ -x "$ORA_HOME"/perl/bin/perl ]
  then
    PERL_EXE=`echo $ORA_HOME/perl/bin/perl`
    PERL_LIB=`echo $ORA_HOME/perl/lib`
  elif [ -x "$ORA_HOME"/perl/5*/bin/perl ]
  then
    PERL_EXE=`echo $ORA_HOME/perl/5*/bin/perl`
    PERL_LIB=`echo $ORA_HOME/perl/5*/lib`
  elif [ -x "$ORA_HOME"/Apache/perl/bin/perl ]
  then
    PERL_EXE=`echo $ORA_HOME/Apache/perl/bin/perl`
    PERL_LIB=`echo $ORA_HOME/Apache/perl/lib`
  elif [ -x "$ORA_HOME"/Apache/perl/5*/bin/perl ]
  then
    PERL_EXE=`echo $ORA_HOME/Apache/perl/5*/bin/perl`
    PERL_LIB=`echo $ORA_HOME/Apache/perl/5*/lib`
  elif [ -x "$ORA_HOME"/Apache/perl/5*/bin/*/perl ]
  then
    PERL_EXE=`echo $ORA_HOME/Apache/perl/5*/bin/*/perl`
    PERL_LIB=`echo $ORA_HOME/Apache/perl/5*/lib`
  fi

  if [ -n "$PERL_EXE" ]
  then
    PERL5DIR=`dirname "$PERL_EXE"`
    PERL5LIB="."
    for DIR in `find $PERL_LIB -type d -name auto -exec dirname '{}' \;`
    do
      PERL5LIB="$DIR:$PERL5LIB"
    done
    export PERL5LIB
    "$PERL_EXE" -e "die 'too old' if $] < 5.005; use strict" \
      >/dev/null 2>/dev/null
    if [ $? -ne 0 ]
    then
      PERL5DIR=''
    elif [ -w . ]
    then
      echo "PERL5DIR='$PERL5DIR'" >.config
      echo "PERL5LIB='$PERL5LIB'" >>.config
    fi
  fi

  if [ -z "$PERL5DIR" ]
  then
    echo "Error: Perl not found in the PATH or in known directory locations."
    echo "Although the default RDA engine requires Perl, compiled versions"
    echo "without Perl requirements are available for major platforms. Please"
    echo "download the platform-specific RDA engine from MetaLink and place"
    echo "within the top directory of your RDA installation."
    exit 1
  fi

  PATH="$PERL5DIR:$PATH"
  export PATH PERL5LIB
else
  FOUND='N'
  for DIR in `"$PERL_EXE" -e 'print join("\n",@INC);'`
  do
    if [ "$DIR" = "." ]
    then
      FOUND='Y'
    fi
  done
  if [ "$FOUND" = "N" ]
  then
    if [ -z "$PERL5LIB" ]
    then
      PERL5LIB='.'
    else
      PERL5LIB=".:$PERL5LIB"
    fi
    export PERL5LIB
  fi
fi

# Run the perl script
exec "${PERL_EXE:-perl}" rda.pl "$@"
