#!/usr/bin/env bash
set -euo pipefail

if [[ ${EUID} -ne 0 ]]; then
	printf '%s\n' "seed-admin must run as root" >&2
	exit 1
fi
seeder=${1:-}
test -x "$seeder"

password=$(openssl rand -base64 24 | tr -d '/+=')
set -a
source /etc/harbor/harbor.env
set +a
runuser -u harbor -- env SEED_ADMIN_PASSWORD="$password" "$seeder" -f /etc/harbor/gateway.yaml

credentials=/etc/harbor/initial-admin-credentials
umask 077
cat > "$credentials" <<EOF
email=admin@haixun.local
password=$password
EOF
touch /etc/harbor/admin-seeded
chmod 0600 "$credentials" /etc/harbor/admin-seeded
printf '%s\n' "initial admin credentials written to $credentials"
