#  userv - Makefile.in
#  
#  Copyright (C)1996-1997,1999 Ian Jackson
#  
#  This is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with userv; if not, write to the Free Software
#  Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

VERSION=@VERSION@
VEREXT=std

CC=@CC@
CFLAGS=@CFLAGS@ $(XCFLAGS) -DVERSION='"$(VERSION)"' -DVEREXT='"$(VEREXT)"' $(WERROR)
OPTIMISE=@OPTIMISE@
CPPFLAGS=@DEBUGDEFS@ $(XCPPFLAGS)
LDLIBS=@DEBUGLIBS@ @LIBS@ $(XLDLIBS)

M4=m4
M4FLAGS=
LEX=flex
MD5SUM=@MD5SUM_SIMPLE@
CWD=$(shell pwd)

INSTALL_GROUP=root
INSTALL_FLAGS=-o root -g $(INSTALL_GROUP)

INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@ $(INSTALL_FLAGS)
INSTALL_DATA=@INSTALL_DATA@ $(INSTALL_FLAGS)
prefix=@prefix@
exec_prefix=$(prefix)
bindir=$(exec_prefix)/bin
mandir=$(prefix)/man
man1dir=$(mandir)/man1
sbindir=$(exec_prefix)/sbin
etcdir=/etc
etcsubdir=$(etcdir)/userv
docdir=$(prefix)/userv

TARGETS=	daemon client
TARGETS_DOC_PS=	spec.ps overview.ps
TARGETS_DOC_RM=	$(TARGETS_DOC_PS) spec.html
TARGETS_DOC=	$(TARGETS_DOC_PS) spec.html/index.html
MAN1PAGES=	userv.1

SOURCES=	Makefile.in configure.in acconfig.h			\
		client.c common.h					\
		overlord.c process.c servexec.c 			\
		daemon.h debug.c parser.c lib.c lib.h			\
		language.i4 lexer.l.m4 tokens.h.m4

CONFIG_RESULTS=	config.status config.log config.h config.cache \
		Makefile pcsum.h

GENSHIP_CLEAN=	lexer.l lexer.c tokens.h config.h.in spec.sgml $(TARGETS_DOC)
GENSHIP=	$(GENSHIP_CLEAN) configure

all:		$(TARGETS)

docs:		$(TARGETS_DOC)

install:	all
		$(INSTALL_PROGRAM) -m 755 daemon $(sbindir)/uservd
		$(INSTALL_PROGRAM) -m 4755 client $(bindir)/userv
		$(INSTALL) -d -m 2755 $(etcsubdir) \
	$(etcsubdir)/default.d $(etcsubdir)/services.d $(etcsubdir)/override.d
		if test ! -f $(etcsubdir)/system.default; then \
			$(INSTALL_DATA) system.default $(etcsubdir); fi
		if test ! -f $(etcsubdir)/system.override; then \
			$(INSTALL_DATA) system.override $(etcsubdir); fi

install-strip:
		$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

install-doc:	$(TARGETS_DOC)
		$(INSTALL) -d -m 2755 $(docdir) $(docdir)/spec.html
		$(INSTALL) -d -m 2755 $(mandir) $(man1dir)
		$(INSTALL_DATA) -m 644 $(TARGETS_DOC_PS) $(docdir)/.
		$(INSTALL_DATA) -m 644 spec.html/*.html $(docdir)/spec.html/.
		$(INSTALL_DATA) -m 644 $(MAN1PAGES) $(man1dir)

uninstall:
		rm -f $(bindir)/userv $(sbindir)/uservd

uninstall-doc:
		cd $docdir && rm -rf $(TARGETS_DOC_RM)

daemon:		overlord.o process.o servexec.o parserlexer.o debug.o lib.o both.o
		$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

client:		client.o both.o
		$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

lexer.l:	language.i4

spec.sgml:	spec.sgml.in Makefile
		sed -e '/<version><\/version>/ s/>/&$(VERSION)/' \
			spec.sgml.in >$@.new && mv -f $@.new $@

client.o:	config.h common.h pcsum.h both.h version.h

process.o:	config.h common.h pcsum.h both.h daemon.h lib.h tokens.h

overlord.o:	config.h common.h pcsum.h daemon.h

servexec.o:	config.h common.h pcsum.h daemon.h lib.h version.h

lib.o:		config.h common.h lib.h

debug.o:	config.h common.h pcsum.h daemon.h lib.h tokens.h

parserlexer.o:	lexer.c parser.c config.h common.h pcsum.h daemon.h lib.h tokens.h
# lexer.c #include's parser.c at the end.  Blame flex.
		$(CC) -c $(CPPFLAGS) $(CFLAGS) lexer.c -o $@

pcsum.h:	common.h config.h config.status Makefile
		cat $^ | $(MD5SUM) \
			| sed -e 's/  -$$//; s/../0x&,/g; s/,$$//;' \
			>pcsum.h.new
		cmp pcsum.h.new pcsum.h || mv -f pcsum.h.new pcsum.h
		@rm -f pcsum.h.new

version.h:	Makefile
		echo '#define VERSION "$(VERSION)"' >$@.new && mv -f $@.new $@

tokens.h:	language.i4

autoconf configure:
		autoheader
		autoconf

clean:
		find -name '*.orig' -o -name '*~' -o -name '.*~' \
			-o -name '*#' -o -name '.#*' -o -name '*.bak' \
			| xargs -r rm
		rm -rf $(TARGETS) *.o core version.h
		rm -f overview.eps
		rm -f spec.lout* spec.text* spec.ps?* spec.sgml.new
		rm -f lout.li *.ld *.lix *.ldx
		rm -f userv-*.tar.gz vd/*

distclean mostlyclean:	clean
		rm -f $(CONFIG_RESULTS)

maintainer-clean:	distclean
		rm -rf $(GENSHIP_CLEAN) spec.html/*.html

dist-prep:	$(GENSHIP)

pre-checkin:
		./configure
		$(MAKE) dist-prep

dist_tmp=dist_tmp/userv-$(VERSION)
dist_prune=\( -name CVS -o -name 'dist_tmp*' -o -name slash-etc -o -name vd \)
dist:			dist-prep distclean
	rm -rf dist_tmp*
	mkdir dist_tmp $(dist_tmp)
	find $(dist_prune) -prune -o -type d -print | \
		sed -e 's#.*#mkdir -p $(dist_tmp)/&#' | sh
	find $(dist_prune) -prune -o -type f -print | \
		sed -e 's#.*#ln & $(dist_tmp)/&#' | sh
	cd dist_tmp && tar cf ../$(dist_tmp).tar `basename $(dist_tmp)`
	gzip -9 $(dist_tmp).tar
	mv $(dist_tmp).tar.gz .

linecount:	$(SOURCES)
		wc -l $^

%.html/index.html:	%.sgml
			debiandoc2html $<

%.ps:		%.sgml
		debiandoc2ps -1 -O $< >$@.new && mv $@.new $@

%.ps:		%.fig
		fig2dev -L ps -c -l dummy -P -z A4 $< >$@.new && mv $@.new $@

%.l:		%.l.m4
		$(M4) $(M4FLAGS) -- $< >$@.new && mv $@.new $@

%.h:		%.h.m4
		$(M4) $(M4FLAGS) -- $< >$@.new && mv $@.new $@

%:		%.m4
		$(M4) $(M4FLAGS) -- $< >$@.new && mv $@.new $@
