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)/psastroArguments.$(ARCH).o      	    \
$(SRC)/psastroParseCamera.$(ARCH).o    	    \
$(SRC)/psastroDataLoad.$(ARCH).o       	    \
$(SRC)/psastroDataSave.$(ARCH).o       	    \
$(SRC)/psastroAstromGuess.$(ARCH).o    	    \
$(SRC)/psastroLoadReferences.$(ARCH).o 	    \
$(SRC)/psastroConvert.$(ARCH).o	       	    \
$(SRC)/psastroChipAstrom.$(ARCH).o     	    \
$(SRC)/psastroOneChip.$(ARCH).o	       	    \
$(SRC)/psastroUtils.$(ARCH).o	       	    \
$(SRC)/psastroTestFuncs.$(ARCH).o      	    \
$(SRC)/psastroMosaicAstrom.$(ARCH).o        \
$(SRC)/psastroMosaicGetGrads.$(ARCH).o      \
$(SRC)/psastroMosaicGetRefstars.$(ARCH).o   \
$(SRC)/psastroMosaicChipAstrom.$(ARCH).o    \
$(SRC)/psastroMosaicSetAstrom.$(ARCH).o	    \
$(SRC)/psastroMosaicSetMatch.$(ARCH).o	    \
$(SRC)/psastroMosaicHeaders.$(ARCH).o	    \
$(SRC)/psastroMosaicRescaleChips.$(ARCH).o	    \
$(SRC)/psastroWCS.$(ARCH).o	   

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

INSTALL = psastro

all: psastro.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 "#*"`
