# -*-makefile-*-
# JLdL 23May16.
#
# This is the Makefile for the fismat support material, as well as
# _ for its documentation; so far the following programs exist:
# _ complex-functions / plot_func.
#
# This makefile will compile the binaries and install everything
# _ in the debian/package subdirectory.

### PART 1 ### : macros.

# Package-related macros.

# Edited for Debian GNU/Linux; note that we use a local
# _ default value "tmp", in order to avoid writing in
# _ the actual system directories of the local host.
DESTDIR = tmp

# The package this makefile is meant for.
PACKAGE = math-phys-1

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

# Where the library files go.
LIBDIR = $(DESTDIR)/usr/lib/$(PACKAGE)

# Where the man pages go.
MANDIR = $(DESTDIR)/usr/share/man/man1
#
MANDIR-PT_BR = $(DESTDIR)/usr/share/man/pt_BR/man1

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

# The binary file.
BINFILES = \
complex-functions

# The library file.
LIBFILES = \
plot_func

# The man 1 pages.
MANFILES = \
complex-functions.1.gz
#
MANFILES-PT_BR = \
pt_BR/complex-functions.1.gz

# The doc files.
DOCFILES = \
README \
example.complex-functions.rc

# Compilation-related macros.

# The list of object modules for plot_func.
include INC.PLOT_FUNC-OBJ
OBJ = $(PLOT_FUNC-OBJ)

# The g2-ctof library and its dependency libraries.
LIB = \
/usr/lib/libg2-ctof.a \
/usr/lib/libg2.a \
/usr/lib/x86_64-linux-gnu/libgd.a \
/usr/lib/x86_64-linux-gnu/libjpeg.a \
/usr/lib/x86_64-linux-gnu/libpng.a \
/usr/lib/x86_64-linux-gnu/libz.a

# The most basic shared libraries; the C and F77 libraries are
# _ loaded automatically and do not have to be listed here.
LD = -L/usr/lib/x86_64-linux-gnu -lm -lX11

# Distribution-related macros.

# Define the location of the compiled packages and tar files.
LOC = /local/src

# 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

# Static-tar-file-related macros.

# Define the hardware architecture of the package.
ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Define the host from which the static tar file is to be distributed.
SHOST = latt

# Define the distribution directory of the static tar file.
SDIR = var/www/fismat/software

### PART 2 ### : targets.

# The default target is to make the package, as well as
# _ the tar with the statically-linked version.
default: all

all: package static-tar

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

# Make a tar file with statically-linked versions of the programs,
# _ for people that do not have a good (i.e. Debian) system.
static-tar: $(LOC)/$(PACKAGE)-static-$(ARCH).tgz
$(LOC)/$(PACKAGE)-static-$(ARCH).tgz: \
complex-functions.local \
plot_func_static \
complex-functions.1.gz \
pt_BR/complex-functions.1.gz \
README.static-tar \
LEIAME.static-tar \
example.complex-functions.rc \
.complex-functions.rc
	mkdir -p $(PACKAGE)-static/
	tar -cf - $+ | tar -C $(PACKAGE)-static/ -xpf -
	chown -R root:root $(PACKAGE)-static/ ; \
	chmod -R g-ws $(PACKAGE)-static/ ; \
	cd $(PACKAGE)-static/ ; \
		mv complex-functions.local complex-functions ; \
		mv plot_func_static plot_func ; \
		mv README.static-tar README ; \
		mv LEIAME.static-tar LEIAME ; \
		cd -
	tar -czf $@ $(PACKAGE)-static/

# Make all the compiled binaries.
allbin: plot_func plot_func_static

# Make the complex-functions script.
complex-functions: complex-functions.local
	search="set plot_func ./plot_func" ; \
	replace="set plot_func /usr/lib/$(PACKAGE)/plot_func" ; \
	cat $< | sed -e "s|$$search|$$replace|g" > $@

# The main plot_func target, dynamically linked with everything.
plot_func: $(OBJ)
	gfortran -O -o $@ $+ $(LD) -lg2-ctof -lg2 -lgd
	strip $@

# The main plot_func target, statically linked with the g2-ctof interface
# _ library, the g2 library and the libraries it depends on, but
# _ dynamically linked with the X11, C, math and F77 libraries.
plot_func_static: $(OBJ) $(LIB)
	gfortran -O -o $@ $+ $(LD)
	strip $@

# Install files on the package subdirectory.
install: allbin $(BINFILES) $(MANFILES) $(MANFILES-PT_BR)
	@echo Installing the binary files in $(BINDIR)/
	@mkdir -p $(BINDIR)/
	@cp -af $(BINFILES) $(BINDIR)/
	@echo Installing the library files in $(LIBDIR)/
	@mkdir -p $(LIBDIR)/
	@cp -af $(LIBFILES) $(LIBDIR)/
	@echo Installing the manual pages in $(MANDIR)/
	@mkdir -p $(MANDIR)/
	@cp -af $(MANFILES) $(MANDIR)/
	@echo Installing the manual pages in $(MANDIR-PT_BR)/
	@mkdir -p $(MANDIR-PT_BR)/
	@cp -af $(MANFILES-PT_BR) $(MANDIR-PT_BR)/
	@echo Installing the additional documentation in $(DOCDIR)/
	@mkdir -p $(DOCDIR)/
	@cp -af $(DOCFILES) $(DOCDIR)/

