#*****************************************************************************
# Copyright (C) 2006-2007 NTT
#*****************************************************************************
##
## MACRO definition
##

DIRROOT       = $(PWD)
#DIRPGSRC      = $(DIRROOT)/postgresql-8.2.4
DIRCCSRC      = $(DIRROOT)/clustercommand
DIRFMSIFSRC   = $(DIRROOT)/fmsinterface
DIRWWSRC      = $(DIRROOT)/walwriter
DIRWSSRC      = $(DIRPGSRC)/src/backend/walsender
DIRCMNSRC     = $(DIRROOT)/common
DIRUTILSRC    = $(DIRROOT)/utility
CMPUTLSRC     = $(DIRPGSRC)/contrib/lesslog
TARGETPGSRC   = $(DIRPGSRC)
TARGETPREXSRC = $(DIRCMNSRC) $(DIRCCSRC) $(DIRWWSRC) $(DIRUTILSRC) $(DIRFMSIFSRC)
TARGETDIRS    = $(TARGETPGSRC) $(TARGETPREXSRC)

DIRPGPREXSRC  = $(DIRPGSRC)/src/backend/prex
DIRPGPREXINC  = $(DIRPGSRC)/src/include/prex

CHECKPGDIRS   = $(DIRPGSRC) $(DIRPGPREXSRC) $(DIRPGPREXINC)
CHECKDIRS     = $(CHECKPGDIRS) $(DIRCCSRC) $(DIRWWSRC) $(DIRCMNSRC) $(DIRUTILSRC) \
                $(DIRFMSIFSRC)
RETERR = 1
PATCHSET      = ./patchset
PATCHFILE     = $(PATCHSET)/lesslog4prex.patch


##
## Build
##
all:
	$(MAKE) pgsql ; \
	if [ $$? -ne 0 ] ; then \
		exit $(RETERR) ; \
	fi ; \
	for dir in $(TARGETPREXSRC) ; do \
		cd $$dir ; \
		$(MAKE) $@ ; \
		if [ $$? -ne 0 ] ; then \
			exit $(RETERR) ; \
		fi ; \
		cd .. ; \
	done ; \
	if [ -d $(CMPUTLSRC) ] ; then \
		cd $(CMPUTLSRC); \
		$(MAKE) $@; \
	fi


##
## Install or clean
##
install clean:
	for dir in $(TARGETDIRS) ; do \
		cd $$dir ; \
		$(MAKE) $@ ; \
		if [ $$? -ne 0 ] ; then \
			exit $(RETERR) ; \
		fi ; \
		cd .. ; \
	done ; \
	if [ -d $(CMPUTLSRC) ] ; then \
		cd $(CMPUTLSRC); \
		$(MAKE) $@; \
	fi

##
## Build a target
##
pgsql:
	$(MAKE) check ; \
	if [ $$? -eq $(RETERR) ] ; then \
		exit $(RETERR) ; \
	fi ; \
	$(MAKE) link ; \
	cd $(DIRPGSRC) ; \
	$(MAKE)

cc:
	cd $(DIRCCSRC) ; \
	$(MAKE)

ww:
	cd $(DIRWWSRC) ; \
	$(MAKE)

##
## create some synboric-link for postgresql on PREX
##
link:
	for dir in $(CHECKPGDIRS) ; do \
		if [ ! -d $$dir ] ; then \
			echo "Error: can't find '$$dir' directory." ; \
			exit $(RETERR) ; \
		fi ; \
	done 
	cd $(DIRPGPREXINC) ; \
	ln -sf ../../../../common/include/PREXDefine.h . ;\
	ln -sf ../../../../common/include/PREXMessage.h . ;\
	cd $(DIRROOT) ; \
	cd $(DIRPGPREXSRC) ; \
	ln -sf ../../../../common/src/PREXMessage.c . ;\
	cd $(DIRROOT)

##
## Check existence of the sources of postgresql
##
check:
	for dir in $(CHECKDIRS) ; do \
		if [ ! -d $$dir ] ; then \
			echo "Error: can't find '$$dir' directory." ; \
			exit $(RETERR); \
		fi ; \
	done


##
## do patch
##
dopatch: $(PATCHFILE)
	cp $(PATCHSET)/WALCompress.c $(DIRWSSRC) ; \
	cp $(PATCHSET)/WALCompress.h $(DIRPGSRC)/src/include/walsender ; \
	cp $(PATCHSET)/WALDecompress.c $(DIRWWSRC)/src ; \
	cp $(PATCHSET)/WALDecompress.h $(DIRWWSRC)/include ; \
	cp -r $(PATCHSET)/lesslog $(DIRPGSRC)/contrib ; \
	cd $(DIRPGSRC) ; \
	patch -p1 < ../$(PATCHFILE) ; \





##
## peel patch
##

peelpatch: $(PATCHFILE)
	rm $(DIRWSSRC)/WALCompress.c ; \
	rm $(DIRPGSRC)/src/include/walsender/WALCompress.h ; \
	rm $(DIRWWSRC)/src/WALDecompress.c ; \
	rm $(DIRWWSRC)/include/WALDecompress.h ; \
	rm -rf $(DIRPGSRC)/contrib/lesslog ; \
	cd $(DIRPGSRC) ; \
	patch -R -p1 < ../$(PATCHFILE) ; \
	cd ..


##
## DebugBuild
##
debug:
	$(MAKE) pgsql ; \
	if [ $$? -ne 0 ] ; then \
		exit $(RETERR) ; \
	fi ; \
	for dir in $(TARGETPREXSRC) ; do \
		cd $$dir ; \
		$(MAKE) $@ ; \
		if [ $$? -ne 0 ] ; then \
			exit $(RETERR) ; \
		fi ; \
		cd .. ; \
	done ; \
	if [ -d $(CMPUTLSRC) ] ; then \
		cd $(CMPUTLSRC); \
		$(MAKE) ; \
	fi




include Makefile.global
