#!/bin/bash
# kiwi-ltsp-setup - Part of KIWI-LTSP as created by Cyberorg
#
# Copyright (c) 2007 Cyberorg
# This program 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.
#
# This program 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., 59 Temple
# Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors: Cyberorg Cyberorg <cyberorg@cyberorg.info>
#          Magnus Boman <captain.magnus@gmail.com>
#
# Version	Date		Changes
# 0.1.1		2007-08-25	Initial release
# 0.1.2		2007-08-27	Made the script modular with functions
# 0.1.3         2007-08-27      DHCP server setup '-d', complete server '-s'
# 0.1.4		2007-09-09	Added setup_tftpserver. Changed modify_hosts to run before building image
# 0.1.5		2007-09-10	Move location to /usr/sbin. Search for config file in /etc first and if not there, current directory
# 0.1.6		2007-09-24	Added -l1 switch
# 0.1.7		2008-03-16	Major overhoul, add -e, remove -a, make it l, l1 and l2 for image creation instead

## Check to make we are running as root
if [ x"$USER" != x"root" ]; then
	echo "You have to be root to run this script"
	echo "If you did 'su' to become root, please redo it with 'su -'"
	exit
fi

CONF_FILE="kiwi-ltsp"

if ! . /etc/sysconfig/$CONF_FILE  2>/dev/null; then
	if ! . ./$CONF_FILE 2>/dev/null; then
		echo "Could not find $CONF_FILE in /etc/sysconfig or `pwd`"
		exit 1
	fi
fi

ROOT_DIR=$KIWIPATH

test -z "$DHCP_IFACE" && export DHCP_IFACE=`ip-easy-ltsp-config.py | sed -n 1p`
test -z "$SERVER_IP" && export SERVER_IP=`ip-easy-ltsp-config.py | sed -n 2p`
test -z "$DHCP_SUBNET" && export DHCP_SUBNET=`ip-easy-ltsp-config.py | sed -n 3p`
test -z "$DHCP_NETMASK" && export DHCP_NETMASK=`ip-easy-ltsp-config.py | sed -n 4p`
test -z "$DHCP_RANGE" && export DHCP_RANGE=`ip-easy-ltsp-config.py | sed -n 5p`
test -z "$DOMAIN" && export DOMAIN="digitalairlines.com"
test -z "$NAME_SERVERS" && export NAME_SERVERS="$SERVER_IP"
test -z "$GATEWAY" && export GATEWAY="$SERVER_IP"
test -z "$TFTP_SERVER" && export TFTP_SERVER="$SERVER_IP"

sed -i -e "s@DHCP_IFACE.*@DHCP_IFACE=\"$DHCP_IFACE\"@" -e "s@^SERVER_IP.*@SERVER_IP=\"$SERVER_IP\"@" -e "s@DHCP_SUBNET.*@DHCP_SUBNET=\"$DHCP_SUBNET\"@" -e "s@DHCP_NETMASK.*@DHCP_NETMASK=\"$DHCP_NETMASK\"@" -e "s@DHCP_RANGE.*@DHCP_RANGE=\"$DHCP_RANGE\"@" -e "s@DOMAIN.*@DOMAIN=\"$DOMAIN\"@" /etc/sysconfig/$CONF_FILE

if ! . $KIWIPATH/kiwi-ltsp-functions.sh 2>/dev/null; then
	echo "Could not find $KIWIPATH/kiwi-ltsp-functions.sh"
	exit 1
fi
mv $ROOT_DIR/kiwi-ltsp.log $ROOT_DIR/kiwi-ltsp.log.old
rm $ROOT_DIR/manual_tasks.txt 2>/dev/null || true
log "====== Starting ======"

ARCH=`arch`
KIWICMD="kiwi"

if [ $ARCH == "x86_64" ]; then
	KIWICMD="linux32 "$KIWICMD
	export $KIWICMD
	log "NOTE! kiwi-ltsp environment will be built as a 32bit image."
fi

need_help $1
check_config

if [ x"$RUNPARAM" = x"-h" -o x"$RUNPARAM" = x"-?" ]; then
	need_help
fi

if [ x"$RUNPARAM" = x"-b" ]; then
	backup_config
fi

if [ x"$RUNPARAM" = x"-p" -o x"$RUNPARAM" = x"-s" ]; then
	pre_check_packages
	backup_config
fi

if [ x"$RUNPARAM" = x"-l1" -o x"$RUNPARAM" = x"-l" -o x"$RUNPARAM" = x"-c" -o x"$RUNPARAM" = x"-u" -o x"$RUNPARAM" = x"-u1" -o x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-cd" ]; then
	kiwi_ltsp_setup_sshkeys
	modify_lts
fi

if [ x"$RUNPARAM" = x"-l1" -o x"$RUNPARAM" = x"-l" -o x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-u" -o x"$RUNPARAM" = x"-u1" -o x"$RUNPARAM" = x"-cd" -o x"$RUNPARAM" = x"-cd1" ]; then
	check_installation_source
	copy_config
	run_kiwi_ltsp1
fi

if [ x"$RUNPARAM" = x"-l2" -o x"$RUNPARAM" = x"-l" -o x"$RUNPARAM" = x"-s" ]; then
	run_kiwi_ltsp2
fi

if [ x"$RUNPARAM" = x"-u" -o x"$RUNPARAM" = x"-u2" ]; then
	run_usb_kiwi_ltsp2
fi

if [ x"$RUNPARAM" = x"-cd" -o x"$RUNPARAM" = x"-cd2" ]; then
	run_cd_kiwi_ltsp2
fi


if [ x"$RUNPARAM" = x"-u" -o x"$RUNPARAM" = x"-u3" ]; then
	deploy_usb_kiwi_ltsp
fi


if [ x"$RUNPARAM" = x"-n" -o x"$RUNPARAM" = x"-n1" -o x"$RUNPARAM" = x"-n2" -o x"$RUNPARAM" = x"-s" ]; then
        check_installation_source
	run_kiwi_netboot_phase1
fi

if [ x"$RUNPARAM" = x"-n" -o x"$RUNPARAM" = x"-n2" -o x"$RUNPARAM" = x"-s" ]; then
        run_kiwi_netboot_phase2
        fixup_netboot2
fi

if [ x"$RUNPARAM" = x"-d" -o x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-c" ]; then
	setup_dhcpd_conf
fi

if [ x"$RUNPARAM" = x"-e" -o x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-c" ]; then
        real_nfs_setup
fi

if [ x"$RUNPARAM" = x"-t"  -o x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-c" ]; then
        setup_tftpserver
       	setup_pxe
fi

if [ x"$RUNPARAM" = x"-s" -o x"$RUNPARAM" = x"-c" ]; then
	post_check
fi

if [ -e $ROOT_DIR/manual_tasks.txt ]; then
	echo "The following tasks need to be done manually:"
	cat $ROOT_DIR/manual_tasks.txt
fi

log "====== Setup completed ======"
