#!/bin/bash
# info: list system shells
# options: [FORMAT]
#
# example: v-list-sys-shells
#
# This function for obtaining the list of system shells.

#----------------------------------------------------------#
#                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
# load config file
source_conf "$HESTIA/conf/hestia.conf"

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

# SHELL list function
shell_list() {
	echo "SHELL"
	echo "-----"
	for shell in $shells; do
		echo "$shell"
	done
}

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

# CSV list function
csv_list() {
	echo "SHELL"
	for shell in $shells; do
		echo "$shell"
	done
}

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

# Defining system shells
shells=$(grep -v '#' /etc/shells | awk -F '/' '{print $NF}' | sort -u)

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

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

exit