#!/bin/sh
#
# /usr/sbin/kiwi-ltsp-aoe
#
# LTSP using AoE
#
# Copyright (c) 2009 IntelliAnt, Kolkata, India
# All rights reserved.
#
# 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
#
# Author: Shrenik Bhura, 2009
#
# Version       Date            Changes
# 0.1           2009-02-28      Initial release
#
#################################################################################

KL_CONFIG=/etc/sysconfig/kiwi-ltsp
test -r $KL_CONFIG || { echo "$KL_CONFIG missing, please check if /
                                kiwi-ltsp is installed properly."
        if [ "$1" = "stop" ]; then exit 0;
                else exit 6; fi; }

# Read config
. $KL_CONFIG

KIWI_LTSP_DIR=$TFTPBOOTPATH/KIWI

if [ $IMAGETYPE != "AOE" ]; then 
	echo "KIWI-LTSP is configured for non-AoE setup."
	exit 1
fi
if [ ! -e $AOE_IMG_DIR ]; then
	AOE_IMG_DIR="$LTSSQUASHPATH"
fi
images=$AOE_IMG_ORDER
vbshelf=0
vbslot=1

case "$1" in
	start)
		if [ $AOE_IMG_MAC_MAP == "1" ]; then
			# cleanup the old config files
			rm -f $KIWI_LTSP_DIR/config.00* $KIWI_LTSP_DIR/config_0*
		fi
		for i in $images
		do
			# check to prevent duplicate mounting of same image
			lodev=`losetup -j $AOE_IMG_DIR/$i | cut -d ":" -f 1`
			if [ -z $lodev ]; then
				# determine the next free loop device
				lodev=`losetup -f`
				# Images mounted in default sorted order as returned by ls
				losetup $lodev $AOE_IMG_DIR/$i
			fi
			vbladed $vbshelf $vbslot $DHCP_IFACE $lodev
			if [ $AOE_IMG_MAC_MAP == "1" ] && [ -f $KIWI_LTSP_DIR/config.default ]
			then
				# create the config.ma:ca:dd:re:ss files as per the image to mac mapping in AOE_IMG_0n
				sr=\'1,\$s/0\.1/$vbshelf\.$vbslot/\'
				eval sed $sr $KIWI_LTSP_DIR/config.default > $KIWI_LTSP_DIR/config_0$vbslot
				eval maclist=\$AOE_IMG_0$vbslot
				for mac in $maclist
				do
					ln $KIWI_LTSP_DIR/config_0$vbslot $KIWI_LTSP_DIR/config.$mac
				done
			fi
			vbslot=$((vbslot+1))
		done
		chmod 644 $KIWI_LTSP_DIR/config*
		# to keep this process alive as required by startproc; does not seem to be an overkill, there may be a better way
		coma=1
		while [ $coma = 1 ]; do
        		sleep 100
		done
		;;
	stop)
		killall vblade
		for i in $images
		do
			#determine the used loop devices for kiwi-ltsp-aoe and free each
			lodev=`losetup -j $AOE_IMG_DIR/$i | cut -d ":" -f 1`
			losetup -d $lodev
		done
		;;
	restart) 
		$0 stop
		$0 start
		;;
    	*)
        	## If no parameters are given, print which are avaiable.
	        echo "Usage: $0 {start|stop|restart}"
        	exit 1
        	;;
esac	
