#POPCONFIG=$(CURDIR)/config.dist
## override this

include config.dist
export OWNER
export GROUP
export POPHOME=/usr/pop
export POPCONFIG=/etc/pop.conf
export POP=/pop
#export DATAHOST DATABASE

# Locales haben beim Bauen von Paketen etc. nix zu suchen.
export LANG=C
export LC_CTYPE=C
export LC_ALL=C
export LC_CTYPE=C
export LC_NUMERIC=C
export LC_TIME=C
export LC_COLLATE=C
export LC_MONETARY=C
export LC_MESSAGES=C
export LC_PAPER=C
export LC_NAME=C
export LC_ADDRESS=C
export LC_TELEPHONE=C
export LC_MEASUREMENT=C
export LC_IDENTIFICATION=C

SHELL=/bin/bash -e -o pipefail

VERSION := $(shell dpkg-parsechangelog -ldebian/changelog | sed -ne 's/^Version: *//p')

ifeq (,$(findstring release,$(DEB_BUILD_OPTIONS)))
VERS := test
else
VERS := kunde
endif

cc-option = $(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null \
             > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)

export CFLAGS:=-O2 -g -DPOPCONFIG=\"$(POPCONFIG)\" \
	$(call cc-option,-Wno-pointer-sign)
export LANG=C
export LC_CTYPE=C

PERLLIBS:="$(CURDIR)/dbperl:$(CURDIR)/lib:$(CURDIR)/rt/lib:$(CURDIR)/htdocs/trouble-ticket/lib"

SUBPYTHON:=dbpython
SUBDIRS=acct bin cgi-bin dbperl etc htdocs kunde lib qsu rt test tools $(SUBPYTHON)

####### db templates
## 'db' ist das alte Kompatibilitätsding -- nicht mehr benötigt

TOPDIR=.

#all install clean obj:: # erstes Target ist Default...
#	@echo Using config file $(POPCONFIG)
#
all install obj:: noris
	mkdir -p obj

all:: python-stamp

test-stamp: noris
	$(MAKE) all
	$(MAKE) -C rt all
	touch test-stamp

