#-----------------------------------------------------------------------
#
#                        SYRTHES version 3.4
#                        -------------------
#
#     This file is part of the SYRTHES Kernel, element of the
#     thermal code SYRTHES.
#
#     Copyright (C) 1988-2008 EDF S.A., France
#
#     contact: syrthes-support@edf.fr
#
#
#     The SYRTHES Kernel is free software; you can redistribute it
#     and/or modify it under the terms of the GNU General Public License
#     as published by the Free Software Foundation; either version 2 of
#     the License, or (at your option) any later version.
#
#     The SYRTHES Kernel is distributed in the hope that it will be
#     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
#     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#
#     You should have received a copy of the GNU General Public License
#     along with the Code_Saturne Kernel; if not, write to the
#     Free Software Foundation, Inc.,
#     51 Franklin St, Fifth Floor,
#     Boston, MA  02110-1301  USA
#
#-----------------------------------------------------------------------
#
# Makefile for Syrthes
######################

include $(SYRTHES_HOME)/bin/Makefile.in

# Environment Macros
# ==================

SHELL   = /bin/sh

VERSION = "3.4.2"


# Project configuration macros
# ============================

# Test if variable SYRTHES_HOME is set 

ifndef SYRTHES_HOME 
$(error SYRTHES_HOME is not defined, run 'syrthes.profile' to set a correct value)
endif 

ifeq ($(SYRTHES_HOME),) 
$(error SYRTHES_HOME is a empty string, run 'syrthes.profile' to set a correct value)
endif

# Base project directories

DIR_SYRTHES     = $(SYRTHES_HOME)
BIN_SYRTHES     = $(DIR_SYRTHES)/bin
INC_SYRTHES     = $(DIR_SYRTHES)/include
SRC_SYRTHES     = $(DIR_SYRTHES)/src
UTIL_SYRTHES    = $(SRC_SYRTHES)/util
ARCH_SYRTHES    = $(DIR_SYRTHES)/lib/$(NOM_ARCH)
EXEC_SYRTHES    = $(DIR_SYRTHES)/bin/$(NOM_ARCH)


# Names of the base library and Syrthes executable file

NOM_PREFIX      = "syrthes"
NOM_LIB_MAIN    = "libmain"$(NOM_PREFIX)$(VERSION)_$(NOM_ARCH)
NOM_LIB_SAT     = "libsat"$(NOM_PREFIX)$(VERSION)_$(NOM_ARCH)
NOM_LIB_SYRTHES = "lib"$(NOM_PREFIX)$(VERSION)_$(NOM_ARCH)
NOM_EXE         = "syrthes"
NOM_UTIL1       = "syrthes2ensight"



# Presentation macros
#--------------------

BO = `tput bold`
SO = `tput smso`
NO = `tput rmso; tput sgr0`


# Target definitions
#===================


exe: 
	@for ffile in *.F ; do \
	 if [ -f $${ffile} ] ; then \
	  echo "$${ffile} " ;\
	  $(FC) -c $(FCFLAGS) -I$(INC_SYRTHES) $${ffile} ;\
	 fi ;\
	done

	@for cfile in *.c ; do \
	 if [ -f $${cfile} ] ; then \
	  echo "$${cfile} " ;\
	  $(CC) -c $(CFLAGS) -I$(INC_SYRTHES) $${cfile} ;\
	 fi ;\
	done

	ar xv $(ARCH_SYRTHES)/$(NOM_LIB_MAIN).a syrthes.o

	$(LD) $(LDFLAGS) -o $(NOM_EXE) *.o \
	 $(ARCH_SYRTHES)/$(NOM_LIB_MAIN).a \
	 $(ARCH_SYRTHES)/$(NOM_LIB_SYRTHES).a -lm

	@rm *.o

	@echo "*** Executable '$(NOM_EXE)' created " 


lib:
	$(FC) -c $(FCFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/s/*.F
	$(CC) -c $(CFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/s/*.c
	@ar rv $(NOM_LIB_SYRTHES).a *.o
	@rm -f *.o

	$(FC) -c $(FCFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/ss/*.F
	$(CC) -c $(CFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/ss/*.c
	@ar rv $(NOM_LIB_MAIN).a *.o
	@rm -f *.o

	$(FC) -c $(FCFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/sc/*.F
	$(CC) -c $(CFLAGS) -I$(INC_SYRTHES) $(SRC_SYRTHES)/sc/*.c
	@ar rv $(NOM_LIB_SAT).a *.o
	@rm -f *.o

	@if [ ! -d "$(ARCH_SYRTHES)" ] ;\
	 then mkdir "$(ARCH_SYRTHES)" ;\
	 fi ;\

	@mv *.a $(ARCH_SYRTHES)

#	@echo "*** Libraries created in directory $(ARCH_SYRTHES)" 


install: lib util

	@echo "*** installation successfull" 


util: 
	$(CC) $(CFLAGS) -o $(NOM_UTIL1) $(UTIL_SYRTHES)/$(NOM_UTIL1).c

	@if [ ! -d "$(EXEC_SYRTHES)" ] ; then \
	 mkdir "$(EXEC_SYRTHES)" ;\
	fi ;\

	@mv $(NOM_UTIL1) $(EXEC_SYRTHES)

#	@echo "***Executable '$(NOM_UTIL1)' created in directory $(EXEC_SYRTHES)" 


clean:
	@rm -f *.o


distclean: clean

	@rm -f $(ARCH_SYRTHES)/$(NOM_LIB_SYRTHES).a
	@rm -f $(ARCH_SYRTHES)/$(NOM_LIB_MAIN).a	
	@rm -f $(ARCH_SYRTHES)/$(NOM_LIB_SAT).a
	@rm -f $(NOM_EXE) 
	@rm -f $(EXEC_SYRTHES)/$(NOM_UTIL1) 


help info:
	@echo
	@echo "***  Syrthes $(VERSION) compilation/link"
	@echo "***  System: $(NOM_ARCH)"
	@echo
	@echo
	@echo "Usage : $(BO)make$(NO) $(SO)<target>$(NO) 	(in a user directory)"
	@echo "			 for Syrthes link"
	@echo
	@echo "     with :"
	@echo "     $(SO)<target>$(NO) : $(BO)exe$(NO)         : 'syrthes' executable (default)"
	@echo "                $(BO)install$(NO)     : libraries creation and utilitaries"
	@echo "                $(BO)lib$(NO)         : libraries creation"
	@echo "                $(BO)util$(NO)        : utilitaries"
	@echo "                $(BO)clean$(NO)       : deletes object files"
	@echo "                $(BO)distclean$(NO)   : deletes object files, libraries, and utilitaries"
	@echo "                $(BO)help$(NO)        : this message"
	@echo "                $(BO)info$(NO)        : same"
	@echo
	@echo "     and the additional user options:"
	@echo "     $(SO)<CFLAGS>$(NO)  : C compiler options"
	@echo "     $(SO)<FCFLAGS>$(NO) : Fortran compiler options"
	@echo "     $(SO)<LDFLAGS>$(NO): linker options"
	@echo

