#++++++++++++++++
#.IDENTIFICATION Makefile
#.LANGUAGE       Make (makefile script)
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    Unix
#.KEYWORDS       
#.VERSION  1.0   05-Aug-2006
#.PURPOSE        Build up the NOMAD program
#.COMMENTS       
#----------------

SHELL=/bin/sh
PACKAGE=NOMAD1

# Defaut rule (e.g. copy from a public directory)
.DEFAULT:

PREFIX   = /usr/local
VERSION  = 1.3
DISTRIB  = $(PACKAGE)
#DISTRIB  = $(PACKAGE)-$(VERSION)

# Programs:
CC      = gcc -Wall
COPY    = cp -p
STRIP   = strip
RANLIB  = ranlib
AR      = ar ruv
int4    = int
int8    = long long

# Directories:
BINDIR   = $(PREFIX)/bin
SHSDIR   = $(PREFIX)/bin
MANDIR   = $(PREFIX)/man
ROOTdir  =

# Files:
EXPORT  = nomad1.h nomad1def.h ReadMe \
	  nomad1.c nomad_sub.c
BIN     = nomad1 nomad_make nomad_sub
SHS     =
MAN     =

# Options:
DEBUG = -O
C_OPT = -Dint4="$(int4)" -Wall
SYS =
CFLAGS = $(C_OPT) $(DEBUG) $(SYS) -I. 
COMPILE= $(CC) $(CFLAGS) -c
LIBSYS = -lm

# RULES: If the default rules haven't to be applied, remove the #
.SUFFIXES: 


# Dependencies:
default: nomad1 
	-find . -type d -name \*cache\* -exec rm -rf {} \;

install: install_bin 
	-find . -type d -name \*cache\* -exec rm -rf {} \;
	$(MAKE) test

install_bin: nomad1
	@if test -d $(BINDIR); then echo ....$(BINDIR) exists; \
	else echo ....Creating $(BINDIR) && mkdir $(BINDIR); \
	fi
	-(cd ../bin; for p in $(BIN); do test -x $$p && mv -f $$p $$p.old; done)
	$(COPY) nomad1 ../bin

nomad1: nomad1.c nomad_sub.c nomad1.h nomad1def.h Makefile
	if test "$(ROOTdir)" = ""; then d=`pwd`; d=`dirname $$d`/bindat; fi; \
	echo "....ROOTdir=$$d"; \
	$(CC) -DROOTdir='"'$$d'"' $(CFLAGS) nomad1.c nomad_sub.c -o $@ -lm

tycho_index: tycho_index.c
	$(CC) $(CFLAGS) $@.c nomad_sub.c -o $@ -lm

# Create the compressed version
cat22: cat22.c 
	$(CC) -I. cat22.c -o $@

nomad_make: nomad_make.c nomad1def.h
	if test "$(ROOTdir)" = ""; then d=`pwd`; d=`dirname $$d`; fi; \
	$(CC) -DROOTdir='"'$$d'"' $(CFLAGS) $@.c -lm -o $@

nomad22: nomad22.c nomad1def.h nomad1.h
	if test "$(ROOTdir)" = ""; then d=`pwd`; d=`dirname $$d`; fi; \
	$(CC) -DROOTdir='"'$$d'"' $(CFLAGS) $@.c -lm -o $@

convert_test: nomad_make get_tycho2
	./get_tycho2 1111 | nomad_make ../ori/111/m1111.cat - > m1111.log

convert_restart:
	rm -rf ../bindat/* ../convert.log

convert: nomad_make nomad1def.h get_tycho2 nomad_sub
	test -f ../convert.log || touch ../convert.log
	test -d ../bindat || mkdir ../bindat
	for d in ../ori/???; do \
	    od=../bindat/`basename $$d`; \
	    if test -s $$od.log ; then \
	       ls -l $$od.log; continue; \
	    else echo "....Creating: $$od" ; \
	    fi; \
	    test -d $$od || mkdir $$od; \
	    touch $$od.tmp; \
	    for f in $$d/*.cat; do \
		z=`basename $$f | tr -d '[a-z.]'`; \
		(echo ""; echo "----Convert $$f")  >> $$od.tmp; \
		(echo ""; echo "----Convert $$f")  >> ../convert.log; \
	        ./get_tycho2 $$z | nomad_make -r ../bindat  $$f - >> $$od.tmp \
		   2>> ../convert.log || exit 1 ; \
		(echo ""; echo "====Compare with original") >> $$od.tmp; \
		./nomad_sub -c $$f $$od/N$$z.bin >> $$od.tmp; \
	    done;  \
	    mv $$od.tmp $$od.log; \
	    ls -l $$od.log; \
	done

nomad_sub: nomad_sub.c nomad1def.h
	$(CC) -DTEST -DROOTdir='"'$$d'"' $(CFLAGS) $@.c -o $@ -lm

test:
	./test.sh

export: /tmp/nomad.tar.gz

/tmp/nomad.tar.gz: $(EXPORT) Makefile.in
	$(MAKE) CLEAN
	-test -d /tmp/nomad1 && rm -rf /tmp/nomad1
	mkdir /tmp/nomad1
	cp -p $(EXPORT) Makefile.in configure test.* /tmp/nomad1
	cd /tmp && tar cfv /tmp/nomad.tar nomad1
	gzip -v9 /tmp/nomad.tar
	rm -rf /tmp/nomad1

# Clean up everything
clean:
	rm -f core a.out *.o *.log *.lis *.dvi *.ps *.aux *.trace $(BIN)

CLEAN:	clean
