#
# Template for all Makefiles in efltk src.
#
# These variables are MUST:
#  - EFLTK_TOPDIR: Path to efltk root
#  - CFILES / CPPFILES: Files to build
#
# Possible variables to set:
#  - DIRS: Defines all sub-dirs, separated by whitespace
#  - LIBNAME: If this is set, files will be built to separate library.
#             If it's empty, only files are compiled
#  - PROGRAM: If this is set, application will be built.
#  - PROGRAM_LIBS: This must be set, if program needs to link with some external lib. 
#                  e.g. for images: PROGRAM_LIBS=$(IMAGESLIB)
#

OS_NAME := $(shell uname -s | sed "s/\//-/" | sed "s/_/-/" | sed "s/-.*//g")

ifneq ($(MAKEINCLUDE_INCLUDED), 1)
 ifeq ($(OS_NAME), MINGW32)
  include $(EFLTK_TOPDIR)/MinGW/makeinclude
 else
  include $(EFLTK_TOPDIR)/makeinclude
 endif
endif

OUT_MAGIC := $(shell mkdir $(EFLTK_TOPDIR)/lib > /dev/null 2>&1 || :)

################################################################

all: $(BUILDTARGETS)
install: $(INSTALLTARGETS)

OBJECTS = $(CPPFILES:.cpp=.o) $(CFILES:.c=.o)
ifeq ($(PROGRAM), )
 ifeq ($(STATIC_OBJECTS), )
  STATIC_OBJECTS = $(OBJECTS:.o=.static.o)
 endif
 ifeq ($(SHARED_OBJECTS), )
  SHARED_OBJECTS = $(OBJECTS:.o=.shared.o)
 endif
endif

# If LIBNAME is not empty, we are building separate library
# E.g. xml, gl
ifneq ($(LIBNAME), )

DSOLIBNAME = $(LIBNAME)
LIBRARY = $(EFLTK_TOPDIR)/lib/$(LIBNAME)
DSO = $(EFLTK_TOPDIR)/lib/$(LIBNAME)
DSONAME = $(LIBNAME)$(DSOEXT)

static: $(LIBRARY)$(LIBEXT)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) static) || exit;\
	done
endif

shared: $(DSO)$(DSOEXT)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) shared) || exit;\
	done
endif

$(LIBRARY).a: $(STATIC_OBJECTS)
	@rm -f $@
	@echo $(LIBCOMMAND) $@ ...
	@$(LIBCOMMAND) $@ $(STATIC_OBJECTS)
	@$(RANLIB) $@

$(DSO).so.$(FL_API_VERSION) $(DSO).sl.$(FL_API_VERSION): $(SHARED_OBJECTS)
	@rm -f $@
	@echo $(DSOCOMMAND) $(DSONAME) ...
	@$(DSOCOMMAND) $(DSONAME) $(SHARED_OBJECTS) $(DSOLIBS)
ifneq ($(STRIP), ) 
	@$(STRIP) $(DSONAME)
endif
	@mv $(DSONAME) $@

DLLIMPORTNAME = $(LIBNAME).2.0.dll$(LIBEXT)
$(DSO).$(FL_API_VERSION).dll: $(SHARED_OBJECTS)
	@rm -f $@
	@echo $(DSOCOMMAND) $@ ...
	$(DSOCOMMAND) $@ $(SHARED_OBJECTS) $(DSOLIBS) $(PROGRAM_LIBS)
	@mv $@.import $(EFLTK_TOPDIR)/lib/$(DLLIMPORTNAME)

clean:
	@rm -f *~ *.o *.bak $(LIBRARY).* makedepend
	@touch makedepend
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
		(cd $$dir;$(MAKE) $(MFLAGS) clean) || exit;\
	done
endif

################################################################

install_static: static
	@echo "Installing static libraries..."
	@mkdir -p $(libdir)
	@rm -f $(libdir)/$(LIBNAME)$(LIBEXT)
	@cp -f $(LIBRARY)$(LIBEXT) $(libdir)
	@chmod 644 $(libdir)/$(LIBNAME)$(LIBEXT)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) install_static) || exit;\
	done
endif

