#!/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/"
	SHAREDIR="share/"
elif echo $0 | egrep "^./x2gothinclient_.*$" >/dev/null; then
	ETCDIR="../etc"
	SHAREDIR="../share/"
else
	ETCDIR=/etc/x2go
	SHAREDIR="/usr/share/x2go/tce"
fi

source $ETCDIR/x2gothinclient_settings

TC_BASE="${TC_BASE:-/opt/x2gothinclient}"
TC_CHROOT="${TC_CHROOT:-$TC_BASE/chroot}"
TC_TFTP_FILES="${TC_TFTP_FILES:-$SHAREDIR/tftpboot}"
TC_TFTP_BOOT="${TC_TFTP_BOOT:-/srv/tftp}"

mkdir -p "$TC_TFTP_BOOT"

test -e "$TC_TFTP_BOOT/pxelinux.cfg" && {
	echo "PXE boot files already found at $TC_TFTP_BOOT."
	echo "Clear $TC_TFTP_BOOT/ and then run $(basename $0) again..."
	exit -1
}

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

echo "Preparing PXE boot configuration for X2Go TCE..."
echo "------------------------------------------------"
echo

echo "Copying X2Go's TFTP (syslinux) template files..."
cp -av "$TC_TFTP_FILES"/* "$TC_TFTP_BOOT"
echo

echo "Linking files for X2Go's TFTP (syslinux) boot environment..."
cd "$TC_TFTP_BOOT"


if [ "$TC_ARCH" == "amd64" ]
then
  KERNEL_VERSION="amd64"
fi

if [ "$TC_ARCH" == "i386" ]
then
  KERNEL_VERSION="686"
fi


# kernel image
ln -sfv "$TC_CHROOT/vmlinuz.$KERNEL_VERSION" "$TC_TFTP_BOOT/vmlinuz"
ln -sfv "$TC_CHROOT/initrd.img.$KERNEL_VERSION" "$TC_TFTP_BOOT/initrd.img"
# memtest86+
ln -sfv "$TC_CHROOT/boot/memtest86+.bin" "$TC_TFTP_BOOT/memtest86+.bin"
# syslinux components
ln -sfv "/usr/lib/PXELINUX/pxelinux.0" "$TC_TFTP_BOOT/pxelinux.0"
ln -sfv "/usr/lib/syslinux/modules/bios/vesamenu.c32" "$TC_TFTP_BOOT/vesamenu.c32"
ln -sfv "/usr/lib/syslinux/modules/bios/ldlinux.c32" "$TC_TFTP_BOOT/"
ln -sfv "/usr/lib/syslinux/modules/bios/libcom32.c32" "$TC_TFTP_BOOT/"
ln -sfv "/usr/lib/syslinux/modules/bios/libutil.c32" "$TC_TFTP_BOOT/"
cd - > /dev/null

echo
echo "DONE with X2Go PXE/Syslinux environment setup."
echo "However, 2-3 manual steps are still needed..."

echo
echo "1. Make sure your DHCP server's configuration points to:"
echo
echo "    next-server <IP-or-hostname-of-this-server>;"
echo "    filename '/pxelinux.0';"
echo
echo "2. Export the X2Go Thin Client chroot environment via NFSv3:"
echo
echo "    Add this line to /etc/exports..."
echo "    $TC_CHROOT    <your-network>/<netmask>(ro,async,no_root_squash,no_subtree_check)"
echo
echo "    ... and then restart your NFS server with:"
echo "    /etc/init.d/nfs-kernel-server restart"
echo
echo "3. Customize PXE/Syslinux files in $TC_TFTP_BOOT/ (optional):"
echo
echo "    - customize PXE boot menu, take a look at $TC_TFTP_BOOT/default.cfg"
echo "      and proceed from there on..."
echo "    - use your own boot loader background image: 640x480px, 16bit colour depth,"
echo "      file: $TC_TFTP_BOOT/x2go-tce.png"
echo
