################################################################################
#
# This Makefile creates the executables "ana", "ave" and "rod" of the ANAROD
# analysis package.
#
# There is a "standard" and an "experimental" version of ANAROD. Both can be
# compiled with the same Makefile, but the macro "EXTENSIONS" should be set to
# EXTENSIONS for the "experimental" version and not be set for the "standard"
# one.
#
# The string defined in the file "version.mk" is concatenated with either
# "_standard" or "_experimental" and passed on to the compiler. It is used by
# the programs to create a version identifier.
#
# The user interface of ANAROD can operate with "ncurses" (default) or with the
# GNU "readline". If the latter is requested, the macro "READLINE" should be set
# to READLINE.
#
# This Makefile can compile ANAROD for HP-UX, SunOS, LINUX, Mac OS X 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.
#
# Targets of this Makefile are:
#
# - all               (default) to make all executables ("ana", "ave" and "rod")
# - ana               to make only "ana"
# - ave               to make only "ave"
# - rod               to make only "rod"
#
# - clean             to clean everything that was created by the Makefile
# - clean-bin         to clean all intermediate files ("*.o", YACC-output)
# - clean-ana         to clean only the "ana" specific parts
# - clean-ave         to clean only the "ave" specific parts
# - clean-rod         to clean only the "rod" specific parts
#
# - depend            to create a list of dependencies for the ANAROD package,
#                     using the program "makedepend"
#
####### Note: the targets mentioned below are only available at the ESRF.
#
# - dist              to make a binary distribution file. This is a g-zipped
#                     tar file with the executables "ana", "ave", "rod", and the
#                     information files (see explanation below). For CYGWIN,
#                     additionally create a "zip" archive with the same files.
# - distclean         to remove the binary distribution file
#
# - source-dist       to make a source distribution file. This is a g-zipped tar
#                     file with
#                     - all the sources for "ana", "ave" and "rod" with the
#                       exception of "numrec.c"
#                     - the "pgplot" libraries (for the standard version only)
#                     - the binary file of "numrec.c"
#                     - all auxiliary files needed (see explanation below)
#                     - the "Makefile" and the dummy include file for the ESRF-
#                       internal targets (file "dum_targ.mk" copied to
#                       "esrf_targ.mk" in the distribution file)
#                     - all information files (see explanation below)
#                     For CYGWIN, additionally create a "zip" archive with the
#                     same files.
# - source-distclean  to remove the source distribution file
#
# - devrun-dist       to make a "development and runtime" distribution file with
#                     the external files needed for linking and running ANAROD.
#                     This is a g-zipped tar file containing the PGPLOT
#                     libraries and runtime files. For CYGWIN, this file also
#                     contains the GRWIN library and server as well as some
#                     CYGWIN system libraries and TERMINFO files.
#                     For CYGWIN, additionally create a "zip" archive with the
#                     same files.
# - devrun-distclean  to remove the "development and runtime" distribution file
#
#
# - install-sware     to install on the ESRF computers
#                     - the ANAROD executables "ana", "ave" and "rod"
#                     - all information files (see Makefile)
#                     The installation is done into the directory given by the
#                     macros INSTDIR/OSTYPE, where INSTDIR is the general ESRF
#                     installation directory for ANAROD and OSTYPE an
#                     OS-dependent subdirectory.
#                     This should be used to replace an old version of the
#                     ANAROD executables with a new one.
# - install-sware-new to install a second version of the ANAROD executables on
#                     the ESRF computers. The installation is done into the
#                     same directory as for target "install-sware", but the
#                     executables are named  "ana_new", "ave_new" and "rod_new".
#                     This could e.g. be used to make a preliminary version of
#                     the ANAROD executables available.
#
################################################################################
# Update: 24/09/2007 R. Wilcke (wilcke@esrf.fr)
#                    copy the GRWIN_FIL instead of the RUN_FILS files to the
#                    runtime directory (CYGWIN only);
#                    remove macro RUN_FILS (no longer needed).
# Update: 17/09/2007 R. Wilcke (wilcke@esrf.fr)
#                    use new macro RMALL to remove directories (the command
#                    "rm -rf" does not work properly on CYGWIN);
#                    remove macro RMDIR;
#                    new macros PGPLOT_DIR and PGPLOT_FIL to define the location
#                    of the PGPLOT package and list the runtime files in it;
#                    new macros GRWIN_DIR and GRWIN_FIL to define (for CYGWIN)
#                    the location and files of the "GrWin" package;
#                    new macros TERMDIR, TERMLIST and TERMFILS to define the
#                    location of the "terminfo" files and the terminals needed;
#                    new macro DRDIS_NAM with the name of the development and
#                    runtime distribution directory;
#                    create new targets "devrun-dist" and "devrun-distclean" to
#                    make and clean the distributions with the development and
#                    runtime files;
#                    rename targets
#                         "clean_bin", "clean_ana", "clean_ave" and "clean_rod"
#                    to   "clean-bin", "clean-ana", "clean-ave" and "clean-rod".
# Update: 06/09/2007 R. Wilcke (wilcke@esrf.fr)
#                    remove macros ANA, AVE and ROD, define new macro EXESFX for
#                    the suffix of names of executables for CYGWIN;
#                    add new target "clean_bin".
#                    use modified include file "version.mk" to get the version
#                    numbers of both experimental and standard ANAROD as well
#                    as the version numbers of PGPLOT and GrWin.
# Update: 22/08/2007 R. Wilcke (wilcke@esrf.fr)
#                    add for CYGWIN "grwnd.ini" to the auxiliary files.
# Update: 17/08/2007 R. Wilcke (wilcke@esrf.fr)
#                    distinguish for Mac between PowerPC and Intel architecture;
#                    redefine DIRNAM for all other operating systems as well;
#                    use the DIRNAM macro as the second part of the VERSION
#                    string;
#                    remove "pgxwin_server", "grfont.dat" and "rgb.txt" from the
#                    list of auxiliary files.
# Update: 29/06/2007 R. Wilcke (wilcke@esrf.fr)
#                    remove "../standard/lib/$(DIRNAM)" from CURSES_LFLAGS.
# Update: 29/06/2007 R. Wilcke (wilcke@esrf.fr)
#                    do no longer install the auxiliary files on the ESRF
#                    computers.
# Update: 20/06/2007 R. Wilcke (wilcke@esrf.fr)
#                    distinguish for Linux between 32-bit (LINUX) and 64-bit
#                    (LINUX64) architectures;
#                    modify SUN part to correspond to "solaris8";
#                    use "yacc" instead of "bison" for SUN and the "-b" option
#                    to name the output file (-o does not exist for yacc).
# Update: 30/03/2007 R. Wilcke (wilcke@esrf.fr)
#                    changes to reflect the new structure of the "lsqfit" files
#                    (copyright protected code is now in "numrec.c").
# Update: 27/02/2007 R. Wilcke (wilcke@esrf.fr)
#                    add -Isrc/include to INCLUDE_PATH to allow header files in
#                    an "include" directory of the "experimental" version.
# Update: 10/04/2006 R. Wilcke (wilcke@esrf.fr)
#                    updated the comments on the ESRF-internal targets;
#                    add "README_MAC.txt" to the INFOFILS for MACOSX;
#                    define macro RMDIR for command "rmdir", with special option
#                    for LINUX and CYGWIN;
#                    define macro GREP for command "grep", with special path for
#                    for SUNOS to allow use of "-e" option;
#                    define macro OSTYPE as the target subdirectory for
#                    installing ANAROD at the ESRF computers.
# Update: 24/03/2006 R. Wilcke (wilcke@esrf.fr)
#                    get the ESRF-internal targets from the include file
#                    "esrf_targ.mk".
# Update: 23/03/2006 R. Wilcke (wilcke@esrf.fr)
#                    correct and streamline code for the source distribution
#                    target.
# Update: 20/03/2006 R. Wilcke (wilcke@esrf.fr)
#                    get the version number from the include file "version.mk";
#                    replace installation host and directory by macros INSTHOST
#                    and INSTDIR;
#                    create macro INFOFILS for the information files;
#                    use implicit pattern rules instead of static pattern rules
#                    to make the "ana", "ave" and "rod" object files from the
#                    "C" source, and add a rule for "C++" source files.
# Update: 23/02/2006 R. Wilcke (wilcke@esrf.fr)
#                    add statements for MACOSX; streamline code for binary and
#                    source distribution targets.
# Update: 27/04/2004 R. Wilcke (wilcke@esrf.fr)
#                    remove "Wall" flag from CFLAGS (too many warnings).
# Update: 25/03/2002 O. Svensson (svensson@esrf.fr)
#                    Removed cygwin1.dll from auxiliary files.
# Update: 31/01/2002 C.J.Walker <walker@esrf.fr>
#                    Use $(CC) rather than call gcc explicitly
# Update: 28/11/2001 O. Svensson (svensson@esrf.fr)
#                    Fixed problem with readline for HPUX (patch from
#                    Chris Walker).
# Update: 26/11/2001 O. Svensson (svensson@esrf.fr)
#                    Updated version to 1.1
#                    Added CFLAGS += -I../standard/src/include/curses
#                    which contains the curses.h for the SUN and HPUX versions
#                    since curses.h has been removed from src/include
# Update: 11/01/2001 O. Svensson (svensson@esrf.fr)
#                    Added new targets "source-dist" and "source-distclean".
# Update: 10/01/2001 O. Svensson (svensson@esrf.fr)
#                    Removed the creation of the library MEFIPL_LIB
#                    (libMenuFitPlot.a) and replaced it with individual
#                    object files MENU_OBJ, PLOT_OBJ and LSQ_OBJ.
# Update: 21/12/2000 O. Svensson (svensson@esrf.fr)
#                    Increased version number for standard version to 1.0.
# Update: 01/12/2000 O. Svensson (svensson@esrf.fr)
#                    Turned on optimization (-O2), added new targets
#                    "install-sware" and "install-sware-new" which are
#                    useful for installing new versions at the ESRF,
#                    (can presently only be run by user "svensson".)
#                    Fixed "depend" target, however the output appended
#                    to the makefile cannot be used directly:
#                    * "$(DIRNAM)/src" must be changed to $(DIRNAM)
#                    * "bin/$(DIRNAM)/../standard/src/" must be changed to
#                       "../standard/bin/$(DIRNAM)/".
#                    * lsqfit/lsqfit.o must be changed to lsqfit.o
#                    * plot/PGmyplot.o must be changed to PGmyplot.o
#                    * menu/menu.tab.c must be changed to menu.tab.c
#                    Forced the compilation of ana.c, ave.c and rod.c when
#                    linking in order to have the correct compilation date
#                    displayed by the programs.
# Update: 28/09/2000 R. Wilcke (wilcke@esrf.fr)
#                    rename (again!) target "clean_dist" to "distclean".
# Update: 27/09/2000 R. Wilcke (wilcke@esrf.fr)
#                    Make target "rod" also dependent on "$(RUN_DIR)";
#                    modify some lines (SUN specific setup, "dist" and
#                    "dist-clean" targets) to make the "experimental" and
#                    "standard" Makefile as identical as possible;
#                    added -L../standard/lib/$(DIRNAM) to GRAPHICAL_LFLAGS in
#                    order to find libcpgplot.a and libpgplot.a;
#                    remove target "clean_full" and change target "clean" to do
#                    what "clean_full" used to do;
#                    rename target "dist-clean" to "clean_dist".
# Update: 18/07/2000 R. Wilcke (wilcke@esrf.fr)
#                    Include the version string ("experimental-x-x" or
#                    "standard-x-x") as macro VERSION in the CFLAGS options;
#                    use the Makefile in the "standard" tree to make "ave" if
#                    there is no "ave" source code in the "experimental" tree;
#                    in the "experimental" tree, create the "runtime" directory
#                    with the corresponding subdirectory for the operating
#                    system and put the auxiliary runtime files into it;
#                    create the directory for the common object files and the
#                    one for the corresponding library if necessary.
# Update: 17/07/2000 R. Wilcke (wilcke@esrf.fr)
#                    For CYGWIN, test only for the "CYGWIN" substring in the
#                    "uname" command output (avoids dependency on version).
# Update: 12/07/2000 R. Wilcke (wilcke@esrf.fr)
#                    For LINUX: added "-static" to LFLAGS and moved "-lX11"
#                    behind "-lcpgplot" and "-lpgplot".
# Update: 11/07/2000 R. Wilcke (wilcke@esrf.fr)
#                    First working version for new directory structure.
################################################################################
#
# Uncomment "READLINE" if the "readline" package is to be used for user I/O.
#
# READLINE = READLINE

