#!/bin/bash
# info: list sshd port
# options: [FORMAT]
#
# example: v-list-sys-sshd-port
#
# This function for obtainings the port of sshd listens to

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

# Argument definition
format=${1-shell}

# Includes
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh

json_list() {
	sh_counter=$(echo "$ports" | wc -l)
	i=1
	echo '['
	for port in $ports; do
		if [ "$i" -lt "$sh_counter" ]; then
			echo -e "\t\"$port\","
		else
			echo -e "\t\"$port\""
		fi
		((++i))
	done
	echo "]"
}

# SHELL list function
shell_list() {
	echo "PORT"
	echo "-----"
	for port in $ports; do
		echo "$port"
	done
}

# PLAIN list function
plain_list() {
	for port in $ports; do
		echo "$port"
	done
}

# CSV list function
csv_list() {
	echo "PORT"
	for port in $ports; do
		echo "$port"
	done
}

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

version=$(lsb_release -s -r)

if [[ "$version" = 9 || "$version" = 10 ]]; then
	ports=$(sshd -T -C "user=root" | grep '^port' | cut -d ' ' -f2)
else
	ports=$(sshd -T | grep '^port' | cut -d ' ' -f2)
fi

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

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

exit
