#!/bin/bash

# Copyright (C) 2010-2019 by X2Go project, https://wiki.x2go.org
#       Oleksandr Shneyder <o.shneyder@phoca-gmbh.de>
#       Moritz 'Morty' Struebe <Moritz.Struebe@informatik.uni-erlangen.de>
#       Mike Gabriel <mike.gabriel@das-netzwerkteam.de>

# X2Go 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.
#
# X2Go 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.

set -e

if echo $0 | egrep "^./bin/.*$" >/dev/null; then
	ETCDIR="etc/"
elif echo $0 | egrep "^./x2gothinclient_.*$" >/dev/null; then
	ETCDIR="../etc"
else
	ETCDIR=/etc/x2go
fi

source $ETCDIR/x2gothinclient_settings

TC_BASE="${TC_BASE:-/opt/x2gothinclient}"
TC_CHROOT="${TC_CHROOT:-$TC_BASE/chroot}"
TC_CONFIG="${TC_CONFIG:-$TC_BASE/etc}"

test -e "$TC_CHROOT" || {
	echo "ERROR: X2Go Thin Client chroot does not exist at $TC_CHROOT."
	echo "Run x2gothinclient_create to create it..."
	exit -1
}

test -e "$TC_CONFIG" || {
	echo "ERROR: No X2Go Thin Client configuration found"
	echo "at $TC_CONFIG. Please check..."
	exit -1
}

[ "x$USER" == "xroot" ] || {
	echo "ERROR: X2Go Thin Client management scripts have to run"
	echo "as super-user root."
	exit -2
}

mkdir -p "$TC_CHROOT/etc/x2go"

echo "Installing X2Go TCE configuration into chroot environment..."
echo "------------------------------------------------------------"


mkdir -p "$TC_CHROOT/etc/x2go/"

ls "$TC_CONFIG/x2gothinclient_init.d/" | while read init_part; do
	mkdir -p "$TC_CHROOT/etc/x2go/x2gothinclient_init.d/"
	if test -f "$TC_CONFIG/x2gothinclient_init.d/$init_part"; then
		cp -v "$TC_CONFIG/x2gothinclient_init.d/$init_part" "$TC_CHROOT/etc/x2go/x2gothinclient_init.d/"
		chmod a+rx "$TC_CHROOT/etc/x2go/x2gothinclient_init.d/$init_part"
	fi
done

if test -f "$TC_CONFIG/x2gothinclient-${TC_FLAVOUR}_start"; then
	cp -v "$TC_CONFIG/x2gothinclient-${TC_FLAVOUR}_start" "$TC_CHROOT/etc/x2go/x2gothinclient-${TC_FLAVOUR}_start"
	chmod a+rx "$TC_CHROOT/etc/x2go/x2gothinclient-${TC_FLAVOUR}_start"
fi

if test -f "$TC_CONFIG/x2gothinclient_sessions"; then
	cp -v "$TC_CONFIG/x2gothinclient_sessions" "$TC_CHROOT/etc/x2go/x2gothinclient_sessions"
	chmod a+rx "$TC_CHROOT/etc/x2go/x2gothinclient_sessions"
fi

if test -f "$TC_CONFIG/x2gothinclient-${TC_FLAVOUR}_background.svg"; then
	cp -v "$TC_CONFIG/x2gothinclient-${TC_FLAVOUR}_background.svg" "$TC_CHROOT/etc/x2go/x2gothinclient-${TC_FLAVOUR}_background.svg"
	ln -sf "/etc/x2go/x2gothinclient-${TC_FLAVOUR}_background.svg" "$TC_CHROOT/etc/x2go/x2gothinclient-background.svg"
fi

if test -f "$TC_CONFIG/freerdp/known_hosts" || test -f "$TC_CONFIG/freerdp/known_hosts2" ; then
	mkdir -p "$TC_CHROOT/etc/x2go/freerdp/"
	cp -v "$TC_CONFIG/freerdp/known_hosts*" "$TC_CHROOT/etc/x2go/freerdp/"
fi

echo
