#!/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_BASE="${IK_BASE:-/opt/internet-kiosk}"
IK_CHROOT="${IK_CHROOT:-$IK_BASE/chroot}"
IK_CONFIG="${IK_CONFIG:-$IK_BASE/etc}"
IK_TFTP_BOOT="${IK_TFTP_BOOT:-/srv/tftp}"

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

echo "Internet Kiosk Diskless Terminal cleanup..."
echo "-------------------------------------------"

echo "Hit <RETURN> to purge all Internet Kiosk Diskless Terminal related files"
echo "(chroot, config, PXE boot env)..."
echo
echo "Alternatively, hit STRG-C to cancel the operation now!!!"
read

# CHROOT + DTE config
test -e "$IK_CHROOT" && {
    echo "Removing $IK_CHROOT..."
    rm -Rf "${IK_CHROOT}"
} || {
    echo "WARNING: Internet Kiosk Diskless Terminal chroot does not exist"
    echo "at $IK_CHROOT. No cleanup necessary..."
}
test -e "$IK_CONFIG" && {
    echo "Removing $IK_CONFIG..."
    rm -Rf "${IK_CONFIG}"
} || {
    echo "WARNING: Internet Kiosk Diskless Terminal config does not exist"
    echo "at $IK_CONFIG. No cleanup necessary..."
}

### PXE/Syslinux
test -e "$IK_TFTP_BOOT/pxelinux.cfg" && {
    echo "Emptying $IK_TFTP_BOOT..."
    rm -Rf "${IK_TFTP_BOOT}"/*
} || {
    echo "WARNING: Internet Kiosk PXE/Syslinux boot environment does not exist"
    echo "at $IK_TFTP_BOOT. No cleanup necessary..."
}

echo
echo "Internet Kiosk Diskless Terminal cleanup is DONE."
echo
