####################################################################################
## Makefile for the preparation of the release and the package on Mac.
##
## This Makefile should be used after having built the package:
##	 
## on the ppc:
##   make -f Makefile_mac    (this will also call the configure script)
##   sudo make -f Makefile_mac release
## on the macIntel:
##   make -f Makefile_mac    (this will also call the configure script)
##   sudo make -f Makefile_mac release
## copy one of the /release_build dirs on the other machine in the directory /other
##   sudo make -f Makefile_mac universal
##
## .. voila', the package with the universal binaries is there.
##
## To build the SDK:
##   make -f Makefile_mac sdk
##
## To build a release without the 3rd party libs (so this release can
## be safely installed on your test machine):
##   make -f Makefile_mac    (this will also call the configure script)
##   sudo make -f Makefile_mac testrelease
##
## If you want to add or remove files from the release, please edit the following variables:
## BASE_LIBS --> implicit names of the libraries (as from the definition file *versions.mak)
## ETC --> implicit names of the config file(s)
## OLD_LIBS --> names of the old (V2.6) libs
## OLD_ETC --> names of the old (V2.6) config (are already universal binaries)
## BASE_SISPLUGINS --> implicit names of the libraries (as from the definition file *versions.mak)
## BASE_BINS --> names to the executables 
## APP_BINS --> paths to the executables 
## TRANSLATIONS --> language files for the GUI
## HEADERS --> header files needed to use the SDK
## CONFIG --> configuration files, e.g. for the GUI
## DOCS --> documentation files, man pages
## SCRIPTS --> script files
## ... 
####################################################################################
# check if the QTBASEDIR is set. If so, use this as a bas directory to copy the Qt libraries
# to the pkg.
# The environment variable is used to indicate which Qt libraries have to be taken.
ifdef QTBASEDIR
        QTFRAMEWORKSDIR:=$(QTBASEDIR)/lib/
	QTPLUGINDIR=$(QTBASEDIR)/plugins
else
        QTFRAMEWORKSDIR=/Library/Frameworks/
	QTPLUGINDIR=/Developer/Applications/Qt/plugins/
endif
QT4VERSION=$(shell otool -L $(QTFRAMEWORKSDIR)/QtCore.framework/QtCore)
VERSIONSTR=$(shell expr "$(QT4VERSION)" : '.*current version \(4\.[0-9]\.[0-9]\)')
QT_VERSION=beid_$(VERSIONSTR)



## If you want to build the release for another country,
## substitute beidversions.mak with the country-specific one (e.g. pteidversions.mak for Portugal)
## Other things to change:
## - value of REL_NAME
## - value of APPS_DIR

include beidversions.mak

REL_NAME=beid
PKCS11_BUNDLE = beid-pkcs11.bundle

## For Qt4 we will try to figure out the version starting from 'qmake -v'
## Sometimes qmake is link to the real qmake executable, therefore we first
## do a 'readlink' and then use the directory of it to find the libs etc. 
#QT_VERSION=beid_4.3.4

#QT4=$(shell which qmake)
#LINKPATH=$(shell readlink $(QT4))
#QMAKEPATH=$(shell which $(LINKPATH))
#THEPATH=$(dir $(QMAKEPATH))
#QT4VERSION=`$(THEPATH)/moc -v 2>&1`
#VERSIONSTR=$(shell expr "$(QT4VERSION)" : '.*\([1-9]\.[0-9]\.[0-9]\)')

