#! /usr/bin/make -f
#
# Invoke each target with `./debian/rules <target>'.  All targets should be
# invoked with the package root as the current directory.
#
# The `binary' target must be run as root, as it needs to install files with
# specific ownership.

PACKAGE         = acua
VERSION         = 3.01
ARCH            = i386
PKG             = $(PACKAGE)-$(VERSION)
SRC_PKG         = $(PKG).tar.gz
RPM_SPEC        = $(PKG).spec
RPM             = $(PKG)-1
RPM_PKG         = $(RPM).$(ARCH).rpm
SRPM_PKG        = $(RPM).src.rpm
CC              = g++
CCOPTS          = -Wall -O -g -DBIN=\"$(BIN)\" -DSBIN=\"$(SBIN)\" -DLIB=\"$(LIB)\"
SHLIB           = libacua.so.2
BINS            = $(ROOT_BINS) $(USER_BINS)
ROOT_SCRIPTS    = acua_adduser acua_deluser acua_nounsub
ROOT_BINS       = acua_login acua_logout acua_updated acua_verify clean_proc \
		  clean_tmp acua117-200 acua200-202
USER_SCRIPTS    = acua_stats acua_bytes acua_viewRec
USER_BINS       = acua
ACUA_OBJS       = acua_addRec acua_ban acua_daysLeft acua_delRec \
                  acua_dumpStats acua_dump acua_expire acua_forEach \
                  acua_kickUser acua_lock acua_modRec acua_purge \
                  acua_pp acua_renew acua_subscribe acua_sync \
                  acua_touch acua_timeLeft acua_unBan acua_unLock \
                  acua_unSubscribe
PREFIX          = /usr
BIN             = $(PREFIX)/bin
SBIN            = $(PREFIX)/sbin
LIB             = $(PREFIX)/lib/acua

build:
	$(checkdir)
# Builds the binary package.
	make depend
	make CC="$(CC)" CFLAGS="$(CCOPTS)"
	strip --strip-debug $(SHLIB)
	strip $(ROOT_BINS) $(USER_BINS)
	touch build

clean:
	rm -f *~ TAGS core *.o $(SHLIB) $(BINS) lib/*~ lib/TAGS doc/*~ doc/TAGS .depend build
	rm -f *.rpm *.gz *.bz2
	rm -rf debian/tmp

binary:	checkroot build
#
# Clean up
#
	rm -rf debian/tmp
#
# Create new temporary directories
#
	install -d debian/tmp debian/tmp/DEBIAN
#
# Create and populate documentation directory for this package
#
	install -d debian/tmp/usr/doc/$(PACKAGE)
	install -m 644 debian/changelog debian/tmp/usr/doc/$(PACKAGE)/changelog.Debian
	cp -R doc/* debian/tmp/usr/doc/$(PACKAGE)
#
# Compress document files
#
	gzip -9rf debian/tmp/usr/doc/$(PACKAGE)/*
#
# Make directorys we will need
#
	install -d debian/tmp/usr/bin debian/tmp/usr/sbin \
	     debian/tmp/usr/lib/acua
#
# Install binary files
#
	strip --strip-debug $(SHLIB)
	strip $(ROOT_BINS) $(USER_BINS)
	install -m 0755 $(SHLIB) debian/tmp/usr/lib
	install -m 0700 $(ROOT_SCRIPTS) debian/tmp/usr/sbin
	install -m 0700 $(ROOT_BINS) debian/tmp/usr/sbin
	install -m 0755 $(USER_SCRIPTS) debian/tmp/usr/bin
	install -m 0755 $(USER_BINS) debian/tmp/usr/bin
	chmod 6755 debian/tmp/usr/sbin/acua_login
	chmod 6755 debian/tmp/usr/sbin/acua_logout
	chmod 6755 debian/tmp/usr/sbin/acua_verify
#
# Install configuration files
#
	cp lib/* debian/tmp/usr/lib/acua
#
# Don't compress the copyright file
#
	cp LICENSE debian/tmp/usr/doc/$(PACKAGE)/copyright
#
# Install configuration scripts
#
	cp debian/preinst debian/tmp/DEBIAN/.
	chmod +x debian/tmp/DEBIAN/preinst
	cp debian/postinst debian/tmp/DEBIAN/.
	chmod +x debian/tmp/DEBIAN/postinst
	cp debian/prerm debian/tmp/DEBIAN/.
	cp debian/conffiles debian/tmp/DEBIAN/conffiles
	chmod +x debian/tmp/DEBIAN/prerm
#
# Set this to keep dpkg-shlibdeps happy
	OLD_LD_LIBRARY_PATH=$(LD_LIBRARY_PATH)
	export LD_LIBRARY_PATH="debian/tmp/usr/lib:$(OLD_LD_LIBRARY_PATH)"
#
# Declare dependencies
#
	dpkg-shlibdeps $(PACKAGE)
#
#
# Put the LD_LIBRARY_PATH back
#
	export LD_LIBRARY_PATH="$(OLD_LD_LIBRARY_PATH)"
#
# Generate control structures
#
	dpkg-gencontrol
#
# Asure proper permissions and ownership
#
	chown -R root.root debian/tmp
#
# Build the binary
#
	dpkg --build debian/tmp ..

checkroot:
	test root = "`whoami`"

.PHONY: binary binary-arch clean checkroot
