# EKU: taken from Makefile.in.firebird
ROOT=..

include	$(ROOT)/gen/make.defaults
include	$(ROOT)/gen/make.platform
include	$(ROOT)/gen/make.rules
include	$(ROOT)/gen/make.shared.variables



DIR = $(ROOT)/gen/firebird/installer_scratch

export BLOCKSIZE


all: package

FB_INST_VERS:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/buildsuffix.c | tail -1 | sed -e 's/" "//g' -e 's/"//g' -e 's/Firebird[ ]*//' -e 's/Release Candidate/RC/' -e 's/ /-/g')
FB_VER_SUFFIX:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/buildsuffix.c | tail -1 | sed -e 's/" "//g' -e 's/"//g')
FB_VER_SUFFIX_SM:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/buildsuffix.c | tail -1 | sed -e 's/" "//g' -e 's/"//g' -e 's/Firebird[ ]*//' -e 's/Release Candidate/RC/')
FB_VER_SUFFIX_M:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/buildsuffix.c | tail -1 | sed -e 's/" "//g' -e 's/"//g' -e 's/Release Candidate/RC/')
FB_MAJOR_VERS:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/majorvers.c | tail -1 | sed -e 's/" "//g' -e 's/"//g')
FB_MINOR_VERS:=$(shell cpp -DDARWIN -I.  ../builds/install/arch-specific/darwin/minorvers.c | tail -1 | sed -e 's/" "//g' -e 's/"//g')
FB_REV_NO:=$(shell cpp -DDARWIN -I. ../builds/install/arch-specific/darwin/revno.c | tail -1 | sed -e 's/" "//g' -e 's/"//g')
FB_BUILD_NO:=$(shell cpp -DDARWIN -I. ../builds/install/arch-specific/darwin/buildno.c | tail -1 | sed -e 's/" "//g' -e 's/"//g')
FB_PLATFORM:=$(CpuType)

package packages dist: package_@FIREBIRD_ARCH_TYPE@

