#!/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_letsencrypt_enable" in
    [Yy][Ee][Ss])
        : ${weekly_dehydrated_enable:=$weekly_letsencrypt_enable}
        : ${weekly_dehydrated_user:=$weekly_letsencrypt_user}
        : ${weekly_dehydrated_flags:=$weekly_letsencrypt_flags}
        : ${weekly_dehydrated_deployscript:=$weekly_letsencrypt_deployscript}
        ;;
    *)
        ;;
esac

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

	if [ -z "$weekly_dehydrated_user" ]
	then
		%%PREFIX%%/bin/dehydrated -c $weekly_dehydrated_flags
	else
		su -m "$weekly_dehydrated_user" -c "%%PREFIX%%/bin/dehydrated -c $weekly_dehydrated_flags"
	fi

	echo "Deploying Let's Encrypt certificates:"

	if [ -x "$weekly_dehydrated_deployscript" ]
	then
		$weekly_dehydrated_deployscript
	else
		echo 'Skipped, deploy script not set or not executable.'
	fi
        ;;
    *)
        ;;
esac
