################################################################################
# Filename: ./src/plugins/iscsi/Makefile
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
#
# Qrm iscsi Makefile
#

ifndef SOURCEDDIR
	export SOURCEDDIR=../..
endif

ifndef OUTDIR
	export OUTDIR=../../../out/
endif

PACKAGE_CONFIG_FILE_ISCSI_2_4=$(SOURCEDDIR)/plugins/iscsi/build/iscsi-2.4.conf
PACKAGE_CONFIG_FILE_ISCSI_2_6=$(SOURCEDDIR)/plugins/iscsi/build/iscsi-2.6.conf
PACKAGE_CONFIG_FILE_KERNEL_2_4=$(SOURCEDDIR)/plugins/iscsi/build/kernel-2.4.conf
PACKAGE_CONFIG_FILE_KERNEL_2_6=$(SOURCEDDIR)/plugins/iscsi/build/kernel-2.6.conf
KERNEL_VERSION_2_4=$(shell cat $(PACKAGE_CONFIG_FILE_KERNEL_2_4) | grep ^PACKAGE_VERSION | cut -d'"' -f 2)
KERNEL_VERSION_2_6=$(shell cat $(PACKAGE_CONFIG_FILE_KERNEL_2_6) | grep ^PACKAGE_VERSION | cut -d'"' -f 2)
QRM_CACHE_DIR=$(shell $(SOURCEDDIR)/make_helper get_cache_dir)
QRM_ROOT_DIR=$(shell $(SOURCEDDIR)/make_helper get_root_dir)
QRM_ISCSI_PLUGIN_DIR=$(shell pwd)

# please notice that iscsi is using a make script for compilation
# it does not work without .SILENT:
.SILENT:

 all:	check
	if which genksyms 1>/dev/null 2>&1; then        \
		if ! gcc --version | grep "gcc.*3.4.4" 1>/dev/null 2>&1; then   \
			make iscsi24;   \
		else    \
			echo "WARNING: Detected gcc - 3.4.4 which is too buggy to compile the iscsi-module";    \
			echo;   \
			echo "         Please updated your gcc compiler";       \
			echo;   \
			echo "         Not compiling the 2.4. kernel version of the iscsi-kernel.";     \
			echo;   \
			echo;   \
			sleep 4;        \
		fi	\
	else    \
		echo; \
		echo; \
		echo "WARNING: No such file genksyms. Missing requirements to compile a 2.4. based kernel.";    \
		echo "         Not compiling the 2.4. kernel version of the iscsi-kernel.";     \
		echo; \
		echo "         Please notice that 'newer' linux distributions using gcc4";      \
		echo "         may have problems to compile a 2.4. based kernel!";      \
		echo;   \
		echo;   \
		echo;   \
		sleep 4;        \
	fi
	make iscsi26

iscsi24:
	@echo "making the iscsi-plugin for the 2.4. kernel version"
	# 2.4
	@echo "make mrproper" > $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.pre
	@echo "make clean" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.pre
	@echo "/bin/cp -f $(QRM_ISCSI_PLUGIN_DIR)/build/kernelconfig-2.4 .config" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.pre
	@echo "make oldconfig dep" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.pre

	@echo "export INSTALL_MOD_PATH=$(QRM_CACHE_DIR)/iscsi-2.4/" > $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post
	@echo "make modules_install" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.4/boot" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post
	@echo "/bin/cp -f System.map $(QRM_CACHE_DIR)/iscsi-2.4/boot/System.map-$(KERNEL_VERSION_2_4)" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post
	@echo "/bin/cp -f arch/i386/boot/bzImage $(QRM_CACHE_DIR)/iscsi-2.4/boot/vmlinuz-$(KERNEL_VERSION_2_4)" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post
	@chmod +x $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.*

	QRM_PRE_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.pre \
	QRM_POST_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.4.post \
	QRM_MAKE_OPTIONS="bzImage modules" \
	$(SOURCEDDIR)/make_helper build_package $(PACKAGE_CONFIG_FILE_KERNEL_2_4)

	# iscsi module
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.4/lib/modules/$(KERNEL_VERSION_2_4)/misc" > $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.4/sbin" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.4/etc/init.d" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/kobj/iscsi_sfnet.o $(QRM_CACHE_DIR)/iscsi-2.4/lib/modules/$(KERNEL_VERSION_2_4)/misc/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/obj/iscsid $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/obj/iscsi-device $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/obj/iscsigt $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/obj/iscsi-iname $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/Linux-i686/obj/iscsi-ls $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/iscsi-mountall $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/iscsi-umountall $(QRM_CACHE_DIR)/iscsi-2.4/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.4/linux-iscsi-*/rc.iscsi $(QRM_CACHE_DIR)/iscsi-2.4/etc/init.d/iscsi" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post
	@chmod +x $(QRM_ROOT_DIR)/qrm-iscsi-module*

	QRM_MAKE_OPTIONS="TOPDIR=$(QRM_ROOT_DIR)/build/kernel-iscsi-2.4/linux-$(KERNEL_VERSION_2_4)" \
	QRM_POST_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-module-2.4.post \
	$(SOURCEDDIR)/make_helper build_package $(PACKAGE_CONFIG_FILE_ISCSI_2_4)


