#!/bin/sh
#
# Copyright (c) 1994,1996 Pure Atria Corp.
#
# This file contains proprietary and confidential information and
# remains the unpublished property of Pure Atria. Use, disclosure,
# or reproduction is prohibited except as permitted by express written
# license agreement with Pure Atria.
#
# This script is intended to be called by a C program wrapper,
# with some special arguments, followed by the compiler and its arguments.
# The program wrapper does Other Important Things, so it shouldn't be
# bypassed, no matter what you do to this script itself.


# -----------------------------------------------------------------------------

# protect ourselves from the user's path (save original in $userpath)

userpath=$PATH
PATH=/usr/bin:$PATH
export PATH

IFS=' 	
'
PV_REAL_ARGV0=`dirname $0`/rld/ld
export IFS PV_REAL_ARGV0

# -----------------------------------------------------------------------------

# extract the arguments
# examples for each argument are shown; these particular examples
# would never work together.

# the name of this driver (without .sh), ex.: "purify"
driver_name=$1;         shift;

# the name of the product, ex.: "Purify"
pretty_name=$1;         shift;

# the uname, ex.: "SunOS 4.1.1"
os_version=$1;		shift;

# the directory this product is installed in, ex.: "/usr/local/purify"
install_dir=$1;		shift;

# the dir with a fake ld, ex.: "/usr/local/purify/nld"
fake_linker_dir=$1;     shift;

# if collect is in use, a perhaps different dir to use instead of
# fake_linker_dir, ex.: "/usr/local/purelink/ild".
fake_collector_dir=$1;  shift;

# the program to use as the the real linker, ex.: "/usr/local/purelink/ald"
real_linker=$1;	        shift;

# the program which is the real collector, ex.: "/usr/local/gnu/collect"
real_collector=$1;	shift;

# some special option to handle and then exit; ignore rest of line
preemptive_option=$1;   shift;

# whether to trace what doing or not, either "1" or ""
is_trace=$1;            shift;

# where the compiler was found
compiler_path=$1;	shift;

# base name of the compiler, w/o directory
compiler_basename=$1;	shift;

# the name of the executable to be purify'd
executable_name=$1;	shift;


# the next arguments are reserved for future use
# the compiler and its arguments follow these.
dummy_b=$1;		shift;

# -----------------------------------------------------------------------------

# maybe trace
if test -n "$is_trace"; then
  echo "driver_name=${driver_name}"
  echo "pretty_name=${pretty_name}"
  echo "os_version=${os_version}"
  echo "install_dir=${install_dir}"
  echo "fake_linker_dir=${fake_linker_dir}"
  echo "fake_collector_dir=${fake_collector_dir}"
  echo "real_linker=${real_linker}"
  echo "real_collector=${real_collector}"
  echo "preemptive_option=${preemptive_option}"
  echo "is_trace=${is_trace}"
  echo "compiler_path=$compiler_path"
  echo "compiler_basename=$compiler_basename"
# echo "dummy_a=${dummy_a}"
  echo "executable_name=$executable_name"
  echo "compilername=${1}"
  echo "compilerplusargs=$@"
  echo "PURIFYOPTIONS=${PURIFYOPTIONS}"
  echo "PUREBUILDOPTIONS=${PUREBUILDOPTIONS}"
  echo "PURECOMMANDOPTIONS=${PURECOMMANDOPTIONS}"
  echo "QUANTIFYOPTIONS=${QUANTIFYOPTIONS}"
  echo "PURELINKOPTIONS=${PURELINKOPTIONS}"
  echo "PVOPTIONS=${PVOPTIONS}"
  echo "PVOPTIONS_PVENGINE=${PVOPTIONS_PVENGINE}"
  echo "PVOPTIONS_PVWATCH=${PVOPTIONS_PVWATCH}"
  echo "PUREVISION_HOME=${PUREVISION_HOME}"
  echo "PURE_LINKER=${PURE_LINKER}"
  echo "PURE_COLLECTOR=${PURE_COLLECTOR}"
  echo "PURE_HOME=${PURE_HOME}"
  echo "ALD_DONT=${ALD_DONT}"
  echo "DRIVER_STDERR=${DRIVER_STDERR}"
fi

# -----------------------------------------------------------------------------

# Check version of OS.
case $os_version in
  IRIX\ 5.*|IRIX*\ 6.[234]*)
	;;

  IRIX*)
	echo "$pretty_name Warning: this version of $driver_name is only verified under"
	echo "    IRIX 5.2, 5.3, 6.2, 6.3, or 6.4."
	echo "You are apparently running on $os_version."
	;;

  *)
	echo "$pretty_name: this version of $driver_name only works under IRIX 5.2, 5.3, 6.2, 6.3, or 6.4."
	echo "You are apparently running on $os_version"
	exit 1
	;;
