# Template file for 'firebird3'
pkgname=firebird3
version=3.0.6.33328
revision=2
_build=0
_uver=${version//./_}
build_style=gnu-configure
build_helper="qemu"
configure_args="--prefix=/usr
 --with-fbbin=/usr/bin
 --with-fbconf=/etc/firebird
 --with-fbdoc=/usr/share/doc/firebird
 --with-fbhelp=/usr/share/doc/firebird/help
 --with-fbinclude=/usr/include/firebird
 --with-fblib=/usr/lib
 --with-fblog=/var/log
 --with-fbmsg=/usr/lib/firebird/msg
 --with-fbplugins=/usr/lib/firebird/plugins
 --with-fbsbin=/usr/lib/firebird/bin
 --with-fbudf=/usr/lib/firebird/UDF
 --with-fbsecure-db=/var/lib/firebird/system
 --with-fbintl=/usr/lib/firebird/intl
 --with-fbmisc
 --without-fbsample
 --without-fbsample-db
 --with-system-icu
 --with-system-editline"
hostmakedepends="automake libtool pkg-config icu"
makedepends="boost-devel libedit-devel icu-devel ncurses-devel libatomic_ops-devel
 libtommath-devel zlib-devel"
short_desc="Relational database offering many ANSI SQL standard features (V3)"
maintainer="Orphaned <orphan@voidlinux.org>"
license="custom:IDPL-1.0, Interbase-1.0"
homepage="https://www.firebirdsql.org/en/start/"
distfiles="https://github.com/FirebirdSQL/firebird/releases/download/R${_uver%_*}/Firebird-${version}-${_build}.tar.bz2"
checksum=34c1d2a29bbaf288e682cd1b5f8083f2baf73f351062245ace0bee35a3f7d35f
replaces="firebird>=0"
nocross="Fails to build gpre_boot for host but builds for target"

CFLAGS="-fno-strict-aliasing"
CXXFLAGS="-fno-delete-null-pointer-checks -Wno-deprecated -Wno-deprecated-declarations"

if [ "$CROSS_BUILD" ]; then
	configure_args+=" --enable-binreloc"
	configure_args+=" ac_cv_func_sem_init=no"
	configure_args+=" ac_cv_c_alignment=$((${XBPS_TARGET_WORDSIZE}/8))"
	configure_args+=" ac_cv_c_double_align=$((${XBPS_TARGET_WORDSIZE}/8))"
	configure_args+=" ac_cv_sys_file_offset_bits=64"
	CXXFLAGS+=" -D_FILE_OFFSET_BITS=64"
fi

pre_configure() {
	local _arch
	if [ "$CROSS_BUILD" ]; then
		# XXX: This is not yet working right and needs more work.
		# The generated gpre_boot is built with the target C++ and can
		# not be executed. Trying to run it with qemu-<target>-static
		# fails because it cannot connect to a localhost port.
		#
		patch -Np0 -i ${FILESDIR}/cross-compile.patch
		case "$XBPS_TARGET_MACHINE" in
		aarch64*) _arch="linux_arm64";;
		arm*) _arch="linux_arm";;
		mipsel*) _arch="linux_mipsel";;
		mips*) _arch="linux_mips";;
		ppc64le*) _arch="linux_powerpc64le";;
		ppc64*) _arch="linux_powerpc64";;
		ppc*) _arch="linux_powerpc";;
		x86_64*) _arch="linux_amd64";;
		esac
		configure_args+=" --with-cross-build=$_arch"
		# Remove -msse4 for non-x86_64 arch because it leaks
		# into the target CXXFLAGS.
		if [ "$_arch" != "linux_amd64" ]; then
			vsed -i builds/posix/prefix.linux{,_amd64} -e "s;-msse4;;"
		fi
	fi

	if [ "$XBPS_TARGET_LIBC" = musl ]; then
		vsed -i -e '/FLAGS=/s/ \-p / /g' \
			builds/posix/prefix.linux_powerpc* \
			builds/posix/prefix.linux_arm* \
			builds/posix/prefix.linux_mips*
	fi

	# 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;"\x60whoami\x60";"root";' \
		-e 's;chown root:root;#&;' \
		-e 's;chown -R;#&;' \
		-e 's;chmod 0444;chmod 0644;' \
		-e 's;chmod 0555;chmod 0755;' \;

	NOCONFIGURE=1 ./autogen.sh
}

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 "{}" \;

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

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

	# Conflicts with isql from unixodbc.
	mv ${DESTDIR}/usr/bin/isql{,-fb}

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

libfbclient3_package() {
	short_desc+=" - client libraries"
	replaces="libfbclient>=0"
	pkg_install() {
		vmove "usr/lib/libfbclient.so.*"
		vmove usr/lib/firebird/msg/firebird.msg
		vlicense doc/license/IDPL.txt IDPL
		vlicense doc/license/README.license.usage.txt LICENSE
	}
}

libfbclient3-devel_package() {
	short_desc+=" - development files"
	replaces="libfbclient-devel>=0"
	depends="libfbclient3>=${version}_${revision}"
	pkg_install() {
		vmove usr/lib/firebird/bin/fb_config
		vmkdir usr/bin
		ln -s ../lib/firebird/bin/fb_config ${PKGDESTDIR}/usr/bin/fb_config
		vmove usr/include/firebird
		vmove usr/lib/libfbclient.so
	}
}
