#!/bin/bash
# info: unsuspend cron job
# options: USER JOB [RESTART]
#
# example: v-unsuspend-cron-job admin 7 yes
#
# This function unsuspend certain cron job.

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

# Argument definition
user=$1
job=$2
restart="${3-yes}"

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'USER JOB [RESTART]'
is_format_valid 'user' 'job' 'restart'
is_object_valid 'user' 'USER' "$user"
is_object_valid 'cron' 'JOB' "$job"
is_object_suspended 'cron' 'JOB' "$job"

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Unsuspending job
update_object_value 'cron' 'JOB' "$job" '$SUSPENDED' 'no'
decrease_user_value "$user" '$SUSPENDED_CRON'

# Sync system cron with user
sync_cron_jobs

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Restarting crond
$BIN/v-restart-cron "$restart"
check_result $? "Cron restart failed" > /dev/null

# Logging
$BIN/v-log-action "system" "Info" "Cron Jobs" "Unsuspended cron job (User: $user, Job: $job)."
log_event "$OK" "$ARGUMENTS"

exit
