PROJECT = webserver
VERSION = 2.4.11
APACHE_VERSION = 2.4

PACKAGE = $(shell dpkg-parsechangelog -S Source)_$(shell dpkg-parsechangelog -S Version)
CHANGES = ../$(PACKAGE)_$(shell dpkg-architecture -qDEB_HOST_ARCH).changes

APACHE_DIRECTIVES = http://httpd.apache.org/docs/$(APACHE_VERSION)/mod/directives.html
PHPINI_DIRECTIVES = http://php.net/manual/en/ini.list.php
MOD_WSGI_DIRECTIVES = http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives
#MOD_AUTH_OPENIDC_DIRECTIVES = https://raw.githubusercontent.com/zmartzone/mod_auth_openidc/master/auth_openidc.conf
MOD_AUTH_OPENIDC_DIRECTIVES = https://raw.githubusercontent.com/OpenIDC/mod_auth_openidc/master/auth_openidc.conf
MOD_SHIB2_DIRECTIVES = https://wiki.shibboleth.net/confluence/display/SHIB2/NativeSPApacheConfig

PREFIX = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/usr,/usr/machine)
ROOT = /var/www

ETCDIR = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/etc/apache2,$(PREFIX)/etc/apache2)
MULTICONF = $(if $(DEB_BUILD_ARCH),$(ETCDIR),$(ROOT)/conf)
BINDIR = $(PREFIX)/bin
SBINDIR = $(PREFIX)/sbin
LIBDIR = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/usr/lib,$(PREFIX)/lib/apache2)
LIBEXECDIR = $(if $(DEB_BUILD_ARCH),$(LIBDIR),$(PREFIX)/libexec)
PERL5DIR = $(if $(DEB_BUILD_ARCH),,$(PREFIX)/lib/perl5)
# Nowadays, ca-certificates.crt is too long for SSL ClientHello to use as a default chain
#ALLCRT = $(if $(DEB_BUILD_ARCH),/etc/ssl/certs/ca-certificates.crt,/usr/common/machine/etc/X.509/all.crt)
ALLCRT = $(if $(DEB_BUILD_ARCH),/etc/ssl/certs/ISRG_Root_X1.pem,/usr/common/machine/etc/X.509/all.crt)
CONFDIR = $(if $(DEB_BUILD_ARCH),$(ETCDIR)/conf-available,$(ETCDIR)/conf)
SSLDIR = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/etc/ssl/certs,$(ETCDIR)/conf)
DISTRIBUTION = $(shell . /etc/os-release && echo $$VERSION_CODENAME)
INCDIR = $(ETCDIR)/include
TEMPLDIR = $(ETCDIR)/templates
DEHYDRATEDDIR = $(ETCDIR)/dehydrated
WNDIR = $(ETCDIR)/well-known
DOCDIR = $(ETCDIR)/htdocs
CGIDIR = $(LIBDIR)/cgi-bin
#WSGIDIR = $(LIBDIR)/wsgi-scripts
CRONDAILY = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/etc/cron.daily,/tmp/cron.daily)
CRONHOURLY = $(if $(DEB_BUILD_ARCH),$(DESTDIR)/etc/cron.hourly,/tmp/cron.hourly)

PHPVERSION = $(if $(DEB_BUILD_ARCH),7.3,7.0)

SYSTPAGE = systpage -d etcdir=$(ETCDIR) -d bindir=$(BINDIR) -d sbindir=$(SBINDIR) -d cgidir=$(CGIDIR) -d perl5dir=$(PERL5DIR) -d multiconf=$(MULTICONF) -d phpversion=$(PHPVERSION) -d allcrt=$(ALLCRT) -d distribution=$(DISTRIBUTION)

NOIFS = ../noifs-2.0/noifs.c

