#!/bin/bash
# info: delete backup ftp server
# options: TYPE [HOST]
#
# example: v-delete-backup-host sftp
#
# This function deletes ftp backup host

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

# Argument definition
type=$1

# 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 '1' "$#" 'TYPE [HOST]'
types=$(echo "$BACKUP_SYSTEM" | sed "s/,/\n/g" | grep "^$type$")
if [ -z "$types" ]; then
	echo "Error: invalid backup type"
	log_event "$E_INVALID" "$ARGUMENTS"
	exit "$E_INVALID"
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

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

# Deleting host config
rm -f $HESTIA/conf/$type.backup.conf

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

# Updating hestia.conf
bckp=$(echo "$BACKUP_SYSTEM" \
	| sed "s/,/\n/g" \
	| sed "s/^$type$//" \
	| sed "/^$/d" \
	| sed ':a;N;$!ba;s/\n/,/g')
sed -i "s/BACKUP_SYSTEM=.*/BACKUP_SYSTEM='$bckp'/g" $HESTIA/conf/hestia.conf

# Logging
$BIN/v-log-action "system" "Info" "Backup" "Removed remote backup host (Host: $host, Type: $type)."
log_event "$OK" "$ARGUMENTS"

exit
