# -*-makefile-*-
# JLdL 01Sep13.
#
# This makefile closes the "dynahost" package, as an "all" arch
# _ package; the main part of its structure is meant to install
# _ everything within the debian/package subdirectory.

# Edited for Debian GNU/Linux.
DESTDIR = 

# The name of the package this makefile is meant for.
NAME = dynahost

# Where the configuration files go.
ETC = $(DESTDIR)/etc/$(NAME)

# Where the cron-job file goes.
CRON = $(DESTDIR)/etc/cron.d

# Where the init files go.
INIT = $(DESTDIR)/etc/init.d

# Where the public CGI scripts go.
CGI = $(DESTDIR)/usr/lib/cgi-bin

# Where the private CGI scripts go.
PCGI = $(DESTDIR)/usr/lib/cgi-bin-priv

# Where the system library files go.
SLIB = $(DESTDIR)/usr/lib/$(NAME)

# Where the man pages go.
MANS = $(DESTDIR)/usr/share/man

# Where the additional documentation goes.
DOCS = $(DESTDIR)/usr/share/doc/$(NAME)

# The configuration files.
CONFFILES = \
conf/defaultdomain \
conf/$(NAME)dns.conf \
conf/$(NAME)nis.conf \
conf/$(NAME)nisd.conf \
conf/mailrelaydns.conf \
conf/mailrelaynis.conf \
conf/forbidnetsnis.conf

# The cron-job files.
CRONFILES = \
cron/$(NAME)

# The init files.
INITFILES = \
init/$(NAME)nis \
init/http-ssl-tunnel

# The public CGI scripts.
CGIFILES = \
cgi/$(NAME)dns-gethostaddr_en.cgi \
cgi/$(NAME)dns-gethostaddr_pt.cgi \
cgi/$(NAME)nis-gethostaddr_en.cgi \
cgi/$(NAME)nis-gethostaddr_pt.cgi \
cgi/gethostaddr.cgi

# The private CGI scripts.
PCGIFILES = \
cgi/$(NAME)dns-ssl.cgi \
cgi/$(NAME)nis-ssl.cgi

# The system library files.
SLIBFILES = \
lib/check-and-clean-var-lock \
lib/disable-nishosts \
lib/disable-nismailrelays \
lib/disable-nismailrelays.qmail \
lib/$(NAME)dns \
lib/$(NAME)dns_en.html \
lib/$(NAME)dns_en.php \
lib/$(NAME)dns_pt.html \
lib/$(NAME)dns_pt.php \
lib/$(NAME)dns-ssl_en.html \
lib/$(NAME)dns-ssl_pt.html \
lib/$(NAME)nis \
lib/$(NAME)nisd \
lib/$(NAME)nis_en.html \
lib/$(NAME)nis_en.php \
lib/$(NAME)nis_pt.html \
lib/$(NAME)nis_pt.php \
lib/$(NAME)nis-ssl_en.html \
lib/$(NAME)nis-ssl_pt.html \
lib/update-dnsmailrelay \
lib/update-dnsmailrelay.qmail \
lib/update-dnszone \
lib/update-nishost \
lib/update-nismailrelay \
lib/update-nismailrelay.qmail

# The file man pages.
MAN5FILES = \
$(NAME)dns.conf.5.gz \
$(NAME)nis.conf.5.gz \
$(NAME)nisd.conf.5.gz \
mailrelaydns.conf.5.gz \
mailrelaynis.conf.5.gz

# The descriptive man page.
MAN7FILES = \
$(NAME).7.gz

# The root man pages.
MAN8FILES = \
disable-nishosts.8.gz \
disable-nismailrelays.8.gz \
dynahostnisd.8.gz \
update-dnsmailrelay.8.gz \
update-dnszone.8.gz \
update-nishost.8.gz \
update-nismailrelay.8.gz

# Extra documentation files and directories.
DOCSFILES = \
debian/README.Debian \
remote/ \
snips/

# The date is used as a version number.
#THEDATE = $(shell date +%y%m%d)
THEDATE = $(shell date +%Y%m%d)

# Define the host from which the package is to be distributed.
DHOST = fmasft

