#!/bin/bash
# info: list system themes
# options: [FORMAT]
#
# example: v-list-sys-themes
#
# This function for obtaining the list of themes in the theme
# library and displaying them in the backend or user interface.

#----------------------------------------------------------#
#                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"

# Define array for available themes
available_themes=()

# Function to process CSS theme files
get_themes() {
	# Retrieve list of system themes
	if [ -d "$HESTIA_THEMES" ]; then
		for file in "$HESTIA_THEMES"/*.min.css; do
			filename=$(basename "$file" .min.css)
			available_themes+=("${filename%.*}")
		done
	fi

	# Check for existence of custom themes folder and iterate through items
	if [ -d "$HESTIA_THEMES_CUSTOM" ] && [ "$(ls -A "$HESTIA_THEMES_CUSTOM")" ]; then
		for file in "$HESTIA_THEMES_CUSTOM"/*.css; do
			filename=$(basename "$file" .css)
			available_themes+=("${filename%.*}")
		done
	fi

	# Sort theme list alphabetically
	IFS=$'\n' available_themes=($(sort <<< "${available_themes[*]}"))
	unset IFS

	# Get count of themes (for proper JSON formatting)
	theme_count="${#available_themes[@]}"
}

# JSON list function
json_list() {
	i=1
	echo '['
	for theme in "${available_themes[@]}"; do
		if [ "$i" -lt "$theme_count" ]; then
			echo -e "\t\"$theme\","
		else
			echo -e "\t\"$theme\""
		fi
		((++i))
	done
	echo ']'
}

# SHELL list function
shell_list() {
	echo "THEME"
	echo "-----"
	for theme in "${available_themes[@]}"; do
		echo "$theme"
	done
}

# PLAIN list function
plain_list() {
	for theme in "${available_themes[@]}"; do
		echo "$theme"
	done
}

# CSV list function
csv_list() {
	echo "THEME"
	for theme in "${available_themes[@]}"; do
		echo "$theme"
	done
}

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

get_themes

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

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

exit
