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

source $ETCDIR/ik_settings

IK_BASE="${IK_BASE:-/opt/internet-kiosk}"
IK_CHROOT="${IK_CHROOT:-$IK_BASE/chroot}"
IK_TFTP_FILES="${IK_TFTP_FILES:-$SHAREDIR/tftpboot}"
IK_TFTP_BOOT="${IK_TFTP_BOOT:-/srv/tftp}"

mkdir -p "$IK_TFTP_BOOT"

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

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

echo "Preparing PXE boot configuration for Internet Kiosk Terminals..."
echo "----------------------------------------------------------------"
echo

echo "Copying the Internet Kiosk Terminal's TFTP (syslinux) template files..."
cp -av "$IK_TFTP_FILES"/* "$IK_TFTP_BOOT"
echo

echo "Linking files for the Internet Kiosk Terminal's TFTP (syslinux) boot"
echo "environment..."
cd "$IK_TFTP_BOOT"

# 486 kernel image
ln -sfv "$IK_CHROOT/vmlinuz.486" "$IK_TFTP_BOOT/vmlinuz.486"
ln -sfv "$IK_CHROOT/initrd.img.486" "$IK_TFTP_BOOT/initrd.img.486"
# 686 kernel image
ln -sfv "$IK_CHROOT/vmlinuz.686" "$IK_TFTP_BOOT/vmlinuz.686"
ln -sfv "$IK_CHROOT/initrd.img.686" "$IK_TFTP_BOOT/initrd.img.686"
# memtest86+
ln -sfv "$IK_CHROOT/boot/memtest86+.bin" "$IK_TFTP_BOOT/memtest86+.bin"
# syslinux components
ln -sfv "/usr/lib/syslinux/pxelinux.0" "$IK_TFTP_BOOT/pxelinux.0"
ln -sfv "/usr/lib/syslinux/vesamenu.c32" "$IK_TFTP_BOOT/vesamenu.c32"

cd - > /dev/null

echo
echo "DONE with Internet Kiosk Terminal 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 Internet Kiosk Terminal's chroot environment via NFSv3:"
echo
echo "    Add this line to /etc/exports..."
echo "    $IK_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 $IK_TFTP_BOOT/ (optional):"
echo
echo "    - customize PXE boot menu, take a look at $IK_TFTP_BOOT/default.cfg"
echo "      and proceed from there on..."
echo "    - use your own boot loader background image: 640x480px, 16bit colour depth,"
echo "      file: $IK_TFTP_BOOT/ik-dte.png"
echo
