
# config options, set the main boost source directory which contains a boost/... headers subdirectory
BOOSTDIR=

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

# no presets from environment? Remember some optimization flags then...
ifeq ($(CXXFLAGS),)
__acng_opt_flags=-O2 -Wall
endif

ifneq ($(BOOSTDIR),)
CXXFLAGS += "-I$(BOOSTDIR)"
endif

# some useful or required flags
CXXFLAGS += -D_FILE_OFFSET_BITS=64 "-I$(CURDIR)/include" -I.

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

acsyscap.h: stampbuild-checkenv

SRCS = $(wildcard source/*.cc)
SRCSFS = source/header.cc fs/httpfs.cc source/lockable.cc source/meta.cc source/acbuf.cc source/caddrinfo.cc source/acfg_defaults.cc source/acfg.cc source/filereader.cc source/md5.cc source/sha1.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 = client.o
OBJSINSUP = acbuf.o acfg.o filereader.o meta.o md5.o sha1.o aclogger.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 checkenv_testbin checkenv_testbin.exe stampbuild-* acsyscap.h getlongsize getlongsize.exe testendian testendian.exe checksuf checksuf.exe link.flags

acng: apt-cacher-ng

ifeq ($(DEBUG),)

CXXFLAGS += $(__acng_opt_flags)

# 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) $(CXXFLAGS) -o $@ $(OBJS) -lpthread $(LDFLAGS) `cat link.flags`
	@-test -d ../unstripped && cp $@ ../unstripped/$@.$$(dpkg-parsechangelog |grep ^Version: | cut -f2 -d' ') || true
#@-test -d ../unstripped && cp $@ ../unstripped/$@.$$(dpkg-parsechangelog |grep ^Version: | cut -f2 -d' ') && strip $@ || true

else

CXXFLAGS += -DDEBUG -g -Wall -O0

in.acng: $(OBJSIN) $(OBJSINSUP)
	$(CXX) -o $@ $(OBJSIN) $(OBJSINSUP) -Wl,--as-needed
	strip $@

apt-cacher-ng: $(OBJS)
	$(CXX) -o $@ $(OBJS) -lpthread $(LDFLAGS) `cat link.flags`
endif

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

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

unrelease: tarball-remove
	svn rm $(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) $(FUSECFLAGS)  $< -o $@

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

# 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) $(MAKE_FLAGS) _dbconvert

get_deb_input:
	mkdir -p $(DBTMP)
	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)/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: Makefile dbgen/sig-cygwin
	wget -q -O- http://cygwin.com/mirrors.lst | grep ^http | cut -f1 -d\; > conf/cygwin_mirrors

conf/archlx_mirrors: Makefile
	wget -q --no-check-certificate  -O- 'https://archlinux.de/?page=MirrorStatus' | grep nofollow | cut -f2 -d'"'  | grep ^http > 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
	gzip -f -9 conf/deb_mirrors conf/debvol_mirrors

.PHONY: conf/archlx_mirrors conf/cygwin_mirrors

# help the automatic dependency tracking
acsyscap.h: stampbuild-checkenv
include/../acsyscap.h: acsyscap.h
client/../acsyscap.h: acsyscap.h
fs/../acsyscap.h: acsyscap.h

-include .depend
