
ifneq ($(DEBUG),)
CXXFLAGS += -DDEBUG 
endif

hints:
	@echo To build apt-cacher-ng and/or helper programs, use:
	@echo 'make acng        (to use CMake assisted build, normal and debug versions)'
	@echo 'make acng-static (simple GCC based build, static linking)'
	@echo 'make acng-whole  (simple GCC based build, dynamic linking, optimized)'
	@echo 'make in.acng     (wrapper client for use with inetd)'
	@echo 'make mount.acng  (http file system emulator, needs FUSE library)'

# quick and dirty, mini-build
SRCS = $(wildcard source/*.cc) 3rd-party/md5.cc
embedded: acng-small
acng-small: $(SRCS)
	$(CXX) -o $@ -combine -Os -fno-exceptions $(SRCS) $(CXXFLAGS) -I include -lpthread -lz -lbz2
	strip $@

embstatic: acng-static
acng-static: $(SRCS)
	$(CXX) -o $@ -combine -Os -fno-exceptions $(CXXFLAGS) $(SRCS) -I include -static $(LDFLAGS) -lpthread -lz -lbz2

acng-whole: $(SRCS)
	mkdir -p build
	# for some reason -combine doesn't work well with -fwhole-program, trying a workaround...
	cat $(SRCS) > build/all.cc
	$(CXX) -o $@ -fwhole-program -O2 $(CXXFLAGS) build/all.cc -Iinclude -I3rd-party -lpthread $(LDFLAGS) -lz -lbz2

lite: $(SRCS)
	$(CXX) -o $@ -combine -Os -fno-exceptions $(CXXFLAGS) $(SRCS) -I include $(LDFLAGS) -lpthread

in.acng: source/acbuf.cc client/client.cc
	mkdir -p build/cl
	$(CXX) -O2 -c -o build/cl/client.o client/client.cc -Iinclude
	$(CXX) -O2 -c -o build/cl/acbuf.o source/acbuf.cc -Iinclude
	$(CC) -o in.acng build/cl/client.o build/cl/acbuf.o -lsupc++
	strip in.acng



SRCSMOUNT=source/dlcon.cc source/header.cc fs/src/httpfs.cc source/lockable.cc source/meta.cc source/acbuf.cc
mount.acng: fs/src/httpfs.cc source/acbuf.cc
	mkdir -p build/fs
	$(CXX) -Iinclude -o mount.acng `pkg-config fuse --cflags` $(SRCSMOUNT) `pkg-config fuse --libs`
#	$(CXX) -O2 -c -o build/fs/httpfs.o fs/src/httpfs.cc -Iinclude $(FUCONF)
#	$(CXX) -O2 -c -o build/fs/acbuf.o source/acbuf.cc -Iinclude
#	$(CXX) -o in.acng build/fs/httpfs.o build/fs/acbuf.o $(FULINK)
#	strip in.acng

doc: doc/src/README.but doc/src/manpage.but doc/src/mount.acng.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/mount.acng.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

ifneq ($(CXXFLAGS),)
CMAKETWEAKS += ( cd build ; cmake .. -DCMAKE_CXX_FLAGS="$(CXXFLAGS)" ) || exit 1; 
endif

ifneq ($(LDFLAGS),)
CMAKETWEAKS += (cd build ; cmake .. -DCMAKE_EXE_LINKER_FLAGS:STRING="$(LDFLAGS)" -DCMAKE_MODULE_LINKER_FLAGS:STRING="$(LDFLAGS)" -DCMAKE_SHARED_LINKER_FLAGS:STRING="$(LDFLAGS)" ) || exit 1; 
endif

ifneq ($(PREFIX),)
CMAKETWEAKS += ( cd build ;  cmake .. -DCMAKE_INSTALL_PREFIX="$(PREFIX)") || exit 1; 
endif

ifneq ($(MANSUBDIR),)
CMAKETWEAKS += ( cd build ;  cmake .. -DMANSUBDIR="$(MANSUBDIR)" ) || exit 1; 
endif


build/Makefile:
	@echo Purging CMakeCache.txt, it caches too much... :-\(
# only effective when set PHONY
	rm -f build/CMakeCache.txt
	@-mkdir build 2>/dev/null
	cd build && cmake ..

cmakepurge:
	rm -rf install_manifest.txt progress.make CMakeFiles CMakeCache.txt cmake_install.cmake 
	rm -rf */install_manifest.txt */progress.make */CMakeFiles */CMakeCache.txt */cmake_install.cmake 
	rm -rf */*/install_manifest.txt */*/progress.make */*/CMakeFiles */*/CMakeCache.txt */*/cmake_install.cmake 
	rm */Makefile */*/Makefile

clean:
	rm -rf build acng-small acng-whole acng-static in.acng doc/.build

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

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)

#%::
#	$(MAKE) $(MAKE_FLAGS) build/Makefile
#	$(CMAKETWEAKS)
#	$(MAKE) -C build $(MAKE_FLAGS) $@

# needs to be explicite, for PHONY and install (AKA INSTALL) file on cygwin
#install: build/Makefile
#	$(CMAKETWEAKS)
#	$(MAKE) -C build $(MAKE_FLAGS) $@

acng: build/Makefile
	$(CMAKETWEAKS)
	$(MAKE) -C build $(MAKE_FLAGS) all

.PHONY: install all

# FIXME, forcing purge of cmakecache for now, it caches too much
.PHONY: build/Makefile

.PHONY: doc

