# License: GPL v2 or later
# Copyright Red Hat Inc.  2001 - 2007
# Copyright Brent Fox <bfox@redhat.com> 2001, 2002

PKGNAME=system-config-date

HGUPSTREAM_RE=^ssh://[^@]+@hg.fedoraproject.org//hg/hosted/$(PKGNAME)$$
HGREPO=$(shell hg showconfig | awk -F= '/paths.default=/ { print $$2 }')
VERSION=$(shell awk '/Version:/ { print $$2 }' ${PKGNAME}.spec)
HGTAG=$(PKGNAME)-$(subst .,_,$(VERSION))

SUBDIRS=man po

PREFIX=/usr

BINDIR=${PREFIX}/bin
SBINDIR=${PREFIX}/sbin
MANDIR=${DATADIR}/man
DATADIR=${PREFIX}/share
SYSCONFDIR=/etc

PKGDATADIR=${DATADIR}/${PKGNAME}

PAMD_DIR        = ${SYSCONFDIR}/pam.d
SECURITY_DIR    = ${SYSCONFDIR}/security/console.apps

DOC_MODULE		= $(PKGNAME)
DOC_ABS_SRCDIR	= $(CURDIR)/doc
DOC_FIGURES_DIR	= images
DOC_FIGURES		= date-time-ntp.png date-time.png timezone.png
DOC_ENTITIES	= distro-specifics.ent system-config-date-distro-specifics.ent system-config-date-content.xml
DOC_LINGUAS		=

all:    $(PKGNAME).desktop doc-all

include doc_rules.mk

po/$(PKGNAME).pot:: subdirs

subdirs:	$(PKGNAME).desktop.in.h
	for d in $(SUBDIRS); do make -C $$d; [ $$? = 0 ] || exit 1; done

%.desktop.in.h: %.desktop.in
	intltool-extract --type=gettext/ini $<

