#!/bin/bash
# -----------------------------------------------------------------------------
#
# FDPR-Pro host setup script for hybrid application use.
#
# -----------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5724-S84
# (C) Copyright IBM Corp. 2007,2007 All Rights Reserved
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
#

export OPT_OPTIONS
export PROGRAM_STRING

# ==========================================
# Process the fdprproh command arguments.
# ==========================================

# Note that we use '"$@"' to let each command-line parameter expand to a 
# separate word. The quotes around '$@' are essential!
# We need TEMP as the 'eval set --' would nuke the return value of getopt.

TEMP=`getopt -o hlo: --long help,listenv,optimization-options: -n '$SCRIPT' -- "$@"`

if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around '$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
	case "$1" in
		-h|--help) 
                     printf "%s\n"   "usage: fdprproh [OPTION] ... <application name> [<application arguments>]"
                     printf "%s\n"   "  NOTE 1: if application arguments contain switches (ex/ -p),"
                     printf "%s\n\n" "         then all the arguments must be placed in double quotes"
                     printf "%s\n"   "  -l, --listenv   list the environment variables for the script processing "
                     printf "%s\n"   "  -h, --help "
                     printf "%s\n"   "  -o, --optimization-options      \"<fdprpro optimization options>\" "
                     printf "%s\n\n" "        NOTE 2: the optimization options must be enclosed in double quotes"

                exit 0 ;;
		-l|--listenv) 
                     export SCRIPT_DEBUG=true; 
                     shift 1 ;;
		-o|--optimization-options) 
                     OPT_OPTIONS=$2
                     shift 2 ;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
done
for arg do 
  PROGRAM_STRING="$PROGRAM_STRING $arg"
done


# ==========================================
# Application Environment Variables

# include the common setup file
if [ ! "$SDK_PROTOTYPE_ROOT" ] ; then
  export SDK_PROTOTYPE_ROOT=/opt/cell/sdk/prototype
fi
if [ ! "$HYBRID_PERF_TOOLS_DIR" ] ; then
  export HYBRID_PERF_TOOLS_DIR=$SDK_PROTOTYPE_ROOT/usr/bin
fi
source $HYBRID_PERF_TOOLS_DIR/perfToolHostSetup

if [ "$OPT_OPTIONS" ] ; then
  export DACS_START_ENV_LIST="$DACS_START_ENV_LIST;OPT_OPTIONS"
fi

export DACS_START_PARENT="$HYBRID_PERF_TOOLS_DIR/fdprproa %e %a"

if [ ! "$PROGRAM_STRING" ] ; then
  printf "$SCRIPT: Error - no program name specified.\n"
  exit 22
fi

if [ "$SCRIPT_DEBUG" ] ; then
  echo
  echo "****** fdprproh Environment Variables *******"
  echo "DACS_START_PARENT       = $DACS_START_PARENT"
  echo "DACS_START_ENV_LIST     = $DACS_START_ENV_LIST"
  echo "PROGRAM_STRING          = $PROGRAM_STRING"
  echo "ACCEL_LD_LIBRARY_PATH64 = $ACCEL_LD_LIBRARY_PATH64"
  echo "****** perfToolHostSetup complete *******"
  echo
fi

exec $PROGRAM_STRING

if [ "$result != 0" ] ; then
  echo "return code from $PROGRAM_STRING = $result"
fi

exit $result

