# Template file for 'Ice'
pkgname=Ice
version=3.5.1
revision=19
build_wrksrc=cpp
homepage="http://www.zeroc.com"
# XXX: other language bindings
makedepends="zlib-devel bzip2-devel mcpp-devel db-devel expat-devel libressl-devel"
short_desc="The Internet Communications Engine (Ice)"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
license="GPL-2, ICE"
distfiles="${homepage}/download/Ice/${version%.*}/${pkgname}-${version}.tar.gz"
checksum=989e51194c6adadbd156da3288e37bad847b93b3b876502e83033b70493af392

case "$XBPS_TARGET_MACHINE" in
	aarch64*) broken="https://build.voidlinux.eu/builders/aarch64-musl_builder/builds/739/steps/shell_3/logs/stdio";;
esac

if [ "$CROSS_BUILD" ]; then
	# Cross build requires the host's slice2cpp and slice2freeze
	hostmakedepends+=" Ice"
fi

do_configure() {
	sed -i -e "s|^#OPTIMIZE|OPTIMIZE|" config/Make.rules
	sed -i -e "s|x)/config|x)/lib/Ice/config|" config/Make.rules
	sed -i '/^embedded_runpath_prefix*/d' config/Make.rules
	sed -i -e 's|x)/man/man1|x)/share/man/man1|' config/Make.rules
	sed -i -e "s|test demo||" Makefile

	# Simple fixes for musl libc instead of a proper patch
	sed -i -e 's|__GNUC__|__GLIBC__|' src/IceUtil/Exception.cpp
	sed -i -e 's|PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_RECURSIVE|' src/IceUtil/RecMutex.cpp
	sed -i -e 's|sys/poll\.h|poll.h|' src/Ice/*.h

	# Patch to find our $CXX and always use our $CXXFLAGS
	sed -i config/Make.rules.Linux \
		-e "s|= g++|= ${CXX}|" \
		-e "s|ifeq (\$(CXX),g++)|ifeq (\$(CXX),${CXX})|" \
		-e "/CXXARCHFLAGS\s+=/ s| -m.*|${CXXFLAGS}|" \
		-e "s|\$(CXX) -shared|${CXX} -shared|" \
		-e "s|-rdynamic|-rdynamic -Wl,-rpath,\$(libdir) ${LDFLAGS}|" \
		-e "s|-Werror|-Wno-error|"
	# Fix cross building
	if [ "$CROSS_BUILD" ]; then
		# Use host's slice2cpp and slice2freeze
		sed -i config/Make.rules \
			-e 's|$(SLICE2CPP) $(SLICE2CPPFLAGS)|LD_LIBRARY_PATH=/usr/lib /usr/bin/slice2cpp $(SLICE2CPPFLAGS)|'
		find . -name Makefile -exec sed -i "{}" \
			-e 's|^\(\s\)$(SLICE2FREEZE)|\1LD_LIBRARY_PATH=/usr/lib /usr/bin/slice2freeze|' \
			-e '/SLICE2FREEZECMD\s=/ s|$(SLICE2FREEZE)|LD_LIBRARY_PATH=/usr/lib /usr/bin/slice2freeze|' \
			\;
	fi
}

do_build() {
	export CXXFLAGS+=" -fPIC -DPIC"
	make prefix=/usr ${makejobs}
}

do_install() {
	case "$XBPS_MACHINE" in
		x86_64*)
			vmkdir usr/lib
			ln -s lib ${DESTDIR}/usr/lib64
			;;
	esac
	make prefix=${DESTDIR}/usr install
	[ -h ${DESTDIR}/usr/lib64 ] && rm -f ${DESTDIR}/usr/lib64

	vmkdir usr/share/licenses/${pkgname}
	mv ${DESTDIR}/usr/slice ${DESTDIR}/usr/share
	rm ${DESTDIR}/usr/LICENSE
	mv ${DESTDIR}/usr/ICE_LICENSE \
		${DESTDIR}/usr/share/licenses/${pkgname}/LICENSE

	vmkdir usr/share/doc/${pkgname}
	mv ${DESTDIR}/usr/CHANGES ${DESTDIR}/usr/share/doc/${pkgname}/
	mv ${DESTDIR}/usr/RELEASE_NOTES ${DESTDIR}/usr/share/doc/${pkgname}/

	# remove unused cruft
	rm -f ${DESTDIR}/usr/lib/*.class
}

libIce_package() {
	short_desc+=" - runtime libraries"
	pkg_install() {
		vmove "usr/lib/*.so.*"
		vmove usr/lib/Ice
	}
}

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