#!/bin/sh
#/****************************************************************************
#**  SCALASCA    http://www.scalasca.org/                                   **
#*****************************************************************************
#**  Copyright (c) 1998-2009                                                **
#**  Forschungszentrum Juelich, Juelich Supercomputing Centre               **
#**                                                                         **
#**  See the file COPYRIGHT in the package base directory for details       **
#****************************************************************************/


VERSION="1.2.2"

# Validate command line (to some extent)
if [ $# -eq 0 ]; then
  echo "SCALASCA $VERSION: application instrumenter"
  echo "usage: `basename $0` [-v] [-pomp] [-tau] [-user] <compile-or-link-command>"
  echo "  -pomp: process source files for POMP directives"
  echo "  -tau:  process source files with TAU instrumenter (if configured)"
  echo "  -user: enable EPIK user instrumentation API macros"
  echo "  -v:    enable verbose commentary"
  exit 1
fi

# Process options
unset POMP
unset TAU
unset USER
unset VERBOSE
while [ $# -ne 0 ]; do
  case $1 in
    -pomp)
      POMP="-pomp"
      shift
      ;;
    -tau)
      TAU="-tau"
      shift
      ;;
    -user)
      USER="EPIK"
      shift
      ;;
    -@|-v)
      VERBOSE="-@"
      shift
      ;;
    -h|-help)
      exec $0 # usage
      ;;
    *)
      break
      ;;
  esac
done

if [ $# -eq 0 ]; then
  echo "Error: no compile or link command"
  exec $0 # usage
fi

# Simple check whether a Fortran compiler is used
if [ -n "${USER}" ]; then
  COMPILER=`basename $1`
  case "${COMPILER}" in
    *f*|*F*)
      USER="-D${USER}"
      ;;
    *)
      USER="-D${USER}"
      ;;
  esac
fi

# Determine installation directory
BINDIR=`dirname $0`

if [ -n "${USER}" ]; then
  USER_CFLAGS=`${BINDIR}/kconfig --cflags`
  USER="$USER $USER_CFLAGS"
fi

# Check whether "kinst" script is available
if [ \( ! -x "${BINDIR}/kinst" \) -a \( -z "${POMP}" \) ]; then
  echo "ERROR: No compiler instrumentation available. Consider using"
  echo "ERROR: POMP directives and calling 'skin' with the '-pomp'"
  echo "ERROR: option to get useful results."
  exit 1
fi

# Execute corresponding "kinst/kinst-pomp" script
${BINDIR}/kinst${POMP} ${TAU} ${VERBOSE} "$@" ${USER}
