#!/bin/bash
# info: list supported database types
# options: [FORMAT]
#
# example: v-list-database-types json
#
# This function for obtaining the list of database types.

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

# Argument definition
format=${1-shell}

# Includes
# shellcheck source=/etc/tuliocp/tulio.conf
source /etc/tuliocp/tulio.conf
# shellcheck source=/usr/local/tulio/func/main.sh
source $TULIO/func/main.sh
# load config file
source_conf "$TULIO/conf/tulio.conf"

# JSON list function
json_list() {
	objects=$(echo "${DB_SYSTEM//,/ }" | wc -w)
	i=1
	echo '['
	for type in ${DB_SYSTEM//,/ }; do
		echo -n '    "'$type'"'
		if [ "$i" -lt "$objects" ]; then
			echo ','
		else
			echo
		fi
		((i++))
	done
	echo "]"
}

# SHELL list function
shell_list() {
	echo -e "TYPE\n----"
	echo "$DB_SYSTEM" | sed -e "s/,/\n/"
}

# PLAIN list function
plain_list() {
	for type in ${DB_SYSTEM//,/ }; do
		echo "$type"
	done
}

# CSV list function
csv_list() {
	echo "TYPE"
	for type in ${DB_SYSTEM//,/ }; do
		echo "$type"
	done
}

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

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

#----------------------------------------------------------#
#                       Tulio                             #
#----------------------------------------------------------#

exit
