#!/bin/sh

env

set -e

if [ "$(whoami)" = root ]; then
  if [ -n "$SUDO_USER" ] && getent passwd "$SUDO_USER" > /dev/null; then
    su_user="$SUDO_USER"
  else
    su_user=nobody
  fi

  if [ -e /etc/default/apport ]; then
    # stop apport
    service apport stop 2>/dev/null || true
    sed -i '/^enabled=/s/=.*/=0/' /etc/default/apport 2>/dev/null
  fi
fi
tmphome=$AUTOPKGTEST_TMP/home
mkdir -p $tmphome
if [ -n "$su_user" ]; then
  chmod go+rx $AUTOPKGTEST_TMP
  chown $su_user:nogroup $tmphome
fi
ls -la $AUTOPKGTEST_TMP

debian_dir=$(dirname $(dirname $0))

# no root access needed after this point

TESTPYTHON="python3.14 -W default -bb -E -R -m test"
TESTEXCLUSIONS="-x"
TESTNEVERCOMPLETE=""

. $debian_dir/tests/test-common.sh
TESTEXCLUSIONS="$TESTEXCLUSIONS $TESTNEVERCOMPLETE"

if [ "$su_user" = nobody ]; then
  log=/dev/null
else
  log=testsuite.log
fi

# several tests fail with configured proxy; we don't need internet access, so
# unset proxy variables
EXTRAENV="env -u https_proxy -u http_proxy -u no_proxy HOME=$tmphome"

script=$debian_dir/script.py
if [ -f "$script" ]; then
  cmd="$EXTRAENV python3.14 $script \"$log\" \"$TESTPYTHON $TESTOPTS $TESTEXCLUSIONS\""
else
  cmd="$EXTRAENV $TESTPYTHON $TESTOPTS $TESTEXCLUSIONS"
fi

echo "Running the python testsuite with the standard interpreter:"
if [ "$(whoami)" = root ]; then
  echo "su -s /bin/sh -c $cmd $su_user"
  su -s /bin/sh -c "$cmd" $su_user
else
  echo "$cmd"
  eval $cmd
fi
