#!/bin/sh
# @configure_input@
#
# Wrapper script for running xapian-progsrv and xapian-tcpsrv from test
# programs (using libtool and valgrind if required).
#
# We use this wrapper script so that we can run the remote servers under
# valgrind if required.  This is needed on x86 when the testsuite is using
# valgrind to avoid issues due to valgrind not supporting x87 excess precision.
#
# We don't actually need valgrind's checking here, so we use "--tool=none"
# which is much quicker than using valgrind's default memcheck tool.
#
# Copyright (C) 2003,2004,2007,2009,2012,2019 Olly Betts
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see
# <https://www.gnu.org/licenses/>.

if test 0 = "$#"; then
  echo "syntax: $0 <remote backend server program> <arguments>"
  exit 1
fi

# We used to only do this on x86 when also building the library without SSE
# instructions (which works around valgrind implementing 80 bit FP using
# 64 bit FP instructions), but the addition of multi_glass_remoteprog_glass
# resulted in 3 failing testcases under valgrind on x86 even when built with
# -msse2 - presumably there's some libc function using x87 FP.
case "@host_cpu@" in
  i*86) ;;
  *)
    # Just run the server directly for other architectures.
    exec "$@" ;;
esac

# Set srcdir if it isn't already.
: ${srcdir="@srcdir@"}
export srcdir

# We're only run from BackendManager if it's running under valgrind, so if
# we can't find valgrind, something is wrong.
test -z "${VALGRIND+set}" && VALGRIND="@VALGRIND@"
if test -z "$VALGRIND" ; then
    echo "VALGRIND not set"
    exit 1
fi

# Get libtool to run the test program under valgrind.  We have to use
# libtool --mode=execute because we want to run valgrind on the compiled
# C++ test program, not on the shell running the shell script wrapper libtool
# generates.
LIBTOOL=`echo '@LIBTOOL@'|sed 's,\$(SHELL),@SHELL@,g;s,\$(top_builddir),@top_builddir@,g'`

vg_opts='-q --tool=none'
exec $LIBTOOL --mode=execute $VALGRIND $vg_opts "$@"
