#!/bin/sh
# Puts the first regular person of the system in the group of the people who develop on it.
#
# It runs when the package is installed and at the first boot after that: an installer creates the person when the system
# starts for the first time (cloud-init), so at install time there may be nobody to add yet. Once it has added somebody it
# does not run again, so a person that was taken out of the group on purpose stays out.
set -eu

MARKER=/var/lib/totaljs-developer/first-user

getent group totaljs-developer >/dev/null || groupadd --system totaljs-developer

[ ! -e "${MARKER}" ] || exit 0

FIRST="$(getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 { print $1; exit }')"
[ -n "${FIRST}" ] || exit 0

usermod -aG totaljs-developer "${FIRST}"
# the person works with Docker: the group `docker` can start containers (and so it is as powerful as root; that is what a developer machine is)
# In Ubuntu the package of Docker makes the group `docker` before this runs; in Arch it is made at the end of the transaction (sysusers), after the scripts of the packages:
# it is made here if it is not there, and the later one finds it.
getent group docker >/dev/null || groupadd --system docker
usermod -aG docker "${FIRST}"
mkdir -p "$(dirname "${MARKER}")"
printf '%s\n' "${FIRST}" > "${MARKER}"
