#! /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.,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# 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

build:
	for f in sdraw3 scalc3 schart3 simage3 smath3; do \
	    cat swriter3|sed "s/swriter3/$$f/g" > $$f; \
	done
	touch stamp-build

clean: debclean
	rm -f sdraw3 scalc3 schart3 simage3 smath3
	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) -d debian/tmp/usr/doc/$(package)
	$(installbin) debian/post{inst,rm} debian/tmp/DEBIAN
	$(installbin) debian/pre{inst,rm} debian/tmp/DEBIAN
	$(installdoc) debian/conffiles debian/tmp/DEBIAN
	$(installdoc) debian/copyright debian/tmp/usr/doc/$(package)/copyright
	#
	$(installdoc) debian/changelog debian/tmp/usr/doc/$(package)/changelog.Debian
#	$(installdoc) CHANGES debian/tmp/usr/doc/$(package)/changelog
#	$(installdoc) README debian/tmp/usr/doc/$(package)/readme
	$(installdoc) SO-Linux-FAQ debian/tmp/usr/doc/$(package)/faq
	$(installdoc) LICENCE.TXT debian/tmp/usr/doc/$(package)/licence
	$(installdoc) README.StarOffice debian/tmp/usr/doc/$(package)/readme
	$(installdoc) debian/important debian/tmp/usr/doc/$(package)
	gzip -9f debian/tmp/usr/doc/$(package)/{changelog.Debian,readme,licence,faq}
	#
	$(installbin) -d debian/tmp/usr/bin
	for f in swriter3 sdraw3 scalc3 schart3 simage3 smath3; do \
	  $(installbin) $$f debian/tmp/usr/bin; \
	done
	#
	$(installbin) -d debian/tmp/etc/init.d
	$(installbin) debian/rc debian/tmp/etc/init.d/staroffice
	#
	$(installbin) -d debian/tmp/usr/man/man1
	$(installdoc) debian/swriter3.1 debian/tmp/usr/man/man1
	gzip -9 debian/tmp/usr/man/man?/*
	for f in sdraw3 scalc3 schart3 simage3 smath3; do \
	  (cd debian/tmp/usr/man/man1 && \
	  ln -s swriter3.1.gz $$f.1.gz); \
	done
	#
	$(installbin) -d debian/tmp/usr/lib/menu/default
	$(installdoc) debian/menu debian/tmp/usr/lib/menu/default/$(package)
	#
	dpkg-gencontrol -pstaroffice3
	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: clean
	-test -d debian/tmp && $(MAKE) -f debian/rules debclean
	-test -f ../$(source)_$(version).orig.tar.gz -a \
	  -f ../orig/$(source)_$(version).orig.tar.gz \
	  || (ln -s orig/$(source)_$(version).orig.tar.gz ../$(source)_$(version).orig.tar.gz; \
	  touch /tmp/stamp-$(source)-link)
	cd .. && dpkg-source -b $(source)-$(version)
	-test -f /tmp/stamp-$(source)-link && \
	  (rm ../$(source)_$(version).orig.tar.gz /tmp/stamp-$(source)-link ) || true

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

dist: binary dsc

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