esac

# -----------------------------------------------------------------------------

show_usage()
{
    echo "Usage: ${driver_name} [${driver_name} options] cc foo.o ..."
    echo "  or ${driver_name} [${driver_name} options] a.out"
    echo "For an options list, type: ${driver_name} -options"
    echo "For the version ID, type:  ${driver_name} -version"
echo " Contact us in the US at:"
echo "   support@pureatria.com        or (408) 720 1600"
echo " In Europe:"
echo "   support@europe.pureatria.com or (+31) 2356 94310"
echo " In Japan:"
echo "   support@japan.pureatria.com  or (03) 3551 9370"
}

# maybe handle some preemptive option and exit
if test -n "${preemptive_option}"; then
  case $preemptive_option in
    -usage|-help)
      case $driver_name in
	pvwatch)
	PVOPTIONS_PVENGINE="-help" ${fake_linker_dir}/ld
        ;;
	*)
	show_usage
        ;;
      esac
      exit 0;
    ;;

    -options)
      case $driver_name in
	purify)
	  echo "Please see the Purify manual or man page, which list additional options."
	  echo "Some commonly used options include:"
	  echo " -version		# give the version ID of Purify."
	  echo " -printhomedir		# print installation directory."
	  echo " -cache-dir=/usr/home/my-cache # place to store Purify'd object files."
	  echo " -always-use-cache-dir	# don't store Purify'd files alongside originals."
	  echo " -windows=no		# ASCII output instead of Purify Viewer."
	  echo " -verbose		# obtain a trace of Purify's build-time behavior."
	  echo "To view saved .pv files use:"
	  echo " purify -view {files.pv} # view saved files.pv."
	;;
	purecov)
	  echo "Please see the PureCoverage manual or man page, which list additional options."
	  echo "Some commonly used options include:"
	  echo " -version		# give the version ID of PureCoverage."
	  echo " -printhomedir		# print installation directory."
	  echo " -linker=[/bin/ld]	# specify an alternate linker to use."
	  echo " -collector=[/usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.1/ld # use with g++."
	  echo " -cache-dir=/usr/home/my-cache # place to store instrumented object files."
	  echo " -always-use-cache-dir	# don't store instrumented files alongside originals."
	  echo " -force-merge		# don't discard old data when"
	  echo "                          merging data from recompiled code."
	  echo " -verbose		# obtain a trace of PureCoverage build-time behavior."
	  echo "To view accumulated coverage data, use:"
	  echo " purecov -view {files.pcv}"
	  echo "To export coverage data to a script, use:"
	  echo " purecov -export {files.pcv}"
	  echo "To merge coverage data from separate files.pcv, use:"
	  echo " purecov -merge=result.pcv [-force-merge] {files.pcv}"
	;;
	quantify)
  	  echo "Please see the Quantify man page or the Quantify manual, which lists additional options."
  	  echo "Frequently used Quantify options:"
  	  echo " -linker			- the linker to use"
  	  echo " -collection-granularity	- the granularity of instrumentation"
  	  echo " 				  possible values: function, block, or line"
  	  echo " -use-machine			- the machine to use for instrumentation"
  	  echo " -record-data			- whether to record any data initially"
  	  echo " -record-child-process-data	- whether to record data of forked processes"
  	  echo " -record-system-calls		- whether system calls are timed"
  	  echo " -avoid-recording-system-calls	- a list of syscall numbers to avoid timing"
  	  echo " -save-data-on-signals		- whether to save data on fatal signals"
  	  echo " -run-at-exit			- a sh(1) script to run when the program exits"
  	  echo " -run-at-save			- a sh(1) script to run whenever data is saved"
  	  echo " -windows			- whether to start qv(1) in display mode"
	;;
	pvwatch)
  	  PVOPTIONS_PVENGINE="-options" ${fake_linker_dir}/ld
	;;
	purelink)
