#!/bin/sh

set -eu

if [ -e /usr/lib/$(py3versions -d)/EXTERNALLY-MANAGED ]; then
    echo "Removing EXTERNALLY-MANAGED marker to test behaviour"
    rm /usr/lib/$(py3versions -d)/EXTERNALLY-MANAGED
fi

export HOME=$AUTOPKGTEST_TMP

if [ -n "${AUTOPKGTEST_NORMAL_USER:-}" ]; then
    user=$AUTOPKGTEST_NORMAL_USER
elif [ $(id -u) = 0 ]; then
    adduser --quiet --system --group --no-create-home testing
    user=testing
else
    user=""
    runuser=""
fi

cd $HOME
mkdir stupid
cat > stupid/setup.py <<EOF
from setuptools import setup

setup(
    name='stupid',
    version='0.1',
    extras_require = {
        'testing': [],
        }
    )
EOF

if [ -n "$user" ]; then
    mkdir $HOME/.local
    chown -R $user $AUTOPKGTEST_TMP
    runuser="runuser -p -u $user --"
fi

cd stupid
$runuser python3 -m pip install -e .[testing]
