#!/bin/bash
# info: list web domains
# options: USER [FORMAT]
#
# example: v-list-web-domains alice
#
# This function to obtain the list of all user web domains.

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

# Argument definition
user=$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() {
	IFS=$'\n'
	i=1
	objects=$(grep DOMAIN $USER_DATA/web.conf | wc -l)
	echo "{"
	while read str; do
		parse_object_kv_list "$str"
		# Set correct document root path
		if [ -n "$CUSTOM_DOCROOT" ]; then
			DOCROOT="$CUSTOM_DOCROOT"
		else
			DOCROOT="$HOMEDIR/$user/web/$DOMAIN/public_html/"
		fi
		echo -n '    "'$DOMAIN'": {
        "IP": "'$IP'",
        "IP6": "'$IP6'",
        "DOCUMENT_ROOT": "'$DOCROOT'",
        "U_DISK": "'$U_DISK'",
        "U_BANDWIDTH": "'$U_BANDWIDTH'",
        "TPL": "'$TPL'",
        "ALIAS": "'$ALIAS'",
        "STATS": "'$STATS'",
        "STATS_USER": "'$STATS_USER'",
        "SSL": "'$SSL'",
        "SSL_HOME": "'$SSL_HOME'",
        "LETSENCRYPT": "'$LETSENCRYPT'",
        "FTP_USER": "'$FTP_USER'",
        "FTP_PATH": "'$FTP_PATH'",
        "AUTH_USER": "'$AUTH_USER'",
        "BACKEND": "'$BACKEND'",
        "PROXY": "'$PROXY'",
        "PROXY_EXT": "'$PROXY_EXT'",
        "SUSPENDED": "'$SUSPENDED'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done < <(cat $USER_DATA/web.conf)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "DOMAIN   IP   TPL   SSL   DISK   BW   SPND   DATE"
	echo "------   --   ---   ---   ----   --   ----   -----"
	while read str; do
		parse_object_kv_list "$str"
		echo "$DOMAIN $IP $TPL $SSL $U_DISK $U_BANDWIDTH $SUSPENDED $DATE"
	done < <(cat $USER_DATA/web.conf)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		parse_object_kv_list "$str"
		# Set correct document root path
		if [ ! -z "$CUSTOM_DOCROOT" ]; then
			DOCROOT="$CUSTOM_DOCROOT"
		else
			DOCROOT="$HOMEDIR/$user/web/$DOMAIN/public_html/"
		fi
		echo -ne "$DOMAIN\t$IP\t$IP6\t$DOCROOT\t$U_DISK\t$U_BANDWIDTH\t$TPL\t"
		echo -ne "$ALIAS\t$STATS\t$STATS_USER\t$SSL\t$SSL_HOME\t$LETSENCRYPT\t"
		echo -ne "$FTP_USER\t$FTP_PATH\t$AUTH_USER\t$BACKEND\t$PROXY\t"
		echo -e "$PROXY_EXT\t$SUSPENDED\t$TIME\t$DATE"
	done < <(cat $USER_DATA/web.conf)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo -n "DOMAIN,IP,IP6,DOCROOT,U_DISK,U_BANDWIDTH,TPL,ALIAS,STATS,STATS_USER,"
	echo -n "SSL,SSL_HOME,LETSENCRYPT,FTP_USER,FTP_PATH,AUTH_USER,BACKEND,PROXY,"
	echo "PROXY_EXT,SUSPENDED,TIME,DATE"
	while read str; do
		parse_object_kv_list "$str"
		# Set correct document root path
		if [ ! -z "$CUSTOM_DOCROOT" ]; then
			DOCROOT="$CUSTOM_DOCROOT"
		else
			DOCROOT="$HOMEDIR/$user/web/$DOMAIN/public_html/"
		fi
		echo -n "$DOMAIN,$IP,$IP6,$DOCROOT,$U_DISK,$U_BANDWIDTH,$TPL,"
		echo -n "\"$ALIAS\",$STATS,\"$STATS_USER\",$SSL,$SSL_HOME,$LETSENCRYPT,"
		echo -n "\"$FTP_USER\",\"$FTP_PATH\",\"$AUTH_USER\",$BACKEND,$PROXY,"
		echo "\"$PROXY_EXT\",$SUSPENDED,$TIME,$DATE"
	done < <(cat $USER_DATA/web.conf)
}

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

check_args '1' "$#" 'USER [FORMAT]'
is_format_valid 'user'
is_object_valid 'user' 'USER' "$user"

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

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

unset docroot

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

exit
