#
#  The contents of this file are subject to the Initial
#  Developer's Public License Version 1.0 (the "License");
#  you may not use this file except in compliance with the
#  License. You may obtain a copy of the License at
#  http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
#
#  Software distributed under the License is distributed AS IS,
#  WITHOUT WARRANTY OF ANY KIND, either express or implied.
#  See the License for the specific language governing rights
#  and limitations under the License.
#
#  The Original Code was created by Mark O'Donohue
#  for the Firebird Open Source RDBMS project.
#
#  Copyright (c) Mark O'Donohue <mark.odonohue@ludwig.edu.au>
#  and all contributors signed below.
#
#  All Rights Reserved.
#  Contributor(s): ______________________________________.
#
# 
#
ROOT=..



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


 @SET_MAKE@

.PHONY: install dist packages tarfile rpmfile 
.PHONY: buildTarDir buildImageDir buildRoot buildDebugInfo

# Some of these targets are run from the root tree of the build.
# those need to know where the install subdirectory is kept.
# Others are run locally these need to know the path back to the
# root of the build tree.

BuildRoot=$(GEN_ROOT)

InstallRoot=.

BuildVersion=$(FirebirdVersion).$(BuildNum)

Version=$(BuildVersion)-$(PackageVersion)



# This means something like: FirebirdCS-2.0.0-1.i386.tar.gz

TarDir= Firebird$(ArchPrefix)-$(Version).$(CpuType)
TarFile=Firebird$(ArchPrefix)-$(Version).$(CpuType).tar.gz
RPMFile=Firebird$(ArchPrefix)-$(Version).$(CpuType).rpm
DebugDir=Firebird$(ArchPrefix)-debuginfo-$(Version).$(CpuType)
DebugFile=Firebird$(ArchPrefix)-debuginfo-$(Version).$(CpuType).tar.gz


# Determine where the packages are built.  Each distribution builds them in
# their own spot.
# This should be moved to configure.in file
RedhatPackageDir=/usr/src/redhat/RPMS/$(CpuType)
MandrakePackageDir=/usr/src/RPM/RPMS/$(CpuType)
SuSEPackageDir=/usr/src/packages/RPMS/$(CpuType)

PackageDir:=$(shell if [ -d $(RedhatPackageDir) ]; then echo $(RedhatPackageDir); \
		           elif [ -d $(MandrakePackageDir) ]; then echo $(MandrakePackageDir); \
		           elif [ -d $(SuSEPackageDir) ]; then echo $(SuSEPackageDir); \
				   fi)



PkgSrcDir=$(InstallRoot)/$(ArchType)
ScriptDir=$(GEN_ROOT)/install/scripts/
RPMSrcDir=$(GEN_ROOT)/install/scripts


TarInstallDir=$(GEN_ROOT)/install

#Target for required libraries detection
RpmRequiredVersionTarget=$(GEN_ROOT)/firebird/bin/isql
#Curses library
TermLib=@TERMLIB@.so


dist : packages

packages: tarfile rpmfile debugfile

tarfile:  $(TarFile)

$(TarFile) :  buildTarDir
	(cd $(GEN_ROOT); tar -czf $(TarFile) $(TarDir))

debugfile:  $(DebugFile)

$(DebugFile) :  buildImageDir
	(cd $(GEN_ROOT)/$(DebugDir); tar -czf ../$(DebugFile) *)

# objcopy --add-gnu-debuglink fails with binutils 2.14 or older
# its failure affects only GDB ability to pick up external debug 
# symbols automatically. We may use eu-strip command from elfutils 
# instead, but it doesn't work or compile with such distros 
# as Mandrake 9.0 not saying about older ones. New binutils
# seem to compile and work everywhere I tried, but we try to 
# use eu-strip first since it is a part of default Red Hat 9 
# install and is available in binary form for Mandrake 9.1 and newer.
#
#     28-Dec-2003. Nickolay Samofatov
#
# Do not use eu-strip any longer because version 0.89/AMD64 shipped with 
# Mandrake 9.2 produces broken debuginfo packages and there is no easy way to 
# detect the breakage. Binutils seem to be more reliable alternative to me.
#
#     05-Apr-2004. Nickolay Samofatov
#

buildRoot:
	(cd $(GEN_ROOT); ./install/makeInstallImage.sh)

