PACKAGE = ddccontrol-db

INSTALL = install
INSTALL_DATA = $(INSTALL) -m 644
MKDIR_P = mkdir -p
MSGFMT = msgfmt
XGETTEXT = xgettext
MSGMERGE = msgmerge
PERL = perl
DDCCONTROL = ddccontrol

all: db/options.xml
	@set -e; \
	. ./build-aux/make-config.sh; \
	load_build_config; \
	targets=; \
	for language in `selected_languages`; do \
		targets="$$targets po/$$language.gmo"; \
	done; \
	if test -n "$$targets"; then $(MAKE) $$targets; fi

.SUFFIXES: .po .gmo

.po.gmo:
	$(MSGFMT) --check -o "$@" "$<"

db/options.xml: db/options.xml.in VERSION
	@set -e; \
	version=`sed -n '1p' VERSION`; \
	tmp="$@.tmp"; \
	awk -v version="$$version" 'NR == 1 { print; print "<!---Do not edit this file, edit options.xml.in instead.-->"; next } { gsub(/\$$DATE/, version); print }' db/options.xml.in > "$$tmp"; \
	mv "$$tmp" "$@"

install: all
	@set -e; \
	. ./build-aux/make-config.sh; \
	load_build_config; \
	$(MKDIR_P) "$$destdir_value$$dbdir_value/monitor"; \
	$(INSTALL_DATA) db/options.xml "$$destdir_value$$dbdir_value/options.xml"; \
	$(INSTALL_DATA) db/monitor/*.xml "$$destdir_value$$dbdir_value/monitor/"; \
	for language in `selected_languages`; do \
		directory="$$destdir_value$$localedir_value/$$language/LC_MESSAGES"; \
		$(MKDIR_P) "$$directory"; \
		$(INSTALL_DATA) "po/$$language.gmo" "$$directory/$(PACKAGE).mo"; \
	done

uninstall:
	@set -e; \
	. ./build-aux/make-config.sh; \
	load_build_config; \
	rm -f "$$destdir_value$$dbdir_value/options.xml"; \
	for file in db/monitor/*.xml; do \
		rm -f "$$destdir_value$$dbdir_value/monitor/$${file##*/}"; \
	done; \
	for language in `selected_languages`; do \
		rm -f "$$destdir_value$$localedir_value/$$language/LC_MESSAGES/$(PACKAGE).mo"; \
	done; \
	rmdir "$$destdir_value$$dbdir_value/monitor" "$$destdir_value$$dbdir_value" 2>/dev/null || :

check: all check-version

check-version: db/options.xml
	@set -e; \
	version=`sed -n '1p' VERSION`; \
	case "$$version" in \
		[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ;; \
		*) echo "VERSION must use YYYYMMDD format, got '$$version'" >&2; exit 1 ;; \
	esac; \
	grep -q "<options date=\"$$version\" dbversion=\"3\">" db/options.xml || { \
		echo "db/options.xml does not contain version $$version" >&2; \
		exit 1; \
	}

check-db: db/options.xml
	@set -e; \
	command -v "$(DDCCONTROL)" >/dev/null 2>&1 || { \
		echo "$(DDCCONTROL) is required for make check-db" >&2; \
		exit 1; \
	}; \
	for file in db/monitor/*.xml; do \
		if ! grep -q NOCHECKDB "$$file"; then \
			name=$${file##*/}; \
			name=$${name%.xml}; \
			$(DDCCONTROL) -b db -v -v -i "$$name"; \
		fi; \
	done

db/options.xml.h: db/options.xml xml2h.pl
	$(PERL) xml2h.pl db/options.xml

update-po: db/options.xml.h
	@set -e; \
	. ./build-aux/make-config.sh; \
	load_build_config; \
	version=`sed -n '1p' VERSION`; \
	$(XGETTEXT) --from-code=UTF-8 --keyword=_ --keyword=N_ \
		--package-name="$(PACKAGE)" --package-version="$$version" \
		--msgid-bugs-address="ddccontrol-devel@lists.sourceforge.net" \
		-o po/$(PACKAGE).pot db/options.xml.h; \
	for language in `selected_languages`; do \
		$(MSGMERGE) --update --backup=none "po/$$language.po" po/$(PACKAGE).pot; \
	done

distdir: all
	@set -e; \
	. ./build-aux/make-config.sh; \
	load_build_config; \
	targets=; \
	for language in `available_languages`; do \
		targets="$$targets po/$$language.gmo"; \
	done; \
	if test -n "$$targets"; then $(MAKE) $$targets; fi; \
	version=`sed -n '1p' VERSION`; \
	dist_name="$(PACKAGE)-$$version"; \
	dist_root=build/dist; \
	dist_directory="$$dist_root/$$dist_name"; \
	rm -rf "$$dist_root"; \
	$(MKDIR_P) "$$dist_directory"; \
	git ls-files --cached | \
		while IFS= read -r file; do test ! -e "$$file" || printf '%s\n' "$$file"; done \
		> build/dist-files; \
	COPYFILE_DISABLE=1 tar --no-xattrs -cf - -T build/dist-files | \
		COPYFILE_DISABLE=1 tar --no-xattrs -xf - -C "$$dist_directory"; \
	$(INSTALL_DATA) db/options.xml "$$dist_directory/db/options.xml"; \
	for language in `available_languages`; do \
		$(INSTALL_DATA) "po/$$language.gmo" "$$dist_directory/po/"; \
	done

dist-gzip: distdir
	@set -e; \
	version=`sed -n '1p' VERSION`; \
	dist_name="$(PACKAGE)-$$version"; \
	COPYFILE_DISABLE=1 tar --no-xattrs -C build/dist -czf "$$PWD/$$dist_name.tar.gz" "$$dist_name"

dist-bzip2: distdir
	@set -e; \
	version=`sed -n '1p' VERSION`; \
	dist_name="$(PACKAGE)-$$version"; \
	COPYFILE_DISABLE=1 tar --no-xattrs -C build/dist -cjf "$$PWD/$$dist_name.tar.bz2" "$$dist_name"

dist-xz: distdir
	@set -e; \
	version=`sed -n '1p' VERSION`; \
	dist_name="$(PACKAGE)-$$version"; \
	COPYFILE_DISABLE=1 tar --no-xattrs -C build/dist -cJf "$$PWD/$$dist_name.tar.xz" "$$dist_name"

dist: dist-gzip dist-bzip2 dist-xz

clean:
	rm -f db/options.xml db/options.xml.h po/*.gmo po/$(PACKAGE).pot
	rm -rf build

distclean: clean
	rm -f config.sh config.mk
	rm -f $(PACKAGE)-*.tar.gz $(PACKAGE)-*.tar.bz2 $(PACKAGE)-*.tar.xz