package_classic package_embedded: INST_NM=FirebirdCS-$(FB_MAJOR_VERS).$(FB_MINOR_VERS).$(FB_REV_NO)-$(FB_BUILD_NO)-$(FB_PLATFORM)
package_classic package_embedded: BLOCKSIZE=1048576
package_classic package_embedded:
	-rm -rf $(DIR)
	mkdir -p $(DIR)/t
	mv $(ROOT)/gen/firebird/frameworks/FirebirdCS.framework \
			$(DIR)/t/Firebird.framework
	sh -c 'cd $(DIR)/t; pax -w -f ../Archive.pax .; mkbom . ../Archive.bom'
	gzip $(DIR)/Archive.pax
	mv $(DIR)/t/Firebird.framework \
		$(ROOT)/gen/firebird/frameworks/FirebirdCS.framework
	mkdir -p $(DIR)/$(INST_NM).pkg/Contents/Resources
	mv $(DIR)/Archive.* $(DIR)/$(INST_NM).pkg/Contents
	cp ../builds/install/arch-specific/darwin/PkgInfo $(DIR)/$(INST_NM).pkg/Contents
	sed -e 's/_FB_BUILD_SUFFIX_/$(FB_VER_SUFFIX)/g' \
			-e 's/_SMFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_SM)/g' \
			-e 's/_MFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_M)/g' \
			-e 's/_MAJOR_VERS_/$(FB_MAJOR_VERS)/g' \
			-e 's/_MINOR_VERS_/$(FB_MINOR_VERS)/g' \
			-e 's/_REV_NO_/$(FB_REV_NO)/g' \
			../builds/install/arch-specific/darwin/Info.plist \
			> $(DIR)/$(INST_NM).pkg/Contents/Info.plist
	sed -e 's/_FB_BUILD_SUFFIX_/$(FB_VER_SUFFIX)/g' \
			-e 's/_SMFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_SM)/g' \
			-e 's/_MFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_M)/g' \
			-e 's/_MAJOR_VERS_/$(FB_MAJOR_VERS)/g' \
			-e 's/_MINOR_VERS_/$(FB_MINOR_VERS)/g' \
			-e 's/_REV_NO_/$(FB_REV_NO)/g' \
			../builds/install/arch-specific/darwin/Description.plist \
			> $(DIR)/$(INST_NM).pkg/Contents/Resources/Description.plist
	cp -r ../builds/install/arch-specific/darwin/Firebird.startupitem \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/StartupItem
	rm -rf `find $(DIR)/$(INST_NM).pkg/Contents/Resources/StartupItem -name CVS`
	cp ../builds/install/arch-specific/darwin/installer_background.jpg \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/background.jpg
	cp ../builds/install/arch-specific/darwin/install-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/postinstall
	cp ../builds/install/arch-specific/darwin/install-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/postupgrade
	cp ../builds/install/arch-specific/darwin/preinstall-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/preinstall
	cp ../builds/install/arch-specific/darwin/preupgrade-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/preupgrade
	chmod a+rx $(DIR)/$(INST_NM).pkg/Contents/Resources/pre*
	chmod a+rx $(DIR)/$(INST_NM).pkg/Contents/Resources/post*
	cp ../builds/install/arch-specific/darwin/*.txt \
			$(DIR)/$(INST_NM).pkg/Contents/Resources
	ln -s ../Archive.bom $(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).bom
	ln -s ../Archive.pax.gz \
				$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).pax.gz
	echo "NumFiles 0" > \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	echo -n "InstalledSize " >> \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	du -c $(ROOT)/gen/firebird/frameworks/FirebirdCS.framework \
			| tail -1 | sed -e 's/[\t ]*total//' \
			>> $(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	echo "CompressedSize 0" >> \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	-rm -rf $(ROOT)/gen/firebird/$(INST_NM).pkg
	mv $(DIR)/$(INST_NM).pkg $(ROOT)/gen/firebird
	rm -rf $(DIR)

package_super package_server: INST_NM=FirebirdSS-$(FB_MAJOR_VERS).$(FB_MINOR_VERS).$(FB_REV_NO)-$(FB_BUILD_NO)-$(FB_PLATFORM)
package_super package_server: BLOCKSIZE=1048576
package_super package_server:
	-rm -rf $(DIR)
	mkdir -p $(DIR)/t
	mv $(ROOT)/gen/firebird/frameworks/FirebirdSS.framework \
			$(DIR)/t/Firebird.framework
	sh -c 'cd $(DIR)/t; pax -w -f ../Archive.pax .; mkbom . ../Archive.bom'
	gzip $(DIR)/Archive.pax
	mv $(DIR)/t/Firebird.framework \
		$(ROOT)/gen/firebird/frameworks/FirebirdSS.framework
	mkdir -p $(DIR)/$(INST_NM).pkg/Contents/Resources
	mv $(DIR)/Archive.* $(DIR)/$(INST_NM).pkg/Contents
	cp ../builds/install/arch-specific/darwin/PkgInfo $(DIR)/$(INST_NM).pkg/Contents
	sed -e 's/_FB_BUILD_SUFFIX_/$(FB_VER_SUFFIX)/g' \
			-e 's/_SMFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_SM)/g' \
			-e 's/_MFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_M)/g' \
			-e 's/_MAJOR_VERS_/$(FB_MAJOR_VERS)/g' \
			-e 's/_MINOR_VERS_/$(FB_MINOR_VERS)/g' \
			-e 's/_REV_NO_/$(FB_REV_NO)/g' \
			../builds/install/arch-specific/darwin/Info.plist \
			> $(DIR)/$(INST_NM).pkg/Contents/Info.plist
	sed -e 's/_FB_BUILD_SUFFIX_/$(FB_VER_SUFFIX)/g' \
			-e 's/_SMFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_SM)/g' \
			-e 's/_MFB_BUILD_SUFFIX_/$(FB_VER_SUFFIX_M)/g' \
			-e 's/_MAJOR_VERS_/$(FB_MAJOR_VERS)/g' \
			-e 's/_MINOR_VERS_/$(FB_MINOR_VERS)/g' \
			-e 's/_REV_NO_/$(FB_REV_NO)/g' \
			../builds/install/arch-specific/darwin/Description.plist \
			> $(DIR)/$(INST_NM).pkg/Contents/Resources/Description.plist
	cp ../builds/install/arch-specific/darwin/installer_background.jpg \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/background.jpg
	cp ../builds/install/arch-specific/darwin/install-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/postinstall
	cp -r ../builds/install/arch-specific/darwin/Firebird.startupitem \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/StartupItem
	rm -rf `find $(DIR)/$(INST_NM).pkg/Contents/Resources/StartupItem -name CVS`
	cp ../builds/install/arch-specific/darwin/install-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/postupgrade
	cp ../builds/install/arch-specific/darwin/preinstall-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/preinstall
	cp ../builds/install/arch-specific/darwin/preupgrade-script \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/preupgrade
	chmod a+rx $(DIR)/$(INST_NM).pkg/Contents/Resources/pre*
	chmod a+rx $(DIR)/$(INST_NM).pkg/Contents/Resources/post*
	cp ../builds/install/arch-specific/darwin/*.txt \
			$(DIR)/$(INST_NM).pkg/Contents/Resources
	ln -s ../Archive.bom $(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).bom
	ln -s ../Archive.pax.gz \
				$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).pax.gz
	echo "NumFiles 0" > \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	echo -n "InstalledSize " >> \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	du -c $(ROOT)/gen/firebird/frameworks/FirebirdSS.framework \
			| tail -1 | sed -e 's/[\t ]*total//' \
			>> $(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	echo "CompressedSize 0" >> \
			$(DIR)/$(INST_NM).pkg/Contents/Resources/$(INST_NM).sizes
	-rm -rf $(ROOT)/gen/firebird/$(INST_NM).pkg
	mv $(DIR)/$(INST_NM).pkg $(ROOT)/gen/firebird
	rm -rf $(DIR)


install: install_@FIREBIRD_ARCH_TYPE@ 

	
install_super:
	sudo installer -verbose -dumplog -pkg \
			firebird/FirebirdSS-$(FB_MAJOR_VERS).$(FB_MINOR_VERS).$(FB_REV_NO)-$(FB_BUILD_NO)-$(FB_PLATFORM).pkg -target /

install_classic:
	sudo installer -verbose -dumplog -pkg \
			firebird/FirebirdCS-$(FB_MAJOR_VERS).$(FB_MINOR_VERS).$(FB_REV_NO)-$(FB_BUILD_NO)-$(FB_PLATFORM).pkg -target /

