#!/bin/sh

test "$BUILD_USER" = "root" && exit 0 || true

test -z "$ABUILD_UID" && ABUILD_UID=399
test -z "$ABUILD_GID" && ABUILD_GID=399

grep -q "^abuild:" /etc/passwd || {
    echo "abuild:x:${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >> /etc/passwd
    echo 'abuild:*:::::::' >> /etc/shadow
    echo "abuild:x:${ABUILD_GID}:" >> /etc/group
    echo 'abuild:*::' >> /etc/gshadow
    mkdir -p /home/abuild
    chown "$ABUILD_UID:$ABUILD_GID" /home/abuild

}

if id abuild >/dev/null 2>/dev/null; then
    # add abuild to trusted auxiliary group
    if ! id -nG abuild 2> /dev/null | grep -q trusted; then
        sed -i -E -e 's|trusted:([^:]+):([^:]+):(.*)|trusted:\1:\2:abuild,\3|' /etc/group
    fi
fi
