# $Id$

SOURCE_DIR=		.
SOURCE_BUILD=		$(SOURCE_DIR)/build

## ======================================================================

RELEASE=		1
DESTARCH=		smbldap-tools-$(PACKAGE_VERSION)

# where to build the rpm
TOPDIR=			$$HOME/redhat
GPG_PATH=		$$HOME/.gnupg
RPMBUILD_COMMAND=	rpmbuild -ba --sign

BUILD_TARGETS= \
	$(BUILD_PERL_MOD_TARGETS) \
	$(BUILD_PERL_CMD_TARGETS) \
	$(BUILD_PERL_MAN_TARGETS) \
	smbldap-config.cmd \
	smbldap-upgrade-0.9.6.cmd \
	doc/smb.conf.example \
	doc/smb.smbldap.conf.example \
	#

INSTALL_DIR=		$(PERL_LIBDIR)
INSTALL_FILES=		$(BUILD_PERL_MOD_TARGETS)
INSTALL_CMD_DIR=	$(sbindir)
INSTALL_CMDS=		$(BUILD_PERL_CMD_TARGETS)

PERL_MOD_SOURCES=	smbldap_tools.pl

PERL_CMD_SOURCES= \
	smbldap-groupadd.pl \
	smbldap-groupdel.pl \
	smbldap-grouplist.pl \
	smbldap-groupmod.pl \
	smbldap-groupshow.pl \
	smbldap-passwd.pl \
	smbldap-populate.pl \
	smbldap-useradd.pl \
	smbldap-userdel.pl \
	smbldap-userinfo.pl \
	smbldap-userlist.pl \
	smbldap-usermod.pl \
	smbldap-usershow.pl

PERL_MAN_SECTION=	8

BUILD_PERL_MOD_TARGETS= $(PERL_MOD_SOURCES:.pl=.pm)
BUILD_PERL_CMD_TARGETS= $(PERL_CMD_SOURCES:.pl=.cmd)
BUILD_PERL_MAN_TARGETS= $(BUILD_PERL_CMD_TARGETS:.cmd=.$(PERL_MAN_SECTION))

## ======================================================================

include $(SOURCE_BUILD)/Makefile.common
include $(SOURCE_BUILD)/Makefile.top

## ======================================================================

.SUFFIXES: .cmd .$(PERL_MAN_SECTION)

.cmd.$(PERL_MAN_SECTION):
	@echo "Building $@ from $< ..."
	@$(POD2MAN_COMMAND) --section=$(PERL_MAN_SECTION) $< >$@.tmp
	@mv $@.tmp $@

## ======================================================================

all: build

build::

install:: install-files install-cmds

## ======================================================================


build_dir:
	@echo '%_topdir $(TOPDIR)' > $(HOME)/.rpmmacros
	@echo '%_signature gpg' >> $(HOME)/.rpmmacros
	@echo '%_gpg_name Jerome Tournier <jtournier@gmail.com>' >> $(HOME)/.rpmmacros
	@echo '%_gpg_path $(GPG_PATH)' >> $(HOME)/.rpmmacros
	@mkdir -p $(TOPDIR)/BUILD
	@mkdir -p $(TOPDIR)/RPMS/i386
	@mkdir -p $(TOPDIR)/SOURCES
	@mkdir -p $(TOPDIR)/SPECS
	@mkdir -p $(TOPDIR)/SRPMS

rpm: dist build_dir
	@cp -f $(DESTARCH).tgz $(TOPDIR)/SOURCES/
	@cp -f smbldap-tools.spec $(TOPDIR)/SPECS/
	@perl -i -pe's@^\%define version(.*)@\%define version $(VERSION)@' $(TOPDIR)/SPECS/smbldap-tools.spec
	@perl -i -pe's@^\%define release(.*)@\%define release $(RELEASE)@' $(TOPDIR)/SPECS/smbldap-tools.spec
	@perl -i -pe's@^Source0(.*)@Source0: smbldap-tools-$(VERSION).tgz@' $(TOPDIR)/SPECS/smbldap-tools.spec
	@cd $(TOPDIR)/SPECS/ && $(RPMBUILD_COMMAND) smbldap-tools.spec
	@echo "Signing packages smbldap-tools-$(VERSION)-$(RELEASE).noarch.rpm"
	@cd $(TOPDIR)/RPMS/noarch &&  gpg --detach smbldap-tools-$(VERSION)-$(RELEASE).noarch.rpm
	@echo "Signing packages smbldap-tools-$(VERSION)-$(RELEASE).src.rpm"
	@cd $(TOPDIR)/SRPMS/ && gpg --detach smbldap-tools-$(VERSION)-$(RELEASE).src.rpm
	@echo "Signing packages smbldap-tools-$(VERSION).tgz"
	@cd $(TOPDIR)/SOURCES/ && gpg --detach smbldap-tools-$(VERSION).tgz
	@echo "Arch: $(DESTARCH).tgz"


home_devel: rpm
	@mkdir -p iallanis/{docs/{smbldap-tools,samba-ldap-howto},old,development_release}
	@cp -f iallanis/development_release/ChangeLog "/tmp/ChangeLog-homedevel-`date`"
	@cp -f ChangeLog iallanis/development_release/
	@cp $(TOPDIR)/SOURCES/smbldap-tools-$(VERSION){.tgz,.tgz.sig} iallanis/development_release/
	@cp $(TOPDIR)/RPMS/noarch/smbldap-tools-$(VERSION)-$(RELEASE).noarch{.rpm,.rpm.sig} iallanis/development_release/
	@cp $(TOPDIR)/SRPMS/smbldap-tools-$(VERSION)-$(RELEASE).src{.rpm,.rpm.sig} iallanis/development_release/
	@rsync -avz --delete --delete-excluded --exclude .svn -e "ssh -p 443" iallanis/ 192.168.10.1:/home/www/html/smbldap-tools

home: rpm
	@cp -f iallanis/ChangeLog "/tmp/ChangeLog-home-`date`"
	@cp -f ChangeLog iallanis/
	@cp doc/smbldap-tools/smbldap-tools.html iallanis/docs/smbldap-tools/index.html
	@cp doc/samba-ldap-howto/smbldap-howto.html iallanis/docs/samba-ldap-howto/index.html
	@cp $(TOPDIR)/SOURCES/smbldap-tools-$(VERSION){.tgz,.tgz.sig} iallanis/
	@cp $(TOPDIR)/RPMS/noarch/smbldap-tools-$(VERSION)-$(RELEASE).noarch{.rpm,.rpm.sig} iallanis/
	@cp $(TOPDIR)/SRPMS/smbldap-tools-$(VERSION)-$(RELEASE).src{.rpm,.rpm.sig} iallanis/
	rsync -avz --delete --delete-excluded --exclude .svn -e "ssh -p 443" iallanis/ 192.168.10.1:/home/www/html/smbldap-tools

gna:
	@cp -f ChangeLog GNA/packages/
	@cp $(TOPDIR)/SOURCES/smbldap-tools-$(VERSION){.tgz,.tgz.sig} GNA/packages/
	@cp $(TOPDIR)/RPMS/noarch/smbldap-tools-$(VERSION)-$(RELEASE).noarch{.rpm,.rpm.sig} GNA/packages/
	@cp $(TOPDIR)/SRPMS/smbldap-tools-$(VERSION)-$(RELEASE).src{.rpm,.rpm.sig} GNA/packages/
	rsync -avz -e ssh --delete --delete-excluded --exclude .svn GNA/ download.gna.org:/upload/smbldap-tools/

