#!/bin/bash

# Copyright (C) 2007-2023 X2Go Project - https://wiki.x2go.org
# Copyright (C) 2011-2023 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>
# Copyright (C) 2011-2023 Heinz-Markus Graesing <heinz-m.graesing@obviously-nice.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

X2GO_LIB_PATH="$(x2gopath 'libexec')"

"${X2GO_LIB_PATH}/x2gosyslog" "${0}" 'info' "$(basename "${0}") called with options: ${@}"

X2GO_VERSIONS_BASEPATH="$(dirname "${0}")/../share/x2go/versions"

get_version() {
	grep -Ev "^#.*" "${1}" | head -n '1' | cut -d ' ' -f '1'
}

if [ -n "${1}" ]; then
	X2GO_COMPONENT="${1}"
	X2GO_COMPONENT_VERFILE="${X2GO_VERSIONS_BASEPATH}/VERSION.${X2GO_COMPONENT}"

	if [ -f "${X2GO_COMPONENT_VERFILE}" ]; then
		echo "$(get_version "${X2GO_COMPONENT_VERFILE}")"
	else
		echo "Version information for X2Go component '${X2GO_COMPONENT}' is not available." >&1
		exit '1'
	fi
else
	cd "${X2GO_VERSIONS_BASEPATH}"
	for compfile in "${X2GO_VERSIONS_BASEPATH}"/*; do
		X2GO_COMPONENT="$(echo "$(basename ${compfile})" | cut -d '.' -f '2')"
		X2GO_COMPONENT_VERSION="$(get_version "${compfile}")"
		echo "${X2GO_COMPONENT}: ${X2GO_COMPONENT_VERSION}"
	done
	cd '-' >'/dev/null'
fi

exit '0'
