###############################################################################
#
# Copyright (c) 2005 by Turku PET Centre 
#
# Purpose: Makefile for PET Image Image I/O C library (libtpcimgio).
#
# 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'
#	PET_WW_ARCHIVE = 'where to put www content' (used in Doxyfile)
#
# Required files:
#	Doxyfile
#
# Main targets:
#	compile: compile the library and the test program
#	install: install the library and the test program
#	html: create the html documentation with doxygen
#	archive: archive the source code
#	test: run tests on library
#	clean & clobber: clean the directory
#	upgrade: clean compile test install clobber html 
#
# 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-19 JJ
#	Targets for installation and htlm creation added.
#	Htlm creation with doxygen requires the Doxyfile.
#
# 2005-05-04 JJ
#	Target for archiving added (works only in UNIX/Linux).
#
# 2005-11-21 VO
#	Information is extracted always from local directory (added ./).
#       Progvers and adddate are called without version numbers.
#
###############################################################################
#------      Settings:      ------#
CC 		= gcc
AR 		= ar
DOXYGEN		= doxygen
ZIP		= zip
PRVERS		= progvers
ADDDATE		= adddate
ifneq ($(OS),Windows_NT)
WD_NAME		= `basename ${PWD}`
endif

#------      Compilation parameters:      ------#
LIBS  		= -L. -ltpcimgio
INCLUDE		= -Iinclude -I../libtpcimgp_1_1_0/include -I../libtpcmisc_1_1_0/include
CFLAGS 		= -s -Wall -O2 ${INCLUDE}



#------      Library related variables:      ------#
LIB		= libtpcimgio
LIB_CFILES 	= ecat63r.c ecat63w.c ecat63p.c ecat63ml.c \
		  ecat7r.c ecat7w.c ecat7p.c ecat7ml.c \
		  analyze.c imgfile.c interfile.c vol.c img.c \
		  halflife.c libtpcimgiov.c \
		  sifio.c sif.c weight.c
LIB_OBJECTS 	= ${LIB_CFILES:.c=.o}
LIB_ARCHIVE	= ${LIB}.a
ifeq ($(OS),Windows_NT)
LIB_EXEC	= ${LIB}.exe
else
LIB_EXEC	= ${LIB}
endif
LIB_EXEC_CFILES	= ${LIB}.c
LIB_EXEC_OBJECTS= ${LIB_EXEC_CFILES:.c=.o}

#------      Function serial numbers for testing the library functions;
#------      these should correspond to serial numbers defined in libtpcimgio.c:
ECAT63	 	= 1
ECAT7		= 2


#------      Library compilation rules:      ------#
compile: ${LIB_ARCHIVE} ${LIB_EXEC}

${LIB_ARCHIVE}: ${LIB_OBJECTS}
	${AR} -rvs ${LIB_ARCHIVE} ${LIB_OBJECTS}

${LIB_EXEC}: ${LIB_ARCHIVE} ${LIB_EXEC_OBJECTS}
	${LINK.c} -o ${LIB_EXEC} ${LIB_EXEC_OBJECTS} ${LIBS}

${LIB_VERSION:.c=.o}: ${LIB_CFILES}

#------      Library version rule (create or update the version information file):
libversion: ${LIB}.version

${LIB}.version: ${LIB_CFILES}
	rm -f ${LIB}.version
ifeq ($(OS),Windows_NT)
	${PRVERS} .\\${LIB_EXEC} ${LIB}.version
else
	${PRVERS} ./${LIB_EXEC} ${LIB}.version
endif

#------      Installation rules:      ------#
install: install_lib install_lib_exec

install_lib: libversion
	${ADDDATE} -i=${LIB}.version -o=${PET_LIB} -nodate -copy ${LIB_ARCHIVE}

install_lib_exec: libversion
	${ADDDATE} -i=${LIB}.version -o=${PET_BIN} -nodate -copy ${LIB_EXEC}


#------      Html creation rules:      ------#
html: ${LIB_EXEC}
ifeq ($(OS),Windows_NT)
	.\\${LIB_EXEC} -R > Readme
	.\\${LIB_EXEC} -H > History
else
	./${LIB_EXEC} -R > Readme
	./${LIB_EXEC} -H > History
endif
	${DOXYGEN}


#------      Archiving rules (SORRY, does not work on windows):      ------#
archive: libversion clean
ifneq ($(OS),Windows_NT)
	${ZIP} -r ../${LIB}.zip ../${WD_NAME}
	cp ../${LIB}.zip ${PET_WWW_ARCHIVE}/${LIB}_src.zip
	${ADDDATE} -i=${LIB}.version -o=${PET_ARCHIVE}/lib/${LIB} ../${LIB}.zip
else
	echo "Run 'make archive' only on UNIX/Linux"
endif

#------      Testing rules:      ------#


#------      Cleaning rules:      ------#
clean:
	rm -f ${LIB_OBJECTS} ${LIB_EXEC_OBJECTS}
	rm -f *~ ./include/*~ History Readme
	rm -rf ./html

clobber: clean
	rm -f ${LIB_ARCHIVE} ${LIB_EXEC}


#------      Upgrade rules; do all:      ------#
upgrade: clean compile test install clobber html 
