# -*- makefile -*-
# $Id: Makefile.in,v 1.14 1999/02/23 19:06:21 krasel Exp $

VERSION = 1.9.9

srcdir = @srcdir@
VPATH = @srcdir@

# Programs
CC      = @CC@
INSTALL = @INSTALL@
RANLIB  = @RANLIB@
AWK	= @AWK@
AR	= @AR@

# Libraries
PCRELIB = @PCRELIB@

# Flags
DEBUG  = -g
STRIP  = -s
CFLAGS = -I. @DEFS@ @GCC_FLAGS@ $(DEBUG)
LIBS   = @LIBS@

# Directories and Paths
# News is stored in SPOOLDIR, configuration is kept in LIBDIR
# LOCKFILE is the lockfile generated by the leafnode programs
PREFIX_USR = @prefix@
PREFIX_VAR = /var
USRDIR = $(PREFIX_USR)/bin
BINDIR = $(PREFIX_USR)/sbin
MANDIR = $(PREFIX_USR)/share/man
SPOOLDIR = $(PREFIX_VAR)/spool/news
LIBDIR   = /etc/news/leafnode
LOCKFILE = /var/lock/news/fetchnews.lck

# RPM crap
RPM = @RPM@

SRCFILES = nntputil.c configutil.c xoverutil.c activutil.c miscutil.c \
	   artutil.c filterutil.c config.c \
	   applyfilter.c checkgroups.c newsq.c nntpd.c fetchnews.c texpire.c \
	   lsort.c
LIBFILES = nntputil.o configutil.o xoverutil.o activutil.o miscutil.o \
	   artutil.o filterutil.o

all: liblnutil.a @PCRELIB@ nntpd fetchnews texpire checkgroups applyfilter newsq lsort

liblnutil.a: $(LIBFILES) leafnode.h
	$(AR) rc liblnutil.a $(LIBFILES)
	$(RANLIB) liblnutil.a

libpcre.a: pcre/libpcre.a
	@cp pcre/libpcre.a libpcre.a

pcre/libpcre.a:
	@echo "Building Philip Hazel's Perl regular expressions library..."
	@cd pcre && $(MAKE) $(MFLAGS) -$(MAKEFLAGS) libpcre.a

nntpd: nntpd.o config.o liblnutil.a
	$(CC) $(CFLAGS) -o $@ nntpd.o config.o -L. -llnutil $(LIBS)

fetchnews: fetchnews.o config.o liblnutil.a @PCRELIB@
	$(CC) $(CFLAGS) -o $@ fetchnews.o config.o -L. -llnutil -lpcre $(LIBS)

texpire: texpire.o config.o liblnutil.a
	$(CC) $(CFLAGS) -o $@ texpire.o config.o -L. -llnutil $(LIBS)

checkgroups: checkgroups.o config.o liblnutil.a
	$(CC) $(CFLAGS) -o $@ checkgroups.o config.o -L. -llnutil $(LIBS)

applyfilter: applyfilter.o config.o liblnutil.a @PCRELIB@
	$(CC) $(CFLAGS) -o $@ applyfilter.o config.o -L. -llnutil -lpcre $(LIBS)

newsq: newsq.o config.o liblnutil.a
	$(CC) $(CFLAGS) -o $@ newsq.o config.o -L. -llnutil $(LIBS)

lsort: lsort.o config.o liblnutil.a
	$(CC) $(CFLAGS) -o $@ lsort.o config.o -L. -llnutil $(LIBS)

nntpd.o: nntpd.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/nntpd.c

fetchnews.o: fetchnews.c leafnode.h
	@echo fetchnews: please disregard warning about the year having only two digits
	$(CC) -c $(CFLAGS) $(srcdir)/fetchnews.c

texpire.o: texpire.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/texpire.c

checkgroups.o: checkgroups.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/checkgroups.c

applyfilter.o: applyfilter.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/applyfilter.c

newsq.o: newsq.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/newsq.c

lsort.o: lsort.c leafnode.h
	$(CC) -c $(CFLAGS) $(srcdir)/lsort.c

clean:
	@rm -f *.o *.a *~ \#*
	@rm -f core nntpd fetchnews texpire checkgroups applyfilter newsq lsort
	@rm -f config.status config.cache config.log
	@cd pcre && $(MAKE) $(MFLAGS) -$(MAKEFLAGS) clean

realclean: clean
	@rm -f config.h Makefile pcre/Makefile *.rej *.orig

update:
	@$(srcdir)/update.sh $(SPOOLDIR) $(LIBDIR) $(LOCKFILE)

depend:
	makedepend *.c 2> /dev/null

config.o: config.c Makefile
	$(CC) -c -DSPOOLDIR=\"$(SPOOLDIR)\" \
		-DLIBDIR=\"$(LIBDIR)\" \
		-DBINDIR=\"$(BINDIR)\" \
		-DLOCKFILE=\"$(LOCKFILE)\" \
		-DVERSION=\"$(VERSION)\" $<

