#!/bin/bash
# info: delete cron job
# options: USER JOB
#
# The function deletes cron job.


#----------------------------------------------------------#
#                    Variable&Function                     #
#----------------------------------------------------------#

# Argument definition
user=$1
job=$2

# Includes
source $VESTA/func/main.sh
source $VESTA/conf/vesta.conf


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

check_args '2' "$#" 'USER JOB'
is_format_valid 'user' 'job'
is_system_enabled "$CRON_SYSTEM" 'CRON_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'cron' 'JOB' "$job"
is_object_unsuspended 'cron' 'JOB' "$job"


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

# Deleting job
sed -i "/JOB='$job' /d" $USER_DATA/cron.conf

# Sorting jobs by id
sort_cron_jobs

# Sync system cron with user
sync_cron_jobs


#----------------------------------------------------------#
#                       Vesta                              #
#----------------------------------------------------------#

# Decreasing cron value
decrease_user_value "$user" '$U_CRON_JOBS'

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

# Logging
log_history "deleted cron job $job"
log_event "$OK" "$ARGUMENTS"

exit
