#!/bin/sh

srcdir='@abs_top_srcdir@'
builddir='@abs_top_builddir@'

rundir="${0%/*}"
[ "$rundir" = "$0" ] || builddir="$rundir"

# Absolutify the paths.
case "$srcdir" in
/*) ;;
*) srcdir=`cd "$srcdir" && pwd` || exit ;;
esac
case "$builddir" in
/*) ;;
*) builddir=`cd "$builddir" && pwd` || exit ;;
esac

# If there were private elfutils libs built, use them.
if [ -d "$rundir/lib-elfutils" ]; then
  lib_elfutils="$rundir/lib-elfutils"
  elfutils_path="${lib_elfutils}:${lib_elfutils}/systemtap"
  LD_LIBRARY_PATH="${elfutils_path}${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
  export LD_LIBRARY_PATH
fi

# Passing LD_LIBRARY_PATH to sudo is a bit unusual, and possibly dangerous, but
# we'll trust that pro users with sudo access know how to keep their noses^W
# environment clean.  Ditto for PATH and HOME, which help -c CMD work nonroot.

exec sudo -P "SYSTEMTAP_STAPIO=${builddir}/stapio" \
             "SYSTEMTAP_STAPRUN=${builddir}/run-staprun" \
             "SYSTEMTAP_RUNTIME=${srcdir}/../runtime" \
             "SYSTEMTAP_REAL_UID=`id -ru`" \
             "SYSTEMTAP_REAL_GID=`id -rg`" \
             ${LD_LIBRARY_PATH:+"LD_LIBRARY_PATH=$LD_LIBRARY_PATH"} \
             ${PATH:+"PATH=$PATH"} \
             ${HOME:+"HOME=$HOME"} \
             ${builddir}/staprun ${1+"$@"}
