# License: GPL v2 or later
# Copyright Red Hat Inc. 2001 - 2007

PKGNAME=system-config-samba

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

SUBDIRS=po

PREFIX=/usr
BINDIR=${PREFIX}/bin
DATADIR=${PREFIX}/share
MANDIR=${DATADIR}/man
PKGDATADIR=${DATADIR}/${PKGNAME}
PKGIMAGEDIR=${PKGDATADIR}/pixmaps

PAMD_DIR        = /etc/pam.d
SECURITY_DIR    =/etc/security/console.apps

MAKEFILE        := $(lastword $(MAKEFILE_LIST))
TOPDIR          := $(abspath $(dir $(abspath $(MAKEFILE))))
DOC_MODULE      = $(PKGNAME)
DOC_ABS_SRCDIR  = $(TOPDIR)/doc
DOC_FIGURES_DIR = images
DOC_FIGURES     = system-config-samba-create-share.png system-config-samba.png system-config-samba-server-settings-basic.png system-config-samba-server-settings-security.png system-config-samba-users.png
DOC_ENTITIES    = distro-specifics.ent system-config-samba-distro-specifics.ent system-config-samba-abstract.xml system-config-samba-content.xml
DOC_LINGUAS     = af sq am ar hy as az bal eu eu_ES be be@latin bn bn_IN bs pt_BR en_GB bg my ca zh_CN zh_TW hr cs da nl dz et fi fr gl ka de el gu he hi hu is ilo id it ja kn ko ku lo lv lt mk mai ms ml mr mn ne nso no nb nn or fa pl pt pa ro ru sr si sk sl es sv tl ta te th tr uk ur vi cy zu

all:	$(PKGNAME).desktop $(PKGNAME).console doc-all

include doc_rules.mk
include console_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:	all doc-install
	mkdir -p $(DESTDIR)/$(BINDIR)
	mkdir -p $(DESTDIR)$(PKGDATADIR)
	mkdir -p $(DESTDIR)$(PKGIMAGEDIR)
	mkdir -p $(DESTDIR)$(PAMD_DIR)
	mkdir -p $(DESTDIR)$(SECURITY_DIR)
	mkdir -p $(DESTDIR)/$(DATADIR)/pixmaps
	mkdir -p $(DESTDIR)/$(DATADIR)/applications
	mkdir -p $(DESTDIR)/$(DATADIR)/icons/hicolor/48x48/apps
	install -m 0644 src/*.py src/smb.conf.template $(DESTDIR)$(PKGDATADIR)
	chmod 0755 $(DESTDIR)$(PKGDATADIR)/system-config-samba.py
	for py in src/*.py ; do \
		sed -e s,@VERSION@,$(VERSION),g $${py} > $(DESTDIR)$(PKGDATADIR)/`basename $${py}` ; \
	done
	install -m 0644 src/*.glade $(DESTDIR)$(PKGDATADIR)
	install -m 0644 $(PKGNAME).pam $(DESTDIR)$(PAMD_DIR)/$(PKGNAME)
	install -m 0644 $(PKGNAME).console $(DESTDIR)$(SECURITY_DIR)/$(PKGNAME)
	install -m 0644 pixmaps/*.png $(DESTDIR)$(PKGIMAGEDIR)
	install -m 0644 pixmaps/${PKGNAME}.png $(DESTDIR)/$(DATADIR)/icons/hicolor/48x48/apps
	install -m 0644 ${PKGNAME}.desktop $(DESTDIR)/$(DATADIR)/applications/${PKGNAME}.desktop
	ln -sf consolehelper $(DESTDIR)/$(BINDIR)/$(PKGNAME)
	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"

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 console-clean
	@rm -fv *~
	@rm -fv *.pyc
	@rm -fv system-config-samba.desktop system-config-samba.desktop.in.h

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

llog:	lastlog

lastlog:
	@echo Log since tag $(HGTAG):
	@hg log -v -r $(HGTAG):
