#!/bin/sh
# $Id$
# *===========================================================================*
# |                 Copyright (c) 2008 Applaud Solutions UK Ltd               |
# |                             All rights reserved                           |
# *===========================================================================*

header_string="$Id$"
prog_version=`echo "$header_string" | awk '{print $3}'`
program=`basename $0 .sh`

usage="\t$program"

#
# Set the applyfile script
#
applyfile=$XXAS_PATCH_BIN/xxas_com_applyfile.sh

#
# Verify that the applyfile exists
#
if test ! -f "$applyfile" ; then
  printf "\nERROR: The following file cannot be found:\n"
  printf "$applyfile\n\n"
  exit 1;
fi

#
# Apply wrapper function
#
apply () {
  $XXAS_LOG "Applying: $1..."
  $XXAS_LOG "Action: $2"
  $applyfile $1 $2
  exit_code=$?
  if [ $exit_code -ne 0 ];
  then
    exit 1;
  fi
  $XXAS_LOG "Finished applying: $1"
}

#
# Install the java utility (this will take no action if it's already
# installed).
#
apply "file:JAVA_TOP:xxas/oracle/apps/xxas/com:CommandLineParser.class:16:copyonly"
apply "file:JAVA_TOP:xxas/oracle/apps/xxas/com:JDBCConnectionTester.class:16:copyonly"

#
# Attempt to derive the JDBC connect string
#
$XXAS_LOG "Setting JDBC Connection String:"
if [ "${APPS_JDBC_URL}x" != "x" ];
then
  $XXAS_LOG "Setting JDBC to APPS_JDBC_URL."
  XXAS_JDBC_CONN=$APPS_JDBC_URL
else
  $XXAS_LOG "Deriving JDBC from DBC file."
  getdbc_file=$XXAS_PATCH_SQL/xxas_com_dbc.sql

  #
  # Check the get DBC SQL file exist
  #
  if test ! -f "$getdbc_file" ; then
    printf "\nERROR: The following SQL file cannot be found:\n"
    printf "$getdbc_file\n\n"
    exit 1;
  fi

  #
  # Get the DBC file
  #
  $XXAS_LOG "Fetching name of DBC file..."
  dbc=`$XXAS_SQLPLUS -s $XXAS_APPS_LOGIN @$getdbc_file`
  if [ ${dbc}x = "x" ];
  then
    $XXAS_LOG "DBC profile option not set."
  else
    $XXAS_LOG "$dbc"

    #
    # Check FND_SECURE is set
    #
    if test ! "$FND_SECURE" ; then
      $XXAS_LOG "The "\$"FND_SECURE environment variable is not set."
    else

      #
      # Derive the DBC file name
      #
      dbc_file=$FND_SECURE/${dbc}.dbc

      #
      # Check that the actual DBC file exists
      #
      if test ! -f "$dbc_file" ; then
        $XXAS_LOG "The DBC file $dbc_file cannot be found."
      else
        $XXAS_LOG "DBC file: $dbc_file."

        #
        # Loop through each line and fetch the required parameters
        #
        for line in `cat $dbc_file`;
        do

          #
          # Remove comments from the line
          #
          content_line=`echo $line | sed '/^ *#/d;s/#.*//'`

          #
          # Set the current parameter and value
          #
          param=`echo "$content_line" | cut -f 1 -d\=`
          value=`echo "$content_line" | cut -f 2- -d\=`

          #
          # Convert the required parameter to an environment variable
          #
          case $param in
            APPS_JDBC_URL)
              jdbc_url=$value
              $XXAS_LOG "DBC APPS_JDBC_URL: $jdbc_url."
            ;;
            DB_NAME)
              db_name=$value
              $XXAS_LOG "DBC DB_NAME: $db_name."
            ;; 
            TWO_TASK)
              two_task=$value
              $XXAS_LOG "DBC TWO_TASK: $two_task."
            ;; 
            DB_PORT)
              db_port=$value
              $XXAS_LOG "DBC DB_PORT: $db_port."
            ;; 
            DB_HOST)
              db_host=$value
              $XXAS_LOG "DBC DB_HOST: $db_host."
              ;;
          esac
        done

        #
        #
        #
        if [ "${jdbc_url}x" != "x" ];
        then
          $XXAS_LOG "Setting JDBC to DBC APPS_JDBC_URL."
          #
          # Strip out any backslash escape characters
          #
          XXAS_JDBC_CONN=`echo $jdbc_url | sed 's/\\\//g'`
        else
          $XXAS_LOG "JDBC will be constructed from DBC host, port and name."
          #
          # If DB_NAME is blank attempt to set it to TWO_TASK
          #
          if [ ${db_name}x = "x" -a ${two_task}x != "x" ]; then
            $XXAS_LOG "DB Name set to TWO_TASK."
            db_name=$two_task
          fi
        fi
      fi
    fi
  fi