SOURCES = vhost.tmpl apache2cfg.tmpl apache2postrotate.tmpl myapacheconf.tmpl $(NOIFS) mods_required.tmpl conf/common.conf.tmpl conf/server.conf templates/httpd.conf templates/vhost.conf templates/user.conf templates/server.conf.tmpl templates/awstats.conf.tmpl templates/awstats-cluster.conf.tmpl templates/logrotate.tmpl templates/shibboleth2.xml@SHIB2 templates/shibboleth2.xml@SHIB3 include/security.tmpl include/lock.tmpl fix_vhost.$(DISTRIBUTION) mediawiki.$(DISTRIBUTION) acme_cert.tmpl ssl_check.tmpl
TARGETS = vhost apache2cfg apache2postrotate myapacheconf noifs.myapacheconf modulify mods_required conf/common.conf templates/test-server.conf templates/test-httpd.conf templates/test-vhost.conf templates/test-user.conf templates/server.conf templates/awstats.conf templates/awstats-cluster.conf templates/logrotate templates/shibboleth2.xml include/security include/lock fix_vhost mediawiki acme_cert ssl_check
# myapacheconf.source_file
PHPINI = $(if $(DEB_BUILD_ARCH),,myapacheconf.phpini.directives)
NETWORK_TARGETS = myapacheconf.module_identifier myapacheconf.modules $(PHPINI)
# nagios-check
#SCRIPTS = anonlog apache2cfg mods_required vhost ssl_check acme_cert apache2postrotate run_awstats select_node getlogs analyze_error_log check_dns run_linkchecker linkchecker_add_meta linkchecker_meta_mysql sharepath old-webserver new-webserver test-webserver fix_vhost template errlogstats ssh_access
SCRIPTS = apache2cfg modules_conf mods_required vhost ssl_check acme_cert apache2postrotate run_awstats select_node getlogs analyze_error_log check_dns check_php sharepath old-webserver new-webserver test-webserver fix_vhost fix_vhost.bookworm template errlogstats ssh_access logdu migrate_gre
LIBEXECSCRIPTS = myapacheconf
CGIS = 401 vhosts id printenv hostname showmyip myaccesslog myerrorlog myserverphpfpmerrorlog myclaccesslog myclerrorlog myclserverphpfpmerrorlog csp-report csp-stats acme-challenge
DEBHOSTS = web-master.scc.kit.edu web1.scc.kit.edu web2.scc.kit.edu web3.scc.kit.edu web4.scc.kit.edu web5.scc.kit.edu lists-mgmt-bookworm.scc.kit.edu lists-kit-01.scc.kit.edu lists-kit-02.scc.kit.edu lists-dfn-02.scc.kit.edu lists-test-bookworm.scc.kit.edu lists-test-trixie.scc.kit.edu ftp.scc.kit.edu proxy1.scc.kit.edu proxy2.scc.kit.edu andy.scc.kit.edu
#DEBHOSTS = andy.scc.kit.edu

#INSTALL = /usr/bin/install
INSTALL_PROGRAM = $(INSTALL) -s
INSTALL_SCRIPT = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644

.SUFFIXES: .tmpl

.tmpl:
	test ! -e $@ || chmod u+w $@
	$(SYSTPAGE) $< >$@ && chmod a-w,a+rx $@ && touch -r $< $@

.PHONY: all install clean dist deb dput

all: $(TARGETS) $(NETWORK_TARGETS)
	perl -c vhost
	perl -c myapacheconf
	perl -CA -c acme_cert
	perl -c fix_vhost
	perl -c fix_vhost.bookworm
	perl -c analyze_error_log
	perl -c cgi-bin/csp-stats

myapacheconf: myapacheconf.tmpl
	# Avoid accidently editing generated file
	test ! -e $@ || chmod u+w $@
	$(SYSTPAGE) -d MYAPACHECONF $< >$@ && chmod a-w,a+rx $@ && touch -r $< $@
	./$@ -T