#
# Define some commands that might need customization on different systems.
#
AR = ar ru
CC = gcc
CP = cp -p
CXX = g++
GREP = grep
MAKE = gmake
RANLIB = ranlib
RM = rm -rf
#
# "rm -rf" does not delete directories under CYGWIN (CYGWIN bug). To delete
# directories, the macro RMALL has been defined, which contains special DOS
# commands for this task.
#
# The macro NAMECONV is used on CYGWIN to convert filenames from DOS to UNIX
# notion. For UNIX, this is a dummy.
#
RMALL = rm -rf
NAMECONV = echo
RSH = rsh
SHELL = /bin/sh
YACC = bison

#
# Define where the package is to be installed.
# The values given are the default values at the ESRF. Note that INSTHOST must
# be able to write to INSTDIR (this directory is read-only on most ESRF
# computers).
#
# The executables are installed in the OS-dependent subdirectory contained in
# OSTYPE. At the ESRF, this subdirectory is defined by a script for those
# architectures that are supported by the ESRF Computing services. For all
# others, the value "unknown" is used.
#
INSTHOST = scisoft8
INSTDIR = /sware/exp/anarod
ifneq ($(wildcard /csadmin/common/scripts/get_os),)
OSTYPE = $(shell /csadmin/common/scripts/get_os)
else
OSTYPE = unknown
endif

#
# PGPLOT_DIR is the location of the PGPLOT package. This needs to be re-defined
# for some operating systems (at present SUN, MACOSX PowerPC and CYGWIN).
#
# PGPLOT_FIL lists the PGPLOT files that ANAROD needs for linking and running.
#
PGPLOT_DIR := /opt/pgplot
PGPLOT_FIL := libcpgplot.a libpgplot.a pgxwin_server grfont.dat rgb.txt

#
# On CYGWIN, ANAROD needs the GrWin package. There is at present no standard
# location for this, an installation-dependent value is defined in the CYGWIN
# specific part of the Makefile further below.
#
# GRWIN_FIL lists the GrWin files that ANAROD needs for linking and running.
#
# Technical note: on CYGWIN, there is no "pgxwin_server", it will thus be
# removed from PGPLOT_FIL in the CYGWIN-specific part of the Makefile.
#
GRWIN_DIR :=
GRWIN_FIL := libGrWin.a grwnd.exe grwnd.ini

#
# If the CYGWIN version of ANAROD is to be run on a PC where CYGWIN is not
# installed, the files below are needed as well:
# - the CYGWIN and the "ncurses" libraries;
# - the terminfo files for the terminals "ansi", "linux" and "vt100".
#
CYGWIN_FIL := /usr/bin/cygwin1.dll /usr/bin/cygncurses-8.dll
TERMLIST := ansi linux vt100

