SUMMARY="Utility to update GeoIP2 and GeoIP Legacy binary databases"
DESCRIPTION="geoipupdate can download both GeoIP Legacy and GeoIP2 binary \
databases from MaxMind's website provided a valid UserID and its LicenseKey \
are supplied in the configuration file for the set of chosen ProductIds. A \
generic UserId may be used, however, to download some databases, including \
GeoLite2 Country and GeoLite2 City."
HOMEPAGE="https://github.com/maxmind/geoipupdate"
COPYRIGHT="2002-2017 MaxMind, Inc."
LICENSE="GNU GPL v2"
REVISION="2"
SOURCE_URI="https://github.com/maxmind/geoipupdate/releases/download/v$portVersion/geoipupdate-$portVersion.tar.gz"
CHECKSUM_SHA256="8b4e88ce8d84e9c75bc681704d19ec5c63c54f01e945f7669f97fb0df7e13952"
PATCHES="geoipupdate-$portVersion.patchset"

GLOBAL_WRITABLE_FILES="settings/GeoIP.conf keep-old"
USER_SETTINGS_FILES="
	settings/GeoIP.conf template data/geoipupdate/GeoIP.conf.default
	"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="!x86_gcc2 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

PROVIDES="
	geoipupdate$secondaryArchSuffix = $portVersion
	cmd:geoipupdate$commandSuffix = $portVersion
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libGeoIP$secondaryArchSuffix
	lib:libcurl$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:libGeoIP$secondaryArchSuffix
	devel:libcurl$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	"
BUILD_PREREQUIRES="
	cmd:aclocal
	cmd:autoconf
	cmd:automake
	cmd:make
	cmd:gcc$secondaryArchSuffix
	cmd:libtoolize$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	"

BUILD()
{
	libtoolize --force --copy --install
	aclocal
	autoconf
	automake --add-missing
	CACHE_DIR="`finddir B_SYSTEM_CACHE_DIRECTORY`"
	runConfigure --omit-dirs binDir,sharedStateDir ./configure \
		--bindir=$commandBinDir \
		--sharedstatedir="${CACHE_DIR#/boot}"
	make $jobArgs
}

INSTALL()
{
	make install

	CACHE_DIR="`finddir B_SYSTEM_CACHE_DIRECTORY`"

	sed -i \
		-e "s,$dataDir/GeoIP,${CACHE_DIR#/boot}/GeoIP," \
		-e "s,$sysconfDir/,/system/$relativeSettingsDir/," \
		-e "s,BIN_DIR/,/bin/," \
		$manDir/man1/geoipupdate.1 \
		$manDir/man5/GeoIP.conf.5

	sed -i -e "s,/usr/local/share/GeoIP,${CACHE_DIR#/boot}/GeoIP," \
		$docDir/GeoIP.conf.default \
		$settingsDir/GeoIP.conf

	mkdir -p $dataDir/geoipupdate
	mv $docDir/GeoIP.conf.default $dataDir/geoipupdate

	rm $docDir/LICENSE
	ln -r -s $dataDir/licenses/GNU\ GPL\ v2 $docDir/LICENSE
}

TEST()
{
	make check
}
