#!/bin/bash

exec 10>&1
export BASH_XTRACEFD=10
set -ex

test -c /dev/fuse || sudo mknod -m 666 /dev/fuse c 10 229
test -f /etc/mtab || sudo ln -s ../proc/self/mounts /etc/mtab

if [ -z "$AUTOPKGTEST_TMP" ]; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/mount-zip-test.XXX)
  trap 'rm -r "$AUTOPKGTEST_TMP"' EXIT
else
  # We are sure to be run from within autopkgtest, then apply a change
  # to the config that is required for the test "special-device-create":
  # We must permit 'allow_other' option, otherwise error is
  #    Filesystem output: fusermount: option allow_other only allowed if 'user_allow_other' is set in /etc/fuse.conf
  #
  sudo sed -i "/^#user_allow_other/ s/^#//" /etc/fuse.conf
fi

mkdir "$AUTOPKGTEST_TMP"/x
cp -r tests/blackbox/ "$AUTOPKGTEST_TMP"/x/blackbox
cd "$AUTOPKGTEST_TMP"
ln -s /usr/bin/fuse-zip .
cd x/blackbox/
if command -v valgrind > /dev/null; then
  exec ./fuse-zip.test -valgrind -sudo
else
  # valgrind is not available on all arch. Don't run valgrind tests when its binary is not installed.
  exec ./fuse-zip.test -sudo
fi
