##################################################################
############## Copyright (C) 1996-97 Stenio Brunetta #############
#################### See COPYRIGHT for details ###################
##################################################################

# $Id: Makefile.in,v 1.18 2001/01/02 19:14:33 ste Exp $

SHELL    = /bin/sh
CC       = @CC@
DEFS     = @DEFS@
CFLAGS   = @CFLAGS@ -Wall
CPPFLAGS = @CPPFLAGS@ -I. $(DEFS) @ENABLE_DEBUG@
LIBS     = @LIBS@
LIBOBJS  = @LIBOBJS@
LDFLAGS  = @LDFLAGS@
INSTALL  = @INSTALL@
LN_S     = @LN_S@ -f

# asp.c, aspd.c, asp.1 and aspd.8 are missing because they
# are modified by the distrib rule.
DISTFILES  = addr.c comm.c asplib.c asp.h Makefile.in		\
             configure.in config.h.in acconfig.h aclocal.m4	\
             configure install-sh INSTALL COPYRIGHT .version
OBJECTS    = asp.o aspd.o addr.o comm.o asplib.o

prefix     = @prefix@
exec_prefix= @exec_prefix@
bindir     = @bindir@
sbindir    = @sbindir@
mandir     = @mandir@
man1dir    = $(mandir)/man1
man8dir    = $(mandir)/man8

services   = /etc/services
inetd.conf = /etc/inetd.conf

.PHONY: all install bin-install man-install \
	de-install append-entry clean distclean \
	distrib

.c.o:
	$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

all:	asp aspd

asp:	asp.o addr.o comm.o asplib.o $(LIBOBJS)
	$(CC) $(LDFLAGS) -o asp asp.o addr.o comm.o asplib.o $(LIBOBJS) $(LIBS)

aspd:	aspd.o asplib.o comm.o $(LIBOBJS)
	$(CC) $(LDFLAGS) -o aspd aspd.o asplib.o comm.o $(LIBOBJS) $(LIBS)

$(OBJECTS):	asp.h

install:	all bin-install man-install

bin-install:	all append_entry
	$(INSTALL) -o root -g root -m 0755 -s asp $(bindir)
	$(INSTALL) -o root -g root -m 0755 -s aspd $(sbindir)

append_entry:
	@if [ -w $(services) ]; then \
	  if ! grep -q '^asp' $(services); then \
	    ANS=foo; \
	    until [ "$$ANS" = "y" ] || [ "$$ANS" = "n" ] || \
	          [ "$$ANS" = "Y" ] || [ "$$ANS" = "N" ] || \
	          [ "$$ANS" = ""  ]; do \
	      echo -n "Attach asp protocol to $(services) [Y/n]: "; \
	      read "ANS"; \
	    done; \
	    if [ "$$ANS" = "Y" ] || [ "$$ANS" = "y" ] || \
	       [ "$$ANS" = ""  ]; then \
	      if cp $(services) $(services).asp > /dev/null 2>&1; then \
	         echo "$(services) copied to $(services).asp"; \
	      else echo "Cannot copy $(services) to $(services).asp"; \
	      fi; \
	      echo "# Address Search Protocol" >> $(services); \
	      echo "asp		27374/udp" >> $(services); \
	    fi; \
	  fi; \
	else echo "File $(services) not writable"; \
	fi
	@if [ -w $(inetd.conf) ]; then \
	  if ! grep -q '^asp' $(inetd.conf); then \
	    ANS=foo; \
	    until [ "$$ANS" = "y" ] || [ "$$ANS" = "n" ] || \
	          [ "$$ANS" = "Y" ] || [ "$$ANS" = "N" ] || \
	          [ "$$ANS" = ""  ]; do \
	      echo -n "Attach asp entry to $(inetd.conf) [Y/n]: "; \
	      read "ANS"; \
	    done; \
	    if [ "$$ANS" = "Y" ] || [ "$$ANS" = "y" ] || \
	       [ "$$ANS" = ""  ]; then \
	      if cp $(inetd.conf) $(inetd.conf).asp > /dev/null 2>&1; then \
	         echo "$(inetd.conf) copied to $(inetd.conf).asp"; \
	      else echo "Cannot copy $(inetd.conf) to $(inetd.conf).asp"; \
	      fi; \
	      echo "# Address Search Protocol" >> $(inetd.conf); \
	      ANS=foo; \
	      until [ "$$ANS" = "y" ] || [ "$$ANS" = "n" ] || \
	            [ "$$ANS" = "Y" ] || [ "$$ANS" = "N" ] || \
	            [ "$$ANS" = ""  ]; do \
	         echo -n "Use an alternate host name [y/N]: "; \
	         read "ANS"; \
	      done; \
	      if [ "$$ANS" = "Y" ] || [ "$$ANS" = "y" ]; then \
	         ANS=""; \
	         while [ "$$ANS" = "" ]; do \
	            echo -n "Alternate hostname: "; \
	            read "ANS"; \
	         done; \
	         echo "asp	dgram	udp	wait	root	$(sbindir)/aspd	in.aspd $$ANS" >> $(inetd.conf); \
	      else echo "asp      dgram   udp     wait    root    $(sbindir)/aspd in.aspd" >> $(inetd.conf); \
	      fi; \
	    fi; \
	  fi; \
	else echo "File $(inetd.conf) not writable"; \
	fi

man-install:
	$(INSTALL) -o root -g root -m 0644 asp.1 $(man1dir)
	$(INSTALL) -o root -g root -m 0644 aspd.8 $(man8dir)
	cd $(man8dir) && $(LN_S) aspd.8 in.aspd.8

de-install:
	rm -f $(bindir)/asp
	rm -f $(sbindir)/aspd
	rm -f $(man1dir)/asp.1
	rm -f $(man8dir)/aspd.8
	rm -f $(man8dir)/in.aspd.8

clean:
	rm -f *.o *~ core
	rm -f $(TARGETS) asp-*.tar.gz

distclean:	clean
	rm -f Makefile config.cache config.h \
	      config.log config.status

heavyclean: distclean
	rm -f config.h config.h.in configure

distrib:
	autoreconf
	VER=`cat .version`; \
	if [ ! -d asp-$$VER ]; then \
	   mkdir asp-$$VER; \
	else \
	   rm -rf asp-$$VER; \
	   mkdir asp-$$VER; \
	fi; \
	cp $(DISTFILES) asp-$$VER; \
	sed '22s/[0-9][0-9]*\(\.[0-9][0-9]*\)*/'$$VER'/' asp.c > asp-$$VER/asp.c; \
	sed '22s/[0-9][0-9]*\(\.[0-9][0-9]*\)*/'$$VER'/' aspd.c > asp-$$VER/aspd.c; \
	sed '10s/"[A-Z][a-z]*/"'`date +%B`'/' asp.1 > asp-$$VER/asp.1; \
	sed '10s/"[A-Z][a-z]*/"'`date +%B`'/' aspd.8 > asp-$$VER/aspd.8; \
	chmod 644 asp-$$VER/* asp-$$VER/.version; \
	chmod 755 asp-$$VER/configure asp-$$VER/install-sh asp-$$VER; \
	tar -zcvf asp-$$VER.tar.gz asp-$$VER; \
	chmod 644 asp-$$VER.tar.gz
