#!/bin/bash 
shopt -o monitor
#
# PDTRH script for hybrid application use.
#
# Use the tracea script to gather performance data on an application, 
# then this script can be used to run the PDTR trace analysis tool.
#
# -----------------------------------------------------------------------------
# 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 PDTR_OPTIONS=" -trc"

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

RUNID=""

TEMP=`getopt -o hl --long help,listenv,runid:,pdtr-options: -n pdtrh -- "$@"`

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

eval set -- "$TEMP"

while true ; do
	case "$1" in
		-h|--help)
            echo
			printf "%s\n\n" "usage: pdtr --runid=ID [options]"
            printf "%s\n" "--runid=ID  Required.  Must either match the runID given to traceh"
            printf "%s\n\n" "            when generating the trace, or match the default date-timestamp."
            printf "%s\n" "Options:"
			printf "%s\n" "  -h,--help    print this help"
            printf "%s\n" "  -l,--listenv Prints out trace environment variable information "
            printf "%s\n" "  -o, --pdtr-options      \"<pdtr command line options>\" "
            printf "%s\n" "        NOTE: if more than one option is supplied it must"
            printf "%s\n\n" "        be enclosed in double quotes"
			exit 0 ;;
		-l|--listenv) 
            export SCRIPT_DEBUG=true
            shift 1 ;;
		--runid)
			RUNID=$2
			shift 2 ;;
		-o|--pdtr-options) 
                     PDTR_OPTIONS=$2
                     shift 2 ;;
		--)
			shift ; break ;;
		*) 
			echo "Internal error" ; exit 1 ;;
	esac
done

if [ "$RUNID" = "" ] ; 		then 
	echo "Must specify --runid option on the command line."
	exit 1
fi

# Application Environment Variables
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
export PDT_DATA_DIR=$PERF_DATA_DIR/trace/$RUNID

if [ "$SCRIPT_DEBUG" ] ; then
	echo "****** pdtr Environment Variables ******"
	echo "RUNID       = $RUNID"
	echo "PDT_DATA_DIR= $PDT_DATA_DIR"
	echo "****** pdtr Environment Variable Setup Complete ******"
fi

echo "PDTR output will be stored in: $PDT_DATA_DIR"
for HOST_NAME in `ls $PDT_DATA_DIR | grep .pex | sed -e '/_/!d' -e "s/.pex//g" -e "s/_.*//" | uniq`; do

  echo "Running PDTR for host: $HOST_NAME"
  cd $PDT_DATA_DIR
  for BASENAME in `eval ls $HOST_NAME\_* | grep .pex | sed -e "s/.pex//g"`; do
    echo "HOST_NAME=$HOST_NAME  BASENAME=$BASENAME"
    ssh $HOST_NAME "`which pdtr` $PDTR_OPTIONS  -ip $PDT_DATA_DIR   -op $PDT_DATA_DIR    $BASENAME" &
  done

done

wait

echo "$result"
exit $result

