#! /usr/bin/env bash

set -eo pipefail

cd ~/

# If a CRL exists, show the last validity range
if OLD_CRL_TEXT="$(openssl crl -text -noout -in ./pki/crl.pem)"; then
	echo "Previous CRL validity:" >&2
	echo "$OLD_CRL_TEXT" | grep -e 'Last Update:' -e 'Next Update:' >&2
else
	echo "No previous CRL found." >&2
fi

# Generate a new CRL.
/usr/share/easy-rsa/easyrsa --days=3650 gen-crl

# Apply the new CRL to the apache2 config
sudo bash -c 'cp ./pki/crl.pem /etc/apache2/private-ca/; systemctl restart apache2'

# Show the new validity
echo "New CRL validity:" >&2
openssl crl -text -noout -in ./pki/crl.pem | grep -e 'Last Update:' -e 'Next Update:' >&2
