#!/bin/sh
#
# $Header: usm/src/cmds/acfstoolsdriver/acfstoolsdriver.sh /main/12 2010/10/07 16:33:34 averhuls Exp $
#
# acfstoolsdriver.sh
#
# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      acfstoolsdriver - front end for the Perl scripts that do the work.
#
#    DESCRIPTION
#      common driver for:
#        acfsload, acfsroot, acfsregistrymount,
#        acfssinglefsmount, acfsdriverstate.
#
#    NOTES
#      This wrapper program supports Linux/Unix platforms.
#
#      ORA_CRS_HOME and ORACLE_HOME have been exported by the caller
#      (see DESCRIPTION). They are NOT inherited from the shell.
#
#    MODIFIED   (MM/DD/YY)
#    averhuls    10/06/10 - Retarget ADE targets to ./usm/bin.
#    agraves     10/13/09 - Add -t to clsecho to print time stamp.
#    abakst      10/05/09 - Set up LIBPATH so clsecho will run on AIX
#    averhuls    06/29/09 - Fix Perl version calculation.
#    agraves     05/08/09 - Change messages to be consistent with Bill Manry's
#                           review.
#    averhuls    04/28/09 - Separate ORACLE_HOME from ORA_CRS_HOME.
#    averhuls    04/01/09 - Add Solaris support.
#    averhuls    02/19/09 - Add unix_linux lib.
#    averhuls    01/29/09 - rename usm_ to acfs.
#    averhuls    01/06/09 - Creation
#
#############################################################################

# One echo to just the console.
CLSECHO="$ORA_CRS_HOME/bin/clsecho -p usm -f acfs  -c err "
# One echo to the log, with timestamps.
CLSECHOTL="$ORA_CRS_HOME/bin/clsecho -p usm -f acfs -l -c err -t -n"
SED=/bin/sed
PATH=/bin:/usr/bin:/usr/sbin:/sbin:$PATH
PLATFORM=`uname`

# Add Solaris32 backward compatibility for LD_LIBRARY_PATH.  This extra
# path is used for Solaris64 Oracle homes that come with a Solaris32
# Perl build.
LD_LIBRARY_PATH="${ORACLE_HOME}/lib:${LD_LIBRARY_PATH}"
LD_LIBRARY_PATH="${ORACLE_HOME}/lib32:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

# AIX uses LIBPATH, not LD_LIBRARY_PATH.
if [ "${PLATFORM}" = "AIX" ] ; then
  LIBPATH="${ORACLE_HOME}/lib:${LIBPATH}"
  export LIBPATH
fi

# HP-UX uses SHLIB_PATH, not LD_LIBRARY_PATH.
if [ "${PLATFORM}" = "HP-UX" ] ; then
  SHLIB_PATH="${ORACLE_HOME}/lib32:${SHLIB_PATH}"
  export SHLIB_PATH
fi

# the basename of the command that the user entered
CMD=$1

# Set path to Perl.
PERLBIN="${ORACLE_HOME}/perl/bin/perl"

if [ ! -r $PERLBIN ] ; then
  # 9207: "The user this command is executing as does not have permission to execute Perl in '%s'"
  ${CLSECHO} -m 9207 $PERLBIN
  ${CLSECHOTL} -m 9207 $PERLBIN
  if [ $CMD = "acfsdriverstate" ] ; then
    # 9208: "Enter the location of the db home for this database" to '-orahome'
    ${CLSECHO} -m 9208
    ${CLSECHOTL} -m 9208
    # 9206: usage: %s [-orahome <ORACLE_HOME>] <installed | loaded | version | supported> [-s]
    ${CLSECHO} -m 9206 $CMD
    ${CLSECHOTL} -m 9206 $CMD
  else
    # 9130: "Root access required."
    ${CLSECHO} -m 9130
    ${CLSECHOTL} -m 9130
  fi
  exit 1
fi

# Get Perl version.
PERLV=`${PERLBIN} -e 'printf "%vd", $^V'`

# Set PERLINC, which is a list of -I options passed to the perl
# interpreter, telling perl where to look for modules.
PERLINC="-I ${ORACLE_HOME}/perl/lib/${PERLV}"
PERLINC="${PERLINC} -I ${ORACLE_HOME}/perl/lib/site_perl/${PERLV}"

# if the OSD module is not found, the platform is not supported
FOUND_OSD_MODULE=0
if [ ! -d "$ADE_VIEW_ROOT" ] ; then
  if [ -r $ORA_CRS_HOME/lib/osds_${CMD}.pm ] ; then
    FOUND_OSD_MODULE=1
  fi
  PERLINC="${PERLINC} -I ${ORACLE_HOME}/lib -I ${ORA_CRS_HOME}/lib"
  CMD_LOC="${ORA_CRS_HOME}/lib/${CMD}.pl"
else
  if [ -r $ADE_VIEW_ROOT/usm/bin/osds_${CMD}.pm ] ;
  then
    FOUND_OSD_MODULE=1
  fi
  PERLINC="${PERLINC} -I $ADE_VIEW_ROOT/usm/bin"
  CMD_LOC="${ADE_VIEW_ROOT}/usm/bin/${CMD}.pl"
fi

if [ $FOUND_OSD_MODULE = 0 ] ; then
  # 9125: "ADVM/ACFS is not supported on $PLATFORM."
  ${CLSECHO} -m 9125 $PLATFORM
  ${CLSECHOTL} -m 9125 $PLATFORM
  exit 1
fi

# Build command to execute the tool.
RUNTHIS="${PERLBIN} -w ${PERLINC} ${CMD_LOC}"

# Now run command with all arguments!
exec ${RUNTHIS} $@

# Should never get here.
exit -1
