# this is a Makefile that can be used to create template.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 template.so
default:	$(LDIR)/template.so

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

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

# dependencies on .h files
template.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 template.o

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

# rebuild from scratch
rebuild:	cleanall default

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