#!/bin/bash
# -*- mode: sh -*-

function show_help() {
    cat <<EOF
Usage: intel_aubdump [OPTION]... [--] COMMAND ARGUMENTS

Run COMMAND with ARGUMENTS and dump an AUB file that captures buffer
contents and execution of the GEM application.

  -o, --output=FILE  Name of AUB file. Defaults to COMMAND.aub

      --device=ID    Override PCI ID of the reported device

  -v                 Enable verbose output

      --help         Display this help message and exit

EOF

    exit 0
}

verbose=0
device=0

while true; do
      case "$1" in
	  -o)
	      file=$2
	      shift 2
	      ;;
	  -v)
	      verbose=1
	      shift 1
	      ;;
	  -o*)
	      file=${1##-o}
	      shift
	      ;;
	  --output=*)
	      file=${1##--output=}
	      shift
	      ;;
	  --device=*)
	      device=${1##--device=}
	      shift
	      ;;
	  --help)
	      show_help
	      ;;
	  --)
	      shift
	      break
	      ;;
	  -*)
	      echo "intel_aubdump: invalid option: $1"
	      echo
	      show_help
	      ;;
	  *)
	      break
	      ;;
      esac
done

[ -z $1 ] && show_help

file=${file:-$(basename $1).aub}

prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/i386-linux-gnu

LD_PRELOAD=${libdir}/intel_aubdump.so${LD_PPRELOAD:+:${LD_PRELOAD}} \
	  INTEL_AUBDUMP_ARGS="verbose=$verbose;file=$file;device=$device" \
	  exec -- "$@"
