#!/bin/bash
# info: rebuild cron jobs
# options: USER [RESTART]
#
# The function rebuilds system cron config file for specified user.


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

# Argument definition
user=$1
restart=$2

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


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

check_args '1' "$#" 'USER [RESTART]'
validate_format 'user'
is_system_enabled "$CRON_SYSTEM" 'CRON_SYSTEM'
is_object_valid 'user' 'USER' "$user"


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

# Rebuild cron jobs
sync_cron_jobs


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

# Restarting crond
if [ "$restart" != 'no' ]; then
    $BIN/v-restart-cron
    check_result $? "Cron restart failed" >/dev/null
fi

# Logging
log_event "$OK" "$EVENT"

exit
