PYTHON ?= python3
VENV ?= venv
ACTIVATE = $(VENV)/bin/activate
SRC = src

CURL ?= curl --fail --retry 1 --no-progress-meter --write-out '[%{http_code}] %{url_effective} -> %{filename_effective}\n'

.PHONY: all
all: out/GeoLite2-Country-Locations-en.csv out/geoip-ranges.csv out/geoip-networks.csv out/GeoLite2-Country-Blocks-IPv4.csv out/GeoIP.csv

.PHONY: release
release: out/GeoIP.dat

data/countryInfo.txt:
    @$(CURL) -o $(@) "http://download.geonames.org/export/dump/countryInfo.txt"

data/ipv4-address-space.csv:
    @$(CURL) -o $(@) "https://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.csv"

data/delegated-afrinic.txt:
    @$(CURL) -o $(@) "ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest"

data/transfers-afrinic.json:
    @$(CURL) -o $(@) "ftp://ftp.afrinic.net/stats/afrinic/transfers/transfers_latest.json"

data/whois-afrinic.db.gz:
    @$(CURL) -o $(@) "ftp://ftp.afrinic.net/dbase/afrinic.db.gz"

data/delegated-apnic.txt:
    @$(CURL) -o $(@) "ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-extended-latest"

data/transfers-apnic.json:
    @$(CURL) -o $(@) "ftp://ftp.apnic.net/public/apnic/stats/apnic/transfers/transfers_latest.json"

data/whois-apnic.db.gz:
    @$(CURL) -o data/whois-apnic.db.inetnum.gz "ftp://ftp.apnic.net/public/apnic/whois/apnic.db.inetnum.gz"
    @$(CURL) -o data/whois-apnic.db.route.gz "ftp://ftp.apnic.net/public/apnic/whois/apnic.db.route.gz"
    @cat data/whois-apnic.db.inetnum.gz data/whois-apnic.db.route.gz > $(@)
    @rm data/whois-apnic.db.inetnum.gz data/whois-apnic.db.route.gz

data/delegated-arin.txt:
    @$(CURL) -o $(@) "https://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest"

data/transfers-arin.json:
    @$(CURL) -o $(@) "https://ftp.arin.net/pub/stats/arin/transfers/transfers_latest.json"

data/whois-arin.db.gz:
    @$(CURL) -o $(@) "https://ftp.arin.net/pub/rr/arin.db.gz"

data/delegated-lacnic.txt:
    @$(CURL) -o $(@) "ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest"

data/transfers-lacnic.json:
    @$(CURL) -o $(@) "ftp://ftp.lacnic.net/pub/stats/lacnic/transfers/transfers_latest.json"

data/whois-lacnic.db.gz:
    @$(CURL) -o $(@) "ftp://ftp.lacnic.net/lacnic/dbase/lacnic.db.gz"

data/delegated-ripe.txt:
    @$(CURL) -o $(@) "ftp://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-extended-latest"

data/transfers-ripe.json:
    @$(CURL) -o $(@) "ftp://ftp.ripe.net/pub/stats/ripencc/transfers/transfers_latest.json"

data/whois-ripe.db.gz:
    @$(CURL) -o $(@) "ftp://ftp.ripe.net/ripe/dbase/ripe.db.gz"

data/ipv4-address-space.json: data/ipv4-address-space.csv $(SRC)/parse_delegations.py
    $(PYTHON) -m $(SRC).parse_delegations --format address-space --source iana --in-file $(<) --out-file $(@)

data/transfers.json: data/transfers-afrinic.json data/transfers-apnic.json data/transfers-arin.json data/transfers-lacnic.json data/transfers-ripe.json $(SRC)/parse_transfers.py
    $(PYTHON) -m $(SRC).parse_transfers --in-files data/transfers-*.json --out-file $(@)

data/delegated-%.json: data/delegated-%.txt $(SRC)/parse_delegations.py
    $(PYTHON) -m $(SRC).parse_delegations --format delegated --source $(patsubst data/delegated-%.json,%,$(@)) --in-file $(<) --out-file $(@)

