#!/bin/sh

printf "LET'S ENCRYPT BULK RENEWAL\n%s\n\n" "$(date)"

for f in /etc/letsencrypt/configs/*.ini;
do
	printf "RENEWING CERTIFICATES FROM '%s':\n\n" "$f"

	printf "Moving old files...\n\n"
	basedir=$(basename $f | sed -e "s/\.ini$//" )

	mv /etc/letsencrypt/custom/"$basedir"/certs/cert /etc/letsencrypt/custom/"$basedir"/certs/cert.old
	mv /etc/letsencrypt/custom/"$basedir"/chains/chain /etc/letsencrypt/custom/"$basedir"/chains/chain.old
	mv /etc/letsencrypt/custom/"$basedir"/fullchains/fullchain /etc/letsencrypt/custom/"$basedir"/fullchains/fullchain.old

	printf "Running letsencrypt-auto...\n\n"

	/usr/local/src/letsencrypt/letsencrypt-auto certonly --config "$f"

	printf "\n\n";
done

printf "DONE RENEWING. PLEASE RELOAD THE RELEVANT SERVICES.\n"