#
# There should normally not be any need to change values below this line.
#
################################################################################
#
# Get macros with the version numbers here from the file "version.mk":
# - STANDARD     for the standard version of ANAROD;
# - EXPERIMENTAL for the experimental version of ANAROD;
# - PGPLOT       for the PGPLOT package;
# - GRWIN        for the GrWin package.
#
# Then set "VERSION" macro from "STANDARD" or "EXPERIMENTAL".
# The VERSION number together with the version type ("standard" or
# "experimental") is passed on to the compiler with the "-D" option.
# The backslashes and quotes (\") are necessary to pass it as a string.
#
include version.mk

ifeq (experimental,$(findstring experimental,$(PWD)))
VERSION := $(EXPERIMENTAL)
else
VERSION := $(STANDARD)
endif

VERSION := $(notdir $(shell pwd))_$(VERSION)
CFLAGS := -O2 -DVERSION=\"$(VERSION)\" -DPGPLOT

#
# Define "EXTENSIONS" if "anarod" is to be compiled with extensions
# ("experimental" version).
#
ifeq (experimental,$(findstring experimental,$(PWD)))
EXTENSIONS = EXTENSIONS
endif

#
# Find out the operating system, and define the names of the directories
# for the object files, libraries and executables accordingly:
# - the object files will be in   bin/$(DIRNAM)
# - the libraries will be in      lib/$(DIRNAM)
# - the executables will be in    runtime/$(DIRNAM)
#
# where $(DIRNAM) is
# - hpux            on HP-UX systems
# - solaris         on SunOS systems
# - linux           on 32 bit LINUX systems
# - linux64         on 64 bit LINUX systems (type x86-64)
# - darwin_PowerPC  on PowerPC MacIntoshes with OS X (Darwin)
# - darwin_i386     on Intel MacIntoshes with OS X (Darwin)
# - cygwin          on PCs with the Cygnus user environment
#
# For the object files, the location is further subdivided:
# - object files for "ana" are in                          bin/$(DIRNAM)/ana
# - object files for "ave" are in                          bin/$(DIRNAM)/ave
# - object files for "rod" are in                          bin/$(DIRNAM)/rod
# - object files from "lsqfit" are in                      bin/$(DIRNAM)/lsqfit
# - all other object files (e.g. from "menu") are in       bin/$(DIRNAM)
#
# The names of the executables are different on CYGWIN, as they must have the
# suffix ".exe". On all other operating systems, no specific suffix is needed.
#
# The executables of the ANAROD package need additional files to work. These
# are stored in the same subdirectory as the executables. For the "experimental"
# version, it is permitted that these directories initially do not exist. In
# this case, the Makefile will automatically create them and copy the additional
# files over from the "standard" version.
#
# The auxiliary files are:
# - common (i.e., for all operating systems): none
# - additionally for HPUX, SUN, LINUX and MACOSX: none
# - additionally for CYGWIN: - grfont.dat, rgb.txt, grwnd.exe, grwnd.ini;
#                            - the terminfo files for the terminals defined 
#                              further above in TERMLIST.
#
# Finally, there are files with information on how to install ANAROD, its use
# and development history. These are for:
# - all systems: INSTALL.UNIX README.txt README_SRC.txt license.txt version.mk
# - Macintosh additionally: README_MAC.txt
# - CYGWIN    additionally: README_GNUWIN.txt
#
INFOFILS := INSTALL.UNIX README.txt README_SRC.txt license.txt version.mk

