#! /usr/bin/make -f
#
# Debian rules for building libpaper (by Yves Arrouye), based on:
#
# 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.)
# This file may have to be extensively modified

# There used to be `source' and `diff' targets in this file, and many
# packages also had `changes' and `dist' targets.  These functions
# have been taken over by dpkg-source, dpkg-genchanges and
# dpkg-buildpackage in a package-independent way, and so these targets
# are obsolete.

package=cgi-scripts

install-www = install -g www-data

build:
	$(checkdir)
	cd cgi-src; $(MAKE) CFLAGS=-O2 LDFLAGS=
	touch build

clean:
	$(checkdir)
	-rm -f build
	-cd cgi-src; $(MAKE) -i clean
	@if [ -d debian/tmp ]; then \
	  if [ "`whoami`" = root ]; then \
	    rm -rf *~ debian/tmp debian/*~ debian/files*; \
	  else \
	    if [ -x /usr/bin/ssh ]; \
	    then \
	      echo "Cleaning up the debian/tmp directory as root..."; \
	      ssh -l root localhost "cd `pwd`; 2>/dev/null set -o no_nomatch; rm -rf *~ debian/tmp debian/*~ debian/files*"; \
	    else \
	      echo "Enter the root password to clean up the debian/tmp dir..."; \
	      su -c "rm -rf *~ debian/tmp debian/*~ debian/files*"; \
	    fi; \
	  fi; \
	fi

binary-indep: build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: build
	$(checkdir)
	-rm -rf debian/tmp
	install -d debian/tmp debian/tmp/DEBIAN
	install -d debian/tmp/usr/doc/$(package)
	cp debian/{postinst,postrm} debian/tmp/DEBIAN/.
	chmod +x debian/tmp/DEBIAN/{postinst,postrm}
	$(install-www) -m 2770 -d debian/tmp/usr/lib/httpd/cgi-bin
	$(install-www) -c -s -m 750 cgi-bin/* \
	    debian/tmp/usr/lib/httpd/cgi-bin
	install -d debian/tmp/usr/sbin
	install -m 755 debian/cgiscriptsconfig debian/tmp/usr/sbin
	install -d debian/tmp/usr/man/man8
	install -m 644 debian/cgiscriptsconfig.8 debian/tmp/usr/man/man8
	install -d debian/tmp/usr/doc/examples
	install -d debian/tmp/usr/doc/$(package)/examples
	cd debian/tmp/usr/doc/examples; ln -fs ../$(package)/examples \
	    $(package)
	cp cgi-examples/* debian/tmp/usr/doc/$(package)/examples
	cp debian/copyright debian/tmp/usr/doc/$(package)/.
	cp debian/changelog debian/tmp/usr/doc/$(package)/changelog.Debian
	gzip -9v debian/tmp/usr/doc/$(package)/changelog.Debian
	gzip -9f debian/tmp/usr/man/man*/*.[0-9]*
	dpkg-shlibdeps cgi-bin/count
	dpkg-gencontrol
	#chown -R root.root debian/tmp
	#chmod -R g-ws debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f cgi-src/imagemap.c -a -f debian/rules
endef

# Below here is fairly generic really

binary:
	@if [ "`whoami`" = root ]; then \
	    $(MAKE) -f debian/rules binary-root; \
	else \
	    if [ -x /usr/bin/ssh ]; \
	    then \
		echo "Building the .deb file as root..."; \
		ssh -l root localhost "cd `pwd`; exec $(MAKE) -f debian/rules binary-root"; \
	    else \
	  	echo "Enter the root password to build the .deb file..."; \
	  	su -c "exec $(MAKE) -f debian/rules binary-root"; \
	    fi; \
	fi

binary-root: binary-indep binary-arch

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

.PHONY: binary binary-arch binary-indep clean

