#===========================================================================
# Makefile
#===========================================================================

.PHONY:validate build flint flintq  pragmas pragmasq release install chksum docs copy
.IGNORE:

###  Specify root path (where common.mk is)
#
# Define it once it, so the other makefile call recursively won't
# have to evaluate it
# 

ifndef ROOTPATH
ROOTPATH:=$(shell pwd)/
endif

OS:=$(shell cd / && uname -s)

#
#	Recurse into every project where there is a Makefile
#	and build them
#

ifndef MAKE_VERBOSE
.SILENT:
endif

#
#	SPECIFY LIST OF DIRECTORY TO BUILD FIRST and other specifi OS Stuff
#

ifeq (QNX,$(OS))
    rcpfile:=$(ROOTPATH)rcpdst_qnx6
    EARLY_DIRS:=tools/tput tools/makedepend librairies/std_c partage  librairies/CUnit 
    build_error_cmd:=-/bin/rm -f build_error.log; ./scripts/find_err | tee -a build_error.log
else
    rcpopt:=-b
    rcpfile:=$(ROOTPATH)rcpdst_win
    EARLY_DIRS:=librairies/std_c partage  librairies/CUnit
 	ifeq (tee.exe,$(findstring tee.exe,$(wildcard c:/qnx640/host/win32/x86/usr/bin/tee.exe)))
    	build_error_cmd:=-rm -f build_error.log; ch scripts/Makefile/find_err.ch $(ROOTPATH) ".err" | c:/qnx640/host/win32/x86/usr/bin/tee.exe -a build_error.log
    else
    	build_error_cmd:=
    endif
endif


#
#	SPECIFY LIST OF DIRECTORY TO BUILD LAST

# LATE_DIRS := 

#
#	SPECIFY LIST OF DIRECTORY NOT TO BUILD

#EXCLUDE_DIRS
#

#	SPECIFY LIST OF DIRECTORY TREE NOT TO BUILD
#   note the weird syntax, which is there for performance.  To add new directory to exclude
#   add to this string and start with the -a
EXCLUDE_TREE := -a ! -pname '*/qc/*'

EARLY_DIRS := $(addprefix ./,$(EARLY_DIRS))
EARLY_DIRS := $(addsuffix /,$(EARLY_DIRS))
LATE_DIRS := $(addprefix ./,$(LATE_DIRS))
EXCLUDE_DIRS := $(addprefix ./,$(EXCLUDE_DIRS))

#///////////////////////////////////////////////////////////////////////////////////////////
#// Path to some development tools
#//

ifeq (QNX,$(OS))
	mkd := $(ROOTPATH)tools/makedepend/mkd.exe
	tput := $(ROOTPATH)tools/tput/tput.exe
else
	mkd := ""
	tput := ""
endif

#///////////////////////////////////////////////////////////////////////////////////////////
#// define some variable to help get nice clean output and prevent tput from being.
#// The $(shell) stuff is expensive, but in this case I thing the improved
#// output is worth it
#//

ifeq (QNX,$(OS))
	ifndef bold
	 ifeq ($(wildcard $(tput)),$(tput))
	  bold   := "$(shell $(tput) -o bold)"
	  normal := "$(shell $(tput) -o sgr0)"
	 endif
	endif
else
	  bold   := ""
	  normal := ""
endif

