#!/bin/sh
set -e

# The location your renewal tool places your certificates.
CERT_DIR="/etc/letsencrypt/live/irc.secretbee.buzz"

# The location of the InspIRCd config directory.
INSPIRCD_CONFIG_DIR="/etc/inspircd"

# The location of the InspIRCd pid file.
INSPIRCD_PID_FILE="/var/run/inspircd/inspircd.pid"

# The user:group that owns the inspircd config directory on the host.
INSPIRCD_OWNER="root:root"

if [ -e ${CERT_DIR} -a -e ${INSPIRCD_CONFIG_DIR} ]
then
    cp "${CERT_DIR}/fullchain.pem" "${INSPIRCD_CONFIG_DIR}/cert.pem"
    cp "${CERT_DIR}/privkey.pem" "${INSPIRCD_CONFIG_DIR}/key.pem"
    chown ${INSPIRCD_OWNER} "${INSPIRCD_CONFIG_DIR}/cert.pem" "${INSPIRCD_CONFIG_DIR}/key.pem"

    if podman container exists inspircd 2>/dev/null
    then
        podman kill --signal USR1 inspircd
    elif [ -r ${INSPIRCD_PID_FILE} ]
    then
        kill -USR1 $(cat ${INSPIRCD_PID_FILE})
    elif [ -d /lib/systemd ] && systemctl --quiet is-active inspircd
    then
        systemctl kill --signal USR1 inspircd
    fi
fi