################################################################################
#
#  Makefile for Turku PET Centre libraries.
#
#  Environment variables:
#	OS = 'operating system'
#	INCLUDE = 'include path'
#	LDFLAGS = 'library path'
#	PET_BIN = 'where to install binaries'
#	PET_LIB = 'where to install libraries'
#	PET_ARCHIVE = 'where to archive sources'
#
#  Main targets:
#	compile: compile library and executable
#	install: install library and executable
#	distribution: archiving sources and html docs for backup and distribution
#	clean & clobber: clean the directory
#
#  Usage:
#	Replace <library> with the name of your library (without version number).
#	Replace <library short name> with the short name without lib.
#	Replace <C files> with a list of your C source files.
#	Modify Doxyfile to be used in html creation.
#
#	Compile with: make -e
#       For ANSI compatible version compile with: make "ANSI = -ansi -pedantic"
#
#  Date:
#  2007-03-30 Kaisa Liukko
#             Copied from example_library_project folder
#  2007-17-07 Harri Merisaari
#             Added flag for ANSI (-ansi -pedantic) compiling
#
################################################################################
# ------------- General settings:----------------------------------------------#
LIBRARY		= libtpcmisc
SOURCES 	= substitutions.c idcrypt.c intex.c petc99.c studynr.c swap.c halflife.c \
		  ift.c iftfile.c iftsrch.c decpoint.c quots.c \
                  memory_handler.c libtpcmiscv.c
OBJECTS 	= ${SOURCES:.c=.o}
LIBARCHIVE	= ${LIBRARY}.a
DOXYGEN		= doxygen
CC		= gcc
CFLAGS 		= -std=gnu99 -s -Wall -O2 ${INCLUDE} ${ANSI}
LIBS 		= ${LDFLAGS} -ltpcmisc -L.
ZIP		= zip
PRVERS		= progvers -opt=-b
ADDDATE		= adddate
PDOC2HTML	= pdoc2htm -L

# ------------- OS/terminal dependent settings:--------------------------------#
ifeq ($(OS),Windows_NT)
EXT		= .exe
else
SUF		= ./
endif

LIBEXEC 	= ${LIBRARY}${EXT}

# ------------- Compilation: --------------------------------------------------#
compile: ${LIBARCHIVE} ${LIBEXEC}

# Compile library
${LIBARCHIVE}: ${OBJECTS}
	${AR} -rvs ${LIBARCHIVE} ${OBJECTS}

# Compile executable
${LIBEXEC}: ${LIBARCHIVE} ${LIBRARY}.c
	${CC} ${LIBRARY}.c -o ${LIBEXEC}  ${CFLAGS} ${LIBS}

ifneq ($(OS),Windows_NT)
	 chmod a+x ${LIBEXEC}
endif

# ------------- Version information: ------------------------------------------#
libversion: ${LIBRARY}.version

${LIBRARY}.version: ${LIBEXEC}
	${PRVERS} ${SUF}${LIBEXEC} ${LIBRARY}.version

# ------------- Installation: -------------------------------------------------#
install: libversion libinstall execinstall

libinstall: ${LIBARCHIVE}
	${ADDDATE} -i=${LIBRARY}.version -o=${PET_LIB} -nodate ${LIBARCHIVE}

execinstall: ${LIBEXEC}
	${ADDDATE} -i=${LIBRARY}.version -o=${PET_BIN} -nodate -copy ${LIBEXEC}

# ------------- Distribution: ----------------------------------------------------------#
distribution: libversion doxyapi libbinarchive libsrcarchive

doxyapi: ${LIBARCHIVE} ${LIBEXEC}
	./${LIBEXEC} -R > Readme
	./${LIBEXEC} -H > History
	make_doxyapi.sh ${LIBRARY}

libbinarchive: ${LIBARCHIVE} ${LIBEXEC}
	make_libbinarchive.sh ${LIBRARY}

libsrcarchive: clobber
	make_libsrcarchive.sh ${LIBRARY}

# ------------- Cleanup: ------------------------------------------------------#
clean:
	rm -f ${LIBRARY}${EXT} ${OBJECTS} ${LIBRARY}.a

clobber: clean
	rm -f *~ *.exe

