#++++++++++++++++
#.IDENTIFICATION Makefile.in
#.LANGUAGE       Make (makefile script)
#.AUTHOR         Francois Ochsenbein [CDS]
#.ENVIRONMENT    Unix
#.KEYWORDS
#.COMMENTS
#	The "install" option in the Makefile copies the executables and
#	man pages to public directories. Just edit the Makefile, and modify
#	the PREFIX definition according to your installation.
#	Some alternatives are proposed as comments.
#### Note that the follwing programs are required:
#### rsimbad   rned   asu
#----------------

SHELL=/bin/sh
# ------------------------------------------
# DIRECTORIES (SHSDIR for Shell Scripts)
#   We assume here a general installation;
#   other choice may e.g. be
#################################################################
# For a system-wide installation, suggested to use /usr/local
# For your own installation, we propose your ~/lib ~/bin ~/man ..
#    i.e.  run ./configure -prefix=$HOME
#################################################################
#PREFIX  = /usr/local
#PREFIX  = $(HOME)
PREFIX  = @prefix@
PACKAGE = vizier.c.sesame
VERSION = @version@
DISTRIB = $(PACKAGE)-$(VERSION)
#################################################################

INCDIR  = $(PREFIX)/include
LIBDIR  = $(PREFIX)/lib
BINDIR  = $(PREFIX)/bin
SBINDIR = $(PREFIX)/sbin
SHSDIR  = $(PREFIX)/bin
MANDIR  = $(PREFIX)/man
INIDIR  = $(PREFIX)/init.d
RUNDIR  = $(PREFIX)/run
XMLDIR  = $(PREFIX)/httpd/Pages/xml
LOGDIR  = @LOGDIR@
RUNDIR  = @RUNDIR@
CGIDIR  = $(PREFIX)/httpd/cgi#@CGIDIR@
LOGDIR  = @LOGDIR@
###########
# NOTE: The definition below is required only on SOLARIS
#       Comment the following line if required
###########
#LIBSYS  = -lsocket -lnsl
LIBSYS = @LIBSYS@

# PROGRAMS	--------------------------------
#	 Possible alternatives are commented out.
LOG_CHUNK = 1000
CC     = @CC@ -DLOG_CHUNK=$(LOG_CHUNK) @cc_opt@
RANLIB = @RANLIB@
AWK    = @AWK@
RUNln  = @RUNln@
STRIP  = strip
COPY   = cp -p
RM     = rm

# C options	--------------------------------
DEBUG = -O
C_OPT = @HAS_AS4@
SYS =
CFLAGS = $(C_OPT) $(DEBUG) $(SYS) -I. -I$(INCDIR)

# Files		--------------------------------
VERFILE = $(PREFIX)/versions
BIN  = sesame_server dcache
#SBIN = sesamed3
SRC  = sesame.c dcache.c cooname.c
INC  = cache.h
CGI  = nph-sesame asu-test
XSD  = sesame_4.xsd sesame_2.xsd
DEP  = $(BINDIR)/rsimbad $(BINDIR)/rsimbad1 $(BINDIR)/asu_id \
       ./osimbad
TARGET = "APG 5"
#INI  = sesamed3.ini
MAN  =
SHS  = osimbad sesame_client
OBJ  =

# RULES		--------------------------------
.SUFFIXES: .o .c
.c.o:
	$(CC) $(CFLAGS) -c $<

# DEPENDENCIES	--------------------------------

all: $(BIN) $(SBIN) $(MAN)

#sesamed3: sesamed3.c
#	$(CC) $(CFLAGS) $@.c $(LIBSYS) -o $@
#	$(STRIP) $@

sesame_server: sesame.c dcache.c cooname.c
	$(CC) $(CFLAGS) sesame.c dcache.c -L$(LIBDIR) -las4 $(LIBSYS) -lm -o $@
	$(STRIP) $@

dcache: dcache.c
	$(CC) $(CFLAGS) -DTEST $@.c $(LIBSYS) -o $@
	$(STRIP) $@

#install: check_dep install_bin install_inc install_cgi install_info
install: install_bin install_inc install_cgi install_info

