#! /usr/bin/make -f

#   Copyright 1994-98   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.

# The name and version of the package
#
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')
#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 -o root -g root -m 755
installdoc = install -o root -g root -m 644

build:
	touch stamp-build

clean: debclean
	rm -rf stamp-build *~ 

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/{pre,post}inst debian/{post,pre}rm debian/tmp/DEBIAN/
	#
	$(installbin) -d debian/tmp/usr/lib/$(package)
	$(installbin) rc rcS debian/tmp/usr/lib/$(package)
	$(installbin) update-rc.d debian/tmp/usr/lib/$(package)
	$(installbin) rclink2file.sh rcfile2link.sh debian/tmp/usr/lib/$(package)
	#
	$(installbin) -d debian/tmp/usr/doc/$(package)
	$(installdoc) debian/copyright debian/tmp/usr/doc/$(package)
	cat COPYRIGHT >> debian/tmp/usr/doc/$(package)/copyright
	$(installdoc) README debian/tmp/usr/doc/$(package)
	$(installdoc) debian/README.debian debian/tmp/usr/doc/$(package)
	$(installdoc) debian/changelog debian/tmp/usr/doc/$(package)/changelog
	gzip -9f debian/tmp/usr/doc/$(package)/{README,changelog}
	#
	dpkg-gencontrol
	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