# List of all php directives
myapacheconf.phpini.directives: $(MAKEFILE_LIST)
	test ! -e $@ || chmod u+w $@
	# It seems that php.net's nginx interrupts uncompressed transfers,
	# so we need a download client that supports compression
	#wget -q --proxy=off -O - $(PHPINI_DIRECTIVES) | \
	links -source $(PHPINI_DIRECTIVES) | zcat | \
	./phpini_directives >$@ && \
	chmod a-w,a+r $@

# Mapping of apache directive to module name, if directive isn't implemented in core
# The documentation page may be mod_ldap.html, and the module name may be mod_ldap, however IfModule must be called with either ldap_module or util_ldap.c
# FIXME: Read providing modules from mpm_common
myapacheconf.modules: myapacheconf.module_identifier myapacheconf.source_file $(MAKEFILE_LIST)
	test ! -e $@ || chmod u+w $@
	wget -q --proxy=off -O - $(APACHE_DIRECTIVES) | \
	sed -n 's!<li><a href="\([^#"]*\)[^"]*"[^>]*>\([^<]*\)</a></li>!\1	\2!p' | \
	sed 's!^\(.*\)&lt;\(.*\)&gt;$$!\1<\2\
	\1</\2>!' | \
	while read page directive; do \
		echo "$${directive}	`grep \"^$${page}	\" myapacheconf.module_identifier | cut -d\	 -f2`	`grep \"^$${page}	\" myapacheconf.source_file | cut -d\	 -f2`"; \
	done | sed 's/	*$$//' >$@ && \
	wget -q --proxy=off --ca-directory=/etc/ssl/certs -O - $(MOD_WSGI_DIRECTIVES) | \
		./mod_wsgi_directives -m "wsgi_module	mod_wsgi.c" >>$@ && \
	wget -q --proxy=off --ca-directory=/etc/ssl/certs -O - $(MOD_AUTH_OPENIDC_DIRECTIVES) | \
		./mod_auth_openidc_directives -m "auth_openidc_module	mod_auth_openidc.c" >>$@ && \
	for directive in php_value php_flag php_admin_value php_admin_flag; do \
		echo "$${directive}	php5_module	mod_php5.c"; \
	done >>$@ && \
	chmod a-w,a+r $@

# Mapping of documentation page to module source file
myapacheconf.source_file: #$(MAKEFILE_LIST)
	test ! -e $@ || chmod u+w $@
	wget -q --proxy=off -O - $(APACHE_DIRECTIVES) | \
	sed -n 's!<li><a href="\([^#"]*\)[^"]*"[^>]*>\([^<]*\)</a></li>!\1!p' | \
	sort -u | \
	while read page; do \
		wget -q --proxy=off -O - `dirname $(APACHE_DIRECTIVES)`/$${page} | \
		sed -n 's!.*Source.File:</a></th><td>\([^<]*\)</td>.*!'$${page}'	\1!p'; \
	done >$@ && \
	chmod a-w,a+r $@

# Mapping of documentation page to module identifier
myapacheconf.module_identifier: #$(MAKEFILE_LIST)
	test ! -e $@ || chmod u+w $@
	wget -q --proxy=off -O - $(APACHE_DIRECTIVES) | \
	sed -n 's!<li><a href="\([^#"]*\)[^"]*"[^>]*>\([^<]*\)</a></li>!\1!p' | \
	sort -u | \
	while read page; do \
		wget -q --proxy=off -O - `dirname $(APACHE_DIRECTIVES)`/$${page} | \
		sed -n 's!.*Module.Identifier:</a></th><td>\([^<]*\)</td>.*!'$${page}'	\1!p'; \
	done >$@ && \
	chmod a-w,a+r $@

fix_vhost: fix_vhost.$(DISTRIBUTION)
	cp -p $< $@

mediawiki: mediawiki.$(DISTRIBUTION)
	cp -p $< $@

noifs.myapacheconf: $(NOIFS)
	$(CC) $(CPPFLAGS) $(CFLAGS) -DSCRIPT=\"$(LIBEXECDIR)/myapacheconf\" $(LDFLAGS) -o $@ $(NOIFS)