%.desktop: %.desktop.in po/$(PKGNAME).pot po/*.po
	intltool-merge -u -d po/ $< $@

install: doc-install
	mkdir -p $(DESTDIR)$(BINDIR)
	mkdir -p $(DESTDIR)$(SBINDIR)
	mkdir -p $(DESTDIR)$(PKGDATADIR)
	mkdir -p $(DESTDIR)$(PAMD_DIR)
	mkdir -p $(DESTDIR)$(SECURITY_DIR)
	mkdir -p $(DESTDIR)$(PKGDATADIR)/pixmaps
	mkdir -p $(DESTDIR)$(DATADIR)/applications
	mkdir -p $(DESTDIR)$(DATADIR)/icons/hicolor/48x48/apps
	mkdir -p $(DESTDIR)$(SYSCONFDIR)
	mkdir -p $(DESTDIR)$(SYSCONFDIR)/ntp

	install -m 0644 src/*.py $(DESTDIR)$(PKGDATADIR)
	for py in src/*.py ; do \
		sed -e s,@VERSION@,$(VERSION),g $${py} > $(DESTDIR)$(PKGDATADIR)/`basename $${py}` ; \
	done
	chmod 0755 $(DESTDIR)$(PKGDATADIR)/system-config-date.py
	install -m 0644 src/ntpservers $(DESTDIR)$(SYSCONFDIR)/ntp/
	install -m 0644 src/ntp.template $(DESTDIR)$(PKGDATADIR)
	install -m 0644 src/${PKGNAME}.glade $(DESTDIR)$(PKGDATADIR)
	install -m 0644 ${PKGNAME}.pam $(DESTDIR)$(PAMD_DIR)/${PKGNAME}
	install -m 0644 ${PKGNAME}.console $(DESTDIR)$(SECURITY_DIR)/${PKGNAME}
	install -m 0644 ${PKGNAME}.pam $(DESTDIR)$(PAMD_DIR)/system-config-time
	install -m 0644 ${PKGNAME}.console $(DESTDIR)$(SECURITY_DIR)/system-config-time
	install -m 0644 ${PKGNAME}.pam $(DESTDIR)$(PAMD_DIR)/dateconfig
	install -m 0644 ${PKGNAME}.console $(DESTDIR)$(SECURITY_DIR)/dateconfig
	install -m 0644 pixmaps/*.png $(DESTDIR)$(PKGDATADIR)/pixmaps
	install -m 0644 pixmaps/${PKGNAME}.png $(DESTDIR)$(DATADIR)/icons/hicolor/48x48/apps
	install -m 0644 ${PKGNAME}.desktop $(DESTDIR)$(DATADIR)/applications/${PKGNAME}.desktop
	install -m 0755 timeconfig $(DESTDIR)$(SBINDIR)/timeconfig
	ln -sf consolehelper $(DESTDIR)$(BINDIR)/${PKGNAME}
	ln -sf consolehelper $(DESTDIR)$(BINDIR)/system-config-time
	ln -sf consolehelper $(DESTDIR)$(BINDIR)/dateconfig
	for d in $(SUBDIRS); do \
	(cd $$d; $(MAKE) DESTDIR=$(DESTDIR) MANDIR=$(MANDIR) install) \
		|| case "$(MFLAGS)" in *k*) fail=yes;; *) exit 1;; esac; \
	done && test -z "$$fail"
	sleep 2
	python -c 'import compileall; compileall.compile_dir ("'"$(DESTDIR)$(PKGDATADIR)"'", ddir="'"$(PKGDATADIR)"'", maxlevels=10, force=1)'

checkmods:
	@if [ -n "$$(hg diff -a)" ]; then \
		echo There are modifications not yet committed. Commit these first. >&2; \
		exit 1; \
	fi

checkrepo:
ifndef BYPASSUPSTREAM
	@if [ -z "$$(echo $(HGREPO) | egrep '$(HGUPSTREAM_RE)')" ]; then \
		echo The repository $(HGREPO) is not the upstream of $(PKGNAME). >&2; \
		echo Pushing to anywhere else may not be helpful when creating an archive. >&2; \
		echo Use BYPASSUPSTREAM=1 to not access upstream or FORCEPUSH=1 to push anyway. >&2; \
		exit 1; \
	fi
endif

incoming: checkrepo
	@if [ -n "$$(hg incoming --quiet --bundle $(HGREPO))" ]; then \
		echo There are incoming changes which need to be integrated. >&2; \
		echo Pull them with "hg pull; hg update" and resolve possible conflicts. >&2; \
		exit 1; \
	fi

tag:
ifndef FORCETAG
	@if hg diff -r "$(HGTAG)" >& /dev/null; then \
		echo "Tag $(HGTAG) exists already. Use FORCETAG=1 to force tagging." >&2 ; \
		exit 1; \
	fi
endif
	@if [ -n "$(FORCETAG)" ]; then \
		FORCE=-f; \
	else \
		FORCE=""; \
	fi; \
	LASTTAG="$$(hg tags -q | head -n 2 | tail -n 1)"; \
	if [ -n "$$LASTTAG" -a -z "$$(hg diff --exclude .hgtags -r $$LASTTAG)" ]; then \
		echo "No differences to last tagged release '$$LASTTAG'. Not tagging."; \
	else \
		echo "Tagging '$(HGTAG)'."; \
		hg tag $$FORCE $(HGTAG); \
	fi

ifdef FORCEPUSH
archivepush:
else
archivepush: checkrepo
endif
ifndef BYPASSUPSTREAM
	@echo Pushing to repository $(HGREPO).
	@if ! hg push $(HGREPO); then \
		echo Pushing failed. >&2; \
		echo Use NOPUSH=1 to bypass pushing. >&2; \
		exit 1; \
	fi
endif

archive: checkmods incoming tag archivepush
ifndef FORCEARCHIVE
	@if [ -e "${PKGNAME}-$(VERSION).tar.bz2" ]; then \
		echo "File ${PKGNAME}-$(VERSION).tar.bz2 exists already." >&2; \
		echo "Use FORCEARCHIVE=1 to force overwriting it." >&2; \
		exit 1; \
	fi
endif
	@hg archive -r$(HGTAG) -t tbz2 "${PKGNAME}-$(VERSION).tar.bz2"
	@echo "The archive is in ${PKGNAME}-$(VERSION).tar.bz2"

snapsrc: archive
	@rpmbuild -ta $(PKGNAME)-$(VERSION).tar.bz2

local:
	@hg archive -t tbz2 "${PKGNAME}-$(VERSION).tar.bz2"
	@echo "The _local_ archive is in ${PKGNAME}-$(VERSION).tar.bz2"

clean: doc-clean
	rm -fv $(PKGNAME).desktop
	rm -fv *~
	rm -fv *.pyc

dif:	diff

diff:
	@echo Differences to tag $(HGTAG):
	@echo
	@hg diff -r$(HGTAG) -X .hgtags

sdif:	shortdiff

shortdiff:
	@echo Files changed since tag $(HGTAG):
	@hg diff -r$(HGTAG) -X .hgtags | egrep '^---|^\+\+\+' | sed 's:^...[   ][      ]*[ab]/::g' | sort -u
