.POSIX:

NAME = cngf-pf
VERSION = 0.9.0

# paths
PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man
DOCPREFIX = ${PREFIX}/share/doc/${NAME}

BIN = \
	cngf-pf\
	deborah\
	max_depth_simple_shear\
	shear_flux\

SCRIPTS = \
	cngf-pf-plot-point-evol\
	cngf-pf-plot-depth-evol\

SRC = \
	arrays.c\
	fluid.c\
	simulation.c\

HDR = \
	arg.h\
	arrays.h\
	fluid.h\
	simulation.h\

LIBS = -lm

CNGFPF_CFLAGS = ${CFLAGS} ${INCS} -DVERSION=\"${VERSION}\"
CNGFPF_LDFLAGS = ${LDFLAGS}
CNGFPF_LIBS = ${LIBS}

MAN1 = \
	${BIN:=.1}\
	${SCRIPTS:=.1}\

DOC = \
	README.md\
	LICENSE\

all: ${BIN}

OBJ = ${SRC:.c=.o}

${OBJ}: ${HDR}

cngf-pf: cngf-pf.o
max_depth_simple_shear: max_depth_simple_shear.o
shear_flux: shear_flux.o
deborah: deborah.o

${BIN}: ${OBJ}
	${CC} ${CNGFPF_LDFLAGS} -o $@ ${@:=.o} ${OBJ} ${CNGFPF_LIBS}

.c.o:
	${CC} ${CNGFPF_CFLAGS} -o $@ -c $<

dist:
	rm -rf "${NAME}-${VERSION}"
	mkdir -p "${NAME}-${VERSION}"
	cp -rf ${MAN1} ${DOC} ${HDR} ${SRC} ${BIN:=.c} ${SCRIPTS} Makefile \
		"${NAME}-${VERSION}"
	# make tarball
	tar cf - "${NAME}-${VERSION}" | \
		gzip -c > "${NAME}-${VERSION}.tar.gz"
	rm -rf "${NAME}-${VERSION}"

install: ${BIN}
	# installing executables
	mkdir -p ${DESTDIR}${PREFIX}/bin
	cp -f ${BIN} ${SCRIPTS} ${DESTDIR}${PREFIX}/bin
	for f in ${BIN} ${SCRIPTS}; do chmod 755 ${DESTDIR}${PREFIX}/bin/$$f; done
	# installing documentation and license information
	mkdir -p ${DESTDIR}${DOCPREFIX}
	cp -f ${DOC} ${DESTDIR}${DOCPREFIX}
	for f in ${DOC}; do chmod 644 ${DESTDIR}${DOCPREFIX}/$$f; done
	# installing man pages
	mkdir -p ${DESTDIR}${MANPREFIX}/man1
	cp -f ${MAN1} ${DESTDIR}${MANPREFIX}/man1
	for f in ${MAN1}; do chmod 644 ${DESTDIR}${MANPREFIX}/man1/$$f; done

uninstall:
	# removing executable files
	for f in ${BIN} ${SCRIPTS}; do rm -f ${DESTDIR}${PREFIX}/bin/$$f; done
	# removing documentation and license information
	for f in ${DOC}; do rm -f ${DESTDIR}${DOCPREFIX}/$$f; done
	-rmdir ${DESTDIR}${DOCPREFIX}
	# removing man pages
	for f in ${MAN1}; do rm -f ${DESTDIR}${MANPREFIX}/man1/$$f; done

test: ${BIN}
	make -C test/

clean:
	rm -f ${BIN} ${BIN:=.o} ${OBJ} test/*.txt

.PHONY: all install uninstall test clean dist
