
ifneq ($(CXXFLAGS),)
CMAKEOPTS += "-DCMAKE_CXX_FLAGS=$(CXXFLAGS)" 
else
CMAKEOPTS += "-DCMAKE_CXX_FLAGS=-g -O2 -Wall" 
endif

all:
	ls build/ 2>/dev/null >/dev/null || mkdir build
	cd build && cmake .. $(CMAKEOPTS) && $(MAKE)

DBTMP=dbgen/tmp

clean:
	rm -rf build

distclean: clean

doc: doc/src/README.but doc/src/manpage.but doc/src/acngfs.but
	mkdir -p doc/.build doc/html doc/man
	cd doc/.build && halibut --text=README.txt --html --pdf ../src/README.but
	mv doc/.build/*.pdf doc
	mv doc/.build/README.txt doc/README
	mv doc/.build/*.html doc/html
	cd doc/.build && halibut --man ../src/manpage.but && halibut --man ../src/acngfs.but
	mv doc/.build/*.8 doc/man

fixversion: VERSION
	mkdir -p build/tmp
	sed -e 's,^.*define.*ACVERSION.*,#define ACVERSION "$(VERSION)",' < include/config.h > build/tmp/hh
	cmp include/config.h build/tmp/hh || cp build/tmp/hh include/config.h

VERSION=$(shell cat VERSION)
DISTNAME=apt-cacher-ng-$(VERSION)
DEBSRCNAME=apt-cacher-ng_$(shell echo $(VERSION) | sed -e "s,pre,~pre,").orig.tar.gz

tarball: fixversion doc 
#	if test "$(shell svn status | grep -v -i make)" ; then echo Uncommited files found. Run \"svn status\" to display them. ; exit 1 ; fi
	@if test -f ../$(DISTNAME).tar.gz ; then echo ../$(DISTNAME).tar.gz exists, not overwritting ; exit 1; fi
	-svn up
	rm -rf tmp
	mkdir tmp
	svn export . tmp/$(DISTNAME)
	cp -l tmp/$(DISTNAME)/doc/README tmp/$(DISTNAME)
	rm -rf tmp/$(DISTNAME)/debian tmp/$(DISTNAME)/tmp tmp/$(DISTNAME)/orig tmp/$(DISTNAME)/trash tmp/$(DISTNAME)/contrib
	tar -f - -c -C tmp $(DISTNAME) | gzip -9 > ../$(DISTNAME).tar.gz
	rm -rf tmp
	test -e /etc/debian_version && ln -f ../$(DISTNAME).tar.gz ../$(DEBSRCNAME) || true
	test -e ../tarballs && ln -f ../$(DISTNAME).tar.gz ../tarballs/$(DEBSRCNAME) || true
	test -e ../build-area && ln -f ../$(DISTNAME).tar.gz ../build-area/$(DEBSRCNAME) || true

tarball-remove:
	rm -f ../$(DISTNAME).tar.gz ../tarballs/$(DEBSRCNAME) ../$(DEBSRCNAME) ../build-area/$(DEBSRCNAME)

SVNBASE=$(shell svn info | grep URL: | cut -f2 -d' ' | xargs dirname)
release: tarball
#release:
	svn ci
	svn cp $(SVNBASE)/trunk $(SVNBASE)/tags/release_$(shell cat VERSION)

unrelease: tarball-remove
#unrelease:
	svn rm $(SVNBASE)/tags/release_$(shell cat VERSION)

# first get and check data, then replace the mirror output lists but for some sources do it only when needed 
gendbs: get_deb_input get_ubu_input
	$(MAKE) _dbconvert

get_deb_input:
	mkdir -p $(DBTMP)
	wget -q -O $(DBTMP)/dsnap 'http://anonscm.debian.org/viewvc/webwml/webwml/english/mirror/Mirrors.masterlist?view=co'
	md5sum $(DBTMP)/dsnap > $(DBTMP)/sig-debian
	cmp dbgen/sig-debian $(DBTMP)/sig-debian 2>/dev/null || cp $(DBTMP)/sig-debian dbgen/sig-debian

get_ubu_input:
	mkdir -p $(DBTMP)
	( w3m -dump http://www.iana.org/domains/root/db | perl -pe 'if(/^\.(\w\w)\s/) { $$_="http://".lc($$1).".archive.ubuntu.com/ubuntu\n";} else {undef $$_}';  wget -q -O- 'https://wiki.ubuntu.com/Mirrors?action=show&redirect=Archive' 'https://launchpad.net/ubuntu/+archivemirrors' ) | tr -d ' ' | tr -d '\t' | sed -e 's,",\n,g' | grep ^http | sort -u > $(DBTMP)/usnap
	md5sum $(DBTMP)/usnap > $(DBTMP)/sig-ubuntu
	cmp dbgen/sig-ubuntu $(DBTMP)/sig-ubuntu 2>/dev/null || cp $(DBTMP)/sig-ubuntu dbgen/sig-ubuntu 

_dbconvert: conf/deb_mirrors.gz conf/debvol_mirrors.gz conf/ubuntu_mirrors conf/cygwin_mirrors conf/archlx_mirrors

conf/ubuntu_mirrors: dbgen/sig-ubuntu dbgen/ubuntuscan.sh
	bash dbgen/ubuntuscan.sh $@ $(DBTMP)/usnap $(DBTMP)

conf/cygwin_mirrors: dbgen/sig-cygwin
	wget -q -O- http://cygwin.com/mirrors.lst | grep ^http | cut -f1 -d\; | sort -u > conf/cygwin_mirrors

conf/archlx_mirrors:
	wget -q --no-check-certificate  -O- 'https://archlinux.de/?page=MirrorStatus' | grep nofollow | cut -f2 -d'"'  | grep ^http | sort -u > conf/archlx_mirrors

conf/debvol_mirrors.gz conf/deb_mirrors.gz: dbgen/sig-debian dbgen/debsplit.pl
	perl dbgen/debsplit.pl < $(DBTMP)/dsnap
	echo http://ftp.debian.org/debian/ >> conf/deb_mirrors
	echo http://cdn.debian.net/debian/ >> conf/deb_mirrors
	gzip -f -9 conf/deb_mirrors conf/debvol_mirrors

# execute them always and consider done afterwards
.PHONY: conf/archlx_mirrors conf/cygwin_mirrors _dbconvert gendbs clean distclean


# pass-through
%:
	ls build/ 2>/dev/null >/dev/null || mkdir build
	cd build && cmake .. $(CMAKEOPTS) && $(MAKE) $@

# no MT, cmake doesn't like it
.NOTPARALLEL:
