#if your machine uses cc instead of gcc, edit the following:
export CC=gcc
export FC=g77
export MAKE=make
#note: set FC to $(CC) if not using PGPLOT or if PGPLOT compiled using C linkage

#set the base directory
export BASE_DIR = $(shell pwd)

#uncomment and set PGPLOT files if installed
#export PGHEAD = \"/usr/local/include/cpgplot.h\"
#export PGPLOT = -L/usr/local/lib -lcpgplot -lpgplot -lpng -L/usr/X11R6/lib -lX11 -L/usr/lib -lgcc

#compilation flags
export CFLAGS= -O3 -Wall

#uncomment if you will compile dolphot using multithreaded
#export THREADED=1
#export THREAD_CFLAGS= -DDOLPHOT_THREADED -D_REENTRANT -fopenmp
#export THREAD_LIBS= -lomp
#note: add -Xclang to THREAD_CFLAGS if using clang (Mac default) compiler

#uncomment if you will be using the WFPC2 module
#export USEWFPC2=1

#uncomment if you will be using the ACS module
#export USEACS=1

#uncomment if you will be using the WFC3 module
#export USEWFC3=1

#uncomment if you will be using the ROMAN module
#export USEROMAN=1

#uncomment if you will be using the NIRCAM module
#export USENIRCAM=1

#uncomment if you will be using the NIRISS module
#export USENIRISS=1

#uncomment if you will be using the MIRI module
#export USEMIRI=1

#uncomment if you will be using the EUCLID module
#export USEEUCLID=1

#set the directory where Tiny Tim binaries are located (ACS/WFPC2/WFC3 PSF generation):
#export USETT=1
#export CFLAGS+= -DTTDIR='"/data0/tinytim-7.0"'

#set maximum number of images, and maximum number of stars
export CFLAGS+= -DMAXNIMG=100
export CFLAGS+= -DMAXNSTARS=2000000

#optionally print and/or crash if a NaN occurs (intended for development only)
export CFLAGS+= -DNAN_PRINT
#export CFLAGS+= -DNAN_CRASH

##### NO FURTHER ADJUSTMENTS BELOW THIS LINE ####

#add base directory settings to flags
export CFLAGS += -DBASEDIR='"$(BASE_DIR)"' -I$(BASE_DIR)/include

#definitions
ALLEXE = dolphot_lib.o fits_lib.o fakeproc.o bin/addstars bin/apphot bin/calcsky bin/dolphot bin/synthimg bin/fakelist bin/convertpos
ALL = $(ALLEXE) ccdred
DOLPHOT_DEP = dolphot_lib.o dolphot_common.h dolphot_defs.h
DOLPHOT_OBJ = dolphot_lib.o
DOLPHOT_FOBJ =

#linked libraries
export LIBS = -lm

#add WFPC2 modules, if selected
ifdef USEWFPC2
export CFLAGS+= -DUSEWFPC2
ALL+= wfpc2
DOLPHOT_DEP += wfpc2
DOLPHOT_OBJ += wfpc2/wfpc2phot.o wfpc2/wfpc2filters.o wfpc2/wfpc2distort.o
DOLPHOT_FOBJ += wfpc2/wfpc2filters.o
endif

#add ACS modules, if selected
ifdef USEACS
export CFLAGS+= -DUSEACS
ALL+= acs
DOLPHOT_DEP += acs
DOLPHOT_OBJ += acs/acsphot.o acs/acsfilters.o acs/acsdistort.o
DOLPHOT_FOBJ += acs/acsfilters.o
endif

#add WFC3 modules, if selected
ifdef USEWFC3
export CFLAGS+= -DUSEWFC3
ALL+= wfc3
DOLPHOT_DEP += wfc3
DOLPHOT_OBJ += wfc3/wfc3phot.o wfc3/wfc3filters.o wfc3/wfc3distort.o
DOLPHOT_FOBJ += wfc3/wfc3filters.o
endif

#add ROMAN modules, if selected
ifdef USEROMAN
export CFLAGS+= -DUSEROMAN
ALL+= roman
DOLPHOT_DEP += roman
DOLPHOT_OBJ += roman/romanphot.o roman/romanfilters.o roman/romandistort.o
DOLPHOT_FOBJ += roman/romanfilters.o
endif

#add NIRCAM modules, if selected
ifdef USENIRCAM
export CFLAGS+= -DUSENIRCAM
ALL+= nircam
DOLPHOT_DEP += nircam
DOLPHOT_OBJ += nircam/nircamphot.o nircam/nircamfilters.o nircam/nircamdistort.o
DOLPHOT_FOBJ += nircam/nircamfilters.o
endif

#add NIRISS modules, if selected
ifdef USENIRISS
export CFLAGS+= -DUSENIRISS
ALL+= niriss
DOLPHOT_DEP += niriss
DOLPHOT_OBJ += niriss/nirissphot.o niriss/nirissfilters.o niriss/nirissdistort.o
DOLPHOT_FOBJ += niriss/nirissfilters.o
endif

#add MIRI modules, if selected
ifdef USEMIRI
export CFLAGS+= -DUSEMIRI
ALL+= miri
DOLPHOT_DEP += miri
DOLPHOT_OBJ += miri/miriphot.o miri/mirifilters.o miri/miridistort.o
DOLPHOT_FOBJ += miri/mirifilters.o
endif

