################################################################################
#
# This GNU Makefile creates the "fitpack" library.
#
# It can compile "fitpack" for AIX, HP-UX, SunOS, LINUX, IRIX, DEC-UNIX and the
# CYGNUS user environment on Window-PCs (CYGWIN). The versions for the different
# operating systems are stored in different subdirectories. For details, see
# below.
#
# Possible targets of this Makefile are:
#
#
# Update: 30/03/2007 R. Wilcke (wilcke@esrf.fr)
#                    use new directory name "LINUX64" for 64-bit LINUX.
# Update: 15/01/2004 R. Wilcke (wilcke@esrf.fr)
#                    use the commands of "LINUX" for "CYGWIN".
# Update: 14/02/2002 R. Wilcke (wilcke@esrf.fr)
#                    changed name of object file directory for HP-UX from "HP"
#                    and "HP-UX-10" to HPUX
# Update: 15/03/2001 R. Wilcke (wilcke@esrf.fr)
#                    changed name of object file directory for SUN from "SUN4"
#                    to SUN
# Update: 12/01/2001 R. Wilcke (wilcke@esrf.fr)
#                    changed references to Window-PCs from "WIN32" to "CYGWIN";
#                    target fitpack: remove the "touch" command and change the
#                    "rm" command to include the option "-f";
#                    add new target "$(DIRECTORY)" to create the directory for
#                    the binaries if it does not exist.
#
################################################################################

# Define system (values = "AIX", "HPUX", "LINUX", "SUN", 
# "SOLARIS", "IRIX', "IRIX64", "DEC-UNIX", or "CYGWIN")
HOST = $(shell uname -s)

ifeq ($(word 1, $(HOST)), AIX)
   SYSTEM = AIX
endif
ifeq ($(word 1, $(HOST)), HP-UX)
   SYSTEM = HPUX
endif
ifeq ($(word 1, $(HOST)), IRIX)
   SYSTEM = IRIX
endif
ifeq ($(word 1, $(HOST)), IRIX64)
   SYSTEM = IRIX64
endif
ifeq ($(word 1, $(HOST)), Linux)
   SYSTEM = LINUX
endif
ifeq ($(word 1, $(HOST)), OSF1)
   SYSTEM = DEC-UNIX
endif
ifeq ($(word 1, $(HOST)), SunOS)
   SYSTEM = SUN
   VERSION = $(shell uname -r)
   ifeq ($(word 1, $(VERSION)),5.5.1)
      SYSTEM = SOLARIS
   endif
endif
ifeq ($(word 1, $(HOST)), ULTRIX)
   SYSTEM = ULTRIX
endif
ifeq ($(findstring CYGWIN,$(HOST)),CYGWIN)
   SYSTEM = CYGWIN
endif

# Choice of compiler and linker options depending on system
ifeq ($(SYSTEM), AIX)
   FORTRAN = xlf
   F77_OPTIONS = -c -O -qcharlen=32000 -qnoescape
   CC = c89
   CC_OPTIONS = -c -I. -O 
   DIRECTORY = AIX/
endif
ifeq ($(SYSTEM), DEC-UNIX)
   FORTRAN = f77
   F77_OPTIONS = -c -w1 -assume backslash -O5 -fast -fpe2 # -C
   CC = cc
   CC_OPTIONS = -c -I. -DEXPG_U 
   DIRECTORY = DEC-UNIX/
   POINTERS = 64
endif
ifeq ($(SYSTEM), HPUX)
   FORTRAN = /opt/fortran/bin/fort77
   F77_OPTIONS = -c +E1 +O2 +Opipeline +DAportable # -C -g -N 
   CC = c89 +O2 +DAportable
#   CC = gcc -O2 -ansi # Slower
   CC_OPTIONS = -c -I. -I/usr/local/include # -O 
   DIRECTORY = $(SYSTEM)/
endif
ifeq ($(SYSTEM), IRIX)
   FORTRAN = f77
   F77_OPTIONS = -c -O -backslash # -mips1
   DIRECTORY = IRIX/
endif
ifeq ($(SYSTEM), IRIX64)
   FORTRAN = f77
   F77_OPTIONS = -c -o32 -backslash -O
   DIRECTORY = IRIX64/
