# -*-makefile-*-
# JLdL 07Dec13.
#
# This makefile closes the "ard" 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 = ard

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

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

# Where the root executables go.
SBIN = $(DESTDIR)/usr/sbin

# 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 = \
$(NAME).conf \
excluded.users \
excluded.progs

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

# The root executables.
SBINFILES = \
$(NAME)

# The file man pages.
MAN5FILES = \
$(NAME).conf.5.gz

# The root man pages.
MAN8FILES = \
$(NAME).8.gz

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

# 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) $(MAN8FILES) $(NAME)
	@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 root executables in $(SBIN)/
	@mkdir -p $(SBIN)/
	@cp -pf $(SBINFILES) $(SBIN)/
	@echo Installing man pages in $(MANS)/man5/
	@mkdir -p $(MANS)/man5/
	@cp -pf $(MAN5FILES) $(MANS)/man5/
	@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)/

# A rule to make the executable script.
$(NAME): $(NAME).local
	cat $< | sed -f sed.comm > $@
	chmod +x $@

# 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 $(NAME)

# Clean up the Debian package files.
cleandebfiles:
	rm -f build-stamp configure-stamp
	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 - > $@