modulify: myapacheconf.tmpl
	test ! -e $@ || chmod u+w $@
	$(SYSTPAGE) -d MODULIFY $< >$@ && chmod a-w,a+rx $@ && touch -r $< $@
	./$@ -T

templates/shibboleth2.xml: templates/shibboleth2.xml@SHIB2 templates/shibboleth2.xml@SHIB3
	cp -p templates/shibboleth2.xml@$(if $(DEB_BUILD_ARCH),SHIB3,SHIB2) templates/shibboleth2.xml

templates/test-server.conf: templates/server.conf
	sed 's!Apache user!& test!;s!\(VHOSTS.*/\)httpd.conf!\1test-httpd.conf!g' <$< >$@ && touch -r $< $@

templates/test-httpd.conf: templates/httpd.conf
	sed 's!Apache virtual address!& test!;s!/vhost.conf!/test-vhost.conf!g' <$< >$@ && touch -r $< $@

templates/test-vhost.conf: templates/vhost.conf
	sed 's!Apache virtual host!& test!;s!/user.conf!/test-user.conf!g' <$< >$@ && touch -r $< $@

templates/test-user.conf: templates/user.conf
	sed 's!Apache virtual host!& test!' <$< >$@ && touch -r $< $@

install: all
	test -d $(ETCDIR) || mkdir -p $(ETCDIR)
	chmod u+w,a+rx $(ETCDIR)
	$(INSTALL_DATA) httpd.conf myapacheconf.modules $(PHPINI) $(ETCDIR)
	test -d $(CONFDIR) || mkdir -p $(CONFDIR)
	chmod u+w,a+rx $(CONFDIR)
	$(INSTALL_DATA) conf/* $(CONFDIR)
	rm -f $(CONFDIR)/*.tmpl
	test -d $(SSLDIR) || mkdir -p $(SSLDIR)
	chmod u+w,a+rx $(SSLDIR)
	$(INSTALL_DATA) ssl/* $(SSLDIR)
	test -d $(INCDIR) || mkdir -p $(INCDIR)
	chmod u+w,a+rx $(INCDIR)
	$(INSTALL_DATA) include/* $(INCDIR)
	rm -f $(INCDIR)/*.tmpl
	test -d $(TEMPLDIR) || mkdir -p $(TEMPLDIR)
	chmod u+w,a+rx $(TEMPLDIR)
	$(INSTALL_DATA) templates/* $(TEMPLDIR)
	rm -f $(TEMPLDIR)/*.tmpl
	rm -f $(TEMPLDIR)/*@*
	test -d $(DEHYDRATEDDIR) || mkdir -p $(DEHYDRATEDDIR)
	chmod u+w,a+rx $(DEHYDRATEDDIR)
	$(INSTALL_DATA) dehydrated/apache2 $(DEHYDRATEDDIR)/config
	test -d $(DEHYDRATEDDIR)/conf.d || mkdir -p $(DEHYDRATEDDIR)/conf.d
	test -d $(SBINDIR) || mkdir -p $(SBINDIR)
#	chmod u+w,a+rx $(SBINDIR)
	$(INSTALL_SCRIPT) $(SCRIPTS) $(SBINDIR)
	test -d $(LIBEXECDIR) || mkdir -p $(LIBEXECDIR)
#	chmod u+w,a+rx $(LIBEXECDIR)
	$(INSTALL_SCRIPT) $(LIBEXECSCRIPTS) $(LIBEXECDIR)
	test -d $(BINDIR) || mkdir -p $(BINDIR)
#	chmod u+w,a+rx $(BINDIR)
	$(INSTALL_SCRIPT) htaccess2myapacheconf mediawiki modulify url $(BINDIR)
	eval $(SUDO) $(INSTALL_PROGRAM) -m 4755 noifs.myapacheconf $(BINDIR)/myapacheconf
	test -d $(WNDIR) || mkdir -p $(WNDIR)
	chmod u+w,a+rx $(WNDIR)
	$(INSTALL_DATA) well-known/* $(WNDIR)
	test -d $(DOCDIR) || mkdir -p $(DOCDIR)
	chmod u+w,a+rx $(DOCDIR)
	$(INSTALL_DATA) htdocs/* $(DOCDIR)
#	test -d $(LIBDIR) || mkdir -p $(LIBDIR)
#	chmod u+w,a+rx $(LIBDIR)
	test -d $(CGIDIR) || mkdir -p $(CGIDIR)
	chmod u+w,a+rx $(CGIDIR)
	(cd cgi-bin && $(INSTALL_SCRIPT) $(CGIS) $(CGIDIR))
#	test -d $(WSGIDIR) || mkdir -p $(WSGIDIR)
#	chmod u+w,a+rx $(WSGIDIR)
#	$(INSTALL_SCRIPT) wsgi-scripts/* $(WSGIDIR)
	test -d $(CRONDAILY) || mkdir -p $(CRONDAILY)
	chmod u+w,a+rx $(CRONDAILY)
	$(INSTALL_SCRIPT) check_cert $(CRONDAILY)
	test -d $(CRONHOURLY) || mkdir -p $(CRONHOURLY)
	chmod u+w,a+rx $(CRONHOURLY)
	$(INSTALL_SCRIPT) check_units $(CRONHOURLY)

put:
	for host in $(DEBHOSTS); do \
		echo "*** $${host} ***"; \
		scp -p acmefy root@$${host}:bin; \
	done

clean:
	rm -f $(TARGETS) # $(NETWORK_TARGETS)

netclean:
	rm -f $(TARGETS) $(NETWORK_TARGETS)

dist: clean
	cd ..; 	tar cf - $(PROJECT)-$(VERSION) | gzip -9 >$(PROJECT)-$(VERSION).tar.gz

deb $(CHANGES): $(MAKEFILE_LIST) debian/* debian/*/* $(SOURCES) httpd.conf conf/* ssl/* include/* templates/* run_awstats select_node getlogs analyze_error_log check_dns sharepath old-webserver new-webserver test-webserver template errlogstats ssh_access htaccess2myapacheconf mediawiki url htdocs/* cgi-bin/* wsgi-scripts/* check_cert check_php
	# We have depencencies on SCC repo
	#cme check -strict dpkg
	# While we share directory structure with rzsoft, fix VERSION in Makefile and Homepage in debian/control
	perl -pi -MCwd -e 'BEGIN{$$version=cwd=~s/.*-//r}s/^(VERSION\s*=\s*).*$$/$$1$$version/' Makefile
	perl -pi -MCwd -e 'BEGIN{$$dir=cwd=~s!.*/!!r}s!^(Homepage:.*/)[^/]+/$$!$$1$$dir/!' debian/control
	env -u CPPFLAGS -u CFLAGS -u LDFLAGS LC_ALL=C.UTF-8 dpkg-buildpackage -I -I.old --check-command=lintian --check-option=-i --check-option=-I --check-option=--suppress-tags-from-file=debian/changes/lintian-overrides -tc -d

dput: $(CHANGES)
	dput $(CHANGES) && rm -f ../$(PACKAGE)[_.]*

deploy: $(CHANGES)
	for host in $(DEBHOSTS); do \
		echo "*** $${host} ***"; \
		ssh root@$${host} "dpkg-deb --fsys-tarfile - | tar -C / -x; chmod u+s /usr/bin/myapacheconf" < ../$$(sed -ne '1s/^\([^ ]*\)  *(\([^)]*\)).*/\1_\2/p' debian/changelog)_$$(dpkg-architecture -q DEB_HOST_ARCH).deb; \
	done
	rm -f ../$(PACKAGE)[_.]*
