#++++++++++++++++
#.IDENTIFICATION Makefile
#.LANGUAGE       Make (makefile script)
#.AUTHOR         CDS Catalogues Service
#.ENVIRONMENT    Unix
#.KEYWORDS       
#.VERSION  1.0   08-Feb-1996
#.VERSION  1.1   29-Jul-1997: map to update kslots
#.VERSION  1.2   08-Jun-2000: SimViz.awk
#.VERSION  1.3   11-Dec-2004: Default = take Acronym
#.PURPOSE        (Re)Create dependent files in this Directory
#.COMMENTS       Create *.html from other sources...
#----------------

SHELL=/bin/sh
#MACHINE=local

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

# Programs:
#ISQL    = isql -SNEWVIZ -Uasu -Pasu4VizieR
ISQL    = Vsql -SVIZDB -Uasu -Pasu4VizieR -I /home/cds/interfaces
#ISQLu   = isql -SNEWVIZ -Upipecat -Pvizdb10
ISQLu   = Vsql -SVIZDB -Ucds -P@cds4Viz
AWK	= gawk
CC      = cc
COPY    = cp -p
STRIP   = strip
RANLIB  = ranlib
AR      = ar ruv

# Directories:

# Files:
BIB	= /home/bibcat/bibcat
DIC	= /home/bibcat/Dic.par
DICDIR	= /home/bibcat
BIN     =
SHS     =
MAN     =

# Options:
DEBUG =
C_OPT = -O
SYS =
CFLAGS = $(C_OPT) $(DEBUG) $(SYS) -I. -I$(INCDIR)
COMPILE= $(CC) $(CFLAGS) -c

# RULES: If the default rules haven't to be applied, remove the #
#.SUFFIXES: 
#.SUFFIXES: .tex .dvi .c .o
#.tex.dvi:
#	latex $*
#.c:
#	$(CC) $(CFLAGS) $< $(LIBSYS) -o $@
.SUFFIXES: .htx .html
.htx.html:
	cgiprint -tex $< > $@

# Dependencies:
default: Cats.html SimViz.awk

simviz: SimViz.awk
	 cp -p SimViz.dat $(DICDIR)

SimViz: SimViz.awk

#### Define the link from Simbad to Vizier
#	 echo '{print; print "-go"; fflush() }' >> $@.tmp
SimViz.awk: $(DIC) SimViz.awk0 Makefile
	cat SimViz.awk0 > $@.tmp
	echo "# Acronyms Understood by VizieR (`date`)" > SimViz.dat
	echo "################################" >> SimViz.dat
	echo "%M = Main Acronym Name"           >> SimViz.dat
	echo "%E = Explanations (in gml)"       >> SimViz.dat
	echo "%g = reg.exp. for a matching ID"  >> SimViz.dat
	$(AWK) 'function prdat(txt){ print txt >> "SimViz.dat" }\
	 /^%%.*not yet exam/{exit(st)} END{exit(st)}\
	 /^%M /{M=$$2; prdat($$0)}/^%N /{N=$$2} \
	 /^%E /{prdat($$0)}\
	 /^%g \//{if (k==1) print cchar; k=1; re=substr($$0,4); \
	     prdat($$0); \
	     if (N==0) { print "****N=0 for M=" M > "/dev/stderr"; st=1 } \
	     printf("%s { M=\"%s\"; N=%s;\n", re, M,N); \
		    cchar="    print \"-go %#Sesame#%\"; fflush(); next }";\
	     next} \
	 /^%/{if (k==1) print cchar; k=0;next}\
	 /^$$/{if (k==1) print cchar; k=0; N=0; prdat(""); next}\
	 /^[^%]/{ if(k>0) print }' $(DIC) >> $@.tmp
	 echo '{ c = $$1; s="="; for (i=2; i<=NF; i++) { c = c s $$i; s = " " }\
	    exit(1); pr5s("not_found", c); \
	    print "-go %#Sesame#%"; fflush(); next}' >> $@.tmp
	 @echo "----Test on NGC1123":
	 @echo "NGC1123" | gawk -f $@.tmp && mv $@.tmp $@

OldCode:
	 echo '{ c = $$1; s="="; for (i=2; i<=NF; i++) { c = c s $$i; s = " " }\
	    pr5s($$1, c); \
	    print "-go %#Sesame#%"; fflush(); next}' >> $@.tmp
	 @echo "----Test on NGC1123":
	 echo '{print " *Non-understandable acronym"; \
	   print "#Sorry, but your designation {\\\\bf\\\\fg{red}" $$0 "}";\
	   print "#cannot be interpreted by VizieR."; \
	   print "-go %#Sesame#%"; fflush(); next}' >> $@.tmp

SimViz.exp: SimViz.awk
	echo "#Acronyms understood by VizieR" > $@.tmp
	echo "# Version `date +%Y-%h-%d`" >> $@.tmp
	grep '^/\^' $? | acut -d'{' -f1  | trim >> $@.tmp
	mv $@.tmp $@

install: SimViz.awk SimViz.exp
	cp -p SimViz.awk /home/cds/httpd/lib
	if test -d $(DICDIR); then \
	    cp -p SimViz.dat SimViz.exp $(DICDIR); \
	else echo "++++No directory: $(DICDIR)"; \
	fi
	 

#### Get the counts: tot ftp viz
counts:
	tot=`grep "^%I" $(BIB) | fgrep -v = | wc -l`; \
	ftp=`grep "^%Y .*f" $(BIB) | fgrep -v = | wc -l` ; \
	export LC_ALL=C; \
	( echo "select count(*) from METAcat where catid>0"; echo go) \
	| $(ISQL) > /tmp/$$$$.v; \
	viz=`$(AWK) '{if ($$1 > 0) print}' /tmp/$$$$.v` ; \
	echo ====Counts tot, ftp, viz: $$tot $$ftp $$viz | tee COUNTS ; \
	rm /tmp/$$$$.v
	chmod 664 COUNTS

html:
	for f in *.htx ; do \
	  make `echo $$f | sed 's/x$$/ml/'` ; \
	done

map: 	pattern
	make_map pattern | $(AWK) '{ \
	    print "update METAcat set kslot=" $$3 " where catid=" $$1 "\ngo"\
	}' | $(ISQL) 
	/usr/httpd/local/cgi-bin/catkeys | $(AWK) '/^ *$$/{p++;next}\
	{if (p == 1) print}' > $@
	$(ISQL) < $@.sql >> $@

tar.gz: [IJNUV]*.htx
	tar cvf tar [IJNUV]*.htx cats.htx
	gzip -v9 tar

ucd:
	make_ucd
	@ls -l *tsv*

all: $(BIB)
	@echo "....make all from `pwd`"; 
	@echo "    LD_LIBRARY_PATH=$$LD_LIBRARY_PATH"
	./make_all
	./make_ucd
	./make_metatab

clean:
	rm -f core a.out *.o *.log *.lis *.dvi *.ps *.aux *.trace
	rm -f .sys

CLEAN:	clean
	rm *.a $(BIN)


