#!/bin/sh
# The root start, the same in every base that starts as root (a unit test keeps the copies identical), run by the base's
# init as root at the computer's start: it sets the time zone a deploy passes as AGENTENV_SETTING_TIMEZONE for every
# program (docker exec's included), makes what the session must not touch (the clock page and file, the clock user's
# from their first byte; the egress proxy's CA, its key the egress user's alone, trusted everywhere), then starts root's
# supervisord (root.conf), which runs the clock follower and the egress proxy, each as its own user, and the session's
# supervisord as the session user.
set -eu
timezone="${AGENTENV_SETTING_TIMEZONE:-UTC}"
[ -f "/usr/share/zoneinfo/$timezone" ] || { echo "timezone '$timezone' isn't in /usr/share/zoneinfo" >&2; exit 2; }
[ "$(id -u)" = 0 ] || { echo "init starts as root, not uid $(id -u)" >&2; exit 2; }
export TZ="$timezone"
ln -sfn "/usr/share/zoneinfo/$timezone" /etc/localtime && echo "$timezone" > /etc/timezone
install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock
python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock
/usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \
    || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; }
exec /usr/bin/supervisord -c /etc/supervisor/root.conf