#QT_VERSION=beid_$(VERSIONSTR)
#QT_BASEPATH=$(THEPATH)/..
ifdef UNIVERSAL
	PATH_TO_QTCORE:=$(shell otool -L ../bin/beiddialogsQTsrv | grep QtCore  | sed s/\(.*\)//)
	PATH_TO_QTGUI:=$(shell otool -L ../bin/beiddialogsQTsrv | grep QtGui  | sed s/\(.*\)//)
else
	PATH_TO_QTCORE:=$(shell otool -L ../bin/beiddialogsQTsrv.app/Contents/MacOS/beiddialogsQTsrv | grep QtCore  | sed s/\(.*\)//)
	PATH_TO_QTGUI:=$(shell otool -L ../bin/beiddialogsQTsrv.app/Contents/MacOS/beiddialogsQTsrv | grep QtGui  | sed s/\(.*\)//)
endif

UDIR=/univ
REL_DIR=/release_build

PKG_MAKER=/Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker

REL_VERSION=$(REL_MAJ).$(REL_MIN).$(REL_REV)
BUILD_NR=$(shell cat ../svn_revision | tr -d "\r")

PKG_NAME =   $(REL_DIR)/$(REL_NAME).pkg
DMG_NAME =   $(REL_NAME)_$(REL_VERSION).dmg
DMG_NAME_u = $(REL_NAME)_u_$(REL_VERSION).dmg
DMG_NAME_SDK = $(REL_NAME)_SDK_$(REL_VERSION)_$(BUILD_NR).dmg
VOL_NAME =   $(REL_NAME)_MacOSX_$(REL_VERSION)
VOL_NAME_SDK = $(REL_NAME)_SDK_MacOSX_$(REL_VERSION)

ROOT_DIR =  $(REL_DIR)/root
INST_DIR =  /usr/local
MAN_DIR  =  /usr/share/man/man1
SDK_DIR  = sdk

## Make interpretes spaces as separator. So we specify 2 names:
## the one that will be installed and the one to which this
## name will be renamed into in the postflight script at the end
## of the installation.
APPS_DIR =  /Applications/BelgiumIdentityCard
APPS_DIR_NAME = "/Applications/Belgium\ Identity\ Card"
EIDGUI_NAME = eID-Viewer

## List of the implicit names of the libraries. This is convenient
## when we want to substitue "x_y_z" with "x_y" to obtain e.g.
## DLGLIB_NAME_x_y_dylib and then use its value, resulting in libdialogsQT_1.0.dylib

BASE_LIBS =  DLGLIB_NAME_x_y_z_dylib \
	EIDLIB_NAME_x_y_z_dylib \
	CARDLAYERLIB_NAME_x_y_z_dylib \
	COMMONLIB_NAME_x_y_z_dylib \
	APPLAYERLIB_NAME_x_y_z_dylib \
	PKCS11LIB_NAME_x_y_z_dylib \
	JAVALIB_NAME_x_y_z_dylib

ETC = beid.conf

OLD_LIBS = beidlib.jar libbeidlibjni.2.7.2.dylib libbeid.2.7.4.dylib \
	libbeidlibopensc.2.2.0.dylib libbeidcommon.1.0.0.dylib \
	libbeidlibopenscinternal.2.2.0.dylib libbeidgui.1.5.0.dylib \
	libqt-mt.3.3.8.dylib

OLD_ETC = beidbase.conf beidgui.conf

#BASE_SISPLUGINS = CARDPLUGINSIS_ACR38U_NAME_dylib
BASE_SISPLUGINS = 


## This foreach command loops over the content of BASE_LIBS and substitute
## to each element $(element) --> LIBS contains $(DLGLIB_NAME_x_y_z_dylib),
## $(EIDLIB_NAME_x_y_z_dylib), etc. These variables are defined in beidversions.mak,
## therefore LIBS contains libdialogsQT_1.0.0.dylib, libeidlib_1.0.0.dylib, etc.

LIBS = $(foreach var,$(BASE_LIBS), lib/$($(var)) )

SISPLUGINS = $(foreach var,$(BASE_SISPLUGINS), lib/$($(var)) )
SISPLUGINS_BARE = $(foreach var,$(BASE_SISPLUGINS), $($(var)) )


BASE_BINS = $(DLGSRV)
#	$(CARDLAYERTOOL)
ifdef UNIVERSAL
	APP_BINS =  $(DLGSRV)
else
	APP_BINS =  $(DLGSRV).app/Contents/MacOS/$(DLGSRV)
endif
#       $(CARDLAYERTOOL)/Contents/MacOS/$(CARDLAYERTOOL)


## prepends 'bin/' to all the elements of BASE_BINS
BINS = $(APP_BINS:%= bin/%) 

## to be put in the APPS_DIR
APP_FILES = misc/$(REL_NAME)/$(REL_NAME)-pkcs11-register.html \
	misc/$(REL_NAME)/$(REL_NAME)-pkcs11-unregister.html


## Language files for the GUI
TRANSLATIONS = bin/$(EIDGUI).app/Contents/MacOS/eidmw_de.qm \
	       bin/$(EIDGUI).app/Contents/MacOS/eidmw_en.qm \
	       bin/$(EIDGUI).app/Contents/MacOS/eidmw_fr.qm \
	       bin/$(EIDGUI).app/Contents/MacOS/eidmw_nl.qm 

## SDK files to be included in the release
SDK_HEADERS_CPP = ../eidlib/eidlibdefines.h ../eidlib/eidlib.h ../common/eidErrors.h \
	../eidlib/eidlibException.h
SDK_SAMPLES1_CPP = ../sdk/samples/read_eid/C++/main.cpp
SDK_SAMPLES1_JAVA = ../sdk/samples/read_eid/Java/main.java
SDK_SAMPLES2_CPP = ../sdk/samples/wait_card/C++/main.cpp
SDK_SAMPLES2_JAVA = ../sdk/samples/wait_card/Java/main.java
SDK_SAMPLES3_CPP = ../sdk/samples/pin_eid/C++/main.cpp
SDK_SAMPLES3_JAVA = ../sdk/samples/pin_eid/Java/main.java
SDK_SAMPLES4_CPP = ../sdk/samples/exception/C++/main.cpp
SDK_SAMPLES4_JAVA = ../sdk/samples/exception/Java/get_exception.java
SDK_SAMPLES5_CPP = ../sdk/samples/events/C++/main.cpp
SDK_SAMPLES5_JAVA = ../sdk/samples/events/Java/main.java ../sdk/samples/events/Java/EventCallback.java \
		../sdk/samples/events/Java/EventData.java ../sdk/samples/events/Java/ReaderRef.java
SDK_SAMPLES6_CPP = ../sdk/samples/sign_p11/C++/main.cpp ../sdk/samples/sign_p11/C++/Makefile
SDK_SAMPLES6_JAVA = ../sdk/samples/sign_p11/java/sign_p11.java ../sdk/samples/sign_p11/java/*.sh
SDK_BASE_APPLET = ../eidlibJava_Applet/BEID_Applet.java ../jar/BEID_reader.html \
		../sdk/samples/misc/Applet/Basic/java/beid35test.store \
		../sdk/samples/misc/Applet/Basic/java/*.sh \
		../sdk/samples/misc/Applet/Basic/java/Readme.txt ../jar/*.rtf
SDK_IMAGE_APPLET = ../sdk/samples/misc/Applet/Image/java/excanvas.js \
		../sdk/samples/misc/Applet/Image/java/*.java \
		../sdk/samples/misc/Applet/Image/java/BEID_sample_base64.html \
		../sdk/samples/misc/Applet/Image/java/beid35test.store \
		../sdk/samples/misc/Applet/Image/java/*.sh ../jar/*.rtf
SDK_JAVA_BIN  = ../../ThirdParty/beid_sdk/3.5/Java/SignedJar/*.jar ../jar/*License.rtf \
		../jar/beid.jnlp ../jar/BEID_reader.html
SDK_JAVA_UNSIGNED = ../jar/BEID_Applet.jar ../jar/beid35libJava.jar ../jar/applet-launcher.jar \
		../../ThirdParty/beid_sdk/3.5/Java/*libJava_Wrapper* ../jar/*License.rtf
SDK_PKCS11_FILES = ../sdk/pkcs11/C++/include/*.h
SDK_COMMON_FILES = ../sdk/readme.txt ../misc/licenses_files/License*

## Man pages to be included in the release.
## Will be copied to $(ROOT_DIR)/$(MAN_DIR)
DOCS = 

## Scripts to be included in the release.
## Will be copied to $(ROOT_DIR)/$(INST_DIR)/bin
SCRIPTS = 


## directories that the package installer should create
## on the user's machine in case they are absent
DIRS_TO_CREATE = $(INST_DIR) $(APPS_DIR) /usr/local/lib/pkcs11

INSTALLER_DIR = misc/mac/mw_installer


## directories which will be created by the release rule
DIRS =	$(ROOT_DIR)/$(INST_DIR)/lib/siscardplugins \
	$(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins \
	$(ROOT_DIR)/$(INST_DIR)/bin \
	$(ROOT_DIR)/$(INST_DIR)/etc \
	$(ROOT_DIR)/$(INST_DIR)/include/$(REL_NAME) \
	$(ROOT_DIR)/usr/bin \
	$(ROOT_DIR)/$(MAN_DIR) \
	$(ROOT_DIR)/$(APPS_DIR) \
	$(ROOT_DIR)/$(APPS_DIR)/licenses \
	$(ROOT_DIR)/System/Library/Security/tokend

SDK_DIRS = $(SDK_DIR)/beidlib/C++/include \
	   $(SDK_DIR)/beidlib/Java/bin \
	   $(SDK_DIR)/beidlib/Java/unsigned \
	   $(SDK_DIR)/doc \
	   $(SDK_DIR)/samples/read_eid/C++ \
	   $(SDK_DIR)/samples/read_eid/Java \
	   $(SDK_DIR)/samples/wait_card/C++ \
	   $(SDK_DIR)/samples/wait_card/Java \
	   $(SDK_DIR)/samples/pin_eid/C++ \
	   $(SDK_DIR)/samples/pin_eid/Java \
	   $(SDK_DIR)/samples/exception/C++ \
	   $(SDK_DIR)/samples/exception/Java \
	   $(SDK_DIR)/samples/events/C++ \
	   $(SDK_DIR)/samples/events/Java \
	   $(SDK_DIR)/samples/sign_p11/C++ \
	   $(SDK_DIR)/samples/sign_p11/Java \
	   $(SDK_DIR)/pkcs11/C++/include \
	   $(SDK_DIR)/samples/misc/Applet/Image/Java \
	   $(SDK_DIR)/samples/misc/Applet/Basic/Java
	   

## The source file (or directories) and the destination are
## separated by 5 '_'. These will be parsed in copy_command

FILES_TO_COPY := $(LIBS:%=%_____$(ROOT_DIR)/$(INST_DIR)/lib/) \
		 $(OLD_LIBS:%=../beid-2.6/mac/lib/%_____$(ROOT_DIR)/$(INST_DIR)/lib/) \
		 $(ETC:%=misc/mac/%_____$(ROOT_DIR)/$(INST_DIR)/etc/) \
		 $(OLD_ETC:%=../beid-2.6/mac/etc/%_____$(ROOT_DIR)/$(INST_DIR)/etc/) \
		 $(SISPLUGINS:%=%_____$(ROOT_DIR)/$(INST_DIR)/lib/siscardplugins) \
	         $(TRANSLATIONS:%=%_____$(ROOT_DIR)/$(APPS_DIR)/$(EIDGUI).app/Contents/MacOS/) \
	         $(DOCS:%=%_____$(ROOT_DIR)/$(MAN_DIR)) \
	         $(SCRIPTS:%=%_____$(ROOT_DIR)/$(INST_DIR)/bin) \
                 $(BINS:%=%_____$(ROOT_DIR)/usr/bin) \
		 $(APP_FILES:%=%_____$(ROOT_DIR)/$(APPS_DIR))


SDK_FILES_TO_COPY := $(SDK_HEADERS_CPP:%=%_____$(SDK_DIR)/beidlib/C++/include) \
		     $(SDK_SAMPLES1_CPP:%=%_____$(SDK_DIR)/samples/read_eid/C++) \
		     $(SDK_SAMPLES1_JAVA:%=%_____$(SDK_DIR)/samples/read_eid/Java) \
		     $(SDK_SAMPLES2_CPP:%=%_____$(SDK_DIR)/samples/wait_card/C++) \
		     $(SDK_SAMPLES2_JAVA:%=%_____$(SDK_DIR)/samples/wait_card/Java) \
		     $(SDK_SAMPLES3_CPP:%=%_____$(SDK_DIR)/samples/pin_eid/C++) \
		     $(SDK_SAMPLES3_JAVA:%=%_____$(SDK_DIR)/samples/pin_eid/Java) \
		     $(SDK_SAMPLES4_CPP:%=%_____$(SDK_DIR)/samples/exception/C++) \
		     $(SDK_SAMPLES4_JAVA:%=%_____$(SDK_DIR)/samples/exception/Java) \
		     $(SDK_SAMPLES5_CPP:%=%_____$(SDK_DIR)/samples/events/C++) \
		     $(SDK_SAMPLES5_JAVA:%=%_____$(SDK_DIR)/samples/events/Java) \
		     $(SDK_SAMPLES6_CPP:%=%_____$(SDK_DIR)/samples/sign_p11/C++) \
		     $(SDK_SAMPLES6_JAVA:%=%_____$(SDK_DIR)/samples/sign_p11/Java) \
		     $(SDK_IMAGE_APPLET:%=%_____$(SDK_DIR)/samples/misc/Applet/Image/java) \
		     $(SDK_BASE_APPLET:%=%_____$(SDK_DIR)/samples/misc/Applet/Basic/java) \
		     $(SDK_JAVA_BIN:%=%_____$(SDK_DIR)/beidlib/Java/bin) \
		     $(SDK_JAVA_UNSIGNED:%=%_____$(SDK_DIR)/beidlib/Java/unsigned) \
		     $(SDK_PKCS11_FILES:%=%_____$(SDK_DIR)/pkcs11/C++/include) \
		     $(SDK_COMMON_FILES:%=%_____$(SDK_DIR))

DIRS_TO_COPY := bin/$(EIDGUI).app_____$(ROOT_DIR)/$(APPS_DIR) \
		misc/mac/OOoRegister/OpenOffice_Register.pkg_____$(ROOT_DIR)/$(APPS_DIR) \
		misc/mac/OOo.default_____$(ROOT_DIR)/$(INST_DIR)/lib \
		bin/BEID.tokend_____$(ROOT_DIR)/System/Library/Security/tokend \
		../beid-2.6/mac/share/_____$(ROOT_DIR)/$(INST_DIR)/share

r1=$(ROOT_DIR)
r2=/other/$(ROOT_DIR)
r3=$(UDIR)/$(ROOT_DIR)
 
## List of binaries for which we have to run the lipo command.
## The substitution command prepends $(r3)/$(INST_DIR)/ to all the elements of LIBS and
## $(r3)/$(APPS_DIR)/ to all the elements of BASE_BINS
UNIVERSAL = $(LIBS:%=$(r3)/$(INST_DIR)/%) \
	    $(SISPLUGINS_BARE:%=$(r3)/$(INST_DIR)/lib/siscardplugins/%) \
	    $(BASE_BINS:%=$(r3)/usr/bin/%) \
	    $(r3)/$(APPS_DIR)/$(EIDGUI).app/Contents/MacOS/$(EIDGUI) \
	    $(r3)/System/Library/Security/tokend/BEID.tokend/Contents/MacOS/BEID


#####################################################################
# Rules								    #
#####################################################################
all:
	cd .. && ./configure BUILD_TESTS=y CONFIG+=release && $(MAKE) && cd -
	cd ../tokend && ./buildtokend.sh && cd -
	cd ../misc/mac/OOoRegister && make && cd -

################################################################
clean:
	cd .. && $(MAKE) clean && cd -

################################################################
install:
	cd .. && $(MAKE) install && cd -

################################################################
uninstall: 
	cd .. && $(MAKE) uninstall && cd -

################################################################
POSTFLIGHT=$(REL_DIR)/resources/postflight
PREFLIGHT=$(REL_DIR)/resources/preflight

release: clean_release $(DIRS) rel_start rel_3rd_party rel_end

testrelease: clean_release $(DIRS) rel_start  rel_end

rel_start:
	-@ cd ..; \
	  echo "********** copy dirs **********"; \
	  $(call copy_command,$(DIRS_TO_COPY),-r)

	@## copy files, follow symbolic links
	-@ cd ..; \
	  echo "********** copy files **********"; \
	  $(call copy_command,$(FILES_TO_COPY),-L) ; \
	  cp misc/licenses_files/Dutch/eID-toolkit_licensingtermsconditions.txt \
		$(ROOT_DIR)/$(APPS_DIR)/licenses/license_NL.txt ; \
	  cp misc/licenses_files/English/eID-toolkit_licensingtermsconditions.txt \
		$(ROOT_DIR)/$(APPS_DIR)/licenses/license_EN.txt ; \
	  cp misc/licenses_files/French/eID-toolkit_licensingtermsconditions.txt \
		$(ROOT_DIR)/$(APPS_DIR)/licenses/license_FR.txt ; \
	  cp misc/licenses_files/German/eID-toolkit_licensingtermsconditions.txt \
		$(ROOT_DIR)/$(APPS_DIR)/licenses/license_DE.txt ; \
	  cp misc/licenses_files/THIRDPARTY-LICENSES.txt $(ROOT_DIR)/$(APPS_DIR)/licenses/

	@echo "********** copy/rename the pkcs11 bundle **********"; \
	 cp -r ../misc/mac/pkcs11.bundle $(ROOT_DIR)/$(INST_DIR)/lib/$(PKCS11_BUNDLE)

	@echo "********** change ACs of the OOo.default files ***********"; \
	 chmod 644 $(ROOT_DIR)/$(INST_DIR)/lib/OOo.default/*
	 chmod 755 $(ROOT_DIR)/$(INST_DIR)/lib/OOo.default/

rel_3rd_party:
	@echo "********** copy third party files (QT, ...) **********"
	@echo "QTBASEDIR:$(QTBASEDIR)"
	 mkdir -p $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/
	 cp $(QTFRAMEWORKSDIR)/QtGui.framework/Versions/4/QtGui	$(ROOT_DIR)/$(INST_DIR)/lib/beidqt/
	 cp $(QTFRAMEWORKSDIR)/QtCore.framework/Versions/4/QtCore  $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/
	 mkdir -p $(ROOT_DIR)/$(INST_DIR)/lib
	 cp ../../ThirdParty/Xerces/Xerces-2.8.0-mac/lib/libxerces-c.28.0.dylib $(ROOT_DIR)/$(INST_DIR)/lib/libxerces-c.28.0.dylib 
	 mkdir -p $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins/imageformats/
	 cp $(QTPLUGINDIR)/imageformats/libqjpeg.dylib $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins/imageformats/libbeidjpeg.dylib
	 mkdir -p $(ROOT_DIR)/$(APPS_DIR)/$(EIDGUI).app/Contents/Resources
	 @echo "********** Changing install_names for use with the QT libs **********"
	 install_name_tool -change $(PATH_TO_QTCORE) \
		$(INST_DIR)/lib/beidqt/QtCore \
		$(ROOT_DIR)/usr/bin/$(DLGSRV)
	 install_name_tool -change $(PATH_TO_QTGUI) \
		$(INST_DIR)/lib/beidqt/QtGui \
		$(ROOT_DIR)/usr/bin/$(DLGSRV)
	 install_name_tool -change $(PATH_TO_QTCORE) \
		$(INST_DIR)/lib/beidqt/QtCore \
		$(ROOT_DIR)/$(APPS_DIR)/$(EIDGUI).app/Contents/MacOS/$(EIDGUI)
	 install_name_tool -change $(PATH_TO_QTGUI) \
		$(INST_DIR)/lib/beidqt/QtGui \
		$(ROOT_DIR)/$(APPS_DIR)/$(EIDGUI).app/Contents/MacOS/$(EIDGUI)
	 install_name_tool -change QtCore.framework/Versions/4/QtCore \
		$(INST_DIR)/lib/beidqt/QtCore \
		$(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins/imageformats/libbeidjpeg.dylib
	 install_name_tool -change QtGui.framework/Versions/4/QtGui \
		$(INST_DIR)/lib/beidqt/QtGui \
		$(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins/imageformats/libbeidjpeg.dylib
	 install_name_tool -change $(PATH_TO_QTCORE) \
		$(INST_DIR)/lib/beidqt/QtCore \
		$(ROOT_DIR)/$(INST_DIR)/lib/beidqt/QtGui

rel_end:
	@echo "********** manipulate mw_installer **********"
	-@cd ..; \
	  sed -e "s;REPL_REL_VERSION;$(REL_VERSION);g" \
	  	   $(INSTALLER_DIR)/Info.plist > $(REL_DIR)/Info.plist ; \
	  cp $(INSTALLER_DIR)/Description.plist  $(REL_DIR)/Description.plist ; \
	  cp -r $(INSTALLER_DIR)/resources/ $(REL_DIR)/resources; \
	  sed -e "s;REL_NAME;$(REL_NAME);g" -e "s;PKCS11LIB_NAME_dylib;$(PKCS11LIB_NAME_dylib);g" \
	  	   misc/mac/pkcs11.bundle/Contents/Info.plist > \
	  	   $(ROOT_DIR)/$(INST_DIR)/lib/$(PKCS11_BUNDLE)/Contents/Info.plist

	 @echo "********** generate $(POSTFLIGHT) **********"
	-@test -e $(POSTFLIGHT) && rm $(POSTFLIGHT)
	@touch $(POSTFLIGHT)
	@echo "#!/bin/bash" >> $(POSTFLIGHT)
	@$(call test_and_link_command, $(BASE_LIBS),x_y_z_,x_y_,  $(POSTFLIGHT))
	@$(call test_and_link_command, $(foreach var,$(BASE_LIBS), $(subst x_y_z_,x_y_,$(var)) ),x_y_,x_, $(POSTFLIGHT))
	@$(call test_and_link_command, $(foreach var,$(BASE_LIBS), $(subst x_y_z_,x_,$(var)) ),x_,, $(POSTFLIGHT))
	## Old BE pkcs11 bundle was also put /usr/local/lib/pkcs11/Belgium-EID-pkcs11.bundle,
	## so we create this bundle too for backward compatibility
	@echo "cp -r $(INST_DIR)/lib/$(PKCS11_BUNDLE)/ /usr/local/lib/pkcs11/Belgium-EID-pkcs11.bundle/" >> $(POSTFLIGHT)
	@echo "test -e $(INST_DIR)/lib/$(PKCS11_BUNDLE)/Contents/MacOS/$(PKCS11LIB_NAME_dylib) && unlink $(INST_DIR)/lib/$(PKCS11_BUNDLE)/Contents/MacOS/$(PKCS11LIB_NAME_dylib)" >> $(POSTFLIGHT)
	@echo "ln -fs $(INST_DIR)/lib/$(PKCS11LIB_NAME_x_y_z_dylib) $(INST_DIR)/lib/$(PKCS11_BUNDLE)/Contents/MacOS/$(PKCS11LIB_NAME_dylib)" >> $(POSTFLIGHT)
	@echo "test -e /usr/local/lib/pkcs11/Belgium-EID-pkcs11.bundle/Contents/MacOS/$(PKCS11LIB_NAME_dylib) && unlink /usr/local/lib/pkcs11/Belgium-EID-pkcs11.bundle/Contents/MacOS/$(PKCS11LIB_NAME_dylib)" >> $(POSTFLIGHT)
	@echo "ln -fs $(INST_DIR)/lib/$(PKCS11LIB_NAME_x_y_z_dylib) /usr/local/lib/pkcs11/Belgium-EID-pkcs11.bundle/Contents/MacOS/$(PKCS11LIB_NAME_dylib)" >> $(POSTFLIGHT)
	@echo "" >> $(POSTFLIGHT)
	@echo "test -e $(INST_DIR)/lib/libxerces-c.28.dylib && unlink $(INST_DIR)/lib/libxerces-c.28.dylib" >> $(POSTFLIGHT)
	@echo "ln -fs $(INST_DIR)/lib/libxerces-c.28.0.dylib $(INST_DIR)/lib/libxerces-c.28.dylib" >> $(POSTFLIGHT)
	@echo "test -e /Library/Java/Extensions/$(JAVALIB_NAME).jnilib && unlink /Library/Java/Extensions/$(JAVALIB_NAME).jnilib" >> $(POSTFLIGHT)
	@echo "ln -fs /usr/local/lib/$(JAVALIB_NAME_x_y_z_dylib) /Library/Java/Extensions/$(JAVALIB_NAME).jnilib" >> $(POSTFLIGHT)
	@echo "mv $(APPS_DIR)/$(EIDGUI).app $(APPS_DIR)/$(EIDGUI_NAME).app" >> $(POSTFLIGHT)
	@echo "mv $(APPS_DIR) \"$(APPS_DIR_NAME)\"" >> $(POSTFLIGHT)
	-@test -e tmp && rm temp
	@cat $(POSTFLIGHT) ../../beid-2.6/mac/postflight > temp
	@mv temp $(POSTFLIGHT)
	@chmod a+x $(POSTFLIGHT)

	 @echo "********** generate $(PREFLIGHT) **********"
	-@test -e $(PREFLIGHT) && rm $(PREFLIGHT)
	@touch $(PREFLIGHT)
	@echo "#!/bin/bash" >> $(PREFLIGHT)
	@echo "## Remove parts (GUI, Tray applet, pkcs11) of the previous middleware parts if they exist" >> $(PREFLIGHT)
	@echo "test -e \"$(APPS_DIR_NAME)\" && rm -rf \"$(APPS_DIR_NAME)\"" >> $(PREFLIGHT)
	@echo "test -e /usr/local/lib/$(PKCS11_BUNDLE) && rm -rf /usr/local/lib/$(PKCS11_BUNDLE)" >> $(PREFLIGHT)
	@echo "test -e /usr/local/lib/pkcs11/$(PKCS11_BUNDLE) && rm -rf /usr/local/lib/pkcs11/$(PKCS11_BUNDLE)" >> $(PREFLIGHT)
	@echo "test -e /usr/local/lib/beid-pkcs11.bundle && rm -rf /usr/local/lib/beid-pkcs11.bundle" >> $(PREFLIGHT)
	@echo "" >> $(PREFLIGHT)
	@$(call mkdir_command, $(DIRS_TO_CREATE), $(PREFLIGHT))
	@chmod a+x $(PREFLIGHT)

	@echo "********** generate $(PKG_NAME) and $(DMG_NAME) **********"
	@rm -rf $(PKG_NAME)
	
	@chmod g+w $(ROOT_DIR)/Applications
	@chmod g+w $(ROOT_DIR)/$(INST_DIR)
	@chmod g+w $(ROOT_DIR)/$(INST_DIR)/lib
	@chmod g+w $(ROOT_DIR)/$(INST_DIR)/lib/beidqt
	@chmod g+w $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins
	@chmod g+w $(ROOT_DIR)/$(INST_DIR)/lib/beidqt/plugins/imageformats
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/etc/beid.conf
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/etc/beidbase.conf
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/etc/beidgui.conf
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/lib/beidlib.jar
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/lib/beid-pkcs11.bundle/Contents/Info.plist
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/lib/beid-pkcs11.bundle/Contents/PkgInfo
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/share/beid/certs/beid-cert-belgiumrca.der
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/share/beid/certs/beid-cert-government.der
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/share/beid/certs/beid-cert-government2004.der
	@chmod a-x $(ROOT_DIR)/$(INST_DIR)/share/beid/certs/beid-cert-government2005.der
	@chmod a+x  $(ROOT_DIR)/$(INST_DIR)/lib/libbeid*.dylib
	@chmod a+x  $(ROOT_DIR)/$(INST_DIR)/lib/libqt-mt*.dylib
	@chgrp -R wheel  $(ROOT_DIR)/System
	@chgrp    wheel  $(ROOT_DIR)/usr
	@chgrp    wheel  $(ROOT_DIR)/usr/bin
	@chgrp    wheel  $(ROOT_DIR)/usr/local
	@chgrp    wheel  $(ROOT_DIR)/usr/share
	@chgrp    wheel  $(ROOT_DIR)/usr/local/lib
	@chgrp    wheel  $(ROOT_DIR)/usr/share/man
	@chgrp    wheel  $(ROOT_DIR)/usr/local/lib/siscardplugins
	@chgrp    wheel  $(ROOT_DIR)/usr/local/lib/beidqt
	@chgrp    wheel  $(ROOT_DIR)/usr/local/lib/beidqt/plugins
	@chgrp    wheel  $(ROOT_DIR)/usr/share/man/man1
	@chgrp -R admin  $(ROOT_DIR)/System/Library/Security/tokend/BEID.tokend

	$(PKG_MAKER) -build -p $(PKG_NAME) -f $(r1) -ds -i $(REL_DIR)/Info.plist \
		 -d $(REL_DIR)/Description.plist -r $(REL_DIR)/resources  -v
	hdiutil create -srcfolder $(PKG_NAME) -volname "$(VOL_NAME)" $(DMG_NAME)
	#hdiutil internet-enable -yes $(DMG_NAME)

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

universal: clean_universal $(UNIVERSAL)
	# "This expects that 'sudo make release' was already done on this machine;"
	# "as well as on the other architecture, and that the release_build"
	# "from that architecture was copied a dir called 'other'"

	@echo "********** generate $(UDIR)/$(PKG_NAME) and $(DMG_NAME_u) **********"
	rm -rf $(UDIR)/$(PKG_NAME)
	$(PKG_MAKER) -build -p $(UDIR)/$(PKG_NAME) -f $(r3) -ds -i $(UDIR)/$(REL_DIR)/Info.plist \
		-d $(UDIR)/$(REL_DIR)/Description.plist -r $(UDIR)/$(REL_DIR)/resources  -v
	hdiutil create -srcfolder $(UDIR)/$(PKG_NAME) -volname "$(VOL_NAME)" $(DMG_NAME_u)
	#hdiutil internet-enable -yes $(DMG_NAME_u)


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

sdk: cleanSDK $(SDK_DIRS)
	$(call copy_command,$(SDK_FILES_TO_COPY),-L)

	cp "../sdk/samples/read_eid/C++/Makefile.mac" $(SDK_DIR)/samples/read_eid/C++/Makefile
	cp "../sdk/samples/wait_card/C++/Makefile.mac" $(SDK_DIR)/samples/wait_card/C++/Makefile
	cp "../sdk/samples/pin_eid/C++/Makefile.mac" $(SDK_DIR)/samples/pin_eid/C++/Makefile
	cp "../sdk/samples/exception/C++/Makefile.mac" $(SDK_DIR)/samples/exception/C++/Makefile
	cp "../sdk/samples/events/C++/Makefile.mac" $(SDK_DIR)/samples/events/C++/Makefile

	cp ../sdk/samples/read_eid/Java/mac_compile_read_eid.sh \
		$(SDK_DIR)/samples/read_eid/Java/compile_read_eid.sh
	cp ../sdk/samples/read_eid/Java/mac_run_read_eid.sh \
		$(SDK_DIR)/samples/read_eid/Java/run_read_eid.sh
	cp ../sdk/samples/wait_card/Java/mac_compile_wait_card.sh \
		$(SDK_DIR)/samples/wait_card/Java/compile_wait_card.sh
	cp ../sdk/samples/wait_card/Java/mac_run_wait_card.sh \
		$(SDK_DIR)/samples/wait_card/Java/run_wait_card.sh
	cp ../sdk/samples/pin_eid/Java/mac_compile_pin_eid.sh \
		$(SDK_DIR)/samples/pin_eid/Java/compile_pin_eid.sh
	cp ../sdk/samples/pin_eid/Java/mac_run_pin_eid.sh \
		$(SDK_DIR)/samples/pin_eid/Java/run_pin_eid.sh
	cp ../sdk/samples/exception/Java/mac_compile_exception.sh \
		$(SDK_DIR)/samples/exception/Java/compile_exception.sh
	cp ../sdk/samples/exception/Java/mac_run_exception.sh \
		$(SDK_DIR)/samples/exception/Java/run_exception.sh
	cp ../sdk/samples/events/Java/mac_compile_events.sh \
		$(SDK_DIR)/samples/events/Java/compile_events.sh
	cp ../sdk/samples/events/Java/mac_run_events.sh \
		$(SDK_DIR)/samples/events/Java/run_events.sh
	cp ../sdk/samples/misc/Applet/Image/Java/compile.sh \
		$(SDK_DIR)/samples/misc/Applet/Image/Java
	chmod +x $(SDK_DIR)/samples/misc/Applet/Image/Java/compile.sh

	cd ../_DocsInternal/API; ./doxygen_sdk.sh
	cp -r ../_DocsExternal/C++_Api $(SDK_DIR)/doc/C++_Api
	cp -r ../_DocsExternal/Java_Api $(SDK_DIR)/doc/Java_Api

	hdiutil create -srcfolder $(SDK_DIR) -volname "$(VOL_NAME_SDK)" $(DMG_NAME_SDK)

################################################################
## create directories in list DIRS intermediate directories as well

$(DIRS): FORCE
	@echo "mkdir $@"
	@mkdir -p $@

################################################################
## create directories in list SDK_DIRS intermediate directories as well

$(SDK_DIRS): FORCE
	@echo "mkdir $@"
	@mkdir -p $@

################################################################
cleanSDK:
	- [ -d $(SDK_DIR) ] && rm -rf $(SDK_DIR)
	- [ -e $(DMG_NAME_SDK) ] && rm $(DMG_NAME_SDK)

################################################################
## lipo command explanation:
## $@ -> one element in the $(UNIVERSAL) list (which is a lib or
## an executable with path in $(r3)   (1)
## $(patsubst $(r3)%,$(r1)%,$@) -> substitutes $(r1) to $(r3) in the path of $@   (2)
## $(patsubst $(r3)%,$(r2)%,$@) -> substitutes $(r2) to $(r3) in the path of $@   (3)
## lipo the results of (2) and (3) to create (1)

$(UNIVERSAL) : 	FORCE
	-lipo $(patsubst $(r3)%,$(r1)%,$@) $(patsubst $(r3)%,$(r2)%,$@)  -output $@ -create


################################################################
clean_universal:
	- rm -rf $(UDIR)
	- rm $(DMG_NAME_u)
	mkdir $(UDIR)
	cp -r $(REL_DIR) $(UDIR)/

################################################################
clean_release: 
	- [ -d $(REL_DIR) ] && rm -rf $(REL_DIR)
	- [ -e $(DMG_NAME) ] && rm $(DMG_NAME)


################################################################
## phony target used as a dependency in some cases 
## to enforce the update of other targets
FORCE:
	
	


##############################################################
# Helper functions					     #
##############################################################
##							     #
## These functions generate a sequence of bash commands.     #
## Each command is run in its own shell, therefore they have #
## to be chained with ";"  if one wants to use the result of #
## a command in the following one.			     #
## "\" extends the command over multiple lines.		     #
##							     #
##############################################################

## Copy files or directory with option $(2) (which might be empty)
## The input $(1) contains strings of the form <source>_____<destination>,
## which are parsed to define the source file and the destination file.

define copy_command
for FILE in $(1); do \
  SOURCE=$$( echo $$FILE| sed "s#\(.*\)_____.*#\1#"); \
  DESTINATION=$$(echo $$FILE | sed "s#.*_____\(.*\)#\1#"); \
  echo "copy $(2) $$SOURCE --> $$DESTINATION" ; \
  cp $(2) $${SOURCE} $${DESTINATION}; \
done 
endef

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

## $(1) -> list of variables, the value of which is the name of a library
## $(2) -> string in $(var) to be substituted
## $(3) -> string with which $(2) is substituted
## $(4) -> output file
##
## This function produces lines of the type:
##
## test -e $(INST_DIR)/lib/<lib_x_y_dylib> && unlink  $(INST_DIR)/lib/<lib_x_y_dylib>
## ln -fs $(INST_DIR)/lib/<lib_x_y_z_dylib> $(INST_DIR)/lib/<lib_x_y_dylib>
##
## and sends them to the file specified by argument 4.
##

define test_and_link_command
$(foreach var,$(1),  echo "test -e $(INST_DIR)/lib/$($(subst $(2),$(3),$(var))) && unlink $(INST_DIR)/lib/$($(subst $(2),$(3),$(var)))" >> $(4) ; \
	 echo "ln -fs $(INST_DIR)/lib/$($(var)) $(INST_DIR)/lib/$($(subst $(2),$(3),$(var)))" >> $(4); ) \
echo >> $(4)
endef

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

## Receives in input a list of directory, $(1), and a filename $(2).
## For each elements of the list produces a mkdir command and pipes it to $(2).

define mkdir_command
$(foreach var,$(1),  echo "## Create $(var) if it does not exist yet" >> $(2); \
			echo "mkdir -p $(var)" >> $(2) ; \
			echo "" >> $(2); ) 
endef

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