

hints:
	@echo To build apt-cacher-ng and/or helper programs, use:
	@echo 
	@echo 'make acng    (create apt-cacher-ng binary with some method)'
	@echo 'make in.acng (wrapper client for use with inetd)'
	@echo 'make acngfs  (http file system, needs FUSE library)'
	@echo 'make all     (all of the above)'
	@echo
	@echo 'Parameters: DEBUG=1 -> creates a debug build'
	@echo
	@echo 'make clean   (deletes config and temporary files; call after parameter changes)'
	@echo 'make gendbs  (fetches mirror data from the web, updates databases)'
	@echo 


all: acng in.acng acngfs

CXXFLAGS ?= -g -Os -Wall -D_FILE_OFFSET_BITS=64
CXXFLAGS += "-I$(CURDIR)/include"

stampbuild-checkenv: checkenv.sh
	@sh checkenv.sh $(CXX) $(CXXFLAGS) > acsyscap.h
	@touch $@

SRCS = $(wildcard source/*.cc)
SRCSFS = source/header.cc fs/httpfs.cc source/lockable.cc source/meta.cc source/acbuf.cc source/rechecks.cc source/caddrinfo.cc
SRCSIN = source/acbuf.cc client/client.cc
SRCSALL = $(SRCS) client/client.cc fs/httpfs.cc

OBJS =  $(patsubst source/%.cc,%.o,$(SRCS))
OBJSFS =  $(patsubst fs/%.cc,%.o, $(patsubst source/%.cc,%.o,$(SRCSFS)) )
OBJSIN =  acbuf.o client.o

DBTMP=dbgen/tmp

distclean: clean

clean:
	rm -rf build apt-cacher-ng in.acng acngfs stamp-* $(OBJS) $(OBJSFS) $(OBJSIN) $(DBTMP)
	rm -rf checkenv.cc checkenv.o stampbuild-* acsyscap.h getlongsize getlongsize.exe testendian testendian.exe checksuf checksuf.exe 

acng: apt-cacher-ng

ifeq ($(DEBUG),)

# small build, even replacing libstdc++ with supc++ and gcc linking
in.acng: $(OBJSIN)
	$(CC) -o $@ $(OBJSIN) -lsupc++ -Wl,--as-needed
	strip $@

apt-cacher-ng: $(OBJS)
	$(CXX) -o $@ $(OBJS) -lpthread $(LDFLAGS) -lz -lbz2
	@-test -d ../unstripped && cp $@ ../unstripped/$@.$$(dpkg-parsechangelog |grep ^Version: | cut -f2 -d' ') || true
	strip $@

else

CXXFLAGS += -DDEBUG -Wall

in.acng: $(OBJSIN) $(OBJS)
	$(CXX) -o $@ acbuf.o client.o acfg.o filereader.o meta.o md5.o sha1.o aclogger.o -lz -lbz2 -Wl,--as-needed
	strip $@

apt-cacher-ng: $(OBJS)
	$(CXX) -o $@ $(OBJS) -lpthread $(LDFLAGS) -lz -lbz2
endif

acngfs: $(OBJSFS)
	$(CXX) -o acngfs $(OBJSFS) -Wl,--as-needed `pkg-config fuse --libs`
	@-test -d ../unstripped && cp $@ ../unstripped/$@.$$(dpkg-parsechangelog |grep ^Version: | cut -f2 -d' ') || true
	strip $@

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/acfg.h > build/tmp/hh
	cmp include/acfg.h build/tmp/hh || cp build/tmp/hh include/acfg.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 depend
#	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

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
	svn ci
	svn cp $(SVNBASE)/trunk $(SVNBASE)/tags/release_$(shell cat VERSION)

#.PHONY: install all doc

# some gmake magic to create stubs of implicite rules with gcc-generated prerequisite lists

VPATH = .:source:fs:client:3rd-party:include
$(OBJS) client.o: %.o: %.cc stampbuild-checkenv
	$(CXX) -c $(CXXFLAGS) $< -o $@
httpfs.o: fs/httpfs.cc stampbuild-checkenv
	$(CXX) -c $(CXXFLAGS) `pkg-config fuse --cflags`  $< -o $@

# not calling it .depend, shouldn't be triggered on user's PC
depend: $(SRCSALL) Makefile stampbuild-checkenv
	$(CXX) -Iinclude -MM $(SRCSALL) $(CXXFLAGS) $(CPPFLAGS) > .depend

gendbs:
	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
	wget -q -O $(DBTMP)/dsnap 'http://cvs.debian.org/*checkout*/webwml/english/mirror/Mirrors.masterlist?rev=HEAD&cvsroot=webwml&content-type=text/plain'
	md5sum $(DBTMP)/usnap > $(DBTMP)/sig-ubuntu
	md5sum $(DBTMP)/dsnap > $(DBTMP)/sig-debian
	cmp dbgen/sig-ubuntu $(DBTMP)/sig-ubuntu 2>/dev/null || cp $(DBTMP)/sig-ubuntu dbgen/sig-ubuntu 
	cmp dbgen/sig-debian $(DBTMP)/sig-debian 2>/dev/null || cp $(DBTMP)/sig-debian dbgen/sig-debian
	$(MAKE) $(MAKE_FLAGS) _dbconvert

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

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

conf/debvol_mirrors.gz conf/deb_mirrors.gz: dbgen/sig-debian dbgen/debsplit.pl
	perl dbgen/debsplit.pl < $(DBTMP)/dsnap
	gzip -f -9 conf/deb_mirrors conf/debvol_mirrors


-include .depend
