#!/bin/bash
# oclgrind (Oclgrind)
# Copyright (c) 2013-2015, James Price and Simon McIntosh-Smith,
# University of Bristol. All rights reserved.
#
# This program is provided under a three-clause BSD license. For full
# license terms please see the LICENSE file distributed with this
# source code.

function usage
{
  echo "Usage: "
  echo "  oclgrind [OPTIONS] COMMAND"
  echo "  oclgrind [--help | --version]"
  echo
  echo "Options:"
  echo -n "     --build-options  OPTIONS  "
  echo          "Additional options to pass to the OpenCL compiler"
  echo -n "     --check-api               "
  echo          "Reports errors on API calls"
  echo -n "     --data-races              "
  echo          "Enable data-race detection"
  echo -n "     --disable-pch             "
  echo          "Don't use precompiled headers"
  echo -n "     --dump-spir               "
  echo          "Dump SPIR to /tmp/oclgrind_*.{ll,bc}"
  echo -n "  -h --help                    "
  echo          "Display usage information"
  echo -n "     --inst-counts             "
  echo          "Output histograms of instructions executed"
  echo -n "  -i --interactive             "
  echo          "Enable interactive mode"
  echo -n "     --log            LOGFILE  "
  echo          "Redirect log/error messages to a file"
  echo -n "     --max-errors     NUM      "
  echo          "Limit the number of error/warning messages"
  echo -n "     --num-threads    NUM      "
  echo          "Set the number of worker threads to use"
  echo -n "     --pch-dir        DIR      "
  echo          "Override directory containing precompiled headers"
  echo -n "     --plugins        PLUGINS  "
  echo          "Load colon seperated list of plugin libraries"
  echo -n "  -q --quick                   "
  echo          "Only run first and last work-group"
  echo -n "     --uniform-writes          "
  echo          "Don't suppress uniform write-write data-races"
  echo -n "  -v --version                 "
  echo          "Display version information"
  echo
  echo "For more information, please visit the Oclgrind wiki page:"
  echo "-> https://github.com/jrprice/Oclgrind/wiki"
  echo
}

# Parse arguments
while [ $# -gt 0 -a "${1:0:1}" == "-" ]
do
  if [ "$1" == "--build-options" ]
  then
    shift
    export OCLGRIND_BUILD_OPTIONS="$1"
  elif [ "$1" == "--check-api" ]
  then
    export OCLGRIND_CHECK_API=1
  elif [ "$1" == "--data-races" ]
  then
    export OCLGRIND_DATA_RACES=1
  elif [ "$1" == "--disable-pch" ]
  then
    export OCLGRIND_DISABLE_PCH=1
  elif [ "$1" == "--dump-spir" ]
  then
    export OCLGRIND_DUMP_SPIR=1
  elif [ "$1" == "-h" -o "$1" == "--help" ]
  then
    usage
    exit 0
  elif [ "$1" == "--inst-counts" ]
  then
    export OCLGRIND_INST_COUNTS=1
  elif [ "$1" == "-i" -o "$1" == "--interactive" ]
  then
    export OCLGRIND_INTERACTIVE=1
  elif [ "$1" == "--log" ]
  then
    shift
    export OCLGRIND_LOG="$1"
  elif [ "$1" == "--max-errors" ]
  then
    shift
    export OCLGRIND_MAX_ERRORS="$1"
  elif [ "$1" == "--num-threads" ]
  then
    shift
    export OCLGRIND_NUM_THREADS="$1"
  elif [ "$1" == "--pch-dir" ]
  then
    shift
    export OCLGRIND_PCH_DIR="$1"
  elif [ "$1" == "--plugins" ]
  then
    shift
    export OCLGRIND_PLUGINS="$1"
  elif [ "$1" == "-q" -o "$1" == "--quick" ]
  then
    export OCLGRIND_QUICK=1
  elif [ "$1" == "--uniform-writes" ]
  then
    export OCLGRIND_UNIFORM_WRITES=1
  elif [ "$1" == "-v" -o "$1" == "--version" ]
  then
    echo
    echo "Oclgrind __VERSION__"
    echo
    echo "Copyright (c) 2013-2015"
    echo "James Price and Simon McIntosh-Smith, University of Bristol"
    echo "https://github.com/jrprice/Oclgrind"
    echo
    exit 0
  else
    echo "Unrecognized argument '$1'"
    usage
    exit 1
  fi
  shift
done

# Ensure target command supplied
if [ $# -lt 1 ]
then
  usage
  exit 1
fi

# Inject liboclgrind.{so,dylib} and run command
LIBDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/../lib"
if [ "$(uname -s)" == "Darwin" ]
then
  DYLD_LIBRARY_PATH=$LIBDIR:$DYLD_LIBRARY_PATH \
    DYLD_INSERT_LIBRARIES=$LIBDIR/liboclgrind-rt.dylib \
    DYLD_FORCE_FLAT_NAMESPACE=1 "$@"
else
  LD_LIBRARY_PATH=$LIBDIR:$LD_LIBRARY_PATH \
    LD_PRELOAD=$LIBDIR/liboclgrind-rt.so "$@"
fi