# make sure first option seen is -home
	  PURELINKOPTIONS=""; export PURELINKOPTIONS;
  	  ${fake_linker_dir}/ld -home=${install_dir} -options
	;;
	*)
	  echo "${driver_name}: unknown driver for -options"
	  exit 1;
	;;
      esac
      exit 0;
    ;;

    -version)
      case $driver_name in
	purify*|purecov*|quantify*)
  	  ${install_dir}/rtslave -version
        ;;

	purelink*)
	  PURELINKOPTIONS=""; export PURELINKOPTIONS;
  	  ${fake_linker_dir}/ld -home=${install_dir} -version
	;;
	pvwatch*)
  	  PVOPTIONS_PVENGINE="-version" ${fake_linker_dir}/ld
        ;;
	*)
	  echo "${driver_name}: unknown driver for -version"
	  exit 1;
	;;
      esac
      exit 0;
    ;;

    -test*license)
      case $driver_name in
	purify*|purecov*|quantify*)
	  PURIFYOPTIONS=""; export PURIFYOPTIONS;
	  ${install_dir}/rtslave -test-license -home=${install_dir}
	  exit $?
        ;;
        *)
	  echo "${driver_name}: unknown preemptive option /${preemptive_option}/"
	  exit 1;
	;;
      esac
      exit 0;
    ;;

    -view)
      case $driver_name in
	purify)
	  if test -x ${install_dir}/pv.pure ; then
	    exec ${install_dir}/pv.pure -viewfile= "$@" 
	  else
	    exec ${install_dir}/pv -viewfile= "$@"
	  fi
	  exit 1;
	;;
	quantify)
	  if test -x ${install_dir}/qv.pure ; then
	    exec ${install_dir}/qv.pure "$@"
	  else
	    exec ${install_dir}/qv "$@"
	  fi
	  exit 1;
	;;
	purecov)
	  if test -x ${install_dir}/pcv.pure ; then
	    exec ${install_dir}/pcv.pure $PURECOMMANDOPTIONS "$@" $PUREBUILDOPTIONS
	  else
	    exec ${install_dir}/pcv $PURECOMMANDOPTIONS "$@" $PUREBUILDOPTIONS
	  fi
	  exit 1;
	;;
        *)
	  echo "${driver_name}: unknown preemptive option /${preemptive_option}/"
	  exit 1;
	;;
      esac
      exit 0;
    ;;

    -merge*|-export*|-extract*|-optimize*)
      case "$preemptive_option" in
	-export*)     pcov_key="pcx" ;;
	-extract*)    pcov_key="pca" ;;
	-merge*)      pcov_key="pcm" ;;
	-optimize*)   pcov_key="opt" ;;
      esac
      case $driver_name in
	purecov)
	  if test -x ${install_dir}/pcmx.pure ; then
	    ${install_dir}/pcmx.pure $pcov_key $PURECOMMANDOPTIONS \
				"$preemptive_option" "$@" $PUREBUILDOPTIONS
	  else
	    ${install_dir}/pcmx $pcov_key $PURECOMMANDOPTIONS \
				"$preemptive_option" "$@" $PUREBUILDOPTIONS
	  fi
	  exit $?
	;;
        *)
	  echo "${driver_name}: unknown preemptive option /${preemptive_option}/"
	  exit 1;
	;;
      esac
      exit 0;
    ;;

    *)
      echo "${driver_name}: unknown preemptive option /${preemptive_option}/"
      exit 1;
    ;;

  esac
fi

# -----------------------------------------------------------------------------

# get compilername, and shift
# if no compiler, print usage and exit 1
compilername=$1
if test -z "$compilername"; then
  echo "Not enough arguments supplied to ${driver_name}."
  show_usage
  exit 1
fi
shift

# -----------------------------------------------------------------------------
# site-wide options may be conveniently set here.
# For example, for Purify, you could uncomment the following line:
# PURIFYOPTIONS="$PURIFYOPTIONS -cache-dir=/usr/local/my_cache"; export PURIFYOPTIONS
# Purify options are parsed left-to-right until the required option is found,
# so adding site-wide options at the end allows them to be overridden.


if [ $# -gt 0 ] ; then
  if PATH="$userpath" $compilername "$@" ; then :
  else
    echo $compilername failed. Goodbye. >&2
    exit 1
  fi
fi
if [ -n "$executable_name" ] ; then
 PURIFY_OFFSETS=${install_dir}/Check_Offsets PURE_HOME=${install_dir} \
 PURIFY_HOME=${install_dir} \
 ${install_dir}/${engine_name:-engine} "$executable_name" || exit 1
 engine_output_name=`basename "$executable_name"`.pure
 if [ "$engine_output_name" != "$executable_name".pure -a \
      "./$engine_output_name" != "$executable_name".pure -a \
	$# -gt 0 ] ; then
   mv "$engine_output_name" "$executable_name".pure
 fi
 if [ $# -gt 0 ] ; then
   mv ${executable_name}.pure ${executable_name}
 fi
fi

exit $status
