IP_DATA   = ../src/ip_data.c
PROVIDERS = afrinic arin apnic lacnic ripencc
DEST      = prefix_res
MD5 = $(addsuffix .md5,$(PROVIDERS))

all: clean_md5 $(MD5) $(IP_DATA)

clean_md5:
	@rm -f $(MD5)

$(PROVIDERS):
	@$(foreach provider, $(PROVIDERS),\
		wget "ftp://ftp.ripe.net/pub/stats/${provider}/delegated-${provider}-extended-latest" -O $(provider);)

%.md5:%
	@wget -q "ftp://ftp.ripe.net/pub/stats/$(basename $@)/delegated-$(basename $@)-extended-latest.md5" -O $@
	@cat $@ | grep -oP '[0-9a-f]{32}' | tr -d '\n' > $@.tmp
	@echo "  $(basename $@)" >> $@.tmp
	@rm -f $@
	@mv $@.tmp $@
	@md5sum -c $@ || wget "ftp://ftp.ripe.net/pub/stats/$(basename $@)/delegated-$(basename $@)-extended-latest" -O $(basename $@) ; true

$(DEST): $(PROVIDERS)
	@cat $(PROVIDERS) | grep -v asn | grep -v summary | grep -v '#' | grep ipv[46] | sort -n -k 4 -t '|' > $(DEST)

$(IP_DATA): $(DEST)
	@echo "Rebuild ip_data.c"
	@./build_c_array.py > ../src/ip_data.c

clean:
	rm -f $(PROVIDERS) $(DEST) $(SHA1_FILE) $(MD5)