data/delegated.json: data/transfers.json data/delegated-afrinic.json data/delegated-apnic.json data/delegated-arin.json data/delegated-lacnic.json data/delegated-ripe.json data/geofeed.json $(SRC)/merge_delegations.py
    $(PYTHON) -m $(SRC).merge_delegations --transfers-file data/transfers.json --in-files data/delegated-*.json data/geofeed.json --out-file $(@)

out/ranges.json: data/delegated.json data/ipv4-address-space.json $(SRC)/merge_countries.py
    $(PYTHON) -m $(SRC).merge_countries --format range --address-space-file data/ipv4-address-space.json --in-file $(<) --out-file $(@)

out/networks.json: data/delegated.json data/ipv4-address-space.json $(SRC)/merge_countries.py
    $(PYTHON) -m $(SRC).merge_countries --format net --address-space-file data/ipv4-address-space.json --in-file $(<) --out-file $(@)

data/whois-%.json: data/whois-%.db.gz $(SRC)/parse_whois.py
    $(PYTHON) -m $(SRC).parse_whois --in-file $(<) --out-file $(@)

data/geofeeds.json: data/whois-afrinic.json data/whois-apnic.json data/whois-arin.json data/whois-lacnic.json data/whois-ripe.json $(SRC)/fetch_geofeeds.py
    $(PYTHON) -m $(SRC).fetch_geofeeds --in-files data/whois-*.json --out-file $(@) --out-dir data/

data/geofeed.json: data/geofeeds.json $(SRC)/parse_geofeeds.py
    $(PYTHON) -m $(SRC).parse_geofeeds --in-file $(<) --out-file $(@)

out/geoip-ranges.csv: out/ranges.json out/GeoLite2-Country-Locations-en.csv $(SRC)/dump_csv.py
    $(PYTHON) -m $(SRC).dump_csv --format range --location-file out/GeoLite2-Country-Locations-en.csv --in-file $(<) --out-file $(@)

out/geoip-networks.csv: out/networks.json out/GeoLite2-Country-Locations-en.csv $(SRC)/dump_csv.py
    $(PYTHON) -m $(SRC).dump_csv --format net --location-file out/GeoLite2-Country-Locations-en.csv --in-file $(<) --out-file $(@)

out/GeoLite2-Country-Locations-en.csv: data/countryInfo.txt $(SRC)/parse_locations.py
    $(PYTHON) -m $(SRC).parse_locations --in-file $(<) --out-file $(@)

out/GeoLite2-Country-Blocks-IPv4.csv: out/networks.json out/GeoLite2-Country-Locations-en.csv $(SRC)/dump_csv.py
    $(PYTHON) -m $(SRC).dump_csv --format geoip2 --location-file out/GeoLite2-Country-Locations-en.csv --in-file $(<) --out-file $(@)

out/GeoIP.csv: out/ranges.json out/GeoLite2-Country-Locations-en.csv $(SRC)/dump_csv.py
    $(PYTHON) -m $(SRC).dump_csv --format legacy --location-file out/GeoLite2-Country-Locations-en.csv --in-file $(<) --out-file $(@)

out/GeoIP.dat: out/GeoIP.csv
    /usr/lib/geoip/geoip-generator -o $(@) $(<)

$(ACTIVATE):
    $(PYTHON) -m venv $(VENV)
    . $(ACTIVATE) ; pip install -U pip setuptools wheel
    . $(ACTIVATE) ; pip install -U mypy flake8 bandit
    @touch $(@)

.PHONY: check
check: $(ACTIVATE)
    . $(ACTIVATE) ; mypy -- $(SRC)/
    . $(ACTIVATE) ; flake8 --max-line-length 120 -- $(SRC)/
    . $(ACTIVATE) ; bandit -r --quiet -- $(SRC)/

.PHONY: clean reallyclean
reallyclean: clean
    rm -f -- data/*

clean:
    rm -f -- out/*
    find . -depth -type d -name '__pycache__' -exec rm -rf {} \;
    rm -rf .mypy_cache $(VENV)