#-----------------------------------------------------------------------
#
# Description:  make file for the EPH library, creating a Unix .a
# library, building executables that translate the VSOP2010 ephemeris
# files from the supplied ASCII form into binary form for the present
# platform, and building two example applications.  Designed for
# Linux/gcc but can be adapted for other platforms or run in an
# appropriate way by means of the macros CC (compiler command),
# CFLAGS (compiler qualifiers) and CFLAGL (special linker options).
#
# Usage:
#
#    To build everything and install the library:
#
#      make
#
#    To delete all built files:
#
#      make clean
#
# Last revision:   2021 September 28
#
# Author P.T.Wallace.
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#
#  SYSTEM DEFINITIONS
#
#-----------------------------------------------------------------------

#----YOU MAY HAVE TO MODIFY THE NEXT FEW DEFINITIONS-----

# The installation home directory.
INSTALL_DIR = $(HOME)

# The installation directory for the include files.
INC_DIR = $(INSTALL_DIR)/include

# The installation directory for the library.
LIB_DIR = $(INSTALL_DIR)/lib

# The installation directory for the binary ephemeris files.
ETC_DIR = $(INSTALL_DIR)/etc/eph

# The ANSI C compiler command.
CC = gcc

# The compiler flags.
CFLAGS = -pedantic -Wall -Wextra -O3

# The C preprocessor options.
CPPFLAGS =  -I. -I$(INC_DIR)

# Linker flags.
LDFLAGS = -L. -L$(LIB_DIR)

# Linker libraries.
LDLIBS = -leph -lsofa_c -lm

# Linker special options.
ifneq ($(shell uname),Darwin)       # except for Mac OS X
CFLAGL = -static
endif

# Flags for compiling a function.
CFLAGF = $(CFLAGS) $(CPPFLAGS)

# Flags for compiling an executable.
CFLAGX = $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS) $(CFLAGL)

# Lock files for make -j invocation.
LIBLOCK = lib.lock
BELLOCK = bel.lock
BEPLOCK = bep.lock

#----YOU SHOULDN'T HAVE TO MODIFY ANYTHING BELOW THIS LINE---------

# The command shell to be used.
SHELL = /bin/sh

#-----------------------------------------------------------------------
#
#  INGREDIENTS
#
#-----------------------------------------------------------------------

# The header files.
INCS = eph.h

# The function C source files.
FUNCS = bopr.c \
        earth.c \
        moon.c \
        mooni.c \
        moonc.c \
        planc.c \
        planet.c \
        plani.c \
        rdplan.c \
        rdplanq.c

# The lunar ASCII ephemeris files.
EPHL = elp_main.s1 \
       elp_main.s2 \
       elp_main.s3 \
       elp_pert.s1 \
       elp_pert.s2 \
       elp_pert.s3

# The planetary ASCII ephemeris files (Pluto omitted).
EPHP = VSOP2010p1.dat \
       VSOP2010p2.dat \
       VSOP2010p3.dat \
       VSOP2010p4.dat \
       VSOP2010p5.dat \
       VSOP2010p6.dat \
       VSOP2010p7.dat \
       VSOP2010p8.dat

# The ephemeris ASCII-to-binary translaters C source files.
TRANLS = moon_bin.c
TRANPS = plan_bin.c

# The example programs C source files.
EXAMPLES = occultation.c planets.c

# Everything.
INGRED = $(INCS) $(FUNCS) $(EPHL) $(EPHP) $(TRANLS) $(TRANPS) \
         $(EXAMPLES)

#-----------------------------------------------------------------------
#
#  ITEMS TO BE BUILT
#
#-----------------------------------------------------------------------

# The list of installation directories.
INST_DIR = $(LIB_DIR) $(INC_DIR) $(ETC_DIR)

# The function object files.
OBJS = $(FUNCS:.c=.o)

# The library.
LIB = libeph.a

# The executables.
TRANL = $(TRANLS:.c=)
TRANP = $(TRANPS:.c=)
EXAMS = $(EXAMPLES:.c=)
MAINS = $(TRANL) $(TRANP) $(EXAMS)

# The lunar binary ephemeris files.
EPHBL = ELP_MPP02_JPL.ctx \
        ELP_MPP02_LLR.ctx

# The planetary ephemeris files.
EPHBP = VSOP2010_1.ctx \
        VSOP2010_2.ctx \
        VSOP2010_3.ctx \
        VSOP2010_4.ctx \
        VSOP2010_5.ctx \
        VSOP2010_6.ctx \
        VSOP2010_7.ctx \
        VSOP2010_8.ctx

# The binary ephemeris files.
EPHB = $(EPHBL) $(EPHBP)

# Everything to be built (except directories).
BUILT = $(OBJS) $(LIB) $(EPHB) $(MAINS)

#-----------------------------------------------------------------------
#
#  INSTALLED ITEMS
#
#-----------------------------------------------------------------------

# Header files.
INCSI = $(addprefix $(INC_DIR),$(INCS))

# Library.
LIBI = $(addprefix $(LIB_DIR),$(LIB))

# Binary ephemeris files.
EPHBI = $(addprefix $(ETC_DIR), $(EPHB))

#-----------------------------------------------------------------------
#
#  RULES
#
#-----------------------------------------------------------------------

# The default target.
.PHONY: all
all: $(INGRED) $(BUILT)

# Build (but do not install) the library.
$(LIB): $(OBJS)
	flock $(LIBLOCK) $(AR) ru $@ $?
	ranlib $@
	$(RM) -f $(LIBLOCK)

# How to build an object file.
%.o : %.c $(INCS)
	$(CC) -c $< $(CFLAGF) -o $@

# Build the executables.
$(MAINS): $(TRANLS) $(TRANPS) $(EXAMPLES) $(INCS) $(LIB)
	$(CC) $@.c $(CFLAGX) -o $@

# Build the lunar binary ephemeris files.
$(EPHBL): $(TRANL) $(EPHL)
	-@ flock -n $(BELLOCK) \
        echo "Building lunar binary ephemeris files - please wait."; \
        ./$<; \
        $(RM) $(BELLOCK);

# Build the planetary binary ephemeris files.
$(EPHBP): $(TRANP) $(EPHP)
	-@ flock -n $(BEPLOCK) \
        echo "Building planetary binary ephemeris files - please wait."; \
        ./$<; \
        $(RM) $(BEPLOCK);

# Install.
.PHONY: install
install: $(INCS) $(LIB) $(EPHB)
	mkdir -p $(INST_DIR)
	cp $(INCS) $(INCSI)
	cp $(LIB) $(LIBI)
	cp $(EPHB) $(word 1,$(dir $(EPHBI)))

# Uninstall.
.PHONY: uninstall
uninstall:
	mkdir -p $(INST_DIR)
	rm -f $(INCSI) $(LIBI) $(EPHBI)
	rmdir --ignore-fail-on-non-empty $(INST_DIR)

# Delete all binaries.
.PHONY: clean
clean:
	$(RM) $(BUILT) $(EPH_TEST)

#-----------------------------------------------------------------------