install_bin: $(BIN) $(SHS)
	test -d $(BINDIR) || mkdir $(BINDIR)
	$(COPY) $(BIN) $(SHS) $(BINDIR)

install_inc:
	$(COPY) $(INC) $(INCDIR)

verify: check_dep
check:  check_dep
check_dep: sesame_server
	@for p in $(DEP); do \
	    nl=`($$p $(TARGET) 2>/dev/null) | wc -l`; \
	    echo -n "#... $$p $(TARGET) => $$nl" ; \
	    if test $$nl -lt 3 ; then \
	       echo " *** BAD RESULT ***"; \
	       ls -l $$p; \
	       exit 1; \
	    else \
	       echo " ok." ; \
	    fi \
	done
	@ngc=`date +%M%S`; \
	echo "#...Testing ./sesame_server -ox -test-all NGC $$ngc"; \
	./sesame_server -ox -test-all NGC $$ngc
	#if test -s $(LOGDIR)/sesame.log; then \
	#   ls -l -s $(LOGDIR)/sesame.log; \
	#else \
	#   echo "#...no sesame log file (yet)"; \
	#fi
	#rm -f $(LOGDIR)/../lib/sesame.conf
	#rm -f $(RUNDIR)/sesame.conf
	#-rm -rf $(RUNDIR)/sesame

#install_shs: sesame-all
#	test -d $(SHSDIR) || mkdir $(SHSDIR)
#	$(COPY) sesame-all $(SHSDIR)
#	for a in sesame-viz sesame-sim sesame-ned; do \
#	   test -x $$a || ( cd $(SHSDIR) && ln sesame-all $$a ) ; \
#	done

#install_sbin: $(SBIN)
#	test -d $(SBINDIR) || mkdir $(SBINDIR)
#	for p in $(DEP); do \
#	  test -x $$p || echo "++++Program required for sesamed3: $$p";\
#	done
#	$(COPY) $(SBIN) $(SBINDIR)

install_cgi:
	cp $(CGI) $(CGIDIR)
	@if test -d $(RUNDIR); then \
	   echo "#... 'run' directory exists" ; \
	   ls -ld $(RUNDIR) ; \
	else \
	   mkdir $(RUNDIR) ; \
	fi
	@if test -z "$(RUNln)" ; then \
	   ls -ld $(RUNDIR); \
	else \
	   (cd $(CGIDIR)/..; test -h run || ln -s ../run) \
	fi
	@if test -d $(LOGDIR); then \
	    echo "#... $(LOGDIR) exists ..."; \
	    ls -ld $(LOGDIR) ; \
	else \
	    mkdir $(LOGDIR); \
	fi
	touch $(LOGDIR)/sesame.log ; chmod 666 $(LOGDIR)/sesame.log
	touch $(RUNDIR)/sesame.conf; chmod 666 $(RUNDIR)/sesame.conf
	-rm -rf $(RUNDIR)/sesame
	-mkdir $(RUNDIR)/sesame
	-chmod 777 $(RUNDIR)/sesame
	ls -l $(LOGDIR)/sesame.log

install_info:
	-test -d $(XMLDIR) && cp -p $(XSD) $(XMLDIR)
	touch $(VERFILE)
	(cat $(VERFILE); echo  "$(PACKAGE):V$(VERSION)"; \
	 for  f in $(BIN); do \
	 ./$$f -version | $(AWK) '{printf "-%s:%s\n", $$1, $$NF}'; 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 

clean:
	rm -f *.o
	rm -f $(BIN)
	rm -f *.log *.aux *.dvi *.lis *.ps
	rm -f core


# Generate an exportable package
export: /tmp/$(DISTRIB).tar

tar: /tmp/$(DISTRIB).tar

/tmp/$(DISTRIB).tar: $(SRC) Makefile.in *.c
	$(MAKE) clean
	-test -d /tmp/$(DISTRIB) && rm -rf /tmp/$(DISTRIB)
	autoconf
	mkdir /tmp/$(DISTRIB)
	cp -p $(SRC) $(INC) $(MAN) $(SHS) $(INI) $(CGI) $(XSD) /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 
