# this is a Makefile that can be used to create train2.so on LINUX and MACOS
#
# to use this:
#     cd <into-the-current-directory>
#     make
#
# to remove temporary files:
#     make clean
#
# to remove all files from prior builds
#     make cleanall
#
# to rebuild from scratch
#     make rebuild
#
# to test execution and sensitivities
#     make test

# make sure environment is properly set up
ifndef ESP_ROOT
   $(error ESP_ROOT must be set -- Please fix the environment...)
endif
ifndef ESP_ARCH
   $(error ESP_ARCH must be set -- Please fix the environment...)
endif

# define the directories that hold the header and library files
IDIR  = $(ESP_ROOT)/include
LDIR  = $(ESP_ROOT)/lib

# set up environment variables that are appropriate for this architecture
include $(IDIR)/$(ESP_ARCH)

# default is to build train2.so
default:	$(LDIR)/train2.so

# build directions for the various needed files
$(LDIR)/train2.so:	train2.o
	touch $(LDIR)/train2.so
	-rm $(LDIR)/train2.so
	$(CC) $(SOFLGS) -o $(LDIR)/train2.so train2.o -L$(LDIR) -locsm -legads -lm

train2.o:	train2.c
	$(CC) -c $(COPTS) $(DEFINE) -I$(IDIR) -I. train2.c

# dependencies on .h files
train2.c:		$(IDIR)/udpUtilities.h $(IDIR)/udpUtilities.c
$(IDIR)/udpUtilities.h:	$(IDIR)/udp.h          $(IDIR)/egads.h
$(IDIR)/udpUtilities.c: $(IDIR)/OpenCSM.h      $(IDIR)/common.h

# remove temporary files
clean:
	-rm train2.o

# remove all files from a previous build
cleanall:	clean
	-rm $(LDIR)/train2.so

# rebuild from scratch
rebuild:	cleanall default

# test execution and sensitivities
test:	default
	serveESP -batch -verify train2_1.csm | tail -n 5
	@echo "========================================="
	sensCSM  -geom          train2_1.csm | tail -n 5
	@echo "========================================="
	sensCSM  -tess          train2_1.csm | tail -n 5
	@echo "========================================="
	serveESP -batch -verify train2_2.csm | tail -n 5
	@echo "========================================="
	sensCSM  -geom          train2_2.csm | tail -n 5
	@echo "========================================="
	sensCSM  -tess          train2_2.csm | tail -n 5
	@echo "========================================="
	-rm port*.jrnl
	-rm bad.triangles
