#!/bin/sh

trap exit TERM

if [ -n "${CERTBOT_DOMAINS}" ]; then
    echo "registering..."
    if ! certbot show_account; then
        certbot register -n \
            --agree-tos \
            -m "${CERTBOT_EMAIL}"
    fi

    for d in $(echo "${CERTBOT_DOMAINS}" | sed 's/,/ /g'); do
        echo "requesting for $d..."
        certbot --nginx -n --keep -d "$d"
    done

    while :; do
        echo "renewing domains..."
        certbot --nginx --keep -n renew
        sleep 12h &
        wait $!
    done &
else
    echo "skipping certbot due to no domains!"
fi &

exec "$@"