# License: GPL
# Copyright Red Hat Inc.  Jan 2001

PKGNAME=system-config-samba

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 }' system-config-samba.spec)
HGTAG=$(PKGNAME)-$(subst .,_,$(VERSION))

SUBDIRS=po

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

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

all:	$(PKGNAME).desktop

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
	mkdir -p $(DESTDIR)/usr/bin
	mkdir -p $(DESTDIR)$(PKGDATADIR)
	mkdir -p $(DESTDIR)$(PKGIMAGEDIR)
	mkdir -p $(DESTDIR)$(PAMD_DIR)
	mkdir -p $(DESTDIR)$(SECURITY_DIR)
	mkdir -p $(DESTDIR)/usr/share/pixmaps
	mkdir -p $(DESTDIR)/usr/share/applications
	mkdir -p $(DESTDIR)/usr/share/icons/hicolor/48x48/apps
	install src/*.py src/smb.conf.template $(DESTDIR)$(PKGDATADIR)
	for py in src/*.py ; do \
		sed -e s,@VERSION@,$(VERSION),g $${py} > $(DESTDIR)$(PKGDATADIR)/`basename $${py}` ; \
	done
	install src/*.glade $(DESTDIR)$(PKGDATADIR)
	install $(PKGNAME).pam $(DESTDIR)$(PAMD_DIR)/$(PKGNAME)
	install $(PKGNAME).console $(DESTDIR)$(SECURITY_DIR)/$(PKGNAME)
	install pixmaps/*.png $(DESTDIR)$(PKGIMAGEDIR)
	install pixmaps/${PKGNAME}.png $(DESTDIR)/usr/share/icons/hicolor/48x48/apps
	install ${PKGNAME}.desktop $(DESTDIR)/usr/share/applications/${PKGNAME}.desktop
	ln -sf consolehelper $(DESTDIR)/usr/bin/$(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
	@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 $(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:
	@rm -fv *~
	@rm -fv *.pyc
	@rm -fv system-config-samba.desktop system-config-samba.desktop.in.h