#   POINTERS = 64
endif
ifeq ($(SYSTEM), LINUX)
   ifeq ($(findstring (GCC) 3,$(shell gcc --version)),(GCC) 3)
      FORTRAN = g77
   else
      FORTRAN = gfortran -std=legacy
   endif
   F77_OPTIONS = -c -O -fno-backslash -fno-second-underscore
   CC = gcc -O
   CC_OPTIONS = -c -I. -I/usr/local/include # -O 
   ifeq ($(shell uname -m),x86_64)
      DIRECTORY = LINUX64/
   else
      DIRECTORY = LINUX/
  endif
endif
ifeq ($(SYSTEM), SOLARIS)
   FORTRAN = f77
   F77_OPTIONS = -c -xl -O # -cg89 -C -w 
   DIRECTORY = SOLARIS/
endif
ifeq ($(SYSTEM), SUN)
   ifeq ($(findstring (GCC) 3,$(shell gcc --version)),(GCC) 3)
      FORTRAN = g77
   else
      FORTRAN = gfortran -std=legacy
   endif
   F77_OPTIONS = -c -O -fno-backslash -fno-second-underscore
   DIRECTORY = SUN/
endif
ifeq ($(SYSTEM), ULTRIX)
   FORTRAN = f77
   F77_OPTIONS = -c -O -w1 -assume backslash # -C
   CC = cc
   CC_OPTIONS = -c -I. -DEXPG_U 
   DIRECTORY = ULTRIX/
endif
ifeq ($(SYSTEM), CYGWIN)
   ifeq ($(findstring (GCC) 3,$(shell gcc --version)),(GCC) 3)
      FORTRAN = g77
   else
      FORTRAN = gfortran -std=legacy
   endif
   F77_OPTIONS = -c -O -fno-backslash -fno-second-underscore
   CC = gcc -O
   CC_OPTIONS = -c -I. -I/usr/local/include
   DIRECTORY = CYGWIN/
endif

# Define directory path for dependencies
vpath %.o ./$(DIRECTORY)
vpath %.f ./$(DIRECTORY)

# Define symbol for FITPACK subroutine files
FITPACK_SUBS =  \
bispev.o \
curfit.o \
fpback.o \
fpbspl.o \
fpbisp.o \
fpchec.o \
fpcurf.o \
fpdisc.o \
fpgivs.o \
fpknot.o \
fporde.o \
fprank.o \
fprati.o \
fprota.o \
fpsurf.o \
splev.o \
surfit.o 

# Define "make" command for creating "fitpack" library
fitpack: $(DIRECTORY) $(FITPACK_SUBS)
	rm -f $(DIRECTORY)libfitpack.a
	(cd $(DIRECTORY); ar r libfitpack.a $(FITPACK_SUBS))
	(cd $(DIRECTORY); ranlib libfitpack.a)

# Remove all object files
clean:
	(cd $(DIRECTORY); rm *.o)

# Update all files
touch:
	touch *.f

#
# Make the directory for the binaries and the library if it does not yet exist.
#
$(DIRECTORY):
	mkdir -p $(DIRECTORY)

# Rule to compile all Fortran source files (special processing for 64-bit
# architectures to define all pointer variables as 'Integer*8' instead of
# Integer (THIS SHOULDN'T BE NECESSARY, BUT I'LL LEAVE IT ANYWAY)
ifeq ($(SYSTEM), LINUX) 
$(FITPACK_SUBS): %.o: %.f
	$(FORTRAN) $(F77_OPTIONS) $<
	mv $*.o ./$(DIRECTORY)$*.o
else
ifeq ($(POINTERS), 64)
$(FITPACK_SUBS): %.o: %.f
	@sed -e '/Integer p[A-Z]/s/Integer/Integer*8/' $< > $*.8.f
	$(FORTRAN) $(F77_OPTIONS) $*.8.f -o ./$(DIRECTORY)$(notdir $@)
	@rm -f $*.8.f
else
$(FITPACK_SUBS): %.o: %.f
	$(FORTRAN) $(F77_OPTIONS) $< -o ./$(DIRECTORY)$(notdir $@)
endif
endif
