# Template file for 'firebird'
pkgname=firebird
version=2.5.8.27089
revision=3
_build=0
_uver=${version//./_}
wrksrc="${pkgname^}-${version}-${_build}"
build_style=gnu-configure
configure_args="--prefix=/usr/share/firebird --with-system-icu --with-system-editline"
nocross=yes
hostmakedepends="automake libtool pkg-config icu"
makedepends="boost-devel libedit-devel icu-devel ncurses-devel libatomic_ops-devel"
short_desc="Relational database offering many ANSI SQL standard features"
maintainer="Jürgen Buchmüller <pullmoll@t-online.de>"
license="custom"
homepage="https://www.firebirdsql.org/en/start/"
distfiles="https://github.com/FirebirdSQL/firebird/releases/download/R${_uver%_*}/Firebird-${version}-${_build}.tar.bz2"
checksum=d87d774736edb1eacc242c68831edb3575c28cc61378491f42f8932bf9975678
disable_parallel_build=yes

if [ ${XBPS_GCC_VERSION_MAJOR} -gt 5 ]; then
	CXXFLAGS="-Wno-error=narrowing -fcheck-new -fno-delete-null-pointer-checks -fno-lifetime-dse"
fi

pre_configure() {
	# Avoid errors telling we are not 'root' and
	# do not change ownership to root:root and
	# do not change permissions to read-only
	find -iname "*.sh.in" -exec sed -i "{}" \
		-e 's;"`whoami`";"root";' \
		-e 's;chown root:root;#&;' \
		-e 's;chown -R;#&;' \
		-e 's;chmod 0444;chmod 0644;' \
		-e 's;chmod 0555;chmod 0755;' \;
	mv configure.{in,ac}
	mkdir -p m4
	echo "ACLOCAL_AMFLAGS = -I m4" > Makefile.am
	autoreconf -if

	# Set -fPIC for btyacc tool to support PIE linker flags
	sed -i extern/btyacc/Makefile -e 's;CFLAGS=;CFLAGS=-fPIC;'

	if [ ${XBPS_GCC_VERSION_MAJOR} -gt 5 ]; then
		# Fix gcc6 error for "void * operator new(size_t size) { return 0; }"
		patch -p0 < ${FILESDIR}/fix-gcc6-no_throw_bad_alloc.patch
	fi
}

do_install() {
	local _lib _v0=${version%.*} _v1=${version%.*.*} _v2=${version%.*.*.*}

	cd ${wrksrc}/gen
	./install/makeInstallImage.sh

	cd ${wrksrc}

	# Fix permissions
	find gen/buildroot -type f -perm 0444 -exec chmod 0644 "{}" \;
	find gen/buildroot -type f -perm 0555 -exec chmod 0755 "{}" \;

	# Move some directories and files from /usr/share/firebird to /usr
	mkdir -p gen/buildroot/usr/bin
	mkdir -p gen/buildroot/usr/include
	mkdir -p gen/buildroot/usr/lib
	mv gen/buildroot/usr/share/firebird/bin/fb_config gen/buildroot/usr/bin/
	mv gen/buildroot/usr/share/firebird/include/* gen/buildroot/usr/include/
	mv gen/buildroot/usr/share/firebird/lib/* gen/buildroot/usr/lib/

	# Re-create shared library symlinks in /usr/lib
	for _lib in libfbclient libfbembed; do
		ln -sf ${_lib}.so.${_v0} gen/buildroot/usr/lib/${_lib}.so.${_v1}
		ln -sf ${_lib}.so.${_v0} gen/buildroot/usr/lib/${_lib}.so.${_v2}
		ln -sf ${_lib}.so.${_v0} gen/buildroot/usr/lib/${_lib}.so
	done

	# Add libgds support FS#30062 FS#30282
	ln -sf libfbclient.so.${_v0} gen/buildroot/usr/lib/libgds.so.0
	ln -sf libfbclient.so.${_v0} gen/buildroot/usr/lib/libgds.so

	vlicense doc/license/IDPL.txt IDPL
	vlicense doc/license/README.license.usage.txt LICENSE

	# Modify fb_config for the /usr/include and /usr/lib paths
	sed -i gen/buildroot/usr/bin/fb_config \
		-e "s;/usr/share/firebird/include;/usr/include;g" \
		-e "s;/usr/share/firebird/lib;/usr/lib;g"

	cp -pR gen/buildroot/* ${DESTDIR}/

	# TODO: create superserver?
	# system account?
	# runit service?
	# Example https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=firebird-superserver
}

libfbclient_package() {
	short_desc+=" - client libraries"
	pkg_install() {
		local _v _v0=${version%.*} _v1=${version%.*.*} _v2=${version%.*.*.*}
		vmove usr/bin/fb_config
		for _v in ${_v0} ${_v1} ${_v2}; do
			vmove usr/lib/libfbclient.so.${_v}
			vmove usr/lib/libfbembed.so.${_v}
		done
		vmove usr/lib/libgds.so.0
		vmove usr/lib/libib_util.so
		vmove usr/share/firebird/*.msg
		vlicense doc/license/IDPL.txt IDPL
		vlicense doc/license/README.license.usage.txt LICENSE
	}
}

libfbclient-devel_package() {
	short_desc+=" - development files"
	depends="libfbclient>=${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove usr/lib/libfbclient.so
		vmove usr/lib/libfbembed.so
		vmove usr/lib/libgds.so
	}
}
