#!/bin/bash 
#           -xv
# cpc (CellPerfCtr) accelerator 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` != "ppc64" ] ; then
	echo "This program must run on a Cell Blade of a hybrid system."
	exit 1
fi

# Global Declarations

echo "cpca starting ..."

source "$HYBRID_PERF_TOOLS_DIR/perfToolAcceleratorSetup"

echo "cpca after setup ... "

CPC=`which cpc`

# Sanity Checks
if test ! -x  "$CPC" ;  then
  printf "$SCRIPT:$LINENO: the command cpc is not available - aborting.\n"
  exit 192
fi

TEMP=`getopt -o h --long help -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: cpca is meant to be invoked from the cpch script via DACS"
                        exit 0 ;;
                --)
                        shift ; break ;;
                *)
                        echo "Internal error" ; exit 1 ;;
        esac
done

echo "Remaining arguments:"
for arg do
        echo '-->'"\`$arg'"
        PROGRAM="$PROGRAM $arg"
done

if [ "$CELL_EVENT" = "" ] ;	then echo "CELL_EVENT environment var is required" ; exit 1 ; fi
if [ "$RUNID" = "" ] ;		then echo "RUNID environment var is required" ; exit 1 ; fi

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

if [ "$CELL_EVENT" != "none" ] ; then
	CELL_DATA_DIR=$PERF_DATA_DIR/cpc/$RUNID/cbe/`hostname`
	mkdir -p $CELL_DATA_DIR
	echo "cpc output in: $CELL_DATA_DIR/cpc.out"
fi

echo "Calling CBE app..."
cpc --events $CELL_EVENT $CELL_OPTIONS -o $CELL_DATA_DIR/cpc.out $PROGRAM 
#cpc --events $CELL_EVENT $CELL_OPTIONS $PROGRAM 
echo "Exited CBE app..."

echo "End of cpca"

exit 
