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

CMK_INSTANCE=$(omd sites -b)
for i in $CMK_INSTANCE; do
    # su statt sudo: sudo fehlt auf einem frischen Debian, und das Paket haengt
    # nicht davon ab. "su -" laedt das Profil der Site (OMD_ROOT).
    su - "$i" -c cmk-site-bootstrap
    usermod -aG cmkpush $i
    omd stop $i
    omd start $i
    echo "Your checkmk site $i is now configured for the bashclub push agent."
    echo "Register a host (on the host, with the registration key copied there):"
    echo "  cmk-agent-register --server $(hostname -f) --site $i --folder /New --reg-key /root/cmk-reg-key \"\$(hostname -f)\""
    echo "Registration key: /var/lib/cmkpush/.ssh/id_cmk-agent-bootstrap"
    # Zugang des Registrierungs-Workers zur REST-API (nur auf diesem Server gebraucht)
    echo "REST API user of the registration worker: see /omd/sites/$i/.credentials"
done
