SUMMARY="Library for the MaxMind DB and GeoIP2 file formats"
DESCRIPTION="The libmaxminddb library is a C library for reading MaxMind DB \
files, including the GeoIP2 databases from MaxMind. This is a custom binary \
format designed to facilitate fast lookups of IP addresses while allowing for \
great flexibility in the type of data associated with an address.

The libmaxminddb library comes with a command line utility, mmdblookup, which \
allows to look up an IP address in a GeoIP2 or MaxMind DB file. The \
geoipupdate tool, available in a separate package, allows to download such \
files."
HOMEPAGE="https://github.com/maxmind/libmaxminddb"
COPYRIGHT="2013-2018 Maxmind"
LICENSE="Apache v2"
REVISION="1"
SOURCE_URI="$HOMEPAGE/releases/download/$portVersion/libmaxminddb-$portVersion.tar.gz"
CHECKSUM_SHA256="e6f881aa6bd8cfa154a44d965450620df1f714c6dc9dd9971ad98f6e04f6c0f0"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

# On x86_gcc2 we don't want to install the commands in bin/<arch>/, but in bin/.
commandSuffix=$secondaryArchSuffix
commandBinDir=$binDir
if [ "$targetArchitecture" = x86_gcc2 ]; then
	commandSuffix=
	commandBinDir=$prefix/bin
fi

libVersion="0.0.7"
libVersionCompat="$libVersion compat >= ${libVersion%%.*}"

PROVIDES="
	libmaxminddb$secondaryArchSuffix = $portVersion
	cmd:mmdblookup$commandSuffix
	lib:libmaxminddb$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES="
	haiku$secondaryArchSuffix
	"

PROVIDES_devel="
	libmaxminddb${secondaryArchSuffix}_devel = $portVersion
	devel:libmaxminddb$secondaryArchSuffix = $libVersionCompat
	"
REQUIRES_devel="
	libmaxminddb$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:automake
	cmd:autoreconf
	cmd:awk
	cmd:gcc$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:make
	cmd:perl
	cmd:pkg_config$secondaryArchSuffix
	cmd:sed
	"

defineDebugInfoPackage libmaxminddb$secondaryArchSuffix \
	"$commandBinDir"/mmdblookup \
	"$libDir"/libmaxminddb.so.$libVersion

PATCH()
{
	# replace "[socket]" by "[socket network]" when searching getaddrinfo
	sed -i -e "/^AC_SEARCH_LIBS(\[getaddrinfo\]/ \
		s/\(\[socket\)\]/\1 network\]/" \
		configure.ac

	# remove -pthread to allow "make check" to build.
	sed -i -e "/^threads_t_CFLAGS/ s/ -pthread//" \
		t/Makefile.am

	sed -i -e "/^AM_.*FLAGS/ s/ -pthread//" \
		bin/Makefile.am
}

BUILD()
{
	autoreconf -fi
	runConfigure --omit-dirs binDir ./configure --bindir=$commandBinDir
	make $jobArgs
}

INSTALL()
{
	make install

	rm $libDir/libmaxminddb.la

	prepareInstalledDevelLib libmaxminddb
	fixPkgconfig

	packageEntries devel \
		$developDir \
		$manDir/man3
}

TEST()
{
	make check
}