install: liblnutil.a nntpd fetchnews texpire checkgroups applyfilter newsq
	$(INSTALL) -d -o root -g 0 -m 755 $(INSTALLROOT)$(MANDIR)/man1
	$(INSTALL) -d -o root -g 0 -m 755 $(INSTALLROOT)$(MANDIR)/man7
	$(INSTALL) -d -o root -g 0 -m 755 $(INSTALLROOT)$(MANDIR)/man8
	$(INSTALL) -d -o root -g bin -m 755 $(INSTALLROOT)$(BINDIR)
	$(INSTALL) -d -o root -g bin -m 755 $(INSTALLROOT)$(USRDIR)
	$(INSTALL) -c -g news -m 750 -o news $(STRIP) nntpd $(INSTALLROOT)$(BINDIR)/leafnode
	$(INSTALL) -c -g news -m 750 -o news $(STRIP) fetchnews $(INSTALLROOT)$(BINDIR)/fetchnews
	$(INSTALL) -c -g news -m 750 -o news $(STRIP) texpire $(INSTALLROOT)$(BINDIR)/texpire
	$(INSTALL) -c -g news -m 750 -o news $(STRIP) checkgroups $(INSTALLROOT)$(BINDIR)/checkgroups
	$(INSTALL) -c -g news -m 750 -o news $(STRIP) applyfilter $(INSTALLROOT)$(BINDIR)/applyfilter
	$(INSTALL) -c -g news -m 755 -o news $(STRIP) newsq $(INSTALLROOT)$(USRDIR)/newsq
	cp $(srcdir)/*.8 $(INSTALLROOT)$(MANDIR)/man8
	if test $(PCRELIB) ; then cp $(srcdir)/pcre/pcre.7 $(INSTALLROOT)$(MANDIR)/man7 ; fi
	cp $(srcdir)/*.1 $(INSTALLROOT)$(MANDIR)/man1
	-mkdir -p `dirname $(INSTALLROOT)$(LOCKFILE)`
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR) $(INSTALLROOT)$(LIBDIR)
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR)/leaf.node
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR)/failed.postings
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR)/message.id
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR)/interesting.groups
	-mkdir -p $(INSTALLROOT)$(SPOOLDIR)/out.going
	-chown news:news `dirname $(INSTALLROOT)$(LOCKFILE)`
	-chown news:news $(INSTALLROOT)$(SPOOLDIR) $(INSTALLROOT)$(LIBDIR)
	-chown news:news $(INSTALLROOT)$(SPOOLDIR)/leaf.node
	-chown news:news $(INSTALLROOT)$(SPOOLDIR)/failed.postings
	-chown news:news $(INSTALLROOT)$(SPOOLDIR)/message.id
	-chown news:news $(INSTALLROOT)$(SPOOLDIR)/interesting.groups
	-chown news:news $(INSTALLROOT)$(SPOOLDIR)/out.going
	chmod 2755 $(INSTALLROOT)$(SPOOLDIR)
	@echo Edit /etc/inetd.conf to start $(BINDIR)/leafnode and restart inetd
	@echo If you update from a leafnode version \< 1.9.3, do a \"make update\"

uninstall: uninstall-bins
	-rm -r $(SPOOLDIR)/leaf.node
	-rm -r $(SPOOLDIR)/interesting.groups
	-rm -r $(SPOOLDIR)/message.id/*
	-rmdir $(SPOOLDIR)/out.going
	-rm -r $(LIBDIR)

uninstall-bins:
	-rm $(BINDIR)/fetchnews
	-rm $(BINDIR)/checkgroups
	-rm $(BINDIR)/texpire
	-rm $(BINDIR)/leafnode
	-rm $(BINDIR)/applyfilter
	-rm $(USRDIR)/newsq
	-rm $(MANDIR)/man8/fetchnews.8
	-rm $(MANDIR)/man8/checkgroups.8
	-rm $(MANDIR)/man8/texpire.8
	-rm $(MANDIR)/man8/leafnode.8
	-rm $(MANDIR)/man8/applyfilter.8
	-rm $(MANDIR)/man1/newsq.1
	@echo Edit /etc/inetd.conf to remove $(BINDIR)/leafnode and restart inetd

dist:	realclean
	autoheader
	autoconf
	mkdir -p leafnode-$(VERSION)
	cp *[a-z].8 newsq.1 leafnode.h acconfig.h $(SRCFILES) \
		INSTALL README COPYING CREDITS TODO CHANGES config.example \
		config.h.in configure.in \
		Makefile.in configure install-sh \
		update.sh makerpm.sh leafnode-$(VERSION).lsm \
		leafnode-$(VERSION)
	chmod 644 leafnode-$(VERSION)/*
	mkdir -p  leafnode-$(VERSION)/doc_german
	chmod 755 leafnode-$(VERSION)/doc_german
	mkdir -p leafnode-$(VERSION)/pcre
	chmod 755 leafnode-$(VERSION)/pcre
	mkdir -p leafnode-$(VERSION)/tools
	chmod 755 leafnode-$(VERSION)/tools
	cp -R doc_german/* leafnode-$(VERSION)/doc_german/
	cp tools/* leafnode-$(VERSION)/tools/
	cp pcre/* leafnode-$(VERSION)/pcre/
	chmod 755 leafnode-$(VERSION)/*sh
	chmod 755 leafnode-$(VERSION)/configure
	chmod 755 leafnode-$(VERSION)/tools/*
	chmod 755 leafnode-$(VERSION)/pcre/perltest
	tar cf leafnode-$(VERSION).tar leafnode-$(VERSION)
	gzip -f9 leafnode-$(VERSION).tar
	rm -rf leafnode-$(VERSION)

patch:
	rm -f ../leafnode-$(VERSION).patch
	rm -f ../leafnode-$(VERSION).patch.gz
	@for i in * ; \
	do \
	  if [ -e $$i.orig ] ; \
	  then \
	    echo "diff --unified $$i.orig $$i" ; \
	    diff --unified $$i.orig $$i >> ../leafnode-$(VERSION).patch ; \
	  fi ; \
	done
	gzip -f9 ../leafnode-$(VERSION).patch

rpm: dist
	@./makerpm.sh $(RPM) $(VERSION) $(USRDIR) $(BINDIR) $(MANDIR) $(LOCKFILE) $(SPOOLDIR) $(AWK) $(LIBDIR) $(PREFIX_USR)

# DO NOT DELETE