# Install the packages and tar files in the distribution sites.
distribute:
# Fix the ownership of the files.
	chown root:root $(LOC)/$(PACKAGE)_*.dsc
	chown root:root $(LOC)/$(PACKAGE)_*.tar.gz
#	chown root:root $(LOC)/$(PACKAGE)_*_i386.deb
	chown root:root $(LOC)/$(PACKAGE)_*_amd64.deb
# Copy remotely and backwards because of authorization issues.
	ssh $(DHOST) "cp -pf $(LOC)/$(PACKAGE)_*.dsc $(DIR)/source/"
	ssh $(DHOST) "cp -pf $(LOC)/$(PACKAGE)_*.tar.gz $(DIR)/source/"
#	ssh $(DHOST) "cp -pf $(LOC)/$(PACKAGE)_*_i386.deb $(DIR)/binary-i386/"
	ssh $(DHOST) "cp -pf $(LOC)/$(PACKAGE)_*_amd64.deb $(DIR)/binary-amd64/"
# Refresh the sites remotely because of authorization issues.
	ssh $(DHOST) "cd $(DSITE) ; ./MakePackagesFiles"
# Install the static tar files in the fismat course web area.
	scp -p $(LOC)/$(PACKAGE)-static-*.tgz $(SHOST):/$(SDIR)/
	chown delyra:www-data /amnt/$(SHOST)/$(SDIR)/$(PACKAGE)-static-*.tgz
	chmod g+w /amnt/$(SHOST)/$(SDIR)/$(PACKAGE)-static-*.tgz

# Clean up this directory.
clean:
	rm -f *~ .*~ *.o a.out trash*
	rm -f complex-functions plot_func plot_func_static
	rm -f complex-functions.1.gz pt_BR/complex-functions.1.gz
	rm -f build-stamp configure-stamp
	rm -f debian/files
	rm -f debian/$(PACKAGE).substvars
	rm -fr debian/$(PACKAGE)/
	rm -fr $(PACKAGE)-static/
	rm -fr tmp/

# Clean the packages and related files.
cleandebpacks:
	rm -f $(LOC)/$(PACKAGE)_*.dsc
	rm -f $(LOC)/$(PACKAGE)_*.tar.gz
#	rm -f $(LOC)/$(PACKAGE)_*_i386.changes
	rm -f $(LOC)/$(PACKAGE)_*_amd64.changes
#	rm -f $(LOC)/$(PACKAGE)_*_i386.deb
	rm -f $(LOC)/$(PACKAGE)_*_amd64.deb
	rm -f $(LOC)/$(PACKAGE)-static-*.tgz

# Clean up even more.
veryclean: clean cleandebpacks
	rm -f complex-functions.eps complex-functions.epsf
	rm -f complex-functions.ps pt_BR/complex-functions.ps

### PART 3 ### : extra dependencies.

# Additional dependencies for the plot_func modules.

plot_func.o: cb_gdata.for g2-ctof.for

wait_to_open.o:

read_params.o: cb_gdata.for g2-ctof.for

write_debug.o: cb_gdata.for

calculate_params.o: cb_gdata.for

open_plotting_window.o: cb_gdata.for g2-ctof.for

open_output_file.o: cb_gdata.for g2-ctof.for

draw_plotting_window.o: cb_gdata.for g2-ctof.for

draw_coordinate_axes.o: cb_gdata.for g2-ctof.for

set_color_scheme.o: cb_gdata.for g2-ctof.for

draw_grid_values.o: cb_gdata.for g2-ctof.for

vector_function.o: cb_gdata.for

draw_single_value.o: cb_gdata.for g2-ctof.for

monitor_events.o: cb_gdata.for g2-ctof.for

refresh_plotting_window.o: cb_gdata.for g2-ctof.for

refresh_output_file.o: cb_gdata.for g2-ctof.for

draw_many_values.o: cb_gdata.for g2-ctof.for

draw_integration_contour.o: cb_gdata.for g2-ctof.for

draw_integral_curve.o: cb_gdata.for g2-ctof.for

draw_many_curves.o: cb_gdata.for g2-ctof.for

### PART 4 ### : pattern rules.

# Fortran modules.
%.o: %.f
	gfortran -O -c -o $@ $<

# C modules.
%.o: %.c
	gcc -O -c -o $@ $<

# A pattern rule to make compressed versions of files.
%.gz: %
	@echo Compressing file $<
	@cat $< | gzip -c9 - > $@

# Fix an EPS file for printing in the ljfma printer.
#
# The A4 size, in PS units: 595.27559 841.88976; since there are
# _ some unprintable margins, we must use as the bounding box:
# _ (12.0, 12.0) and (583.27559, 830.88967).
%.epsf: %.eps
	epsffit -c -s 12.0 12.0 583.27559 830.88976 $< $@

# View a man page in ASCII format.
%.view: %.1
	nroff -man $< | less

# View a man page in PS format, in an X11 window, passing geometry
# _ options to the viewer; the minimum dimensions are 637x843.
%.xview: %.1
	groff -X -P-geometry -P640x880 -man $<

# Produce a file containing a man page in PS format.
%.ps: %.1
	groff -man $< > $@

