#!/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}"

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
}

test -e "$IK_CONFIG" || { 
    echo "ERROR: No Internet Kiosk Diskless Terminal configuration found"
    echo "at $IK_CONFIG. Please check..."
    exit -1
}

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

mkdir -p "$IK_CHROOT/etc/internet-kiosk"

echo "Installing Internet Kiosk configuration into chroot environment..."
echo "------------------------------------------------------------------"

if test -f $IK_CONFIG/ik_init; then
	cp -v "$IK_CONFIG/ik_init" "$IK_CHROOT/etc/internet-kiosk/ik_init"
	chmod a+rx $IK_CHROOT/etc/internet-kiosk/ik_init
fi

if test -f $IK_CONFIG/ik_start; then
	cp -v "$IK_CONFIG/ik_start" "$IK_CHROOT/etc/internet-kiosk/ik_start"
	chmod a+rx "$IK_CHROOT/etc/internet-kiosk/ik_start"
fi

if test -f $IK_CONFIG/ik_sessions; then
	cp -v "$IK_CONFIG/ik_sessions" "$IK_CHROOT/etc/internet-kiosk/ik_sessions"
	chmod a+rx "$IK_CHROOT/etc/internet-kiosk/ik_sessions"
fi

echo

