#! /usr/bin/make -f

#   Copyright 1994-97   joey@infodrom.north.de (Martin Schulze)
#
#   This program 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; version 2 dated June, 1991.
#
#   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 this program;  if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave., Cambridge, MA 02139, USA.

SHELL=/bin/bash

# The name and version of the source
#
source = $(shell grep "^Source: " debian/control|head -1|sed 's/Source: \(.*\)/\1/g')
package = $(shell grep "^Package: " debian/control|head -1|sed 's/Package: \(.*\)/\1/g')
version = $(shell grep "^$(source) " debian/changelog|head -1 |sed 's/.*(\(.*\)\-[^\-]*).*/\1/g')
revision = $(shell grep "^$(source) " debian/changelog|head -1 |sed 's/.*([^\-]*\-\(.*\)).*/\1/g')

installbin = install -g root -o root -m 755
installdoc = install -g root -o root -m 644

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
STRIP = -s
endif

build:
	test -f develop/config.cache || ./makeconfig CONFIG=/etc BINDIR=/usr/bin MANDIR=/usr/share/man SERVERDIR=/usr/bin
	perl -i -pe 's/undef\s+ENABLE_MULTIPROTOCOL/define ENABLE_MULTIPROTOCOL/' src/globals.h
	$(MAKE) all
	touch stamp-build

clean: debclean
	rm -rf stamp-build
	$(MAKE) clean

debclean:
# Cleans debian binary directories to allow binary creation
	rm -rf debian/tmp
	rm -f debian/{files,substvars}

binary-indep:
# Nothing to be done here

binary-arch: debclean
	test -f stamp-build || $(MAKE) -f debian/rules build
	$(installbin) -d debian/tmp/DEBIAN
	chown -R root.root debian/tmp
	chmod -R g-ws debian/tmp
	$(installbin) debian/{prerm,postinst,postrm} debian/tmp/DEBIAN
	$(installdoc) debian/conffiles debian/tmp/DEBIAN
	#
	$(installbin) -d debian/tmp/usr/share/doc/$(package){,/german}
	$(installdoc) debian/changelog debian/tmp/usr/share/doc/$(package)/changelog.Debian
	$(installdoc) debian/copyright debian/tmp/usr/share/doc/$(package)
	printf "Authors:\n\n" >> debian/tmp/usr/share/doc/$(package)/copyright
	cat doc/AUTHORS >> debian/tmp/usr/share/doc/$(package)/copyright
	echo >> debian/tmp/usr/share/doc/$(package)/copyright
	cat doc/THANKS >> debian/tmp/usr/share/doc/$(package)/copyright
	$(installdoc) doc/README{,.too,.fetchfile} debian/tmp/usr/share/doc/$(package)
	$(installdoc) doc/ChangeLog debian/tmp/usr/share/doc/$(package)/changelog
	$(installdoc) doc/features debian/tmp/usr/share/doc/$(package)/
	gzip -9f debian/tmp/usr/share/doc/$(package)/{changelog.Debian,changelog,README{,.too,.fetchfile},features}
	$(installdoc) doc/LIESMICH{,.auch,.neu,.entwickler,.spool} debian/tmp/usr/share/doc/$(package)/german
	$(installdoc) doc/doc.txt debian/tmp/usr/share/doc/$(package)
	$(installdoc) doc/{doku.txt,vorteile} debian/tmp/usr/share/doc/$(package)/german
	gzip -9f debian/tmp/usr/share/doc/$(package)/german/LIESMICH{,.auch,.neu,.entwickler}
	gzip -9f debian/tmp/usr/share/doc/$(package)/doc.txt
	gzip -9f debian/tmp/usr/share/doc/$(package)/german/{doku.txt,vorteile}
	#
	$(installbin) -d debian/tmp/usr/bin
	$(installbin) $(STRIP) src/{sendfile,receive,sendmsg,utf7encode,fetchfile,wlock} debian/tmp/usr/bin
	$(installbin) etc/check_sendfile debian/tmp/usr/bin/check-sendfile
	cd debian/tmp/usr/bin && ln -sf utf7encode utf7decode
	$(installbin) -d debian/tmp/usr/sbin
	$(installbin) $(STRIP) src/sendfiled debian/tmp/usr/sbin
	$(installbin) src/pussy debian/tmp/usr/bin
	$(installbin) etc/sfconf debian/tmp/usr/bin
	#
	$(installbin) -d debian/tmp/usr/share/man/man{1,7,8}
	$(installdoc) doc/{sendfile,sendmsg,receive,fetchfile}.1 debian/tmp/usr/share/man/man1
	$(installdoc) debian/pussy.1 debian/tmp/usr/share/man/man1
	$(installdoc) debian/sfconf.1 debian/tmp/usr/share/man/man1
	$(installdoc) debian/check-sendfile.1 debian/tmp/usr/share/man/man1
	$(installdoc) doc/utf7encode.1 debian/tmp/usr/share/man/man1
	$(installdoc) doc/wlock.1 debian/tmp/usr/share/man/man1
	$(installdoc) doc/sendfiled.8 debian/tmp/usr/share/man/man8
	$(installdoc) doc/fetchfile.7 debian/tmp/usr/share/man/man7
	gzip -9f debian/tmp/usr/share/man/man?/*
	cd debian/tmp/usr/share/man/man1 && ln -s utf7encode.1.gz utf7decode.1.gz
	#
	$(installbin) -d debian/tmp/etc/logrotate.d
	$(installbin) -d debian/tmp/etc/csh/login.d
	$(installdoc) debian/sendfile.cf debian/tmp/etc
	$(installdoc) debian/nosendfile debian/tmp/etc/sendfile.deny
	$(installbin) debian/logrotate debian/tmp/etc/logrotate.d/sendfile
	$(installbin) debian/csh debian/tmp/etc/csh/login.d/sendfile
	#
	$(installbin) -d debian/tmp/var/log/sendfile
	$(installbin) -d debian/tmp/var/spool/sendfile
	install -o root -g root -m 1777 -d debian/tmp/var/spool/sendfile/OUTGOING
	#
	dpkg-shlibdeps debian/tmp/usr/bin/{sendfile,receive,sendmsg,utf7encode,fetchfile,wlock}
	dpkg-gencontrol -isp
	dpkg --build debian/tmp ..

binary: binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b' or dsc; false

dsc:
	-test -d debian/tmp && $(MAKE) -f debian/rules clean
	if [ ! -f ../$(source)_$(version).orig.tar.gz -a -f ../orig/$(source)_$(version).orig.tar.gz ]; \
	then \
	  ln -s orig/$(source)_$(version).orig.tar.gz ../$(source)_$(version).orig.tar.gz; \
	  touch /tmp/stamp-$(source)-link; \
	fi; \
	cd .. && dpkg-source -b $(source)-$(version)
	if [ -f /tmp/stamp-$(source)-link ]; then \
	  rm ../$(source)_$(version).orig.tar.gz /tmp/stamp-$(source)-link; \
	fi

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

dist: binary dsc

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

