# -*-makefile-*-
# JLdL 22Dec13.
#
# This makefile closes the "arith-utils" binary package; the
# _ main part of its structure is meant to install everything
# _ within the debian/package subdirectory.
#
# Making only the amd64 package, from now on.

# Edited for Debian GNU/Linux.
DESTDIR = 

# The name of the package this makefile is meant for.
NAME = arith-utils

# Where the user executables go.
BIN = $(DESTDIR)/usr/bin

# Where the library executables go.
LIB = $(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 Fortran-based user executables.
BINFILES = \
add \
aver \
averdis \
aversig \
mult

# The C-based user executables.
BINEXECS = \
rani \
ranr

# The Fortran-based executable library.
LIBEXECS = \
add.bin \
aver.bin \
averdis.bin \
aversig.bin \
mult.bin

# The user man pages.
MAN1FILES = \
add.1.gz \
aver.1.gz \
averdis.1.gz \
aversig.1.gz \
mult.1.gz \
rani.1.gz \
ranr.1.gz

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

# 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

# The Fortran compiler.
GFF = gfortran -c -O

# The C compiler.
GCC = gcc -c -O

# The Fortran linker.
LDF = gfortran -O

# The C linker.
LDC = gcc -O

# Do not do anything by default, just print a help message.
default:
	@echo "Nothing done by default; the targets follow."
	@echo "The most useful targets are:"
	@echo "  'all' to just compile the executables;"
	@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 packages."

# Make just the executables.
all: $(BINEXECS) $(LIBEXECS)

# Install files on the package tree.
install: $(BINFILES) $(BINEXECS) $(LIBEXECS) $(MAN1FILES) $(DOCSFILES)
	@echo Installing user executables in $(BIN)/
	@mkdir -p $(BIN)/
	@cp -pf $(BINFILES) $(BINEXECS) $(BIN)/
	@echo Installing library executables in $(LIB)/
	@mkdir -p $(LIB)/
	@cp -pf $(LIBEXECS) $(LIB)/
	@echo Installing man pages in $(MANS)/man1/
	@mkdir -p $(MANS)/man1/
	@cp -pf $(MAN1FILES) $(MANS)/man1/
	@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)_*_i386.deb $(DIR)/binary-i386/"
#
	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)_*_amd64.deb $(DIR)/binary-amd64/"
# Refresh the sites remotely because of authorization issues.
	ssh $(DHOST) "cd $(DSITE) ; ./MakePackagesFiles"

# Clean up this directory.
clean:
	rm -f *~ *.o a.out *.gz $(BINEXECS) $(LIBEXECS)

# 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)_*_i386.deb
	rm -f ../$(NAME)_*_amd64.deb

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

# Pattern rules.

# Fortran modules.
%.o: %.f
	$(GFF) -o $@ $<

# C modules.
%.o: %.c
	$(GCC) -o $@ $<

# The Fortran-based library executables.
%.bin: %.o
	$(LDF) -o $@ $+
	strip $@

# The C-based random number executables.
ran%: ran%.o
	$(LDC) -o $@ $+
	strip $@

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

