#!/bin/sh
#
# Wrapper script for hpcfnbounds-bin.  Other than setting
# LD_LIBRARY_PATH, all this script does is process the -v option
# for internal debugging.
#
# Run this script with -h for usage message.
#
# $Id: hpcfnbounds.in 3978 2012-10-24 04:51:32Z krentel $
#

# If this script can't find its own install prefix, or if the script
# is moved elsewhere, then set HPCTOOLKIT here.
# HPCTOOLKIT=/path/to/install/prefix

# Relative paths are relative to HPCTOOLKIT.
ext_libs_dir='lib/hpctoolkit/ext-libs'
hpcfnbounds_dir='libexec/hpctoolkit'

# Should be absolute path.
libcxx_path='/usr/lib/gcc/x86_64-redhat-linux/4.4.7'

################################################################

hpc_path_to_root=../..
#
# Set and export HPCTOOLKIT to the root dir of the install (prefix).
# If HPCTOOLKIT already names a valid directory, then use that, else
# try to find the root directory from $0.
#
hpc_path_to_root="${hpc_path_to_root:-..}"
valid_hpctoolkit_dir()
{
    test -d "$1" && test -d "$1/lib/hpctoolkit"
}

# Warn if HPCTOOLKIT is set improperly, set to abs path if ok.
if test "x$HPCTOOLKIT" != x ; then
    if valid_hpctoolkit_dir "$HPCTOOLKIT" ; then
	HPCTOOLKIT=`( cd "$HPCTOOLKIT" 2>/dev/null && pwd )`
    else
	echo "Warning: invalid HPCTOOLKIT directory: $HPCTOOLKIT" 1>&2
    fi
fi

# Try dirname of $0, dereference symlink if needed.
if valid_hpctoolkit_dir "$HPCTOOLKIT" ; then :
else
    if test -L "$0" ; then
	hpc_file=`readlink "$0"`
    else
	hpc_file="$0"
    fi
    hpc_dir=`dirname "$hpc_file"`
    HPCTOOLKIT=`( cd "${hpc_dir}/${hpc_path_to_root}" 2>/dev/null && pwd )`
fi

if valid_hpctoolkit_dir "$HPCTOOLKIT" ; then :
else
    cat <<EOF >&2

$0: Unable to find HPCTOOLKIT root directory.
Please set HPCTOOLKIT to the install prefix, either in this script,
or in your environment, and try again.
EOF
    exit 1
fi

export HPCTOOLKIT

# Relative paths are relative to HPCTOOLKIT.
case "$ext_libs_dir" in
    /* ) ;;
    * )  ext_libs_dir="${HPCTOOLKIT}/${ext_libs_dir}" ;;
esac
case "$hpcfnbounds_dir" in
    /* ) ;;
    * )  hpcfnbounds_dir="${HPCTOOLKIT}/${hpcfnbounds_dir}" ;;
esac

#
# If -v is one of the options, then print extra info about what files
# hpcfnbounds is run on, whether it succeeds, etc, to go in hpcrun
# logfile.
#
server=no
verbose=no
base=
dir=
for arg in "$@"
do
    case "$arg" in
	-s ) server=yes ;;
	-v ) verbose=yes ;;
	-* ) ;;
	* )
	    if test "x$base" = x ; then
		base=`basename "$arg"`
	    elif test "x$dir" = x ; then
		dir="$arg"
	    fi
	    ;;
    esac
done

# FIXME: integrate server and verbose.

if test "$server" = yes ; then
    export LD_LIBRARY_PATH="${ext_libs_dir}:${libcxx_path}"
    exec "${hpcfnbounds_dir}/hpcfnbounds-bin" "$@"
fi

if test "$verbose" = yes ; then
    echo "hpcfnbounds $@"
fi

if test "x$dir" != x && test ! -d "$dir" ; then
    mkdir -p "$dir"
fi

LD_LIBRARY_PATH="${ext_libs_dir}:${libcxx_path}" \
    "${hpcfnbounds_dir}/hpcfnbounds-bin" "$@"

ret=$?
if test "$verbose" = yes ; then
    if test $ret -ne 0 ; then
	echo "hpcfnbounds $base FAILED"
    fi
    ls -l "${dir}/${base}"*
    tail -1 "${dir}/${base}"*.txt
fi

exit $ret
