#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

BUILD_DATE := $(shell LC_ALL=C date -d @$(SOURCE_DATE_EPOCH))

%:
	dh $@

override_dh_install:
	mkdir -p $(CURDIR)/tmp/
	# Convert DB-IP country-lite CSV to MaxMind legacy CSV, split by IP version.
	python3 debian/dbip-to-legacy.py \
		--input $(CURDIR)/dbip-country-lite.csv \
		--country-info /usr/share/geoip/countryInfo.txt \
		--out-v4 $(CURDIR)/GeoIPCountryWhois.csv \
		--out-v6 $(CURDIR)/v6.csv
	# Building geoip v4 country database.
	/usr/lib/geoip/geoip-generator -v -4 \
		--info="$$(date -u +'GEO-106FREE %Y%m%d Build' -d '$(BUILD_DATE)')" \
		-o $(CURDIR)/tmp/GeoIP.dat GeoIPCountryWhois.csv
	# Convert the v4 csv layout to the v6 one.
	perl /usr/lib/geoip/v4-to-v6-layout.pl \
		< GeoIPCountryWhois.csv \
		> $(CURDIR)/tmp/v4_converted.csv
	# Merge the converted v4 with the v6 database.
	cat v6.csv $(CURDIR)/tmp/v4_converted.csv \
		> $(CURDIR)/tmp/v4v6merged.csv
	# Building geoip v6 country database.
	/usr/lib/geoip/geoip-generator -v -6 \
		--info="$$(date -u +'GEO-106FREE %Y%m%d Build' -d '$(BUILD_DATE)')" \
		-o $(CURDIR)/tmp/GeoIPv6.dat $(CURDIR)/tmp/v4v6merged.csv
	dh_install

override_dh_clean:
	rm -rf $(CURDIR)/tmp/
	dh_clean
