################################################################################
# Filename: ./src/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 source Makefile
#
# please notice that iscsi is using a make script for compilation
# it does not work without .SILENT:
.SILENT:

CURRENT=$(shell pwd)

ifeq ($(MAKECMDGOALS),tgz)
MAKECMDGOALS=install
endif

ifeq ($(MAKECMDGOALS),)
MAKECMDGOALS=all
endif

ifeq ($(MAKECMDGOALS),all)
$(shell chmod +x ./prepare_version.sh ./prepare_version.sh)
endif

#repository
export QRM_CACHE_DIR := $(shell $(CURRENT)/make_helper get_cache_dir)
#intermediate
export QRM_ROOT_DIR := $(shell $(CURRENT)/make_helper get_root_dir)

OUTDIR=$(CURRENT)/../out/
VERSION_FILE=$(QRM_ROOT_DIR)/VERSION.txt
@COTAG=$(shell sed 's/.//' CVS/Tag )
ifeq  ($(COTAG),)
COTAG	=	$(shell date "+DATE%Y%m%d-%H%M%S")
endif
DISPLAY_NAME	=	openqrm
DISPLAY_VERSION =	$(shell grep NAME VERSION.txt | cut -d= -f2)
SRC_PACK_NAME 	= 	$(DISPLAY_NAME)-$(DISPLAY_VERSION)_$(COTAG)
SNAPSHOT_FILE_SRC= 	$(SRC_PACK_NAME).src.tgz
SNAPSHOT_FILE_BIN= 	$(SRC_PACK_NAME).i386.tgz


SUB_MAKEFILES := $(wildcard */Makefile)

all install: $(VERSION_FILE)


$(VERSION_FILE): 
	./prepare_version.sh start $(VERSION_FILE)

all install_qrm_data clean realclean: $(SUB_MAKEFILES)

clean:
	rm -f $(OUTDIR)/qrm*.tgz $(OUTDIR)/qrm/qrm-install $(OUTDIR)/qrm/qrm-uninstall $(OUTDIR)/qrm/VERSION.txt

ifeq ($(MAKECMDGOALS),all)
$(SUB_MAKEFILES): $(VERSION_FILE)
endif
ifeq ($(MAKECMDGOALS),install)
$(SUB_MAKEFILES): $(VERSION_FILE)
endif

$(SUB_MAKEFILES):  
	$(MAKE) $(QMAKEFLAGS) -f $(@F) -C $(@D) $(MAKECMDGOALS)

install_qrm_data:
	tar -C $(OUTDIR)/qrm/ -czf /$(OUTDIR)/qrm/usr/tomcat/webapps/ROOT/unsecure/qrm-data.tgz \
                bin/qrm-mon     			\
                bin/qrm-cli     			\
                etc/init.d/qrm  			\
                etc/init.d/qrm-start    		\
                etc/init.d/qrm-stop             	\
                etc/init.d/qrm-emergency-console	\
		etc/init.d/qrm-check			\
                include/qrm-functions   		\
                include/qrm-nodes-functions     	\
                include/dist_specific.d/generic 	\
                include/dist_specific.d/redhat  	\
                include/dist_specific.d/suse_80 	\
                include/dist_specific.d/suse_90 	\
                sbin/qrm-runlevel       		\
                sbin/qrm-infod  			\
                sbin/qrm-tcp-monitor    		\
                sbin/qrm-execd			

install: install_qrm_data
	./prepare_version.sh end $(VERSION_FILE)	
	install -m 0644 -D $(VERSION_FILE) $(OUTDIR)/qrm/VERSION.txt
	install -m 0644 -D LICENSE $(OUTDIR)/qrm/LICENSE
	rm -f $(VERSION_FILE)

realclean:	clean

tgz:	install
	cd $(OUTDIR) && rm -f qrm*.tgz && tar -czvf $(SNAPSHOT_FILE_BIN) qrm

plugin_template:
	./make_helper qrm_create_plugin_template $(CURRENT)/plugins

.PHONY: all install clean realclean $(SUB_MAKEFILES) install_qrm_data 


pack_src:	
	@echo Create src snapshot
	@rm -Rf ../$(SRC_PACK_NAME)
	@mkdir -p ../$(SRC_PACK_NAME)/src
	@cp -r * ../$(SRC_PACK_NAME)/src
	@echo "Inserting tag=$(COTAG) into ../$(SRC_PACK_NAME)/src/VERSION.txt"
	@echo "BRANCH=$(COTAG)" >> ../$(SRC_PACK_NAME)/src/VERSION.txt
	@echo Clean up CVS files and directories
	@find ../$(SRC_PACK_NAME) -type d -name "CVS*" | xargs rm -Rf
	@find ../$(SRC_PACK_NAME) -type f -name .cvsignore | xargs rm -Rf
	@echo Pack source file into $(SNAPSHOT_FILE_SRC)
	@mkdir -p $(OUTDIR)
	@(cd .. ; tar czf $(OUTDIR)/$(SNAPSHOT_FILE_SRC) $(SRC_PACK_NAME))
	@echo "Cleaning up"
	@rm -Rf $(CURRENT)/../$(SRC_PACK_NAME)


