#!/bin/bash
# info: listing available PHP versions installed
# options: [FORMAT]
#
# example: v-list-sys-php
#
# List /etc/php/* version check if folder fpm is available

#----------------------------------------------------------#
#                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() {
	i=1
	objects=$(echo "${versions[@]}" | wc -w)
	echo '['
	for version in "${versions[@]}"; do
		if [ "$i" -lt "$objects" ]; then
			echo -e "\t\"$version\","
		else
			echo -e "\t\"$version\""
		fi
		((++i))
	done
	echo "]"
}

# SHELL list function
shell_list() {
	echo "VERSION"
	echo "--------"
	for version in "${versions[@]}"; do
		echo "$version"
	done
}

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

# CSV list function
csv_list() {
	echo "VERSION"
	for version in "${versions[@]}"; do
		echo "$version"
	done
}

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

declare -a versions
# List through /etc/php
if [ -n "$WEB_BACKEND" ]; then
	for version in /etc/php/*/fpm; do
		ver=$(echo "$version" | awk -F"/" '{ print $4 }')
		versions+=("$ver")
	done
else
	# Fix for modphp
	for version in /etc/php/*/cli; do
		ver=$(echo "$version" | awk -F"/" '{ print $4 }')
		versions+=("$ver")
	done
fi
# Listing data
case $format in
	json) json_list ;;
	plain) plain_list ;;
	csv) csv_list ;;
	shell) shell_list ;;
esac

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

exit