# Define the distribution site within the host.
DSITE = /sft/debian-cluster

# Define the distribution directory within the site.
DIR = $(DSITE)/dists/stable/main

# Define the location of the package just compiled.
LOC = /local/src

# Do not do anything by default, just print a help message.
default:
	@echo "Nothing to do: no actual compilation is needed."
	@echo "The most useful targets are:"
	@echo "  'package' to make the package;"
	@echo "  'distribute' to install on the distribution site;"
	@echo "  'veryclean' to really clean it all up."
	@echo "Other internal targets are:"
	@echo "  'clean' to clean this directory;"
	@echo "  'cleandebfiles' to remove the Debian files;"
	@echo "  'cleandebpacks' to remove the Debian package(s)."

# Install files on the package tree.
install: $(MAN5FILES) $(MAN7FILES) $(MAN8FILES)
	@echo Installing the configuration files in $(ETC)/
	@mkdir -p $(ETC)/
	@cp -pf $(CONFFILES) $(ETC)/
	@echo Installing the cron-job files in $(CRON)/
	@mkdir -p $(CRON)/
	@cp -pf $(CRONFILES) $(CRON)/
	@echo Installing the init files in $(INIT)/
	@mkdir -p $(INIT)/
	@cp -pf $(INITFILES) $(INIT)/
	@echo Installing CGI scripts in $(CGI)/
	@mkdir -p $(CGI)/
	@cp -pf $(CGIFILES) $(CGI)/
	@echo Installing CGI scripts in $(PCGI)/
	@mkdir -p $(PCGI)/
	@cp -pf $(PCGIFILES) $(PCGI)/
	@echo Installing library files in $(SLIB)/
	@mkdir -p $(SLIB)/
	@cp -af $(SLIBFILES) $(SLIB)/
	@echo Installing man pages in $(MANS)/man5/
	@mkdir -p $(MANS)/man5/
	@cp -pf $(MAN5FILES) $(MANS)/man5/
	@echo Installing man pages in $(MANS)/man7/
	@mkdir -p $(MANS)/man7/
	@cp -pf $(MAN7FILES) $(MANS)/man7/
	@echo Installing man pages in $(MANS)/man8/
	@mkdir -p $(MANS)/man8/
	@cp -pf $(MAN8FILES) $(MANS)/man8/
	@echo Installing additional documentation in $(DOCS)/
	@mkdir -p $(DOCS)/
	@cp -af $(DOCSFILES) $(DOCS)/

# Make the package.
package:
	dpkg-buildpackage -kE92A1F5C

# Install the package in the distribution site.
distribute:
# Fix the ownership of the files.
	chown root:root $(LOC)/$(NAME)_*.dsc
	chown root:root $(LOC)/$(NAME)_*.tar.gz
	chown root:root $(LOC)/$(NAME)_*.deb
# Copy remotely and backwards because of authorization issues.
	ssh $(DHOST) "cp -pf $(LOC)/$(NAME)_*.dsc $(DIR)/source/"
	ssh $(DHOST) "cp -pf $(LOC)/$(NAME)_*.tar.gz $(DIR)/source/"
	ssh $(DHOST) "cp -pf $(LOC)/$(NAME)_*_all.deb $(DIR)/binary-all/"
# Refresh the site remotely because of authorization issues.
	ssh $(DHOST) "cd $(DSITE) ; ./MakePackagesFiles"

# Clean up this directory.
clean:
	rm -f *~ *.gz

# Clean up the Debian package files.
cleandebfiles:
	rm -f build-stamp configure-stamp
	rm -f debian/files
	rm -rf debian/$(NAME)/

# Clean the package(s) and related files.
cleandebpacks:
	rm -f ../$(NAME)_*.dsc
	rm -f ../$(NAME)_*.tar.gz
	rm -f ../$(NAME)_*.changes
	rm -f ../$(NAME)_*_all.deb

# Really clean it all up.
veryclean: clean cleandebfiles cleandebpacks

# A pattern rule to make compressed versions of files.
%.gz: %
	@echo Compressing file $<
	@cat $< | sed -e 's|YYYYMMDD|$(THEDATE)|g' | gzip -c9 - > $@