OPSYS := $(shell uname)
MACHTYPE := $(shell uname -m)
X11DIR := /usr/X11R6/lib

ifeq ($(OPSYS),HP-UX)

DIRNAM = hpux
RSH = remsh
INCLUDE_PATH += -I../standard/src/include/curses
#
# HPUX needs the -O flag for compilation, otherwise the resulting executable
# crashes. Why???
#
CFLAGS += -O
X11DIR := /usr/lib/X11R5
GRAPHICAL_LFLAGS = -lcl -lf -L$(PGPLOT_DIR) -lcpgplot -lpgplot -lgcc -lc \
   -lHcurses -L/usr/lib/Motif1.2/ -lXm -L$(X11DIR) -lXt -lX11 -lXhp11 -lm

else
ifeq ($(OPSYS),SunOS)

DIRNAM = solaris
PGPLOT_DIR=../standard/pgplot/$(DIRNAM)
GREP = /usr/xpg4/bin/grep
YACC = yacc
INCLUDE_PATH += -I../standard/src/include/curses
#
# The socket and nsl libraries are only needed if EXTENSIONS are used.
#
#ifdef EXTENSIONS
#LFLAGS += -lsocket -lnsl
#endif
X11DIR := /usr/X/lib
GRAPHICAL_LFLAGS = $(PGPLOT_DIR)/libcpgplot.a $(PGPLOT_DIR)/libpgplot.a \
   -B dynamic -lgcc -lc -lm -L$(X11DIR) -lX11 -lsocket -lnsl \
   -L/opt/SUNWspro/SC5.0/lib -lF77 -lM77 -lsunmath -ldl

else
ifeq ($(OPSYS),Linux)

ifeq ($(MACHTYPE),x86_64)
DIRNAM = linux64
X11DIR := /usr/X11R6/lib64
else
DIRNAM = linux
endif
MAKE = make
LFLAGS = -static
#
# "pthread" library has to be linked BEFORE "c" library, otherwise there are
# doubly and inconsistently defined routines.
#
GRAPHICAL_LFLAGS = -L$(PGPLOT_DIR) -lcpgplot -lpgplot -L$(X11DIR) -lX11 \
   -lg2c -lpthread -lc

else
ifeq ($(OPSYS),Darwin)

LFLAGS = -dynamic -framework Foundation -framework AppKit
ifeq ($(MACHTYPE),i386)
DIRNAM = darwin_i386
# Pick up the Fortran compiler routines needed for the calls to PGPLOT.
LFLAGS += -L/opt/g95/g95-install/lib/gcc-lib/i386-apple-darwin8.10.1/4.0.3 -lf95
else
DIRNAM = darwin_PowerPC
PGPLOT_DIR=../standard/pgplot/$(DIRNAM)
# Pick up the Fortran compiler routines needed for the calls to PGPLOT.
LFLAGS += -L/opt/g95/g95-install/lib/gcc-lib/powerpc-apple-darwin6.8/4.0.3 -lf95
endif      # i386

MAKE = make
INFOFILS += README_MAC.txt
CFLAGS += -dynamic
GRAPHICAL_LFLAGS = -L$(PGPLOT_DIR) -lcpgplot -lpgplot -lpng \
   -L$(X11DIR) -lX11 -lgcc -lc
#   -laquaterm -L$(X11DIR) -lX11 -lg2c -lgcc -lc
CURSES_LFLAGS = -L/sw/lib -L/sw/lib/ncurses

else
ifeq ($(findstring CYGWIN,$(OPSYS)),CYGWIN)

