#!/bin/bash
# info: add cron job for Let's Encrypt certificates
# options: NONE
#
# This function adds a new cron job for Let's Encrypt.

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

is_system_enabled "$CRON_SYSTEM" 'CRON_SYSTEM'

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Add cron job
cmd="bin/v-update-sys-queue letsencrypt"
check_cron=$(grep "$cmd" "/var/spool/cron/crontabs/tulioweb" 2> /dev/null)
if [ -z "$check_cron" ] && [ -n "$CRON_SYSTEM" ]; then
	sed -i -e "\$a*/5 * * * * sudo /usr/local/tulio/bin/v-update-sys-queue letsencrypt" "/var/spool/cron/crontabs/tulioweb"
fi

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

# Logging
log_event "$OK" "$ARGUMENTS"

exit