### GET location of all Makefiles.  Can't use Windows QDE's find because it doesn't support -pname as 
### used in EXCLUDE_TREE
ifeq (QNX,$(OS))
	chmodcmd:=chmod +x scripts/* scripts/svn/* tools/vendor/pclint/pclint_qnx6.exe
	makelist1 := $(shell find -name Makefile $(EXCLUDE_TREE) )
else
	makelist1 := $(shell scripts/makefile/find.ch  $(ROOTPATH) Makefile)
endif

### GET list directory containing  Do Not Makefiles
ifeq (QNX,$(OS))
	dnm := $(dir $(shell $(QNX_HOST)/usr/bin/find -name Makefile.dnm ))
else
	dnm := $(dir $(shell scripts/makefile/find.ch  $(ROOTPATH) Makefile.dnm ))
endif


### get directories name only (remove filename component)
makelist2 := $(dir $(makelist1) )

### Remove directory that contain Makefile.dnm (dnm=Do No Make)
### They won't be called
makelist3 := $(filter-out $(dnm),$(makelist2))

### remove root/current directory
makelist4 := $(filter-out ./, $(makelist3) )

### remove root/current directory
makelist5 := $(filter-out $$(dnm),$(makelist4))

makelist6 :=  $(EARLY_DIRS) $(filter-out $(EARLY_DIRS) $(LATE_DIRS), $(makelist5)) $(LATE_DIRS)
makelist := $(filter-out $(EXCLUDE_DIRS),$(makelist6) )
ifeq (QNX,$(OS))
	makelistnoflt := $(shell find -name Makefile.noflt)
else
	#makelistnoflt := $(shell scripts/makefile/find.ch  $(ROOTPATH) Makefile.noflt)
endif

makelistflt := $(filter-out $(dir $(makelistnoflt)),$(makelist) )

firstdir := $(firstword $(makelist))
restdir  := $(filter-out $(firstdir),$(makelist))

firstdirflt := $(firstword $(makelistflt))
restdirflt  := $(filter-out $(firstdirflt), $(makelistflt) )

sep:=&&

makecmd = $(MAKE) -C$(dir) $(filter-out default build, $@)
#makecmd =  -C$(dir) $(filter-out default build, $@)

makecmdfltq = $(MAKE) -j $(firstword $(JLEVEL) 1) -C$(dir) flint 

default:
	@print "=============================================================================="
	@print "make build    : Batit tout"
	@print "make clean    : Efface executables"
	@print "make release  : make clean, build et cree fichier d'installation: install.tbz"
	@print "make update   : make clean, build et cree fichier d'installation: update.tbz"
	@print "                sans repertoire tab"
	@print "make chksum   : Calcule checksum sur fichier qui vont dans update.tbz"
	@print "make flint    : Execute flint et met resultat dans fling_flt.log"
	@print "make docs     : Cree documentation (dans les repertoires Docs)"
	@print "make pragma   : Extrait les pragmas message dans build_pragma.log"
	@print "make valide   : Execute validate.exe et met resultat dans /data/.../xml"
	@print "make cvslog   : Extrait log et store dans fichier cvs.log"
	@print "============================================================================="

all: build

rcpcopy:
	if test -a $(rcpfile); then \
		echo copying files on `cat $(rcpfile)`;\
		rcp $(rcpopt) bin/*.exe `cat $(rcpfile)`;\
		echo "copy done";\
	else\
		echo "Fichier $(rcpfile) absent";\
	fi 

build:
	$(chmodcmd)
	-$(foreach dir, $(firstdir), $(makecmd))$(foreach dir, $(restdir), $(sep) $(makecmd))
	$(build_error_cmd)
	echo Done! 
	
pragmas:
	-rm build_pragma.log
	@./scripts/find_pragma | tee -a build_pragma.log

pragmasq:
	-rm build_pragma.log
	@./scripts/find_pragma -q | tee -a build_pragma.log

cvslog:
	cvs log -N -S >cvs.log

flint:
	-find . -name '*flint.flt' -exec /bin/rm {}
	-rm flint.flt.log
	$(foreach dir, $(firstdirflt), $(makecmd))$(foreach dir, $(restdirflt), $(sep) $(makecmd))
	@./scripts/find_flint | tee flint.flt.tmp
	@sort flint.flt.tmp >flint.flt.log
	@rm flint.flt.tmp

flintq:
	-find . -name '*flint.flt' -exec /bin/rm {}
	-rm flint.flt.log
	$(foreach dir, $(firstdirflt), $(makecmd))$(foreach dir, $(restdirflt), $(sep) $(makecmd))
	@./scripts/find_flint >flint.flt.tmp
	@sort flint.flt.tmp >flint.flt.log
	@rm flint.flt.tmp

docs:
	$(MAKE) -f scripts/makefile/makefile.doc 2>>docs.log

clean:
	$(foreach dir, $(firstdir), $(makecmd))$(foreach dir, $(restdir), $(sep) $(makecmd))
	-rm -rf librariries/lib/*.* librairies/lib.d/*.* bin/* bin.d/*
	echo Done

$(TARGETS):
	$(foreach dir, $(firstdir), $(makecmd))$(foreach dir, $(restdir), $(sep) $(makecmd))

release:
	$(MAKE) clean
	$(MAKE) build
	$(MAKE) update 
	
update:
	scripts/build_update

update_force:
	scripts/build_update -f
	
validate:
	scripts/validate | tee validate.log

chksum:
	print build on `date` | tee cksum.txt
	print "chksum of all executable" | tee -a cksum.txt
	cat bin/*.exe | cksum | tee -a cksum.txt
	print "chksum of individual files:" | tee -a cksum.txt
	cksum bin/*.exe bin/screens/* bin/font/* >> cksum.txt

cleanerr:
	@find . -name '*.err' -print -remove!;

