#!/bin/bash
# info: list backup host
# options: TYPE [FORMAT]
#
# example: v-list-backup-host local
#
# This function for obtaining the list of backup host parameters.

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

# Argument definition
type=$1
format=${2-shell}

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

# JSON list function
json_list() {
	case $type in
		'sftp' | 'ftp' | 'rclone')
			echo '{'
			echo '    "'$type'": {
        "HOST": "'$HOST'",
        "USERNAME": "'$USERNAME'",
        "PORT": "'$PORT'",
        "TYPE": "'$type'",
        "BPATH": "'$BPATH'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
			echo '}'
			;;
		'b2')
			echo '{'
			echo '    "'$type'": {
         "BUCKET": "'$BUCKET'",
         "TYPE": "'$type'",
         "B2_KEY_ID": "'$B2_KEYID'",
         "TIME": "'$TIME'",
         "DATE": "'$DATE'"
     }'
			echo '}'
			;;
	esac
}

# SHELL list function
shell_list() {
	case $type in
		'sftp' | 'ftp' | 'rclone')
			echo "HOST:           $HOST"
			echo "USERNAME:       $USERNAME"
			echo "PORT:           $PORT"
			echo "TYPE:           $type"
			echo "PATH:           $BPATH"
			echo "TIME:           $TIME"
			echo "DATE:           $DATE"
			;;
		'b2')
			echo "BUCKET          $BUCKET"
			echo "TYPE            $type"
			echo "B2_KEY_ID       $B2_KEYID"
			echo "TIME:           $TIME"
			echo "DATE:           $DATE"
			;;
	esac
}

# PLAIN list function
plain_list() {
	case $type in
		'sftp' | 'ftp' | 'rclone')
			echo -e "$HOST\t$USERNAME\t$PORT\t$type\t$BPATH\t$TIME\t$DATE"
			;;
		'b2')
			echo -e "$BUCKET\t$type\t$B2_KEYID\t$TIME\t$DATE"
			;;
	esac
}

# CSV list function
csv_list() {
	case $type in
		'sftp' | 'ftp' | 'rclone')
			echo "HOST,USERNAME,PORT,TYPE,PATH,TIME,DATE"
			echo "$HOST,$USERNAME,$PORT,$type,$BPATH,$TIME,$DATE"
			;;
		'b2')
			echo "BUCKET,TYPE,B2_KEYID,TIME,DATE"
			echo "$BUCKET,$type,$B2_KEYID,$TIME,$DATE"
			;;
	esac
}

# Type format validator
is_type_format_valid() {
	exclude="[!|#|$|^|&|(|)|+|=|{|}|:|@|<|>|?|/|\|\"|'|;|%|\`| ]|\."
	if [[ "$1" =~ $exclude ]]; then
		check_result "$E_INVALID" "invalid type extention format :: $1"
	fi
}

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

check_args '1' "$#" 'TYPE [FORMAT]'
is_common_format_valid $type "type"

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

# Checking backup config
if [ ! -e "$HESTIA/conf/$type.backup.conf" ]; then
	exit
fi

# Parsing backup config
source_conf "$HESTIA/conf/$type.backup.conf"

# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list | column -t ;;
esac

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

exit
