#!/usr/bin/make -f
# Sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
#
# Changes for 'nn' package by Mike Coleman.  Released into the public domain.
#

package=nn
nnpackage=nn
version=6.5.0.b3.linux.1.1
debian=01
STRIP=strip

CFLAGS=-O2 -g
LDFLAGS=

CPP=gcc -E

# The next section may have to be extensively modified

manpages=$(addprefix man/,nn.1 nncheck.1 nngrep.1 nnpost.1 nntidy.1)

build:
	$(checkdir)
	-rm -f config.h update.h local_curses.h
	sed < /usr/include/curses.h > local_curses.h \
		-e '/^#include <stdarg.h>/d' \
		-e '/^extern int standout/d'
	echo '#define NNTP' >> config.h
	echo '#define TMP_DIRECTORY "/var/tmp"' >> config.h
	# echo '#define INEWS_PATH "/usr/bin/inews"' >> config.h
	sed < config.h-dist >> config.h \
		-e 's/^#define TK.*//' \
		-e 's|^\(#define TKINCLUDE\).*|\1 /usr/include/tcl|' \
		-e 's|^\(#define TKLIB\).*||' \
		-e 's|^#undef NNTP.*||' \
		-e 's|^#define NNTP_MINI_INEWS_HEADER.*||' \
		-e 's|^\(#define NNTP_SERVER\).*|\1 "/etc/news/server"|' \
		-e 's|^\(#define INEWS_PATH\).*|\1 "/usr/bin/inews"|' \
		-e 's|s-sunos5\.h|s-debian.h|' \
		-e 's|s-sys5-4\.h|s-debian.h|' \
		-e 's|^\(#define DEFAULT_PRINTER\).*|\1 "lpr -p -JNEWS"|' \
		-e 's|m-sparc\.h|m-i80386.h|' \
		-e 's|/usr/local|/usr|' \
		-e 's|/usr/spool|/var/spool|' \
		-e 's|/usr/lib/sendmail|/usr/sbin/sendmail|' \
		-e 's|nn-bugs@dkuug\.dk|bugs@nn.org|' \
		-e 's|^#define INEWS_IS_FAST.*||' \
		-e 's|^#define SYNCHRONOUS_POSTING.*||' \
		-e 's|^#define CONFIG_NUM_IN_VERSION.*||' \
		-e 's|^#define ART_GREP.*||'
	@echo '# the two following MF.c warnings seem benign'
	$(MAKE) CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)' CPP='$(CPP)' ymakefile
	$(MAKE) -f ymakefile client
	touch build

clean:
	$(checkdir)
	-rm -f build nn nn-tk
	-$(MAKE) -i clean
	-rm -rf debian/tmp *~ config.h update.h local_curses.h

binary-arch:	checkroot build
	-rm -rf debian/tmp
	install -d debian/tmp/DEBIAN debian/tmp/usr/doc/nn debian/tmp/usr/bin debian/tmp/usr/lib/nn/help debian/tmp/usr/man/man1
	install -m 0755 nn debian/tmp/usr/bin
	$(STRIP) debian/tmp/usr/bin/nn
	cd debian/tmp/usr/bin && \
	for p in nncheck nntidy nngrep nnpost nnbatch nnview; do \
		ln -s nn $$p; \
	done
	install -m 0644 aux upgrade_rc debian/tmp/usr/lib/nn
	./mkprefix conf > debian/tmp/usr/lib/nn/conf
	grep "^#" config.h \
	| sed -e '/_MAN_/d' -e 's/[ 	]*\/\*.*$$//' >> debian/tmp/usr/lib/nn/conf
	chmod 0644 debian/tmp/usr/lib/nn/conf
	for h in `cd help; echo *`; do \
		./cvt-help < help/$$h > debian/tmp/usr/lib/nn/help/$$h; \
		chmod 0644 debian/tmp/usr/lib/nn/help/$$h; \
	done
	install -m 0644 $(manpages) debian/tmp/usr/man/man1
	gzip -9v debian/tmp/usr/man/man1/*
	sed 	-e 's/\\f[BPI]//g' \
		-e 's/\\-/-/g' -e 's/\\&//g' -e 's/\\e/\\/g' \
		-e '/^\.\\"ta/p' -e '/^\.\\"/d' \
		-e '/^\.nf/d' -e '/^\.fi/d' \
		-e '/^\.if/d' -e '/^\.ta/d' -e '/^\.nr/d' \
		-e '/^\.in/d' -e 's/^\.[BI] //' \
		$(manpages) | \
	gawk -f format.awk - > debian/tmp/usr/lib/nn/help/Manual
	chmod 0644 debian/tmp/usr/lib/nn/help/Manual
	#	
	# $(MAKE) CFLAGS=-O2 LDFLAGS=-s prefix=debian/tmp/usr install
	# gzip -9v debian/tmp/usr/info/*
	cp debian/README.debian debian/changelog debian/tmp/usr/doc/$(nnpackage)
	gzip -9v debian/tmp/usr/doc/nn/*
	cp debian/copyright debian/tmp/usr/doc/nn
	install -m 0755 debian/postinst debian/tmp/DEBIAN
	dpkg-shlibdeps debian/tmp/usr/bin/nn
	dpkg-gencontrol
	chown -R root.root debian/tmp
	chmod -R g-ws debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f nn.c
endef

# Below here is fairly generic really

binary:	binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot
