default: tools
help:
	@echo "make options: tools (default)"

include ../../Makefile.System
HOME 	=	$(ROOT)/src/tools
SRC	=	$(HOME)/src
LIB	= 	$(HOME)/lib
MAN	=	$(HOME)/doc
BIN	=	$(HOME)/bin
INC	=	$(HOME)/include
# include ../../Makefile.Common

DVO_LIBS  = $(DESTLIB)/libdvo.a $(DESTLIB)/libFITS.a $(DESTLIB)/libohana.a
DVO_INCS  = $(DESTINC)/dvo.h $(DESTINC)/gfitsio.h $(DESTINC)/ohana.h $(DESTINC)/autocode.h 

PROGRAMS = gconfig fhead ftable fields list_astro glockfile \
radec mktemp precess csystem fits_insert \
medianfilter mefhead ckfits

all tools: $(PROGRAMS)

BASE_CFLAGS   =	$(CFLAGS)
BASE_CPPFLAGS =	$(CPPFLAGS) -I$(INC) -I$(DESTINC) $(INCDIRS) -D$(ARCH)
BASE_LDFLAGS  = $(LDFLAGS) -L$(LIB) -L$(DESTLIB) $(LIBDIRS) $(LIBFLAGS)

FULL_CFLAGS   =	$(BASE_CFLAGS)
FULL_CPPFLAGS = $(BASE_CPPFLAGS)
FULL_LDFLAGS  = -ldvo -lFITS -lohana $(BASE_LDFLAGS)

# these are all programs which just depend on a single c file: foo : foo.c
# we use a special set of rules in this directory which expect this simplification

%.$(ARCH).o : %.c $(DVO_INCS)
	$(CC) $(FULL_CFLAGS) $(FULL_CPPFLAGS) -c $< -o $@
	@echo "compiled $<"
	@echo ""

$(BIN)/%.$(ARCH) : $(SRC)/%.$(ARCH).o $(DVO_LIBS)
	@if [ ! -d $(BIN) ]; then mkdir -p $(BIN); fi
	$(CC) $(FULL_CFLAGS) -o $@ $^ $(FULL_LDFLAGS)
	@echo "compiled $*"
	@echo ""

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

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

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

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

install:
	for i in $(PROGRAMS); do make $$i.install || exit; done


clean:
	rm -f `find . -name "*.o"`
	rm -f `find . -name "*.o"`
	rm -f `find . -name "*~"`
	rm -f `find . -name "#*"`

dist: clean
	rm -rf $(BIN)
	rm -rf $(LIB)

# $@ : target filename
# $* : matched word (%)
# $< : first prereq
# $^ : all prereqs

.PRECIOUS: %.$(ARCH).o
.PRECIOUS: $(BIN)/%.$(ARCH)