#add EUCLID modules, if selected
ifdef USEEUCLID
export CFLAGS+= -DUSEEUCLID
ALL+= euclid
DOLPHOT_DEP += euclid
DOLPHOT_OBJ += euclid/nispphot.o euclid/nispfilters.o euclid/nispdistort.o euclid/visphot.o euclid/visfilters.o euclid/visdistort.o
DOLPHOT_FOBJ += euclid/nispfilters.o euclid/visfilters.o
endif

#add display module, if selected
ifdef PGPLOT
ALL+= display.o
ALLEXE+= display.o
endif

#add threaded libraries, if selected
ifdef THREADED
ALLEXE+= fits_lib_thread.o
DOLPHOT_TDEP:= $(DOLPHOT_DEP) fits_lib_thread.o
DOLPHOT_TOBJ:= $(DOLPHOT_OBJ) fits_lib_thread.o
else
DOLPHOT_TDEP:= $(DOLPHOT_DEP) fits_lib.o
DOLPHOT_TOBJ:= $(DOLPHOT_OBJ) fits_lib.o
endif
DOLPHOT_DEP+= fits_lib.o
DOLPHOT_OBJ+= fits_lib.o

all: $(ALL)

ifdef USEWFPC2
wfpc2::
	cd wfpc2; $(MAKE)
endif

ifdef USEACS
acs::
	cd acs; $(MAKE)
endif

ifdef USEWFC3
wfc3::
	cd wfc3; $(MAKE)
endif

ifdef USEROMAN
roman::
	cd roman; $(MAKE)
endif

ifdef USENIRCAM
nircam::
	cd nircam; $(MAKE)
endif

ifdef USENIRISS
niriss::
	cd niriss; $(MAKE)
endif

ifdef USEMIRI
miri::
	cd miri; $(MAKE)
endif

ifdef USEEUCLID
euclid::
	cd euclid; $(MAKE)
endif

ccdred::
	cd ccdred; $(MAKE)

dolphot_lib.o: dolphot_lib.c include/dolphot.h
	$(CC) $(CFLAGS) -c -o $@ $<

fits_lib.o: fits_lib.c include/dolphot.h include/fits.h
	$(CC) $(CFLAGS) -c -o $@ $<

ifdef THREADED
fits_lib_thread.o: fits_lib.c include/dolphot.h include/fits.h
	$(CC) $(CFLAGS) $(THREAD_CFLAGS) -c -o $@ $<
endif

bin/addstars: addstars.c $(DOLPHOT_TDEP)
	$(CC) $(CFLAGS) $(THREAD_CFLAGS) -o $@ addstars.c $(DOLPHOT_TOBJ) $(LIBS) $(THREAD_LIBS)

bin/convertpos: convertpos.c $(DOLPHOT_TDEP)
	$(CC) $(CFLAGS) $(THREAD_CFLAGS) -o $@ convertpos.c $(DOLPHOT_TOBJ) $(LIBS) $(THREAD_LIBS)

ifdef PGPLOT
display.o: display.c dolphot_lib.o fits_lib.o
	$(CC) $(CFLAGS) -c -o $@ $< -DPGHEAD=$(PGHEAD)

bin/apphot: apphot.c dolphot_lib.o fits_lib.o display.o
	$(CC) $(CFLAGS) -c apphot.c -DPGPLOT -DPGHEAD=$(PGHEAD)
	$(FC) -o $@ apphot.o dolphot_lib.o fits_lib.o display.o $(LIBS) $(PGPLOT)
	rm apphot.o

bin/dolphot: dolphot.c $(DOLPHOT_TDEP)
	$(CC) $(CFLAGS) $(THREAD_CFLAGS) -c dolphot.c -DPGPLOT -DPGHEAD=$(PGHEAD)
	$(FC) -o $@ dolphot.o $(DOLPHOT_TOBJ) $(LIBS) $(PGPLOT) $(THREAD_LIBS)
	rm dolphot.o
else
bin/apphot: apphot.c dolphot_lib.o fits_lib.o
	$(CC) $(CFLAGS) -o $@ apphot.c dolphot_lib.o fits_lib.o $(LIBS)

bin/dolphot: dolphot.c $(DOLPHOT_TDEP)
	$(CC) $(CFLAGS) $(THREAD_CFLAGS) -o $@ dolphot.c $(DOLPHOT_TOBJ) $(LIBS) $(THREAD_LIBS)
endif

bin/calcsky: calcsky.c dolphot_lib.o fits_lib.o
	$(CC) $(CFLAGS) -o $@ calcsky.c dolphot_lib.o fits_lib.o $(LIBS)

bin/synthimg: synthimg.c dolphot_lib.o fits_lib.o
	$(CC) $(CFLAGS) -o $@ synthimg.c dolphot_lib.o fits_lib.o $(LIBS)

bin/fakelist: fakelist.c fakeproc.o fakeproc.h $(DOLPHOT_FOBJ)
	$(CC) $(CFLAGS) -o $@ fakelist.c fakeproc.o $(DOLPHOT_FOBJ) $(LIBS)

clean:
	rm $(ALLEXE)
	cd ccdred ; $(MAKE) clean
ifdef USEWFPC2
	cd wfpc2 ; $(MAKE) clean
endif
ifdef USEACS
	cd acs ; $(MAKE) clean
endif
ifdef USEWFC3
	cd wfc3 ; $(MAKE) clean
endif
ifdef USEROMAN
	cd roman ; $(MAKE) clean
endif
ifdef USENIRCAM
	cd nircam ; $(MAKE) clean
endif
ifdef USENIRISS
	cd niriss ; $(MAKE) clean
endif
ifdef USEMIRI
	cd miri ; $(MAKE) clean
endif
ifdef USEEUCLID
	cd euclid ; $(MAKE) clean
endif
