#!/usr/bin/env bash
# Some tests fail when run as root. And if we use 'docker run --user', this
# user doesn't have an entry in /etc/passwd, also causing some tests to fail.
# We work around this by starting as root, creating a user matching the host's
# UID:GID, then dropping down to that user

if [ "$(id -u)" -eq 0 ]; then
    USERNAME=${USERNAME:-ci}
    groupadd -g $USER_GID $USERNAME
    useradd -m -u $USER_UID -g $USERNAME -s /bin/bash $USERNAME
    exec setpriv --reuid=$USERNAME --regid=$USERNAME --clear-groups "$0" "$@"
fi

exec "$@"