fi

#
# If a JDBC string is already available, strip out everything before
# the @ (including the @).
#
if [ "${XXAS_JDBC_CONN}x" != "x" ];
then
  #
  # Remove any portion prior to the @ sign, eg, jdbc:oracle:thin@.
  #
  case "$XXAS_JDBC_CONN" in
    *@*)
      #
      # Remove any portion prior to the @ sign, eg, jdbc:oracle:thin@.
      #
      $XXAS_LOG "Stripping out portion of JDBC connect string before the @."
      XXAS_JDBC_CONN=`echo $XXAS_JDBC_CONN | cut -f2 -d"@"`
    ;;
  esac

  $XXAS_LOG "Attempting APPS_JDBC_URL connection."
  #
  # Test the JDBC Connect String
  #
  jdbc_rslt=`java xxas.oracle.apps.xxas.com.JDBCConnectionTester \
            -appsUser $XXAS_APPS_UN -appsPwd $XXAS_APPS_PW \
            -connectString "$XXAS_JDBC_CONN" 2> /dev/null`
  exit_code=$?
  if [ $exit_code -ne 0 ];
  then
    #
    # The connection failed.
    #
    $XXAS_LOG "APPS_JDBC_URL connection failed."
    unset XXAS_JDBC_CONN
  fi
fi

#
# If no valid JDBC connect string is available, construct the JDBC string
#
if [ "${XXAS_JDBC_CONN}x" = "x" ];
then
  #
  # Only proceed if all components of the JDBC string are known
  #
  if [ ${db_name}x != "x" -a ${db_port}x != "x" -a \
       ${db_host}x != "x" ]; then
    #
    # First attempt a simple SID connection
    #
    $XXAS_LOG "Attempting SID Connection."
    XXAS_JDBC_CONN="${db_host}:${db_port}:${db_name}"

    #
    # Test the SID connection
    #
    jdbc_rslt=`java xxas.oracle.apps.xxas.com.JDBCConnectionTester \
              -appsUser $XXAS_APPS_UN -appsPwd $XXAS_APPS_PW \
              -connectString "$XXAS_JDBC_CONN" 2> /dev/null`
    exit_code=$?
    if [ $exit_code -ne 0 ];
    then
      #
      # The connection failed. Attempt a Service Name connection.
      #
      $XXAS_LOG "SID connection failed. Attempting Service connection."
      XXAS_JDBC_CONN="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) \
       (HOST=$db_host)(PORT=$db_port))(CONNECT_DATA=(SERVICE_NAME=$db_name)))"
      #
      # Test the Service connection
      #
      jdbc_rslt=`java xxas.oracle.apps.xxas.com.JDBCConnectionTester \
                -appsUser $XXAS_APPS_UN -appsPwd $XXAS_APPS_PW \
                -connectString "$XXAS_JDBC_CONN" 2> /dev/null`
      exit_code=$?
      if [ $exit_code -ne 0 ];
      then
        #
        # All methods failed. JDBC Connection could not be obtained.
        #
        $XXAS_LOG "A JDBC connection could not be established automatically."
        unset XXAS_JDBC_CONN
      fi
    fi
  fi
fi

if [ "${XXAS_JDBC_CONN}x" != "x" ];
then
  #
  # Export the variable
  #
  export XXAS_JDBC_CONN
  $XXAS_LOG "JDBC Connect String: $XXAS_JDBC_CONN"
  $XXAS_LOG "JDBC Connection successful."
fi

$XXAS_LOG "Finished setting JDBC Connect String."