install_shared: shared
	@echo "Installing shared libraries..."
	@mkdir -p $(libdir)
	@rm -f $(libdir)/$(DLLIMPORTNAME)
	@rm -f $(libdir)/$(DSOLIBNAME)$(DSOEXT)
	@if [ "$(DSOSHORTEXT)" ]; then rm -f $(libdir)/$(DSOLIBNAME)$(DSOSHORTEXT); fi
	@cp -f $(DSO)$(DSOEXT) $(libdir)
	@chmod 755 $(libdir)/$(DSOLIBNAME)$(DSOEXT)
	@if [ "$(DSOSHORTEXT)" ]; then ln -s $(DSOLIBNAME)$(DSOEXT) $(libdir)/$(DSOLIBNAME)$(DSOSHORTEXT); fi
	@if [ -f "$(EFLTK_TOPDIR)/lib/$(DLLIMPORTNAME)" ]; then cp $(EFLTK_TOPDIR)/lib/$(DLLIMPORTNAME) $(libdir); fi
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
		(cd $$dir;$(MAKE) $(MFLAGS) install_shared) || exit;\
	done
endif

else
# LIBNAME was empty, compile only files

static: $(STATIC_OBJECTS)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) static) || exit;\
	done
endif

shared: $(SHARED_OBJECTS)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) shared) || exit;\
	done
endif

install_static: static
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) install_static) || exit;\
	done
endif

install_shared: shared
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
		(cd $$dir;$(MAKE) $(MFLAGS) install_shared) || exit;\
	done
endif

clean:
	@rm -f *~ *.o *.bak makedepend
	@touch makedepend
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
		(cd $$dir;$(MAKE) $(MFLAGS) clean) || exit;\
	done
endif

endif
#
# Same for all
#

depend:
ifneq ($(CPPFILES), )
	@echo Resolving dependencies..
	@$(MAKEDEPEND) -I$(EFLTK_TOPDIR) $(CXXFLAGS) $(CPPFILES) $(CFILES) > makedepend
	@sed -e "s'\.o'\.shared\.o'" makedepend > foo
	@cat foo >> makedepend
	@rm foo
endif
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	(cd $$dir;$(MAKE) $(MFLAGS) depend) || exit;\
	done
endif

programs: $(PROGRAM)
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) programs) || exit;\
	done
endif

install_programs: $(PROGRAM)
ifneq ($(PROGRAM), )
	@echo Installing $(PROGRAM)..
	@mkdir -p $(bindir)
	@if [ ! "$(OSTYPE)" = "cygwin32" ]; then \
		cp $(EFLTK_BINDIR)/$(PROGRAM) $(bindir)/; \
		chmod 755 $(bindir)/$(PROGRAM); \
	else \
		cp $(EFLTK_BINDIR)/$(PROGRAM).exe $(bindir)/; \
	fi
endif
ifneq ($(DIRS), )
	@for dir in $(DIRS); do\
        	if test ! -f $$dir/makedepend; then touch $$dir/makedepend; fi;\
        	(cd $$dir;$(MAKE) $(MFLAGS) install_programs) || exit;\
	done
endif

ifneq ($(PROGRAM), )

$(PROGRAM): $(OBJECTS)
	@echo Linking $(PROGRAM)...
	$(CXX) -o $(EFLTK_BINDIR)/$(PROGRAM) $(OBJECTS) $(PROGRAM_LIBS) $(LDLIBS)
ifneq ($(STRIP), ) 
	@$(STRIP) $(EFLTK_BINDIR)/$(PROGRAM)$(EXEEXT)
endif

endif

uninstall: 
	@if [ -f "$(libdir)/$(DSOLIBNAME)$(DSOSHORTEXT)" ]; then rm -f $(libdir)/$(DSOLIBNAME)$(DSOSHORTEXT); fi
	@if [ -f "$(libdir)/$(DLLIMPORTNAME)" ];            then rm -f $(libdir)/$(DLLIMPORTNAME); fi
	@if [ -f "$(libdir)/$(DSOLIBNAME)$(DSOEXT)" ];      then rm -f $(libdir)/$(DSOLIBNAME)$(DSOEXT); fi
	@if [ -f "$(libdir)/$(LIBNAME)$(LIBEXT)" ];         then rm -f $(libdir)/$(LIBNAME)$(LIBEXT); fi
