# You get your choice of rnews.immed or rnews.batch; rnews.immed tries
# to start processing immediately, while rnews.batch waits for somebody
# else (cron) to do it.  Running rnews.immed might, perhaps, be reasonable
# if your news load is light and you are wildly impatient about processing
# incoming news.  Otherwise, leave this alone.
RNEWS = rnews.batch

#-INCLUDE = CN:include
#-DEFINES = -DAMIGA -DFJE
#-COPTS = -bs -sabm -wl
#-LD = ln
#-LDFLAGS = -g -w
#-LIB  = CN:libcnews.lib
#-LIBS = +l $(LIB) +l -lc
#-BATCH = CN:batch

CC = gcc
LD = $(CC)
INCLUDE = ../include
DEFINES = -DFJE
COPTS = -O -DFJE
LIB = ../libcnews.a
LIBS = $(LIB)
BATCH = ../batch

CFLAGS = $(COPTS) $(DEFINES) -I$(INCLUDE)
LINTFLAGS = $(DEFINES) -I$(INCLUDE) -ha

THEMBIN = newsrun newsrunning c7decode bdecode recenews recpnews rnews
THEM = newsspool $(THEMBIN)

RBIN = /bin
# =()<NEWSARTS = @<NEWSARTS>@>()=
NEWSARTS = /usr/spool/news
# =()<NEWSBIN = @<NEWSBIN>@>()=
NEWSBIN = /usr/lib/newsbin

# workaround for System V make bug
SHELL = /bin/sh

#-all:	rnews

all:	$(THEM)
	chmod +x $(THEM)

bininstall:	$(THEM)
	-if test ! -d $(NEWSBIN)/input ; then mkdir $(NEWSBIN)/input ; fi
	rm -f $(NEWSBIN)/input/newsspool
	cp $(THEM) $(NEWSBIN)/input
	cp rnews $(RBIN)/rnews
	cp rnews $(RBIN)/cunbatch
	: "and newsspool needs to be made setuid-news"

cmp:	$(THEM)
	for f in $(THEM) ; do cmp $(NEWSBIN)/input/$$f $$f ; done
	cmp rnews $(RBIN)/rnews
	cmp rnews $(RBIN)/cunbatch
	ls -lg $(NEWSBIN)/input/newsspool|egrep -s '^-rwsrwsr-x  1 news     news'

check:	$(THEM)
	for f in $(THEM) ; do cmp $(NEWSBIN)/input/$$f $$f || true ; done
	cmp rnews $(RBIN)/rnews || true
	cmp rnews $(RBIN)/cunbatch || true
	ls -lg $(NEWSBIN)/input/newsspool|egrep -s '^-rwsrwsr-x  1 news     news'

newsinstall:
	: nothing

#-rnews: rnews.o $(LIB)
#-	$(LD) $(LDFLAGS) rnews.o $(LIBS) -o $@

newsspool: newsspool.o $(LIB)
	$(LD) $(LDFLAGS) newsspool.o $(LIBS) -o $@

c7decode: c7decode.o $(LIB)
	$(LD) $(LDFLAGS) c7decode.o $(LIBS) -o $@

bdecode: bdecode.o $(BATCH)/crctab.o $(LIB)
	$(LD) $(LDFLAGS) bdecode.o $(BATCH)/crctab.o $(LIBS) -o $@

$(BATCH)/crctab.o:	$(BATCH)/crctab.c
	( cd $(BATCH) ; $(MAKE) crctab.o )

bdecode.o:	bdecode.c $(BATCH)/coder.h
	$(CC) -I$(BATCH) $(CFLAGS) -c bdecode.c

lint:	newsspool.c
	lint $(LINTFLAGS) newsspool.c 2>&1 | tee lint

test.1:
	echo '#! rnews' >$@
	echo 'here is a phony first batch' >>$@

test.2:
	echo '#! cunbatch' >$@
	echo '#! rnews' >>$@
	echo 'here is a phony second batch' >>$@

test.3p:
	echo '#! rnews' >$@
	echo 'here is a phony third batch' >>$@

test.3:	test.3c
	( echo '#! cunbatch' ; cat test.3c ) >$@

test.3c:	test.3p
	: compress tends to return silly exit status for tiny inputs
	-compress -b12 <test.3p >$@

test.out:
	echo '#! rnews' >$@
	echo 'here is a phony first batch' >>$@
	echo '#! rnews' >>$@
	echo 'here is a phony second batch' >>$@
	echo '#! rnews' >>$@
	echo 'here is a phony third batch' >>$@

setup:	all
	chmod +x rnews
	rm -rf bin
	mkdir bin
	cp $(THEM) bin
	mkdir bin/input
	cp newsrun bin/input
	rm -f tmp.1
	here=`pwd` ; echo "cat >>$$here/tmp.1" >bin/relaynews
	echo "echo 1" >bin/spacefor
	echo 'ln $$*' >bin/newslock
	echo 'echo 10' >bin/sizeof
	chmod +x bin/* bin/input/*
	rm -rf in.coming
	mkdir in.coming

r:	all test.1 test.2 test.3 test.3c test.out setup
	chmod +x $(THEM)
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./rnews <test.1
	cmp in.coming/* test.1
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./rnews <test.2
	sed 1d test.2 >tmp.2
	cmp `ls -t in.coming | sed -n '1s;^;in.coming/;p'` tmp.2
	rm tmp.2
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./rnews <test.3
	cmp `ls -t in.coming | sed -n '1s;^;in.coming/;p'` test.3c
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./newsrunning off
	test -r in.coming/stop ;
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./newsrunning on
	test ! -r in.coming/stop ;
	mkdir in.coming/bad
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./newsrun
	cmp tmp.1 test.out
	test " `echo in.coming/*`" = ' in.coming/bad' ;
	rm tmp.1
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./rnews <test.1
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./rnews <test.2
	echo >rnews.newsrun
	NEWSARTS=`pwd` NEWSCTL=`pwd` NEWSBIN=`pwd`/bin ./rnews <test.3
	cmp tmp.1 test.out
	test " `echo in.coming/*`" = ' in.coming/bad' ;
	rm tmp.1
	rm -r bin in.coming

clean:
	rm -f *.o newsspool c7decode tmp.? test.* dtr lint rnews.newsrun
	rm -f bdecode
	rm -rf in.coming bin
