#!/bin/sh

# $Id: psrun-batch,v 1.4 2006/01/21 22:45:36 rkufrin Exp $

############################################################################
#
#                      University of Illinois/NCSA
#                          Open Source License
# 
#          Copyright(C) 2004-2006, The Board of Trustees of the
#              University of Illinois. All rights reserved.
#
#                             Developed by:             
# 
#                        The PerfSuite Project
#            National Center for Supercomputing Applications
#              University of Illinois at Urbana-Champaign
# 
#                   http://perfsuite.ncsa.uiuc.edu/
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal with the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, 
# and/or sell copies of the Software, and to permit persons to whom the 
# Software is furnished to do so, subject to the following conditions:
# 
# + Redistributions of source code must retain the above copyright notice, 
#   this list of conditions and the following disclaimers.
# + Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimers in
#   the documentation and/or other materials provided with the distribution.
# + Neither the names of The PerfSuite Project, NCSA/University of Illinois
#   at Urbana-Champaign, nor the names of its contributors may be used to
#   endorse or promote products derived from this Software without specific
#   prior written permission.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
# CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
# DEALINGS WITH THE SOFTWARE.
############################################################################

# psrun-batch: execute multiple instances of psrun using a different
#              configuration file for each run
#
# Usage: psrun-batch [option] config [config ..] -- command
#
# Options:
#   -h   Display usage information
#   -v   Verbose output.  Echoes configuration and command before running
#
# Author: Rick Kufrin
# Date:   2004/11/22
#
# This file is part of PerfSuite.

psrun -h > /dev/null 2>&1

if test $? -eq 127; then
    echo "Cannot execute psrun: check your PATH and LD_LIBRARY_PATH settings"
    echo "Current PATH components:"
    echo $PATH | sed 's/:/\
/g' | sed 's/^/    /g' | sort | uniq
    echo ""
    echo "Current LD_LIBRARY_PATH components:"
    echo $LD_LIBRARY_PATH | sed 's/:/\
/g' | sed 's/^/    /g' | sort | uniq
    exit 1
fi

verbose=0

while test $# -gt 0 ; do
    case $1 in
        -h | --help )
             echo "Usage: $0 config [config ..] -- command"
             exit 0 ;;
        -v | --verbose ) verbose=1 ;;
        -- ) shift; break ;;
        *  ) CONFIGS="$CONFIGS $1" ;;
    esac
    shift 
done

args=$*

if test "$args" = ""; then
    echo "Usage: $0 config [config ..] -- command [args]"
    exit 1
fi

if test x"$CONFIGS" = x; then
    echo "No configuration files supplied."
    exit 1
fi

for config in $CONFIGS; do
    if test $verbose -eq 1; then
        echo "Running \"$args\" with configuration \"$config\"..."
    fi
    psrun -X -C -c $config $args
    if test $? -ne 0; then
        echo "Error: psrun -X -C -c $config $args unsuccessful"
        exit 1
    fi
done

exit 0