python-stamp: noris dbpython/*.py
ifneq ($(SUBPYTHON),)
	python dbpython/setup.py build --build-lib=debian/build
	$(MAKE) -C tools all_p
endif
	touch python-stamp

clean::
	rm -rf obj
	rm -f noris lib/noris test/noris test-stamp
	rm -rf build

noris:
ifneq ($(SUBPYTHON),)
	cd test; rm -f noris; ln -s ../dbpython noris
	cd lib; rm -f noris; ln -s ../dbpython noris
	rm -f noris; ln -s dbpython noris
endif

obj/varsubst: obj $(CONFIG) Makefile 
	@ rm -f obj/varsubst
	@ ( echo MAILDOM=$(REAL_MAILDOM); echo POPHOME=$(POPHOME); echo POPCONFIG=$(POPCONFIG); echo RPM_PACKAGE_VERSION=${VERS}; echo RPM_PACKAGE_RELEASE=${VERSION};  ) \
	  | perl -ne 'chomp; if(s/^([0-9_A-Z]+)=//) { $$_ = $$ENV{$$1} if defined $$ENV{$$1}; print "s'"'"'\\\@$${1}\\\@'"'"'$$_'"'"';\n" } ' >$@
	@echo "'obj/varsubst' created."; echo ""

man:
	$(MAKE) -C bin man
	$(MAKE) -C tools man
	$(MAKE) -C dbperl man
	$(MAKE) -C rt man

install:: obj/varsubst
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)
	install -o $(OWNER) -g $(GROUP) -m 1773 -d $(DESTDIR)$(POPHOME)/tmp
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)/doc
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)/bin
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)/lib
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)/acct
	install -o $(OWNER) -g $(GROUP) -m  755 -d $(DESTDIR)$(POPHOME)/include
	install -o $(OWNER) -g $(GROUP) -m  770 -d $(DESTDIR)$(CACHEDIR)
	install -o $(OWNER) -g $(GROUP) -m  770 -d $(DESTDIR)$(LOGDIR)
	install -o $(OWNER) -g $(GROUP) -m  644 POP-Datenbank $(DESTDIR)$(POPHOME)
	echo ${VERSION} > $(DESTDIR)$(POPHOME)/VERSION
ifneq ($(SUBPYTHON),)
	$(MAKE) -C tools install_p
endif

all install diff::
	for i in $(SUBDIRS) ; do $(MAKE) -e TOPDIR=$(TOPDIR)/$$i -C $$i $@ ; done

clean::
	for i in $(SUBDIRS) ; do $(MAKE) -e TOPDIR=$(TOPDIR)/$$i -C $$i $@ || true; done

install::
	install -o root -g $(GROUP) -m  755 -d $(DESTDIR)/etc/init.d
	fn=/tmp/inst.$$$$; \
	for i in $$(git ls-files init); do \
		perl -p obj/varsubst <$$i >$$fn; \
		install -o root -g root -m 755 $$fn $(DESTDIR)/etc/init.d/kunde.$$(basename $$i); done; \
	rm $$fn

#	if test ! -f $(DESTDIR)/$(POPCONFIG) ; then \
#		fn=/tmp/inst.$$$$; \
#			perl -p obj/varsubst <config.dist >$$fn; \
#			install -o $(OWNER) -g $(GROUP) -m 640 $$fn $(DESTDIR)$(POPCONFIG) ; \
#		rm $$fn; \
#	fi

install:: python-stamp
ifneq ($(SUBPYTHON),)
	$(MAKE) -C dbpython install
endif
	

diff::
	echo Index: $(TOPDIR)/config
	-diff -bu config $(POPHOME)/config
	echo Index: $(TOPDIR)/POP-Datenbank
	-diff -bu POP-Datenbank $(POPHOME)/doc/POP-Datenbank

# Nicht getestet werden:
# Db/*.pm, die brauchen alle Datenbankzugriff
# Web*.pm, die brauchen Apache-Module

T=--modified --others
qtest: obj/varsubst noris
	rm -f obj/kunde_funcs
	$(MAKE) -C kunde
	@ptmp=/tmp/x.$$$$.pl; \
	ok=/tmp/ok.$$$$ ; touch $$ok; \
	err=/tmp/err.$$$$ ; \
	if test -z "$$TESTS" ; then \
	( git ls-files $T lib/acct kunde | egrep '^[a-z][^/]*(/[a-z][^/]*)*$$' ; \
	  git ls-files $T acct rt htdocs bin cgi-bin tools | egrep '^[a-z][^/]*(/[a-z][^/]*)*$$' | \
		xargs grep -l '^#!/usr/bin/perl'; \
	  git ls-files $T test | grep  '/[0-9][0-9]_' ; \
	  git ls-files $T htdocs rt dbperl | grep '\.p[lm]$$' | egrep -v '/(Db/|Web)' ) \
	else echo "$$TESTS" | xargs -n1 echo ; echo "" >&2; echo "" >&2; fi | \
		grep -v 'Makefile$$' | uniq | while read a ; do \
			test -f $$a || git checkout -- $$a; \
			if perl -p obj/varsubst <$$a >$$ptmp; env TESTING=1 KUNDE_NO_PERLPATH=1 POPCONFIG=$(CURDIR)/config.dist KUNDE=$(CURDIR)/kunde \
	PERL5LIB="$(CURDIR)/dbperl:$(CURDIR)/lib:$(CURDIR)/rt/lib:$(CURDIR)/htdocs/trouble-ticket/lib" \
	perl -c -Mstrict -Mwarnings $$ptmp >$$err 2>&1 ; then \
				: ; \
			else \
				rm -f $$ok ; \
			fi; \
			if test $$(grep -v "syntax OK" $$err | wc -l) -ne 0 ; then \
				echo -n "$$a: " ; cat $$err; \
				echo ""; \
			fi; \
		done  2>&1 | if test -z "$$TESTS" ; then less; else cat; fi; \
	rm -f $$ptmp $$err; \
	if test -f $$ok; then rm -f $$ok; else exit 1; fi

TESTHOST ?= $(shell if netcat -z localhost 3306 ; then echo localhost; else echo dev.office.noris.de; fi)
export TESTHOST

test:
	$(MAKE) clean
	$(MAKE) test-stamp
	$(MAKE) T="--cached --others" qtest
	$(MAKE) _rtest
rtest:
	$(MAKE) clean
	$(MAKE) test-stamp
	$(MAKE) _rtest

rrtest:
	$(MAKE) -C test
	@(if test -z "$$TESTS" ; then git ls-files -c -o test | sed -ne 's,^test/\([0-9][0-9]\)_,obj/test/\1_,p' | sort ; else echo "$$TESTS" | xargs -n1 echo | sed -e 's,^,obj/,' ; fi ) | \
	env TESTING3=1 REAL_TEST=1 POPCONFIG=$(CURDIR)/config.dist \
	perl -I/usr/share/kunde/trouble-ticket/lib -Mstrict -Mwarnings -MTest::Harness \
			-e 'my @t; while(<>) {chomp; push(@t,$$_);} runtests(@t);'
sstest:
	$(MAKE) -C test
	@(if test -z "$$TESTS" ; then git ls-files -c -o test | sed -ne 's,^test/\([0-9][0-9]\)_,obj/test/\1_,p' | sort ; else echo "$$TESTS" | xargs -n1 echo | sed -e 's,^,obj/,' ; fi ) | \
	while read t ; do echo "*** $$t ***"; \
	env TESTING3=1 TESTING4=1 REAL_TEST=1 POPCONFIG=$(CURDIR)/config.dist \
	perl -I/usr/share/kunde/trouble-ticket/lib -w -Mstrict -Mwarnings $$t ; echo ""; done \
	2>&1 | less "+/not ok"

ddtest:
	$(MAKE) -C test
	@(if test -z "$$TESTS" ; then git ls-files -c -o test | sed -ne 's,^test/\([0-9][0-9]\)_,obj/test/\1_,p' | sort ; else echo "$$TESTS" | xargs -n1 echo | sed -e 's,^,obj/,' ; fi ) | \
	while read t ; do echo "*** $$t ***"; \
	env TESTING3=1 TESTING4=1 REAL_TEST=1 POPCONFIG=$(CURDIR)/config.dist \
	perl -d -w -I/usr/share/kunde/trouble-ticket/lib -Mstrict -Mwarnings $$t ; echo ""; done 

_rtest:
	@(if test -z "$$TESTS" ; then git ls-files -c -o test | egrep '^test/[0-9][^/]*$$' | sort ; else echo "$$TESTS" | xargs -n1 echo ; fi ) | \
	env KUNDE_NO_PERLPATH=1 POPCONFIG=$(CURDIR)/config.dist KUNDE=$(CURDIR)/kunde TESTING3=1 PERL5LIB="$(PERLLIBS)" \
	perl -w -Mstrict -Mwarnings -MTest::Harness \
			-e 'my @t; while(<>) {chomp; push(@t,$$_);} runtests(@t);'
stest:
	$(MAKE) clean
	$(MAKE) test-stamp

	@(if test -z "$$TESTS" ; then git ls-files -c -o test | egrep '^test/[0-9][^/]*$$' | sort ; else echo "$$TESTS" | xargs -n1 echo ; fi ) | \
	while read t ; do echo "*** $$t ***"; \
	env KUNDE_NO_PERLPATH=1 POPCONFIG=$(CURDIR)/config.dist KUNDE=$(CURDIR)/kunde TESTING3=1 TESTING4=1 PERL5LIB="$(PERLLIBS)" \
	perl -w -Mstrict -Mwarnings $$t ; echo ""; done \
	2>&1 | less "+/not ok"

dtest:
	$(MAKE) clean
	$(MAKE) test-stamp

	@(if test -z "$$TESTS" ; then git ls-files -c -o test | egrep '^test/[0-9][^/]*$$' | sort ; else echo "$$TESTS" | xargs -n1 echo ; fi ) | \
	while read t ; do echo "*** $$t ***"; \
	env KUNDE_NO_PERLPATH=1 POPCONFIG=$(CURDIR)/config.dist KUNDE=$(CURDIR)/kunde PERL5LIB="$(PERLLIBS)" \
	perl -d -w -Mstrict -Mwarnings $$t ; echo ""; done 
ttest:
	$(MAKE) clean
	$(MAKE) test-stamp

	@(if test -z "$$TESTS" ; then git ls-files -c -o test | egrep '^test/[0-9][^/]*$$' | sort ; else echo "$$TESTS" | xargs -n1 echo ; fi ) | \
	while read t ; do echo "*** $$t ***"; \
	env KUNDE_NO_PERLPATH=1 POPCONFIG=$(CURDIR)/config.dist KUNDE=$(CURDIR)/kunde PERL5LIB="$(PERLLIBS)" \
	strace -f -s 300 perl -Mstrict -Mwarnings $$t ; echo ""; done 2>&1 | less

ditest:
	sh qsu/diff_run

atest: qtest rtest ditest

.PHONY: atest ditest _rtest test rtest stest qtest dtest all all_p install install_p diff noris

	
