#++++++++++++++++
#.IDENTIFICATION makefile
#.LANGUAGE       Make (makefile script)
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    Unix
#.KEYWORDS       
#.VERSION  1.0   02-Aug-1994
#.VERSION  1.1   25-Mar-1997
#.VERSION  2.0   26-Nov-2003: configure
#.PURPOSE        (Re)Create dependent files in this Directory
#.COMMENTS       The following parameters can be changed:
#	BZERO   (set to 1 if gcc is used)
#	CC      = C compiler
#----------------

SHELL=/bin/sh
PACKAGE = vizier.c.anafile

PREFIX  = @prefix@
VERSION = @version@
DISTRIB = $(PACKAGE)-$(VERSION)
EXPORT_CVS_OPT=@EXPORT_CVS_OPT@

#########################################################
# Special values:
#########################################################

#  SYBASE is set to 1 if the code generated is meant for SYBASE
SYBASE=0

#  BZERO is set to 0 if the function "bzero" doesn't exist
BZERO=1

#  DIRENT is set to 1 if /usr/include/dirent.h exists
DIRENT=1

# If strncasecmp does not exist, uncomment the following line
#NOstrncasecmp=-DNOstrncasecmp

# If vfork       does not exist, uncomment the following line
#NOvfork=-Dvfork

#########################################################
# MACHINE is used to allow different processors on the same file system.
# A good choice is `uname -m`
#MACHINE=local
#########################################################

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

# Programs:
AWK    = @AWK@
WWWGET  = @wwwget@
CC      = @CC@ @cc_opt@
COPY    = cp -p
STRIP   = @STRIP@
RANLIB  = ranlib
AR      = ar ruv
RM  	= rm -f
int4   = @int4@
intp   = @intp@
int8   = @int8@
int8l  = @int8l@

# Directories:
INCDIR  = $(PREFIX)/include
LIBDIR  = $(PREFIX)/lib
BINDIR  = $(PREFIX)/bin
MANDIR  = $(PREFIX)/man

# Files:
VERFILE = $(PREFIX)/versions
LIB     = libana.a
OBJ1    = buffer.o units.o filutil.o util.o strut.o error.o \
	  catstd.o tabutil.o tabform.o domain.o tabout.o
OBJ     = $(OBJ1) vizcat.o
TEX     = anafile.tex man.tex manpage.tex tofits.tex tabmap.tex
PGM     = anafile tofits fcat trcat units lss readme2f preadme fits2a tabmap \
	  vizcat catfiles
MAN1    = anafile.1 tofits.1 fcat.1 tabmap.1 fits2a.1
HTML    = anafile.html tofits.html tabmap.html
PGMALL  = $(PGM) fits filutil
MAN     = $(MAN1)
BASIC   = buffer.c error.c units.c util.c strut.c filutil.c fits.c \
	domain.c tabform.c tabutil.c catstd.c fcat.c trcat.c vizcat.c \
	anafile.c tofits.c lss.c tabmap.c tabout.c catfiles.c
SRC     = $(BASIC) readme2f.c preadme.c fits2a.c
# INC1 to install (but not INC2)
INC1	= strut.h catstd.h units.h buffer.h journals.h filutil.h 
#	regexp0.h
INC2	= ADC_Keywords adckeys.h labels.h
INC     = $(INC1) $(INC2)
LIST    = units.txt units.htx

# Options:
############## For Dec-Alpha:
DEBUG = -O 
#DEBUG = -O -Olimit 750
####End of ### For Dec-Alpha:
C_OPT = -Wall -I. $(DEBUG) -DBZERO=$(BZERO) -DSYBASE=$(SYBASE) -DDIRENT=$(DIRENT) \
	-Dint8="$(int8)" -Dint4="$(int4)" -Dintp="$(intp)" -Dint8l="$(int8l)" \
	$(cc_opt) -DWWWGET='"$(WWWGET)"' $(NOstrncasecmp) $(NOvfork)
SYS =
CFLAGS = $(C_OPT) $(SYS) -I. 
COMPILE= $(CC) $(CFLAGS) -c

# RULES: If the default rules haven't to be applied, remove the #
.SUFFIXES: 
.SUFFIXES: .tex .dvi .c .o .1
.tex.1:
	texroff $*
.tex.dvi:
	latex $*
%.o: %.c $(INC2)
	$(CC) $(C_OPT) -c $< 

# Dependencies:
# Another possible dedault: anafile tofits lss trcat

default: all
all: init $(LIB) $(PGM) $(LIST)

init:
	rm -f adckeys.txt
	ln -s ADC_Keywords adckeys.txt

# Installation: install also tofits as FTPD program
install: install_lib install_bin install_man install_info

install_lib: $(LIB)
	test -d $(INCDIR) || mkdir $(INCDIR)
	test -d $(LIBDIR) || mkdir $(LIBDIR)
	$(COPY) $(INC1) $(INCDIR)
	$(COPY) $(LIB) $(LIBDIR)/libana.a
	rm -f $(LIBDIR)/adckeys.txt
	$(COPY) adckeys.txt $(LIBDIR)

install_man: $(MAN1)
	test -d $(MANDIR) || mkdir $(MANDIR)
	test -d $(MANDIR)/man1 || mkdir $(MANDIR)/man1
	$(COPY) $(MAN1) $(MANDIR)/man1

install_bin: $(PGM)
	test -d $(BINDIR) || mkdir $(BINDIR)
	$(COPY) $(PGM) $(BINDIR)
	if [ -w /ftp/bin/ftp-exec/tofits ]; then  \
	   $(COPY) tofits /ftp/bin/ftp-exec; \
	fi
	if [ -w /ftp/bin/ftp-exec/fcat ]; then  \
	   $(COPY) fcat /ftp/bin/ftp-exec; \
	fi

#install_info:
#	touch $(VERFILE)
#	v=`grep -w '$(PACKAGE)' $(VERFILE) |wc -l` ; \
#	if test $$v -eq 0 ; then \
#	  echo "$(PACKAGE):V$(VERSION)">>$(VERFILE); \
#	else \
#	  sed "s/^\($(PACKAGE)\):V.*/\1:V$(VERSION)/g" $(VERFILE) >version.tmp;\
#	  cat version.tmp > $(VERFILE); \
#	  rm -f version.tmp; \
#	fi

install_info:
	touch $(VERFILE)
	(cat $(VERFILE); echo  "$(PACKAGE):V$(VERSION)"; \
	 for  f in $(PGM); do \
	 ./$$f -version | $(AWK) '{printf "-%s:%s\n", $$1, $$3}'; done ) \
	| $(AWK) -F: '{n++; P[n]=$$1; V[$$1]=$$2}END{for(i=1;i<=n;i++) { \
	  p=P[i]; v=V[p]; if(v!="") print p ":" v; V[p]="" }}' > version.tmp
	test -s version.tmp && cat version.tmp > $(VERFILE) \
	    && rm -f version.tmp 

#### Construction of Individual Programs
$(LIB): $(OBJ) 
	$(AR) $@ $?
	$(RANLIB) $(LIB)

### ana is a debugging version of anafile
ana: anafile.c catstd.h adckeys.h journals.h
	cc -g -I. -DBZERO=$(BZERO) -DDIRENT=$(DIRENT) anafile.c \
	`echo $(OBJ1) | sed 's/\.o/.c/g'` -lm -o $@

anafile: anafile.c catstd.h adckeys.h $(LIB) journals.h
	$(CC) $(C_OPT) anafile.c $(LIB) -lm -o $@
	$(STRIP) $@

adckeys.h: adckeys.txt
	awk 'BEGIN{print "/* Generated from $? */"; \
	     print "static char *ADCkeys[] = {" }\
	    END{print "  (char *)0\n};"} \
	    {sub(/[ 	]*[(][Ss][e].*/, ""); sub(/ *$$/, ""); \
	     printf("  \"%s\",\n", $$0)}' $? > $@

trcat: trcat.c journals.h
	$(CC) $(C_OPT) trcat.c -o $@

readme2f: readme2f.c catstd.h $(LIB) journals.h
	$(CC) $(C_OPT) readme2f.c $(LIB) -lm -o $@
	$(STRIP) $@

readme2c: readme2c.c catstd.h $(LIB) journals.h
	$(CC) $(C_OPT) readme2c.c $(LIB) -lm -o $@
	$(STRIP) $@

preadme: preadme.c catstd.h catstd.c tabform.c $(LIB) journals.h
	$(CC) $(C_OPT) preadme.c catstd.c tabform.c $(LIB) -lm -o $@
	$(STRIP) $@

tdat: tdat.c catstd.h $(LIB)  journals.h
	$(CC) $(C_OPT) tdat.c catstd.o tabform.o $(LIB) -lm -o $@
	$(STRIP) $@

tofits: tofits.c fits.o domain.o tabutil.o catstd.h $(LIB)  journals.h
	$(CC) $(C_OPT) tofits.c catstd.o tabform.o fits.o domain.o \
	   $(LIB) -lm -o $@
	$(STRIP) $@

fits2a: fits2a.c  journals.h filutil.c strut.c
	$(CC) $(C_OPT) fits2a.c filutil.c strut.c -lm -o $@

units: units.c 
	$(CC) $(C_OPT) -DTEST=1 units.c -lm -o $@
	$(STRIP) $@

units.txt: units
	units \? > $@

units.htx: units
	units -tex \? > $@

filutil: filutil.c 
	$(CC) $(C_OPT) -DTEST=1 filutil.c -lm -o $@
	$(RM) filutil.o
	$(STRIP) $@

util: util.c strut.c units.c domain.c
	$(CC) $(C_OPT) -c units.c
	$(CC) $(C_OPT) -DTEST=1 util.c strut.c domain.c units.o -lm -o $@
	$(RM) util.o
	$(STRIP) $@

vizcat: vizcat.c $(LIB) journals.h
	$(CC) $(DEBUG) $(C_OPT) -DTEST=1 -DCATSTD_H='"$(INCDIR)/catstd.h"'\
	      vizcat.c $(LIB) -lm -o $@
	$(STRIP) $@

tabmap: tabmap.c journals.h $(LIB)
	$(CC) $(DEBUG) $(C_OPT) \
	  -DTEST=1 tabmap.c $(LIB) -lm -o $@
	$(RM) tabmap.o 

map1: map1.c journals.h $(LIB)
	$(CC) $(DEBUG) $(C_OPT) -DTEST=1 map1.c \
	  $(LIB) -lm -o $@
	$(RM) map1.o 

### Documentation in DVI format
man.dvi: $(TEX)
	latex man

#################################################################
#  	Clean and Export
#################################################################

# Remove just useless files
clean:
	$(RM) *.o core a.out $(LIB) *.log *.aux *.dvi *.lis .*.swp
	$(RM) $(PGMALL)
	$(RM) -rf *.cache

export: /tmp/$(DISTRIB).tar

tar: /tmp/$(DISTRIB).tar

/tmp/$(DISTRIB).tar: $(SRC) $(INC) Makefile.in *.c
	$(MAKE) clean
	$(MAKE) $(MAN1)
	-test -d /tmp/$(DISTRIB) && rm -rf /tmp/$(DISTRIB)
	autoconf
	mkdir /tmp/$(DISTRIB)
	cp -p $(SRC) $(INC) $(MAN) $(TEX) /tmp/$(DISTRIB)
	cp -p Makefile.in configure /tmp/$(DISTRIB)
	cd /tmp && tar cf $@ $(DISTRIB)
	rm -rf /tmp/$(DISTRIB)
	gzip -v9 $@

.POSIX:
.SILENT : cvs

cvs:
	echo "obscolete, use tag"
	exit
	d=`echo $(PWD)|sed 's/^.*\///g'`; \
	if test $$d = $(PACKAGE); then \
	   d=`cvs diff configure*|wc -l` ; \
	   if test $$d -eq 0; then \
	      v=`echo $(VERSION)|sed 's/\./_/g'` ; echo "version: V$$v"; \
	      cvs tag V$$v . ; \
	   else \
	      echo "****you must commit 'configure*' before" ; \
	   fi \
   	fi \

tag:
	d=`echo $(PWD)|sed 's/^.*\///g'`; \
	if test $$d = $(PACKAGE); then \
	   d=`git diff configure*|wc -l` ; \
	   if test $$d -eq 0; then \
	      v=`echo $(VERSION)|sed 's/\./_/g'` ; echo "version: V$$v"; \
	      git tag -f -a V$$v -m "version $$v" ; git push --tags ; \
	   else \
	      echo "****you must commit 'configure*' before" ; \
	   fi \
   	fi

fits2a-package:
	rm -rf /tmp/fits2a-cds
	mkdir  /tmp/fits2a-cds
	cp -p filutil.* /tmp/fits2a-cds
	cp -p fits2a.c /tmp/fits2a-cds
	cp -p strut.* /tmp/fits2a-cds
	echo "#fits2a program : CDS "  >/tmp/fits2a-cds/Makefile
	(echo "all: fits2a"; echo "")  >>/tmp/fits2a-cds/Makefile
	echo ".c.o:" >>/tmp/fits2a-cds/Makefile
	(echo '\tgcc -c -I . $$<'; echo "") >>/tmp/fits2a-cds/Makefile
	echo "fits2a: filutil.o strut.o fits2a.o" >>/tmp/fits2a-cds/Makefile
	(echo '\tgcc -o $$@ *.o -lm'; echo "") >>/tmp/fits2a-cds/Makefile
	echo "clean:" >>/tmp/fits2a-cds/Makefile
	echo '\trm -f *.o fits2a' >>/tmp/fits2a-cds/Makefile
	(cd /tmp; tar -czvf fits2a-cds.tar.gz fits2a-cds)
	rm -rf /tmp/fits2a-cds

# Distribution replaces links by actual copy -- here is the code removed
#	tar cf $@.t $(SRC) $(INC) $(MAN)
#	cd /tmp/$(DISTRIB) && tar xf $@.t && rm -f $@.t 