DIRNAM = cygwin
MAKE = make
#
# "rm -rf" does not delete directories under CYGWIN (CYGWIN bug). 
# Instead, use the DOS command "rmdir" from the DOS command interpreter "cmd".
# The options mean:
#    /c   carries out the command specified and then terminates
#    /s   removes all directories and files in the specified directory
#    /q   quiet mode, do not ask if OK to remove a directory tree with /s
#
# The macro NAMECONV is used to convert the filenames from DOS to UNIX notion.
#
RMALL = cmd /c rmdir /s /q
NAMECONV = cygpath -w
PGPLOT_DIR = ../standard/pgplot/$(DIRNAM)
PGPLOT_FIL := $(filter-out pgxwin_server,$(PGPLOT_FIL))
GRWIN_DIR = ../standard/pgplot/$(DIRNAM)
EXESFX = .exe
#
# "terminfo" files for the terminals listed in TERMLIST.
#
TERMDIR := /usr/share
TERMFILS := $(foreach fi,$(TERMLIST),$(shell cd $(TERMDIR);ls terminfo/*/$(fi)))
#
INFOFILS += README_GNUWIN.txt
GRAPHICAL_LFLAGS = -L$(PGPLOT_DIR) -lcpgplot -lpgplot -lGrWin -lg2c -lgcc -lm \
   -Wl,--subsystem,console -mwindows

else
DIRNAM = undefined
endif      # Cygwin
endif      # Darwin
endif      # Linux
endif      # SunOS
endif      # HP-UX

VERSION := $(VERSION)_$(DIRNAM)
#
# The user interface can operate with the GNU "readline" library, or with the
# "ncurses" library. Define the macros for compilation and the library path
# accordingly.
#
ifdef READLINE
READLINE_CFLAGS = -DREADLINE -I/opt/readline/include -I/usr/local/include \
   -I/sw/include/readline
READLINE_LFLAGS = -L/opt/readline/lib -L/usr/local/lib -L/sw/lib \
   -lreadline -lncurses
else
READLINE_CFLAGS =
READLINE_LFLAGS =
endif      # READLINE

CURSES_LFLAGS += -lncurses

ifdef EXTENSIONS
EXTENSIONS_CFLAGS = -DEXTENSIONS
endif

INCLUDE_PATH += -Isrc/include $(addprefix -I../standard/src/, plot menu include)

CFLAGS += -c $(INCLUDE_PATH) $(READLINE_FLAGS) $(READLINE_CFLAGS) \
   $(EXTENSIONS_CFLAGS)

LFLAGS += -lm $(READLINE_LFLAGS) $(CURSES_LFLAGS)
#
# Determine the source file names and the corresponding object file names for
# "ana", "ave" and "rod".
#
# "ana" and "rod" are always in the same directory tree (i.e., "standard" or
# "experimental") as the Makefile. For "ave", there might or might not be an
# experimental version. If there is one, it is taken. If there is none (the
# directory "experimental/src/ave" does not contain any C-source files), then
# the version in "standard" is taken.
#
ANA_SRC_FILES := $(wildcard src/ana/*.c)
ANA_OBJECTS := $(patsubst %.c, %.o, $(notdir $(ANA_SRC_FILES)))

AVE_SRC_FILES := $(wildcard src/ave/*.c)
ifeq ($(AVE_SRC_FILES),)
AVE_DIR := ../standard
AVE_SRC_FILES := $(wildcard $(AVE_DIR)/src/ave/*.c)
else
AVE_DIR := .
endif
AVE_OBJECTS := $(patsubst %.c, %.o, $(notdir $(AVE_SRC_FILES)))

ROD_SRC_FILES := $(wildcard src/rod/*.c src/rod/*.cpp)
ROD_OBJECTS := $(addsuffix .o, $(basename $(notdir $(ROD_SRC_FILES))))

ANA_OBJ_DIR = bin/$(DIRNAM)/ana
AVE_OBJ_DIR = $(AVE_DIR)/bin/$(DIRNAM)/ave
ROD_OBJ_DIR = bin/$(DIRNAM)/rod

ANA_OBJ_FILES = $(addprefix $(ANA_OBJ_DIR)/, $(ANA_OBJECTS))
AVE_OBJ_FILES = $(addprefix $(AVE_OBJ_DIR)/, $(AVE_OBJECTS))
ROD_OBJ_FILES = $(addprefix $(ROD_OBJ_DIR)/, $(ROD_OBJECTS))
#
# Define the source file names and the corresponding object file names for the
# modules that are common for "ana", "ave" and "rod":
# - the fitting package "lsqfit"
# - the plotting package "PGmyplot"
# - the user interface "menu"
#
# "PGmyplot" and "menu" will always be in the "standard" directory tree.
#
# For "lsqfit" directory, the same rule applies as above explained for "ave".
# Also, add "numrec.o" to the list of "lsqfit" objects if it is not there (this
# is the case for the "source-distributed" ANAROD, see remark in target
# "source-dist").
#
PLOT_OBJ := $(addprefix ../standard/bin/$(DIRNAM)/, PGmyplot.o)
MENU_OBJ := $(addprefix ../standard/bin/$(DIRNAM)/, menu.tab.o)

LSQ_SRC_FILES := $(wildcard src/lsqfit/*.c)
ifeq ($(LSQ_SRC_FILES),)
LSQ_DIR := ../standard
LSQ_SRC_FILES := $(wildcard $(LSQ_DIR)/src/lsqfit/*.c)
else
LSQ_DIR := .
endif
LSQ_OBJECTS := $(patsubst %.c, %.o, $(notdir $(LSQ_SRC_FILES)))
LSQ_OBJ_DIR = $(LSQ_DIR)/bin/$(DIRNAM)/lsqfit
LSQ_OBJ_FILRM := $(addprefix $(LSQ_OBJ_DIR)/, $(LSQ_OBJECTS))
LSQ_OBJECTS := $(sort numrec.o $(LSQ_OBJECTS))
LSQ_OBJ_FILES := $(addprefix $(LSQ_OBJ_DIR)/, $(LSQ_OBJECTS))

COMM_OBJ := $(PLOT_OBJ) $(MENU_OBJ) $(LSQ_OBJ_FILES)

#
# Define the name of the runtime directory, where the executables and the
# auxiliary files are stored.
#
# The name is "runtime/$(DIRNAM)", where DIRNAM has the same value as explained
# above.
#
RUN_DIR = runtime/$(DIRNAM)

#
# Define the names of the distribution directories: binary, source and
# development/runtime.
#
BDIS_NAM  := anarod_$(VERSION)
SDIS_NAM  := anarod_src_$(VERSION)
SDIS_DIR  := $(SDIS_NAM)/$(notdir $(shell pwd))
SRC_DIRS  := $(filter-out %CVS,$(wildcard src/*))
ifeq ($(findstring CYGWIN,$(OPSYS)),CYGWIN)
DRDIS_NAM := pgplot$(PGPLOT)_grwin$(GRWIN)_devrun_$(DIRNAM)
else
DRDIS_NAM := pgplot$(PGPLOT)_devrun_$(DIRNAM)
endif      # Cygwin

#
# Make all executables ("ana", "ave" and "rod"). This is the default.
#
all:   ana ave rod

#
# Make each individual executable and store them in the runtime directory.
#
# "ana" and "rod" will always be in the same directory tree as the Makefile.
# "ave" will be in the "experimental" tree if an "experimental" version of the
# "ave" sources was used, otherwise the Makefile in the "standard" tree will be
# called to make "ave" there.
#
ana:   $(RUN_DIR) $(RUN_DIR)/ana

ifeq ($(AVE_DIR),../standard)
ave:
	(cd $(AVE_DIR); $(MAKE) ave)
else
ave:   $(RUN_DIR) $(RUN_DIR)/ave
endif

rod:   $(RUN_DIR) $(RUN_DIR)/rod

$(RUN_DIR)/ana: $(ANA_OBJ_DIR) $(ANA_OBJ_FILES) $(COMM_OBJ)
	$(CC) -o $@ $(ANA_OBJ_FILES) $(COMM_OBJ) $(LFLAGS) $(GRAPHICAL_LFLAGS)

$(RUN_DIR)/ave: $(AVE_OBJ_DIR) $(AVE_OBJ_FILES) $(MENU_OBJ)
	$(CC) -o $@ $(AVE_OBJ_FILES) $(MENU_OBJ) $(LFLAGS)

$(RUN_DIR)/rod: $(ROD_OBJ_DIR) $(ROD_OBJ_FILES) $(COMM_OBJ)
	$(CXX) -o $@ $(ROD_OBJ_FILES) $(COMM_OBJ) $(LFLAGS) $(GRAPHICAL_LFLAGS)

#
# Make the runtime file directory if it does not yet exist, and copy the
# auxiliary files that are needed for ANAROD into it:
# - the files (if any) needed for plotting from GRWIN_DIR (CYGWIN only);
# - the terminfo files (only if TERMFILS is not empty).
#
$(RUN_DIR):
	mkdir -p $(RUN_DIR)
ifeq ($(findstring CYGWIN,$(OPSYS)),CYGWIN)
	if [ -n "$(GRWIN_FIL)" ]; \
	  then (cd $(GRWIN_DIR); $(CP) -R $(GRWIN_FIL) $(PWD)/$(RUN_DIR)); \
	fi
endif      # Cygwin
	if [ -n "$(TERMFILS)" ]; \
	  then (cd $(TERMDIR); $(CP) --parents $(TERMFILS) $(PWD)/$(RUN_DIR)); \
	fi

#
# Make the object file directories if they do not yet exist.
#
$(ANA_OBJ_DIR) $(AVE_OBJ_DIR) $(ROD_OBJ_DIR):
	mkdir -p $@

#
# Make the required object files for "ana", "ave" and "rod" from either their
# "C" (suffix ".c") or their "C++" (suffix ".cpp") source files.
#
bin/$(DIRNAM)/%.o: src/%.c ../standard/src/menu/menu.h
	$(CC) $(CFLAGS) $< -o $@

bin/$(DIRNAM)/%.o: src/%.cpp ../standard/src/menu/menu.h
	$(CXX) $(CFLAGS) $< -o $@

#
# Make the required common object files, and create the required directory for
# the object files if necessary. For "menu", also make the "C" file from the
# "yacc" input file.
#
$(LSQ_OBJ_FILRM): $(LSQ_DIR)/bin/$(DIRNAM)/%.o : $(LSQ_DIR)/src/%.c
	mkdir -p $(LSQ_OBJ_DIR)
	$(CC) $(CFLAGS) $< -o $@

$(PLOT_OBJ):	../standard/src/plot/PGmyplot.c
	mkdir -p ../standard/bin/$(DIRNAM)
	$(CC) $(CFLAGS) $< -o $@

$(MENU_OBJ):	../standard/src/menu/menu.tab.c
	mkdir -p ../standard/bin/$(DIRNAM)
	$(CC) $(CFLAGS) $< -o $@

../standard/src/menu/menu.tab.c:	../standard/src/menu/menu.y
	$(YACC) -b $(subst .tab.c,"",$@) $<;

#	$(YACC) -o $@ $<;

#
# Clean up all files that can be made by "make". Several variations exist:
# - target "clean-ana" cleans only the "ana" specific files
# - target "clean-ave" cleans only the "ave" specific files
# - target "clean-rod" cleans only the "rod" specific files
# - target "clean-bin" cleans all object files ("*.o") and the YACC-created file
#   "menu.tab.c"
# - target "clean" cleans all the files created by the Makefile, including the
#   executables. Note, however, that "numrec.o" will be kept if there is no
#   "numrec.c".
#
clean: clean-bin
	(cd $(RUN_DIR); $(RM) ana$(EXESFX) rod$(EXESFX))
	$(RM) $(AVE_DIR)/$(RUN_DIR)/ave$(EXESFX)

clean-bin:
	$(RM) $(ANA_OBJ_FILES) $(AVE_OBJ_FILES) $(ROD_OBJ_FILES) $(PLOT_OBJ) \
	   $(MENU_OBJ) $(LSQ_OBJ_FILRM) ../standard/src/menu/menu.tab.c

clean-ana:
	$(RM) $(ANA_OBJ_FILES) $(RUN_DIR)/ana$(EXESFX)

clean-ave:
	$(RM) $(AVE_OBJ_FILES) $(AVE_DIR)/$(RUN_DIR)/ave$(EXESFX)

clean-rod:
	$(RM) $(ROD_OBJ_FILES) $(RUN_DIR)/rod$(EXESFX)

#
# Get the ESRF-internal targets.
#
# Note: for a source distribution, this file will be replaced by a dummy.
#
include esrf_targ.mk

#
# make depend: The output appended to the makefile cannot be used directly:
#   * "$(DIRNAM)/src" must be changed to $(DIRNAM)
#   * "bin/$(DIRNAM)/../standard/src/" must be changed to
#     "../standard/bin/$(DIRNAM)/".
#   * plot/PGmyplot.o must be changed to PGmyplot.o
#   * menu/menu.tab.o must be changed to menu.tab.o
#
depend:
	makedepend -p "bin/\$$(DIRNAM)/" -Y -- $(CFLAGS) $(READLINE_FLAGS) \
	   $(INCLUDE_PATH) $(ANA_SRC_FILES) $(AVE_SRC_FILES) $(ROD_SRC_FILES) \
	   $(LSQ_SRC_FILES) ../standard/src/plot/PGmyplot.c \
	   ../standard/src/menu/menu.tab.c

# DO NOT DELETE THIS LINE - makedepend depends on it.
