#!/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_CHROOT="${TC_CHROOT:-/opt/x2gothinclient}"

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
}

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

cat > "$TC_CHROOT/x2go_tce_upgrade.sh" <<EOF
#!/bin/bash
export HOME=/root

export http_proxy=$TC_HTTP_PROXY
export https_proxy=$TC_HTTPS_PROXY
export ftp_proxy=$TC_FTP_PROXY

# migrate start-stop-daemon to wrapper & variable based start-stop-daemon execution
if [ \$(stat --format '%s' /sbin/start-stop-daemon) -gt 200 ]; then
	cp /sbin/start-stop-daemon /sbin/start-stop-daemon.real
	echo '#!/bin/sh'                                                             > /sbin/start-stop-daemon
	echo '#'                                                                     >> /sbin/start-stop-daemon
	echo '# X2Go Wrapper to avoid running daemons while performing maintenance.' >> /sbin/start-stop-daemon
	echo '#'                                                                     >> /sbin/start-stop-daemon
	echo                                                                         >> /sbin/start-stop-daemon
	echo 'if [ "\$X2GO_HANDLE_DAEMONS" != "false" ]; then'                        >> /sbin/start-stop-daemon
	echo '        /sbin/start-stop-daemon.real "\$@"'                             >> /sbin/start-stop-daemon
	echo 'fi'                                                                    >> /sbin/start-stop-daemon
	chmod a+x /sbin/start-stop-daemon
fi

export X2GO_HANDLE_DAEMONS=false
export LANG=C

mount /root

[ \$? -eq 0 ] && apt-get update
[ \$? -eq 0 ] && apt-get upgrade
[ \$? -eq 0 ] && apt-get clean

# check for kernel upgrades
for symlink in /vmlinuz /vmlinuz.old /initrd.img /initrd.img.old; do
	if [ -h \$symlink ]; then
		symlink_target=\$(ls -l "\$symlink" | awk '{print \$11}')
		echo \$symlink_target | egrep ".*-486$" >/dev/null && mv \$symlink \${symlink/.old/}.486
		echo \$symlink_target | egrep ".*-686$" >/dev/null && mv \$symlink \${symlink/.old/}.686
	fi
done

# restore start-stop-daemon
[ \$(stat --format '%s' /sbin/start-stop-daemon) -lt 200 ] && mv /sbin/start-stop-daemon.keep /sbin/start-stop-daemon

cd / && umount -l /root

echo
echo "X2Go Thin Client Shell upgrade has finished."
echo
EOF
chmod u+x "$TC_CHROOT/x2go_tce_upgrade.sh"

mkdir -p "$TC_CHROOT/"{proc,dev/pts,sys}
mount | grep "$TC_CHROOT/proc" >/dev/null || mount -tproc proc "$TC_CHROOT/proc" || true
mount | grep "$TC_CHROOT/sys" >/dev/null || mount -tsysfs sys "$TC_CHROOT/sys" || true
mount | grep "$TC_CHROOT/dev/pts" >/dev/null || mount -tdevpts devts "$TC_CHROOT/dev/pts" || true

if [ -f "$TC_CHROOT/etc/resolv.conf" ] || [ -h "$TC_CHROOT/etc/resolv.conf" ]; then
	mv "$TC_CHROOT/etc/resolv.conf" "$TC_CHROOT/etc/resolv.conf.disabled-by-x2go"
fi
test -f /etc/resolv.conf && cp /etc/resolv.conf "$TC_CHROOT/etc/resolv.conf"

chroot "$TC_CHROOT" /x2go_tce_upgrade.sh

if [ -f "$TC_CHROOT/etc/resolv.conf.disabled-by-x2go" ] || [ -h "$TC_CHROOT/etc/resolv.conf.disabled-by-x2go" ]; then
	mv "$TC_CHROOT/etc/resolv.conf.disabled-by-x2go" "$TC_CHROOT/etc/resolv.conf"
fi

for mountpoint in proc dev/pts sys; do
	while true; do
		cat /proc/mounts | grep "$TC_CHROOT/$mountpoint" >/dev/null && umount -l "$TC_CHROOT/$mountpoint" || break
	done
done
