#!/bin/bash

# Copyright (C) 2007-2023 X2Go Project - https://wiki.x2go.org
# Copyright (C) 2012-2023 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>
# Copyright (C) 2012-2023 Heinz-Markus Graesing <heinz-m.graesing@obviously-nice.de>
# Copyright (C) 2012-2023 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
# Copyright (C) 2012-2015 Jan Engelhard <jengelh@inai.de>
# Copyright (C) 2019-2023 Mihai Moldovan <ionic@ionic.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.

typeset installed_prefix='/usr'
typeset installed_libdir='/usr/lib64/x2go'
typeset installed_nxlibdir='/usr/lib64/nx'
typeset installed_sharedir='/usr/share/x2go'

typeset base="${BASH_SOURCE[0]%/*}"
if [ "${base}" = "${BASH_SOURCE[0]}" ]; then
	base='.'
fi

base="$(readlink -f "${base}/..")"

# Try to sanitize install locations.
typeset installed_prefix_sanitized="$(readlink -e "${installed_prefix}")"
typeset installed_libdir_sanitized="$(readlink -e "${installed_libdir}")"
typeset installed_nxlibdir_sanitized="$(readlink -e "${installed_nxlibdir}")"
typeset installed_sharedir_sanitized="$(readlink -e "${installed_sharedir}")"
typeset -a desc_arr
typeset -a val_arr
desc_arr=( 'prefix' 'library path' 'nx path' 'share path' )
val_arr=( 'prefix' 'libdir' 'nxlibdir' 'sharedir' )

typeset -i i='0'
for i in "${!desc_arr[@]}"; do
	typeset val_indirect="installed_${val_arr[i]}"
	typeset sanitized_val_indirect="${val_indirect}_sanitized"
	if [ -z "${!sanitized_val_indirect}" ]; then
		printf 'WARNING: X2Go Server was originally installed with %s %s, but this path does not on this machine. There is potential for breakage.\n' "${!desc_arr[i]}" "${!val_indirect}"
	else
		typeset "${val_indirect}=${!sanitized_val_indirect}"
	fi
done

if [ "${base}" != "${installed_prefix}" ]; then
	printf 'WARNING: X2Go Server was originally installed into %s, but seems to have been re-rooted to %s. There is potential for breakage.\n' "${installed_prefix}" "${base}" >&2
fi

typeset x2go_share_subdir="${installed_sharedir#${installed_prefix}}"
typeset x2go_lib_subdir="${installed_libdir#${installed_prefix}}"
typeset x2go_nxlib_subdir="${installed_nxlibdir#${installed_prefix}}"

if [ "${x2go_share_subdir}" = "${installed_sharedir}" ] || [ "${x2go_lib_subdir}" = "${installed_libdir}" ] || [ "${x2go_nxlib_subdir}" = "${installed_nxlibdir}" ]; then
	printf 'WARNING: X2Go Server was built with a SHAREDIR (%s), LIBDIR (%s) or NXLIBDIR (%s) not starting with PREFIX (%s). Such a setup will likely not work correctly.\n' "${installed_sharedir}" "${installed_libdir}" "${installed_nxlibdir}" "${installed_prefix}" >&2
fi

# Hopefully helpful diagnostic messages.
typeset x2go_libpath="${base}${x2go_lib_subdir}"
typeset x2go_sharepath="${base}${x2go_share_subdir}"
typeset x2go_nxx11path="${base}${x2go_nxlib_subdir}/X11"
typeset x2go_xineramapath="${x2go_nxx11path}/Xinerama"
desc_arr=( 'library path' 'share path' 'nx-X11 path' )
val_arr=( 'x2go_libpath' 'x2go_sharepath' 'x2go_nxx11path' )

typeset -i i='0'
for i in "${!desc_arr[@]}"; do
	if [ ! -e "${!val_arr[i]}" ]; then
		printf 'WARNING: determined X2Go %s (%s) does not exist. Such a setup will likely not work correctly.\n' "${desc_arr[i]}" "${!val_arr[i]}"
	elif [ ! -d "${!val_arr[i]}" ]; then
		printf 'WARNING: determined X2Go %s (%s) exists, but is not a directory. Such a setup will likely not work correctly.\n' "${desc_arr[i]}" "${!val_arr[i]}"
	fi
done

# Distributions should not modify the following section, but rather build the package with their custom PREFIX, LIBDIR, SHAREDIR etc. values.
case "${1}" in
	('base') printf '%s' "${base}";;
	('lib'|'libexec') printf '%s' "${x2go_libpath}";;
	('share') printf '%s' "${x2go_sharepath}";;
	('nx-x11') printf '%s' "${x2go_nxx11path}";;
	('xinerama') printf '%s' "${x2go_xineramapath}";;
	(*) exit '1';;
esac;

if [ -t '1' ]; then
	printf '\n'
fi