iscsi26:
	# 2.6
	@echo "make mrproper" > $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.pre
	@echo "make clean" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.pre
	@echo "/bin/cp -f $(QRM_ISCSI_PLUGIN_DIR)/build/kernelconfig-2.6 .config" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.pre
	@echo "make oldconfig" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.pre

	@echo "export INSTALL_MOD_PATH=$(QRM_CACHE_DIR)/iscsi-2.6/" > $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post
	@echo "make modules_install" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.6/boot" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post
	@echo "/bin/cp -f System.map $(QRM_CACHE_DIR)/iscsi-2.6/boot/System.map-$(KERNEL_VERSION_2_6)" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post
	@echo "/bin/cp -f arch/i386/boot/bzImage $(QRM_CACHE_DIR)/iscsi-2.6/boot/vmlinuz-$(KERNEL_VERSION_2_6)" >> $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post
	@chmod +x $(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.*

	QRM_PRE_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.pre \
	QRM_POST_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-kernel-2.6.post \
	QRM_MAKE_OPTIONS="bzImage modules" \
	$(SOURCEDDIR)/make_helper build_package $(PACKAGE_CONFIG_FILE_KERNEL_2_6)


	# iscsi module
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.6/lib/modules/$(KERNEL_VERSION_2_6)/misc" > $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.6/sbin" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "mkdir -p $(QRM_CACHE_DIR)/iscsi-2.6/libs" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.6/linux-iscsi-*/kernel/iscsi_if.ko $(QRM_CACHE_DIR)/iscsi-2.6/lib/modules/$(KERNEL_VERSION_2_6)/misc/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.6/linux-iscsi-*/kernel/iscsi_tcp.ko $(QRM_CACHE_DIR)/iscsi-2.6/lib/modules/$(KERNEL_VERSION_2_6)/misc/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.6/linux-iscsi-*/usr/iscsid $(QRM_CACHE_DIR)/iscsi-2.6/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "/bin/cp -f $(QRM_ROOT_DIR)/build/iscsi-2.6/linux-iscsi-*/usr/iscsiadm $(QRM_CACHE_DIR)/iscsi-2.6/sbin/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "if [ -f /lib/tls/libpthread.so.0 ]; then" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "	/bin/cp -aRf /lib/tls/libpthread* $(QRM_CACHE_DIR)/iscsi-2.6/libs/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "elif [ -f /lib/libpthread.so.0 ]; then" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "	/bin/cp -aRf /lib/libpthread* $(QRM_CACHE_DIR)/iscsi-2.6/libs/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "else" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "	echo ERROR: Could not find needed libpthread" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "	exit 1" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "fi" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@echo "/bin/cp -aRf /lib/libdb-* $(QRM_CACHE_DIR)/iscsi-2.6/libs/" >> $(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post
	@chmod +x $(QRM_ROOT_DIR)/qrm-iscsi-module*

	QRM_MAKE_OPTIONS="KSRC=$(QRM_ROOT_DIR)/build/kernel-iscsi-2.6/linux-$(KERNEL_VERSION_2_6)" \
	QRM_POST_MAKE_CMD=$(QRM_ROOT_DIR)/qrm-iscsi-module-2.6.post \
	$(SOURCEDDIR)/make_helper build_package $(PACKAGE_CONFIG_FILE_ISCSI_2_6)


install:
	@echo "installing iscsi for Qrm"
	mkdir -p $(OUTDIR)/qrm/plugins/iscsi
	if [ -f "$(QRM_CACHE_DIR)/iscsi-2.4/boot/System.map-$(KERNEL_VERSION_2_4)" ]; then \
		/bin/rm -f $(OUTDIR)/qrm/plugins/iscsi/iscsi-2.4.tgz;	\
		echo "Packaging the iscsi-2.4 kernel";	\
		tar -C $(QRM_CACHE_DIR)/ -czf $(OUTDIR)/qrm/plugins/iscsi/iscsi-2.4.tgz iscsi-2.4/;	\
	fi
	echo "Packaging the iscsi-2.6 kernel"
	/bin/rm -f $(OUTDIR)/qrm/plugins/iscsi/iscsi-2.6.tgz
	tar -C $(QRM_CACHE_DIR)/ -czf $(OUTDIR)/qrm/plugins/iscsi/iscsi-2.6.tgz iscsi-2.6/
	mkdir -p $(OUTDIR)/qrm/plugins/iscsi/etc
	/bin/cp -u etc/*.ini $(OUTDIR)/qrm/plugins/iscsi/etc/
	mkdir -p $(OUTDIR)/qrm/plugins/iscsi/include
	/bin/cp -rf include/*-functions $(OUTDIR)/qrm/plugins/iscsi/include	

clean:	check
	@echo "cleaning up iscsi"
	/bin/rm -rf $(OUTDIR)/qrm/plugins/iscsi*

realclean: clean
	@echo "running realclean for the iscsi-plugin"
	  /bin/rm -rf $(QRM_CACHE_DIR)/iscsi-* $(QRM_ROOT_DIR)/build/iscsi-* $(QRM_CACHE_DIR)/kernel-iscsi-* $(QRM_ROOT_DIR)/build/kernel-iscsi-*

check:
	@if [ -z $(QRM_CACHE_DIR) ]; then echo "ERROR: QRM_CACHE_DIR not set!"; exit 1; fi
	@if [ -z $(QRM_ROOT_DIR) ]; then echo "ERROR: QRM_ROOT_DIR not set!"; exit 1; fi




	
