#!/bin/bash
# info: List firewall ipset
# options: [FORMAT]
#
# example: v-list-firewall-ipset json
#
# This function prints defined ipset lists

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

# Argument definition
format=${1-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
# shellcheck source=/usr/local/hestia/conf/hestia.conf
source $HESTIA/conf/hestia.conf
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

is_system_enabled "$FIREWALL_SYSTEM" 'FIREWALL_SYSTEM'

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

# JSON list function
json_list() {
	IFS=$'\n'
	i=1
	objects=$(grep LISTNAME $HESTIA/data/firewall/ipset.conf | wc -l)
	echo "{"
	while read str; do
		[[ -z "$str" ]] && continue
		parse_object_kv_list "$str"
		echo -n '    "'$LISTNAME'": {
        "IP_VERSION": "'$IP_VERSION'",
        "AUTOUPDATE": "'$AUTOUPDATE'",
        "SUSPENDED": "'$SUSPENDED'",
        "SOURCE": "'$SOURCE'",
        "TIME": "'$TIME'",
        "DATE": "'$DATE'"
    }'
		[[ "$i" -lt "$objects" ]] && echo ',' || echo
		((i++))
	done < <(cat $HESTIA/data/firewall/ipset.conf)
	echo '}'
}

# SHELL list function
shell_list() {
	IFS=$'\n'
	echo "LISTNAME^IP_VERSION^AUTOUPDATE^SUSPENDED^SOURCE^TIME^DATE"
	echo "----^------^-----^----^--^----^----"
	while read str; do
		[[ -z "$str" ]] && continue
		parse_object_kv_list "$str"
		echo "$LISTNAME^$IP_VERSION^$AUTOUPDATE^$SUSPENDED^$SOURCE^$TIME^$DATE"
	done < <(cat $HESTIA/data/firewall/ipset.conf)
}

# PLAIN list function
plain_list() {
	IFS=$'\n'
	while read str; do
		[[ -z "$str" ]] && continue
		parse_object_kv_list "$str"
		echo -ne "$LISTNAME\t$IP_VERSION\t$AUTOUPDATE\t$SUSPENDED\t$SOURCE\t"
		echo -e "$TIME\t$DATE"
	done < <(cat $HESTIA/data/firewall/ipset.conf)
}

# CSV list function
csv_list() {
	IFS=$'\n'
	echo "LISTNAME,IP_VERSION,AUTOUPDATE,SUSPENDED,SOURCE,SUSPENDED,TIME,DATE"
	while read str; do
		[[ -z "$str" ]] && continue
		parse_object_kv_list "$str"
		echo -n "$LISTNAME,$IP_VERSION,$AUTOUPDATE,$SUSPENDED,\"$SOURCE\","
		echo "$TIME,$DATE"
	done < <(cat $HESTIA/data/firewall/ipset.conf)
}

[ ! -f "$HESTIA/data/firewall/ipset.conf" ] && touch "$HESTIA/data/firewall/ipset.conf"

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

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

exit
