#!/bin/bash

# Copyright (C) 2012 by Debian Edu project, http://wiki.debian.org/DebianEdu
#       Mike Gabriel <mike.gabriel@das-netzwerkteam.de>

# Internet Kiosk 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.
#
# Internet Kiosk 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 "^./ik_.*$" >/dev/null; then
    ETCDIR="../etc"
else
    ETCDIR=/etc/internet-kiosk
fi

source $ETCDIR/ik_settings

IK_CHROOT="${IK_CHROOT:-/opt/internet-kiosk}"

test -e "$IK_CHROOT" || { 
    echo "ERROR: Internet Kiosk Diskless Terminal chroot does not exist"
    echo "at $IK_CHROOT. Run ik_create to create it..."
    exit -1
}

[ "x$USER" == "xroot" ] || { 
    echo "ERROR: Internet Kiosk Diskless Terminal management scripts have to run"
    echo "as super-user root."
    exit -2
}

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

export http_proxy=$IK_HTTP_PROXY
export https_proxy=$IK_HTTPS_PROXY
export ftp_proxy=$IK_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 '# Internet Kiosk Wrapper to avoid running daemons while performing maintenance.' >> /sbin/start-stop-daemon
        echo '#'                                                                               >> /sbin/start-stop-daemon
        echo                                                                                   >> /sbin/start-stop-daemon
        echo 'if [ "\$IK_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

# make sure the /etc/hostname file is not presented in the chroot
[ -e /etc/hostname ] && rm -f /etc/hostname

export IK_HANDLE_DAEMONS=false

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

# 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

echo
echo "Internet Kiosk Diskless Terminal upgrade has finished."
echo
EOF

chmod u+x "$IK_CHROOT/ik_dte_upgrade.sh"

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

chroot "$IK_CHROOT" /ik_dte_upgrade.sh

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

