# Configuration stuff.
COPTS=-O
CFLAGS=$(COPTS) -I../include
LDFLAGS=
LIBS=../libcnews.a
# =()<NEWSARTS = @<NEWSARTS>@>()=
NEWSARTS = /usr/spool/news
# =()<NEWSBIN = @<NEWSBIN>@>()=
NEWSBIN = /usr/lib/newsbin
# =()<NEWSCTL = @<NEWSCTL>@>()=
NEWSCTL = /usr/lib/news
# workaround for System V make bug
SHELL = /bin/sh

OUTGOING = $(NEWSARTS)/out.going
PGMDIR = $(NEWSBIN)/batch

PGMS=batcher batchih batchsm batchsplit comp compcun nocomp viainews viauux \
	sendbatches compc7 c7encode viamail viapmail bencode compb
DTR=Makefile batcher.c batchih batchsplit c7encode.c comp compc7 compcun \
	newsbatch.8 nocomp sendbatches viainews viauux viamail \
	bencode.c coder.h crctab.c compb viapmail
ALL = $(PGMS) batchparms

all:	$(ALL)
	chmod +x $(PGMS)

batcher:	batcher.o $(LIBS)
	$(CC) $(CFLAGS) $(LDFLAGS) batcher.o $(LIBS) -o $@

c7encode:	c7encode.o $(LIBS)
	$(CC) $(CFLAGS) $(LDFLAGS) c7encode.o $(LIBS) -o $@

bencode:	bencode.o crctab.o $(LIBS)
	$(CC) $(CFLAGS) bencode.o crctab.o $(LIBS) -o $@

bencode.o:	coder.h

bininstall:	all
	-if test ! -d $(PGMDIR) ; then mkdir $(PGMDIR) ; fi
	cp $(PGMS) $(PGMDIR)

newsinstall:	batchparms
	-if test ! -d $(OUTGOING) ; then mkdir $(OUTGOING) ; fi
	-if test ! -r $(NEWSCTL)/batchparms ; then cp batchparms $(NEWSCTL) ; fi

batchparms:	Makefile
	echo '# 100KB, after compress, is 10 minutes at 1200 baud' >$@
	echo '# 20 batches is somewhat arbitrary, about 1MB per site' >>$@
	echo '# defaults:  2.11-compatible compression, transfer by uux' >>$@
	echo '#' >>$@
	echo '# site		size	queue	builder	muncher	sender' >>$@
	echo '# ----		----	-----	-------	-------	------' >>$@
	echo '/default/	100000	20	batcher	compcun	viauux' >>$@
	echo '#' >>$@
	echo '# sample ihave/sendme setup' >>$@
	echo 'louie.ihave	100000	20	batchih	nocomp	viainews' >>$@
	echo 'louie.sendme	100000	20	batchsm	nocomp	viainews' >>$@

batchsm:	batchih
	sed 's/ihave/sendme/g' batchih >$@

test.1:
	echo 'a test' >$@

test.2:
	echo 'another test' >$@

test.3:
	cat batcher.c batcher.c batcher.c batcher.c batcher.c >$@

r:	all test.1 test.2 test.3
	echo a 1 >togo
	echo b 1 >>togo
	echo c 1 >>togo
	echo d 1 >>togo
	echo e 1 >>togo
	echo f 1 >>togo
	echo g 1 >>togo
	echo h 3 >>togo
	echo i 20 >>togo
	echo j 999 >>togo
	rm -rf bin
	mkdir bin
	echo 'ln $$* >/dev/null 2>/dev/null' >bin/newslock
	echo 'cat $*' >bin/munch
	echo '( echo --- ; cat $* ) >>output' >bin/xmit
	echo 'echo 2 ; cat $*' >bin/munch2
	echo '( echo +++ ; cat $* ) >>output' >bin/xmit2
	echo 'echo 4' >bin/queuelen
	echo 'echo 99' >bin/spacefor
	ln batcher bin/batcher
	ln batchsplit bin/batchsplit
	chmod +x bin/*
	NEWSCTL=`pwd` ./batchsplit 1
	echo a 1 >test.good ; cmp test.good togo.1
	echo b 1 >test.good ; cmp test.good togo.2
	echo c 1 >test.good ; cmp test.good togo.3
	echo d 1 >test.good ; cmp test.good togo.4
	echo e 1 >test.good ; cmp test.good togo.5
	echo f 1 >test.good ; cmp test.good togo.6
	echo g 1 >test.good ; cmp test.good togo.7
	echo h 3 >test.good ; echo i 20 >>test.good ; cmp test.good togo.next
	echo j 999 >test.good ; cmp test.good togo
	rm togo.*
	echo `pwd`/test.1 >togo
	echo ./test.2 >>togo
	echo `pwd`/test.3 666 >>togo
	./batcher togo >test.out
	echo "#! rnews `wc -c test.1 | awk '{print $$1}'`" >test.good
	cat test.1 >>test.good
	echo "#! rnews `wc -c test.2 | awk '{print $$1}'`" >>test.good
	cat test.2 >>test.good
	( echo --- ; cat test.good ) >test.out1
	( echo +++ ; echo 2 ; cat test.good ) >test.out2
	echo "#! rnews `wc -c test.3 | awk '{print $$1}'`" >test.temp
	cat test.3 >>test.temp
	cat test.temp >>test.good
	( echo --- ; cat test.temp ) >>test.out1
	cmp test.good test.out
	./batcher -d `pwd` togo >test.out
	cmp test.good test.out
	rm test.out togo
	rm -rf out.going batchparms test.out test.good
	mkdir out.going out.going/foo out.going/bar
	echo 'foo	500	10	batcher	munch	xmit' >batchparms
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./sendbatches -d >test.out
	echo foo >test.good
	cmp test.out test.good
	echo '/default/	10000	5	batcher	munch2	xmit2' >>batchparms
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./sendbatches -d | sort >test.out
	( echo foo ; echo bar ) | sort >test.good
	cmp test.out test.good
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./sendbatches -d bletch >test.out
	echo bletch >test.good
	cmp test.out test.good
	>batchlog
	>batchlog.o
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./sendbatches
	test ! -s out.going/foo/output
	test ! -s out.going/bar/output
	echo `pwd`/test.1 200 >out.going/foo/togo
	echo `pwd`/test.2 200 >>out.going/foo/togo
	echo `pwd`/test.3 200 >>out.going/foo/togo
	echo `pwd`/test.1 4000 >out.going/bar/togo
	echo `pwd`/test.2 4000 >>out.going/bar/togo
	echo `pwd`/test.3 4000 >>out.going/bar/togo
	echo `pwd`/test.3 4000 >test.left
	NEWSARTS=`pwd` NEWSCTL=`pwd` ./sendbatches
	cmp out.going/foo/output test.out1
	cmp out.going/bar/output test.out2
	test ! -s out.going/foo/togo
	test ! -s out.going/bar/togo
	cmp test.left out.going/bar/togo.2
	sed 1d batchlog.o >test.log
	test ! -s test.log
	sed 1d batchlog >test.log
	echo 'bar	backlog 1 (batches flowing)' >test.good
	cmp test.good test.log
	rm -rf test.* togo out.going bin batchparms batchlog batchlog.*

dtr:	$(DTR)
	makedtr $(DTR) >$@

clean:
	rm -rf out.going bin
	rm -f *.o test.* togo togo.* batchparms batcher batchsm 
	rm -f batchlog batchlog.* c7encode bencode
