default: psastro
help:
	@echo "USAGE: make psastro"

CC      =       gcc
SRC     =       src
BIN     =       bin

DESTBIN =       $(HOME)/src/bin/$(ARCH)

LPSLIB  :=      $(shell pslib-config --libs)
IPSLIB  :=      $(shell pslib-config --cflags)

LPSMOD  :=      $(shell psmodule-config --libs)
IPSMOD  :=      $(shell psmodule-config --cflags)

INCS	= 	$(IPSLIB) $(IPSMOD)
LIBS	= 	$(LPSLIB) $(LPSMOD)
CFLAGS	=	$(INCS) -std=c99 -Wall -Werror -g
LFLAGS	=	$(LIBS) 

PSASTRO = \
$(SRC)/psastro.$(ARCH).o           \
$(SRC)/psastroIO.$(ARCH).o	   \
$(SRC)/psastroBuildFPA.$(ARCH).o   \
$(SRC)/psastroArguments.$(ARCH).o  \
$(SRC)/psastroUtils.$(ARCH).o	   \
$(SRC)/psModUtils.$(ARCH).o	   \
$(SRC)/psLibUtils.$(ARCH).o	   \
$(SRC)/pmAstrom.$(ARCH).o	   \
$(SRC)/pmAstromGrid.$(ARCH).o

PSASTRO-MKTEST = \
$(SRC)/psastro-mktest.$(ARCH).o    \
$(SRC)/psastroArguments.$(ARCH).o  \
$(SRC)/psastroIO.$(ARCH).o	   \
$(SRC)/psModUtils.$(ARCH).o	   \
$(SRC)/psLibUtils.$(ARCH).o        \
$(SRC)/pmAstrom.$(ARCH).o

psastro: $(BIN)/psastro.$(ARCH)
$(BIN)/psastro.$(ARCH) : $(PSASTRO)
$(PSASTRO): $(SRC)/psastro.h $(SRC)/pmAstrom.h

psastro-mktest: $(BIN)/psastro-mktest.$(ARCH)
$(BIN)/psastro-mktest.$(ARCH) : $(PSASTRO-MKTEST)
$(PSASTRO-MKTEST): $(SRC)/psastro.h $(SRC)/pmAstrom.h

INSTALL = psastro psastro-mktest

all: psastro.install psastro-mktest.install

# dependancy rules for binary code #########################
.PRECIOUS: %.$(ARCH).o
.PRECIOUS: $(BIN)/%.$(ARCH)

%.$(ARCH).o : %.c
	$(CC) $(CFLAGS) -c $< -o $@

$(BIN)/%.$(ARCH) : $(SRC)/%.$(ARCH).o
	@if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi
	$(CC) $^ -o $@ $(LFLAGS)
	@echo "done with $@"

$(DESTBIN)/%: $(BIN)/%.$(ARCH)
	@if [ ! -d $(DESTBIN) ]; then mkdir -p $(DESTBIN); fi
	rm -f $(DESTBIN)/$*
	cp $(BIN)/$*.$(ARCH) $(DESTBIN)/$*

$(INSTALL): % : $(BIN)/%.$(ARCH)

%.clean :
	rm -f $(BIN)/$*.$(ARCH)
	rm -f $(SRC)/*.$(ARCH).o

%.install:
	make $(DESTBIN)/$*

# utilities #################################################

install:
	for i in $(INSTALL); do make $$i.install; done

clean:	
	rm -f $(BIN)/*.$(ARCH)
	rm -f `find . -name "*.o"`
	rm -f `find . -name "*~"`
	rm -f `find . -name "#*"`
