#!/bin/bash
# info: schedule user backup restoration
# options: USER BACKUP [WEB] [DNS] [MAIL] [DB] [CRON] [UDIR]
#
# example: v-schedule-user-restore 2019-04-22_01-00-00.tar
#
# This function for scheduling user backup restoration.

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

# Argument definition
user=$1
backup=$2
web=$3
dns=$4
mail=$5
db=$6
cron=$7
udir=$8

# 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                         #
#----------------------------------------------------------#

check_args '2' "$#" 'USER BACKUP [WEB] [DNS] [MAIL] [DB] [CRON] [UDIR]'
is_format_valid 'user' 'backup'
is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
is_object_valid 'user' 'USER' "$user"
is_backup_enabled
is_backup_scheduled 'restore'

# Perform verification if read-only mode is enabled
check_tulio_demo_mode

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

# Adding restore task to  the queue
log="$TULIO/log/restore.log"
queue_file="$TULIO/data/queue/backup.pipe"
# Store a safely serialized command line for the legacy Bash queue consumer.
printf 'nice -n 19 ionice -c 3 %q %q %q %q %q %q %q %q %q yes >> %q 2>&1\n' \
	"$BIN/v-restore-user" \
	"$user" \
	"$backup" \
	"$web" \
	"$dns" \
	"$mail" \
	"$db" \
	"$cron" \
	"$udir" \
	"$log" >> "$queue_file"

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

# Logging
$BIN/v-log-action "system" "Info" "Backup" "Restore of backup archive requested (User: $user, Archive: $backup)."
log_event "$OK" "$ARGUMENTS"

exit