buildDebugInfo: buildRoot
	mkdir -p $(GEN_ROOT)/$(DebugDir)
	for x in `(cd $(GEN_ROOT)/buildroot; find)`; do \
        FIL=$(GEN_ROOT)/buildroot/$$x; \
        D_FIL=$(GEN_ROOT)/$(DebugDir)/`dirname $${x}`/.debug/`basename $${x}`.debug; \
        if [ ! -h $$FIL ] && readelf -h $$FIL > /dev/null 2> /dev/null; then \
            mkdir -p $(GEN_ROOT)/$(DebugDir)/`dirname $$x`/.debug; \
            echo Generating external debug info for $$x; \
            cp $${FIL} $${D_FIL}; \
            objcopy --add-gnu-debuglink=$${D_FIL} $${FIL}; \
            echo Stripping unneeded symbols from $${x}; \
            objcopy --strip-debug --strip-unneeded $${FIL}; \
        fi; \
    done
# Work around GDB 6.0 bug
#	mkdir -p $(GEN_ROOT)/$(DebugDir)@libdir@/.debug
#	for x in `ls $(GEN_ROOT)/$(DebugDir)@prefix@/lib/.debug`; do \
#       ln -f -s @prefix@/lib/.debug/`basename $$x` $(GEN_ROOT)/$(DebugDir)@libdir@/.debug; \
#   done

# Use this line if you don't want to use separate debug info
#buildImageDir: buildRoot
buildImageDir: buildDebugInfo


buildTarDir: buildImageDir
	mkdir -p $(GEN_ROOT)/$(TarDir)
	cp -r $(GEN_ROOT)/install/scripts $(GEN_ROOT)/$(TarDir)
	cp -r $(GEN_ROOT)/install/scripts/tarMainInstall.sh $(GEN_ROOT)/$(TarDir)/install.sh
	chmod a+x $(GEN_ROOT)/$(TarDir)/install.sh $(GEN_ROOT)/$(TarDir)/scripts/*sh
	(cd $(GEN_ROOT)/$(TarDir); tar -C ../buildroot -czf buildroot.tar.gz . )
	(cd $(GEN_ROOT)/$(TarDir); tar -tzf buildroot.tar.gz > manifest.txt )


install: buildTarDir
	(cd $(GEN_ROOT)/$(TarDir); FIREBIRD= FIREBIRD_LOCK= ./install.sh)
	cp -r $(GEN_ROOT)/$(DebugDir)/* /


silent_install: buildTarDir
	(cd $(GEN_ROOT)/$(TarDir); FIREBIRD= FIREBIRD_LOCK= ./install.sh -silent)
	cp -r $(GEN_ROOT)/$(DebugDir)/* /


rpmfile: $(RPMFile)

$(RPMFile) : $(GEN_ROOT)/install/rpmscript buildImageDir
	rpmbuild --define='_topdir @BUILD_ROOT_DIR@/gen/RPM' -bb --target @CPU_TYPE@ $(GEN_ROOT)/install/rpmscript
#	rpmbuild -bb --buildroot `pwd`/buildroot $(GEN_ROOT)/rpmscript
	cp @BUILD_ROOT_DIR@/gen/RPM/RPMS/@CPU_TYPE@/$(RPMFile) $(GEN_ROOT)


.PHONY: $(GEN_ROOT)/install/rpmscript

$(GEN_ROOT)/install/rpmscript :
	echo $@
	awk < $(RPMSrcDir)/rpmheader.txt 'BEGIN {fl=1;} ($$1 == "Requires:") {fl=0;} (fl) {print $$0;}' > $@
	echo -n "Requires: `ldd $(RpmRequiredVersionTarget) | grep $(TermLib) | awk '{print $$1;}'`" >> $@
	echo ", `ldd $(RpmRequiredVersionTarget) | grep libstdc++ | awk '{print $$1;}'`" >> $@
	awk < $(RPMSrcDir)/rpmheader.txt 'BEGIN {fl=0;} (fl) {print $$0;} ($$1 == "Requires:") {fl=1;}' >> $@
	echo "" >> $@
	echo "%prep" >> $@
#	cat $(ClassicSrcDir)/prepinstall.sh >> $@
	echo "" >> $@
	echo "%build" >> $@
#	cat $(ScriptDir)/buildinstall.sh >> $@
	echo "" >> $@
	echo "%install" >> $@
#	cat $(ScriptDir)/install.sh >> $@
	echo "" >> $@
	echo "%files" >> $@
	cat $(RPMSrcDir)/rpmfiles.txt >> $@
	echo "" >> $@
	echo "%pre" >> $@
	cat $(ScriptDir)/preinstall.sh >> $@
	echo "" >> $@
	echo "%post" >> $@
	cat $(ScriptDir)/postinstall.sh >> $@
	echo "" >> $@
	echo "%preun" >> $@
	cat $(ScriptDir)/preuninstall.sh >> $@
	echo "" >> $@
	echo "%postun" >> $@
	cat $(ScriptDir)/postuninstall.sh >> $@
