
# Makefile to install foomatic
# $Revision: 1.32 $

# PREFIX defaults to /usr/local for manually installed progs, so that they
# are not messed up on a system upgrade.
#
# `architecture independent', static data files i.e. perl libs go into
#   $(PREFIX)/share/foomatic
# (user) executables into $(PREFIX)/bin/
# system binaries go into $(PREFIX)/sbin
# configuration files into /etc/foomatic/*.
# generated data files into /var/share/foomatic
#
# The PERLPREFIX allows a seperate prefix for the Perl libraries. Use this
# when Perl libraries in /usr/local are not found.

PREFIX=/usr/local
PERLPREFIX=$(PREFIX)

LIBDIR=$(PREFIX)/share/foomatic
BINDIR=$(PREFIX)/bin
SBINDIR=$(PREFIX)/sbin
MANDIR=$(PREFIX)/share/man
ETCDIR=/etc/foomatic
#CACHEDIR=/var/cache/foomatic

## Settings for the libxml C library
LIBXML=xml
LIBXMLCONFIG=/usr/bin/$(LIBXML)-config
LIBXMLCONFIGLINE=$(LIBXMLCONFIG) --cflags

## Settings for Perl
# Where to find the perl binary
PERL=/usr/bin/perl
# How to install the perl libs, may be either "site" or "perl" 
# details in the ExtUtils::MakeMaker manpage
PERL_INSTALLDIRS=site

## Settings for C
CC=gcc
CFLAGS=-O3

# Genereal paths for all filter scripts
EXEC_PATH=/usr/local/bin:/usr/bin:/bin
LOG_PATH=/tmp

# Paths for LPD and LPRng (without magicfilter)
LPD_SPOOL=/var/spool/lpd
LPD_LOG=/var/log/lp-errs
LPD_BIN=/usr/sbin/lpd
PRINTCAP=/etc/printcap
LPRNG_CONF=/etc/lpd.conf
LPD_LPR=/usr/bin/lpr
LPD_LPQ=/usr/bin/lpq
LPD_LPRM=/usr/bin/lprm
LPD_LPC=/usr/sbin/lpc
LPRNG_CHECKPC=/usr/sbin/checkpc

# Paths and other settings for CUPS
CUPS_ETC=/etc/cups
CUPS_ADMIN=/usr/sbin/lpadmin
CUPS_LPSTAT=/usr/bin/lpstat
CUPS_PPDS=/usr/share/cups/model
CUPS_FILTERS=/usr/lib/cups/filter
CUPS_BACKENDS=/usr/lib/cups/backend
CUPS_PCONF=$(CUPS_ETC)/printers.conf
CUPS_LPR=/usr/bin/lpr
CUPS_LPQ=/usr/bin/lpq
CUPS_LPRM=/usr/bin/lprm
CUPS_LPC=/usr/sbin/lpc
CUPS_LP=/usr/bin/lp
CUPS_CANCEL=/usr/bin/cancel
CUPS_ENABLE=/usr/bin/enable
CUPS_DISABLE=/usr/bin/disable
CUPS_ACCEPT=/usr/sbin/accept
CUPS_REJECT=/usr/sbin/reject
CUPS_LPMOVE=/usr/sbin/lpmove
CUPS_LPOPTIONS=/usr/bin/lpoptions

# Paths for PDQ
PDQ_CONF=/usr/lib/pdq
PDQ_PRINTRC=$(PDQ_CONF)/printrc
PDQ_FOOMATIC=$(PDQ_CONF)/drivers/foomatic
PDQ_PRINT=/usr/bin/pdq
PDQ_JOBDIR=~/.printjobs

# Paths for PPR
PPR_INTERFACES=/usr/lib/ppr/interfaces
PPR_PPDFILES=/usr/share/ppr/PPDFiles
PPR_ETC=/etc/ppr
PPR_PPR=/usr/bin/ppr
PPR_PPAD=/usr/bin/ppad
PPR_PPOP=/usr/bin/ppop

# Paths for direct, spoolerless printing
DIRECT_ETC=/etc/foomatic/direct
DIRECT_CONFIG=$(DIRECT_ETC)/.config

# Other paths
NC=/usr/bin/nc
RLPR=/usr/bin/rlpr
SMBCLIENT=/usr/bin/smbclient
NPRINT=/usr/bin/nprint
PTAL_CONNECT=/usr/bin/ptal-connect
CAT=/bin/cat

# This is mainly useful for building a binary foomatic package
INSTALLPREFIX=/

### Probably nothing to fiddle past here

# User programs and helper programs
BINFILES:=foomatic-gswrapper foomatic-printjob foomatic-configure \
	foomatic-combo-xml foomatic-perl-data directomatic \
	foomatic-datafile foomatic-compiledb

# Filters and administrative commands, only useful for admins
SBINFILES:=foomatic-kitload foomatic-ppdload \
	foomatic-getpjloptions foomatic-addpjloptions \
	foomatic-preferred-driver foomatic-fix-xml lpdomatic

# Masks for trash files which have to be removed before packaging Foomatic
TRASHFILES:="*~" "*\#*" ".??*" "*.rej"

all: build

