#!/usr/bin/make -f 
#
#
# Normal variables derived from passed variables  
LPATH	:= $(PREFIX)/lib/root
IPATH	:= $(PREFIX)/include/root
BPATH	:= $(PREFIX)/bin
MPATH   := $(PREFIX)/share/man/man1
SOEXT	:= so
MAJOR   := $(shell echo $(VERSION) | tr '.' ' ' | cut -f1 -d' ')
MINOR   := $(shell echo $(VERSION) | tr '.' ' ' | cut -f2 -d' ')
SOVER	:= $(MAJOR).$(MINOR)
RLIBD   := $(PREFIX)/lib/root

# Put all library files into version-specific lists 
ifneq ($(LIB),$(DEV))
SLIB    := $(LIB)$(SOVER)
else 
SLIB	:= $(LIB)
endif
ifneq ($(LIB),$(BIN))
SBIN    := $(BIN)
else 
SBIN	:= $(SLIB)
endif

# Include general and directory specific make rules 
ifneq ($(DIRS),)
include config/Makefile.config 
include config/Makefile.$(ARCH)
include $(foreach i, $(DIRS), $(i)/Module.mk)
# Special case of xrootd 
ifeq ($(DIRS), net/xrootd)
XRDLIBS := $(patsubst lib/%, $(LPATH)/%, \
		$(filter-out lib/libXrdProofd.so, $(wildcard lib/libXrd*.so)))
ALLLIBS	:= $(XRDLIBS:$(RLIBD)/%=$(RLIBD)/%)
ALLEXECS:= $(filter-out bin/xrootd bin/olbd, $(ALLEXECS))
endif # $(DIRS) = xrootd
endif # ! $(DIRS) = 
ifneq ($(filter cint/cint,$(DIRS)),)
include cint/ROOT/cintdlls.mk
DICTLIBS += $(patsubst lib/%, $(RLIBD)/%, $(filter lib/%, $(CINTDICTDLLS)))
ALLMAPS  =  $(patsubst lib/%, $(RLIBD)/%, $(filter lib/%, $(CINTDICTMAPS)))
endif

# Default target and message 
all:	hello liblist devlist binlist
hello:
	@echo "Making list for '$(DIRS)' ($(SLIB),$(DEV),$(SBIN))"
#	@echo "ALLLIBS=$(ALLLIBS) ALLMAPS=$(ALLMAPS) PKGLIBS=$(PKGLIBS)"

# If we have a development package, get the list of headers. 
ifneq ($(DEV),)
PKGHDRS	:= $(shell echo $(ALLHDRS) | sed -e 's,[^ ]*inc/,include/,g' -e 's,include/,usr/include/root/,g')
endif

# If we have a Library, then we get the list of libraries 
ifneq ($(LIB),)
# if the development package and the library package is not the same, 
# we need to append the soversion to the library directory 
ifneq ($(LIB),$(DEV))
PKGLIBS	:= $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(SOVER)) 	\
	   $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(MAJOR)) 	\
	   $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%)		\
	   $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/%.$(SOVER)) 		\
	   $(DICTLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(SOVER)) 	\
	   $(DICTLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(MAJOR)) 	\
	   $(DICTLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%)		
DEVLIBS := $(ALLLIBS) 
else # ! $(LIB) = $(DEV)
# If the development and library package is the same (plugins), then we need
# only add the soversion to the library directory 
ifeq ($(DIRS), net/xrootd)
PKGLIBS := $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%)
else
PKGLIBS	:= $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(SOVER)) 	\
	   $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%.$(MAJOR)) 	\
	   $(ALLLIBS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%) 		
endif # $(DIRS) = net/xrootd
endif # $(LIB) = $(DEV)
endif # $(LIB)
ifneq ($(NOMAP),1) 
#NOXRD	:= $(filter-out $(LIBPATH)/libXrd%, $(ALLLIBS))
#PKGLIBS := $(PKGLIBS)  $(NOXRD:$(RLIBD)/%.so=$(RLIBD)/$(SOVER)/%.rootmap)
PKGLIBS := $(PKGLIBS)  $(ALLMAPS:$(RLIBD)/%=$(RLIBD)/$(SOVER)/%)
endif

# The rule to make library lists 
liblist:
ifneq ($(LIB),)
	@for f in $(PKGLIBS)  ; do echo $$f ; done >> $(OUT)/$(SLIB).install
ifneq ($(LIB), $(DEV))
	@(cd $(BUILD)/$(RLIBD) && \
	   for i in $(ALLLIBS) ; do \
	      b=`basename $$i` ; \
	      ln -fs $(SOVER)/$$b . ; \
	      ln -fs $(SOVER)/$$b.$(SOVER) . ; done)
endif # $(LIB) = $(DEV)
endif # $(LIB)

# If we have a development library, get the list of headers for the package.
ifneq ($(DEV),)
PKGHDRS	:= $(ALLHDRS:include/%=$(IPATH)/%) $(DEVLIBS)
endif # $(DEV)

# Rule to make development package list 
devlist:
ifneq ($(DEV),)
ifneq ($(DEV),$(LIB))
	@for f in $(filter-out %.cw %.pri, $(PKGHDRS)) ; \
		do echo $$f ; done >> $(OUT)/$(DEV).install
endif
endif

# If we have a binary package, then get list of programs and man pages
ifneq ($(BIN),)
PKGEXECS= $(ALLEXECS:bin/%=$(BPATH)/%) 
PKGMANS = $(ALLEXECS:bin/%=$(MPATH)/%.1) 
endif # $(BIN)

# Rule to make program package list 
binlist:
ifneq ($(BIN),)
	@for f in $(PKGEXECS)  ; do echo $$f ; done >> $(OUT)/$(SBIN).install
	@for f in $(PKGMANS)  ; do echo $$f\* ; done >> $(OUT)/$(SBIN).install
endif

#
# EOF
#
