###############################################################################
#
# Copyright (c) 2005-2007 by Turku PET Centre
#
# Purpose: Makefile for PET Miscellaneous C library (libtpcmisc).
#
# Environment variables:
#	OS = 'operating system'
#	INCLUDE = 'include path' (optional)
#	LDFLAGS = 'library path' (optional)
#	PET_BIN = 'where to install binaries'
#	PET_LIB = 'where to install libraries'
#	PET_ARCHIVE = 'where to archive sources'
#
# Required files:
#	Doxyfile
#
# Main targets:
#	compile: compile the library and the test program
#	test: run tests on library
#	install: install the library and the test program
#	distribution: archiving sources and html docs for backup and distribution
#	clean & clobber: clean the directory
#
# Version:
#  2005-22-02 Jarkko Johansson
#  2005-03-03 JJ
#	Set environment variables INCLUDE and LDFLAGS to location of header
#	files and libraries to be linked (linking for the test program).
#
# 2005-04-25 JJ
#	Targets for installation and htlm creation and archiving added.
#	Htlm creation with doxygen requires the Doxyfile.
#
# 2005-05-04 JJ
#	Target for archiving added (works only in UNIX/Linux).
#
# 2006-03-16 VO
#       Removed version numbers from progvers and adddate.
#
# 2006-07-21 VO
#       Changed according to example library project dated 2006-06-20.
#       Compiler flag -std=gnu99 (default is gnu89).
#
# 2006-10-23 VO
#       Added quots.c and quots.h
#
# 2007-01-26 VO
#       Added halflife.c and halflife.h
#
###############################################################################
# ------------- General settings:----------------------------------------------#
LIBRARY		= libtpcmisc
SOURCES 	= idcrypt.c intex.c petc99.c studynr.c swap.c halflife.c \
		  ift.c iftfile.c iftsrch.c decpoint.c quots.c libtpcmiscv.c
OBJECTS 	= ${SOURCES:.c=.o}
LIBARCHIVE	= ${LIBRARY}.a
DOXYGEN		= doxygen
CC		= gcc
CFLAGS 		= -std=gnu99 -s -Wall -O ${INCLUDE}
LDFLAGS         = -L.
LIBS 		= ${LDFLAGS} -ltpcmisc
ZIP		= zip
PRVERS		= progvers -opt=-b
ADDDATE		= adddate
PDOC2HTML	= pdoc2htm -L


# ------------- OS/terminal dependent settings:--------------------------------#
ifeq ($(OS),Windows_NT)
SUF		= 
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

# ------------- Testing: ------------------------------------------------------#
test: ${LIBEXEC}
	${SUF}${LIBRARY} -f 1

# ------------- 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