# The install rule should check for kitloads and avoid stomping.  It doesn't
install: install-bin install-man install-db

check-config:
	@if [ -f .testing-stamp ] ; then \
	   echo 'Cowardly refusing to install testing Defaults.pm.' ;\
	   echo 'Use make testing_clean; make defaults to get a proper one.' ;\
	   exit 1 ;\
	fi

install-bin: check-config lib/Makefile
#	This is the only way to get the Perl-generated Makefile using
#	$(INSTALLPREFIX) properly
	( cd lib && \
	  $(MAKE) PREFIX=$(INSTALLPREFIX)$(PERLPREFIX) && \
	  $(MAKE) PREFIX=$(INSTALLPREFIX)$(PERLPREFIX) pure_install \
	)
	install -d $(INSTALLPREFIX)$(BINDIR)
	install -d $(INSTALLPREFIX)$(SBINDIR)
	install -d $(INSTALLPREFIX)$(ETCDIR)
	install -d $(INSTALLPREFIX)$(CUPS_FILTERS)
	install -d $(INSTALLPREFIX)$(PPR_INTERFACES)
	cp -f $(BINFILES) $(INSTALLPREFIX)$(BINDIR)
	cp -f $(SBINFILES) $(INSTALLPREFIX)$(SBINDIR)
	cp -f cupsomatic $(INSTALLPREFIX)$(CUPS_FILTERS)
	cp -f ppromatic $(INSTALLPREFIX)$(PPR_INTERFACES)
	cp -f filter.conf $(INSTALLPREFIX)$(ETCDIR)

install-man: check-config lib/Makefile
	install -d $(INSTALLPREFIX)$(MANDIR)
	install -d $(INSTALLPREFIX)$(MANDIR)/man1
	install -d $(INSTALLPREFIX)$(MANDIR)/man8
	cp -f *.1 $(INSTALLPREFIX)$(MANDIR)/man1
	cp -f *.8 $(INSTALLPREFIX)$(MANDIR)/man8

install-db: check-config lib/Makefile
	install -d $(INSTALLPREFIX)$(LIBDIR)
#	install -d $(INSTALLPREFIX)$(CACHEDIR)
	tar cf - --exclude=CVS db | ( cd $(INSTALLPREFIX)$(LIBDIR) && tar xf -)
#	( cd $(INSTALLPREFIX)$(LIBDIR)/db && ln -sf $(CACHEDIR)/compiled . )
#	( cd $(INSTALLPREFIX)$(LIBDIR)/db && ln -sf $(CACHEDIR)/pcache . )

build: defaults lib/Makefile foomatic-combo-xml foomatic-perl-data scripts man

lib/Makefile: lib/Makefile.PL
	( cd lib && $(PERL) Makefile.PL verbose INSTALLDIRS=$(PERL_INSTALLDIRS) )

foomatic-combo-xml: foomatic-combo-xml.c
	$(CC) $(CFLAGS) -o foomatic-combo-xml foomatic-combo-xml.c

foomatic-perl-data: foomatic-perl-data.c
	if [ ! -x $(LIBXMLCONFIG) ] ; then \
	   echo 'The configuration tool of libxml, $(LIBXMLCONFIG)' ;\
	   echo 'could not be found. Please check whether you have installed' ;\
	   echo 'libxml, please see the USAGE file for more info' ;\
	   exit 1 ;\
	fi ;\
	$(CC) $(CFLAGS) `$(LIBXMLCONFIGLINE)` -l$(LIBXML) -o foomatic-perl-data foomatic-perl-data.c

man:
	./makeMan

clean: lib/Makefile remove_trash
	$(MAKE) -C lib clean
	$(MAKE) -C src clean
	rm -f lib/Makefile.old
	rm -f lib/Foomatic/Defaults.pm
	rm -f *~ .testing-stamp
	rm -f foomatic-combo-xml
	rm -f foomatic-perl-data
	$(RM) $(BINFILES:=.1) $(SBINFILES:=.8)

# Various testing/debugging/etc targets
testing: INPLACE = --inplace
testing: defaults foomatic-combo-xml foomatic-perl-data scripts
	ln -sf lib/Foomatic .
#	mkdir -p cache
#	( cd db && ln -sf ../cache/compiled . )
#	( cd db && ln -sf ../cache/pcache . )

testing_clean: clean
	rm -f Foomatic
#	rm -rf cache
#	( cd db && rm -f compiled pcache )

# Remove editor backup and temporary files
remove_trash:
	for m in $(TRASHFILES); do \
	  find . -name "$$m" -exec rm "{}" \; ; \
	done

# We need to export all Variables for makeDefaults and the scripts target to
# work.
.EXPORT_ALL_VARIABLES:

scripts:
	$(MAKE) -C src scripts

# Use INPLACE=--inplace to get the special run-in-place Defaults.
defaults: lib/Foomatic/Defaults.pm

lib/Foomatic/Defaults.pm: Makefile makeDefaults
	./makeDefaults $(INPLACE)
	if [ x$(INPLACE) = x--inplace ] ; then touch .testing-stamp ; fi

.PHONY: all defaults check-config build install install-bin install-db \
	man testing clean testing_clean
