#!/bin/sh

if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

PATH=$PATH:%%LOCALBASE%%/bin:%%LOCALBASE%%/sbin
export PATH

case "$weekly_lego_enable" in
    [Yy][Ee][Ss])
	echo
	echo "Checking Let's Encrypt certificate status:"

	if [ -x "$weekly_lego_renewscript" ] ; then
		echo "$weekly_lego_renewscript" | su -fm %%LEGO_USER%% || exit 3
	fi

	if [ -n "$weekly_lego_deployscript" ] ; then
		if [ -x "$weekly_lego_deployscript" ] ; then
			echo "Deploying Let's Encrypt certificates:"
			$weekly_lego_deployscript || exit 3
		else
			echo 'Skipped, deploy script does not exist or is not executable'
		fi
	fi
	;;
    *)
        ;;
esac
