################################################################################
####       QFitsView Makefile
################################################################################
# Type one of the following lines:
#   make                    same as 'make release'
#   make release            (static, 32/64-bit autodetected)
#   make release_shared     (shared, 32/64-bit autodetected)
#   make debug              (static, 32/64-bit autodetected)
#   make debug_shared       (shared, 32/64-bit autodetected)
#   make CYGWIN=-y          WIN only, target for CYGWIN-environment
#   make clean              to clean up the project
#   make dist               LINUX/MACOSX only to create a distribution as
#                           tar.gz-file
#                           (do a checkout to a new location before, since all
#                           .svn-folders will be deleted!)
#
#
#   make parsers PLATFORM=LINUX (or MACOSX or WIN)
#                          to create just the parsers and lexers for dpuser,
#                          dpuser2c & idl2c
#   make dpuser2c_header PLATFORM=LINUX (or MACOSX or WIN)
#                          to create just the dpuser2c.h-file
################################################################################

PARSER_DIR   = ../dpuser/parser
DPUSER2C_DIR = ../dpuser/dpuser2c
DP_PATH      = ../dpuser

ifeq ($(HOMEDRIVE), C:)
   OS     = WIN
   SED    = ../bin/WIN/sed.exe -i
   RM     = del
   RMDIR  = rmdir
else
   ifeq ($(TERM_PROGRAM), Apple_Terminal)
      OS  = MACOSX
      SED = sed -i ''
   else
      OS  = LINUX
      SED = sed -i''
   endif
   RM	  = rm -rf
endif

ifeq ($(wildcard qfitsview.mk),) 
    DO_CLEAN = NO
else 
    DO_CLEAN = YES
endif

################################################################################
##### main targets #####
all: release

all_base: doc_helpmap parsers

# static binaries
release: all_base qmake-release_static qfitsview
debug:   all_base qmake-debug_static qfitsview

ifeq ($(OS), WIN)
# shared binaries for Windows
release_shared: all_base qfits-dll qmake-release_shared qfitsview
debug_shared:   all_base qfits-dll-debug qmake-debug_shared qfitsview
else
# shared binaries forLinux/MacOS 
release_shared: all_base qmake-release_shared qfitsview
debug_shared:   all_base qmake-debug_shared qfitsview
endif

################################################################################
##### sub targets #####
# create documentation
doc_helpmap:
	$(MAKE) -C $(DP_PATH) doc/helpmap.cpp

# builds parser and dpuser2c-source-files
parsers:
	$(MAKE) -C $(DP_PATH) parsers

##### create Makefiles for QFitsView #####
# release-version, shared
qmake-release_shared:
	qmake QFitsView.pro "CONFIG+=release qf_shared"

# release-version, static, without dll's
qmake-release_static:
	qmake QFitsView.pro "CONFIG+=release qf_static"

# debug-version, shared
qmake-debug_shared:
	qmake QFitsView.pro "CONFIG+=debug qf_shared"

# debug-version, static
qmake-debug_static:
	qmake QFitsView.pro "CONFIG+=debug qf_static"

# special targets for Windows, shared-version
ifeq ($(OS), WIN)
qfits-dll:
	qmake QFitsView_dll.pro "CONFIG+=release qf_shared"
	$(MAKE) $(MULTIPROZGEIL) -f QFitsView_dll.mk

qfits-dll-debug:
	qmake QFitsView_dll.pro "CONFIG+=debug qf_shared"
	$(MAKE) $(MULTIPROZGEIL) -f QFitsView_dll.mk
endif

# effectively building QFitsView
qfitsview:
	$(MAKE) $(MULTIPROZGEIL) -f qfitsview.mk

################################################################################
clean:
	$(if $(DO_CLEAN:NO=), \
		$(MAKE) $(MULTIPROZGEIL) -if qfitsview.mk clean \
	)
#	$(MAKE) $(MULTIPROZGEIL) -C $(DP_PATH) clean_qf PLATFORM=$(OS)
	$(if $(OS:WIN=), \
		-$(RM) -rf QFitsView QFitsViewD qfitsview.mk qfitsview.mk.Debug qfitsview.mk.Release QFitsView.app QFitsViewD.app QFitsView.pro.user* ../include/vtk/vtkConfigure.h qfitsviewd_plugin_import.cpp qfitsview_plugin_import.cpp \
	, \
		$(RM) QFitsView_dll.mk QFitsView_dll.mk.Release QFitsView_dll.mk.Debug qfitsview.mk qfitsview.mk.Release qfitsview.mk.Debug object_script.QFitsView.Debug object_script.QFitsView.Release object_script.QFitsViewD.Debug object_script.QFitsViewD.Release /f; \
		$(RMDIR) /s /q release debug \
	)
		
#	$(if $(OS:WIN=), \
#		-$(RM) -rf QFitsView QFitsViewD qfitsview.mk qfitsview.mk.Debug qfitsview.mk.Release QFitsView.app QFitsViewD.app QFitsView.pro.user* ../include/vtk/vtkConfigure.h qfitsviewd_plugin_import.cpp \
#	, \
#		$(RM) QFitsView_dll.mk QFitsView_dll.mk.Release QFitsView_dll.mk.Debug qfitsview.mk qfitsview.mk.Release qfitsview.mk.Debug object_script.QFitsView.Debug object_script.QFitsView.Release object_script.QFitsViewD.Debug object_script.QFitsViewD.Release /f ;\
#		$(RMDIR) release debug \
#	)
	
#		$(MAKE) $(MULTIPROZGEIL) -f QFitsView_dll.mk clean; \
