#<: 'Makefile' v0.011 @ 2012-10-06 00:05:31 UTC (Jonas@Ubbe-III) "Jonas Eckerman" <jonas@truls.org> :>#

# What the DNS?
# make stuff

SUBS=scripts doc etc wtdnsd

IDO=-p -m u=rwx,go=rx,a-s -d

.PHONY: doc etc scripts wtdnsd

now:=$(shell date +%Y-%m-%d\ %Y%m%d\ %H:%M)

date=$(shell echo $(now) | grep -o ^\\S\*)
time=$(shell echo $(now) | grep -o \\S\*\$$)
pkgnam=$(shell grep -i -m1 ^name\\s\*: Package | grep -o \[^:\]\*\$$ | grep -o \\S\*)
pkgver=$(shell grep -i -m1 ^version\\s\*:\\s\*\[.0-9\]\*\$$ Package | grep -o \[.0-9\]\*\$$)
pkgdat=$(shell echo $(now) | grep -o \\s\[0-9\]\*\\s | grep -o \[0-9\]\*)
packagename=$(pkgnam)-$(pkgver)-$(pkgdat)

default:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d; fi; done
	@echo "==============================================================================\\n"
	@echo "It seems like the preparation and compilation was successful.\n"

all:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d all; fi; done
	@echo "==============================================================================\\n"
	@echo "It seems like the preparation and compilation was successful.\n"

html:
	$(MAKE) -C doc html

man:
	$(MAKE) -C doc man

server: wtdnsd wtdnshash

clean:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d clean; fi; done

distclean:
	rm -f *.epp
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d distclean; fi; done

preinstall:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d preinstall; fi; done

postinstall:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d postinstall; fi; done
	@echo "==============================================================================\\n"
	@echo "It seems like the installation was successful.\n"

runinstall:
	install $(IDO) /var/lib/wtdns
	install $(IDO) /etc/wtdns
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d install; fi; done

prepackage:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d prepackage; fi; done

postpackage:
	set -e; for d in $(SUBS); do if [ -e "$$d/Makefile" ]; then $(MAKE) -C $$d prepackage; fi; done
	@echo "==============================================================================\\n"
	@echo "Created package: ../$(packagename).tar.xz\n"

runpackage:
	sed -i -e s/^\\\(date\\s\*:\\s\*\\\)\[-0-9\]\*\$$/\\1$(date)/ -e s/^\\\(time\\s\*:\\s\*\\\)\[:0-9\]\*\$$/\\1$(time)/ Package
	rm -rf .$(packagename) .$(packagename).tar.xz
	mkdir -p .$(packagename)
	cp -lLprx * .$(packagename)/
	rm -rf .$(packagename)/__history .$(packagename)/*/__history .$(packagename)/*/*/__history
	tar -Scha --numeric-owner --owner=root --group=root --xform s/^.$(packagename)/$(packagename)/ -f .$(packagename).tar.xz .$(packagename)
	rm -rf .$(packagename)
	mv .$(packagename).tar.xz ../$(packagename).tar.xz

install: all preinstall runinstall postinstall

package: distclean prepackage runpackage postpackage
