#!/bin/bash
#
# cpc (CellPerfCtr) 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.
#

if [ `uname -m` != "x86_64" ] ; then
	echo "This program must run on an Opteron node of a hybrid system."
	exit 1
fi

# Global Declarations
declare -x SCRIPT=${0##*/}

export RUNID
export CELL_EVENT=""
export CELL_OPTIONS=""
export LD_LIBRARY_PATH
export ACCEL_LD_LIBRARY_PATH64

TEMP=`getopt -o lh --long listenv,help,cell-event:,cell-options:,runid: -n "$SCRIPT" -- "$@"`

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

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-h|--help)
			printf "%s\n" "usage: cpch [options] <appname> [<appargs>]"
			printf "%s\n" "  -h,--help		print this help"
			printf "%s\n" "  -l, --listenv		Prints out trace environment variable information"
			printf "%s\n" "  --cell-event=EVT	cell event, may specify option more than once"
			printf "%s\n" "  --cell-options=OPTS	options for cell cpc command"
			printf "%s\n" "  --runid=ID		run ID, default is a timestamp"
			exit 0 ;;
		-l|--listenv)
			export SCRIPT_DEBUG=true
			shift 1 ;;
		--cell-event)
			if [ "$CELL_EVENT" = "" ] ; then
				CELL_EVENT="$2"
			else
				CELL_EVENT="$2,$CELL_EVENT"
			fi
			shift 2 ;;
		--cell-options)
			CELL_OPTIONS=$2
			shift 2 ;;
		--runid)
			RUNID=$2
			shift 2 ;;
		--)
			shift ; break ;;
		*) 
			echo "Internal error" ; exit 1 ;;
	esac
done
echo "Remaining arguments:"
for arg do
	echo '-->'"\`$arg'"
	PROGRAM="$PROGRAM $arg" 
done

if [ "$PROGRAM" = "" ] ;	then 
	echo "Must specify a program to run on the command line."
	exit 1
fi

if [ "$CELL_EVENT" = "" ] ; 	then 
	echo "At least one Cell event must be specified." 
	exit 1;
fi
if [ "$RUNID" = "" ] ; 		then RUNID=`date +%Y%m%d%H%M%S` ; fi

# Set up the common environment variables, etc.
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

# Not necessary to add anything to the paths at this time.
#export LD_LIBRARY_PATH=$SDK_ROOT/usr/lib64/cpc:$SDK_PROTOTYPE_ROOT/usr/lib64/cpc:$LD_LIBRARY_PATH

#export ACCEL_LD_LIBRARY_PATH32=/usr/lib/cpc:/opt/cell/sdk/prototype/usr/lib/
#export ACCEL_LD_LIBRARY_PATH64=/$SDK_ROOT/usr/lib64/cpc:$SDK_PROTOTYPE_ROOT/usr/lib64/cpc

# Environment variables to be made visible to the accelerator script.
DACS_START_ENV_LIST="$DACS_START_ENV_LIST;LD_LIBRARY_PATH;CELL_EVENT;RUNID"

if [ "$CELL_OPTIONS" ] ; then
	DACS_START_ENV_LIST="CELL_OPTIONS;$DACS_START_ENV_LIST"
fi

if [ "$CELL_EVENT" != "none" ] ; then
	DACS_START_PARENT="/bin/bash $HYBRID_PERF_TOOLS_DIR/cpca %e %a"
fi
export DACS_START_PARENT

if [ "$SCRIPT_DEBUG" ] ; then
	echo "cpch environment:"
	echo "PROGRAM     = $PROGRAM"
	echo "RUNID       = $RUNID"
	echo "CELL_EVENT  = $CELL_EVENT"
	echo "CELL_OPTIONS= $CELL_OPTIONS"
	echo "DACS_START_ENV_LIST= $DACS_START_ENV_LIST"
	echo "DACS_START_PARENT= $DACS_START_PARENT"
fi

echo "Starting application (in cpch)."
$PROGRAM
echo "Returned from application (in cpch)."

echo "$result"
exit $result

