#!/bin/bash
# info: Download backup
# options: USER BACKUP
#
# example: v-download-backup admin admin.2020-11-05_05-10-21.tar
#
# This function download back-up from remote server

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

# Argument definition
user=$1
backup=$2

# Define backup dir
if [ -z "$BACKUP" ]; then
	BACKUP=/backup
fi

# 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
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# shellcheck source=/usr/local/hestia/func/ip.sh
source $HESTIA/func/ip.sh
# shellcheck source=/usr/local/hestia/func/db.sh
source $HESTIA/func/db.sh
# shellcheck source=/usr/local/hestia/func/rebuild.sh
source $HESTIA/func/rebuild.sh
# shellcheck source=/usr/local/hestia/func/backup.sh
source $HESTIA/func/backup.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

check_args '2' "$#" 'USER BACKUP'
is_format_valid 'user' 'backup'

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Checking available disk space
disk_usage=$(df $BACKUP | tail -n1 | tr ' ' '\n' | grep % | cut -f 1 -d %)
if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
	echo "Error: Not enough disk space" | $SENDMAIL -s "$subj" $email $notify
	sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
	check_result $E_DISK "Not enough disk space"
fi

# Checking local backup
if [ ! -e "$BACKUP/$backup" ]; then
	if [[ "$BACKUP_SYSTEM" =~ "google" ]]; then
		google_download "$backup"
		downloaded='yes'
	fi
	if [[ "$BACKUP_SYSTEM" =~ "sftp" ]] && [ -z "$downloaded" ]; then
		sftp_download "$backup"
		downloaded='yes'
	fi
	if [[ "$BACKUP_SYSTEM" =~ "ftp" ]] && [ -z "$downloaded" ]; then
		ftp_download "$backup"
		downloaded='yes'
	fi
	if [[ "$BACKUP_SYSTEM" =~ "rclone" ]] && [ -z "$downloaded" ]; then
		rclone_download "$backup"
		downloaded='yes'
	fi
	if [[ "$BACKUP_SYSTEM" =~ "b2" ]] && [ -z "$downloaded" ]; then
		b2_download "$backup"
		downloaded='yes'
	fi
	if [ -z "$downloaded" ]; then
		subj="Download of $backup failed for $user"
		$BIN/v-add-user-notification $user "$subj" "<p class'u-text-bold'>Unable to retrieve backup file from remote server.</p><p><span class='u-text-bold'>Error:</span> $backup file doesn't exist in <code>'${BACKUP}'</code> directory.</p>"
		sed -i "/v-download-backup $user /d" $HESTIA/data/queue/backup.pipe
		check_result "$E_NOTEXIST" "backup file $backup doesn't exist in '${BACKUP}' folder"
	else
		if [ -e "$BACKUP/$backup" ]; then
			chmod 0640 $BACKUP/$backup
			chown admin:admin $BACKUP/$backup
			echo "rm $BACKUP/$backup" | at now + 1 day
		fi
	fi
fi

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

# Send notification
if [ -e "$BACKUP/$backup" ]; then
	cd $BACKUP
	subj="Download of $backup completed for $user"
	email=$(get_user_value '$CONTACT')
	echo "Backup file $backup was retrieved from the remote server and will be available to download for 12 hours." | $SENDMAIL -s "$subj" "$email" "$notify"
	$BIN/v-add-user-notification "$user" "$subj" "<p>Backup file <code>$backup</code> was retrieved from the remote server and will be available to download for 12 hours.</p>"
fi

# Cleaning restore queue
sed -i "/v-download-backup $user /d" $HESTIA/data/queue/backup.pipe

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

exit
