DESTDIR =
BUILDSUBDIRS = src po
BIN_PERL_PROGS = perl/pqcheck perl/printquote perl/printbill_grapher
SBIN_PERL_PROGS = perl/pqm perl/printbilld perl/printbill perl/printbill.cups perl/printbill_printer perl/printbill_configure perl/init_from_passwd
PERL_MODS = perl/PTDB_File.pm perl/printbill.pm perl/printbill_pcfg.pm
MAN1_PAGES = man/percentblack.1 man/percentcolour.1 man/percent_cmy.1 man/pqcheck.1 man/printbill_printer.1 man/printquote.1 man/printbill.1 man/printbill.cups.1 man/printbilld.1
MAN5_PAGES = man/printbillrc.5
MAN8_PAGES = man/init_from_passwd.8 man/pqm.8 man/printbill.8
CGI_SCRIPTS = web/webpqadmin.pl web/webpqcheck.pl
CONFIG_FILES = config/extra_quota_message config/mail_message config/web_footer config/web_header config/web_message_text

include Config

progs:
	for i in $(BUILDSUBDIRS) ; do (cd $$i; $(MAKE)) || exit 1 ; done

clean:	
	for i in $(BUILDSUBDIRS) ; do (cd $$i; $(MAKE) clean) || exit 1 ; done

install: progs
	for i in $(BUILDSUBDIRS) ; do (cd $$i; DESTDIR=$(DESTDIR) TOPLEVEL_HOME=$(TOPLEVEL_HOME) OWNER=$(OWNER) GROUP=$(GROUP) LOCALEDIR=$(LOCALEDIR) $(MAKE) install) || exit 1 ; done

	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/bin
	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/sbin
	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill
	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man1
	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man5
	mkdir -p $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man8
	mkdir -p $(DESTDIR)$(PMOD_HOME)/Printbill
	
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0755 $(BIN_PERL_PROGS) $(DESTDIR)$(TOPLEVEL_HOME)/bin
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0755 $(SBIN_PERL_PROGS) $(DESTDIR)$(TOPLEVEL_HOME)/sbin
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 $(PERL_MODS) $(DESTDIR)$(PMOD_HOME)/Printbill
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 $(MAN1_PAGES) $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man1
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 $(MAN5_PAGES) $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man5
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 $(MAN8_PAGES) $(DESTDIR)$(TOPLEVEL_HOME)/$(MAN)/man8

	if [ ${DO_CGI} = "y" ] ; then\
		mkdir -p $(DESTDIR)$(CGI_BIN) ; \
		$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0755 $(CGI_SCRIPTS) $(DESTDIR)$(CGI_BIN) ; \
	fi;

	$(INSTALL) -d -m 0755 $(DESTDIR)/etc/printbill
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 644 $(CONFIG_FILES) $(DESTDIR)/etc/printbill
	
	$(INSTALL) -d -m 0755 -o $(OWNER) -g $(GROUP) $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 docs/* $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill
	$(INSTALL) -d -m 0755 -o $(OWNER) -g $(GROUP) $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill/examples
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0755 perl/buy perl/printbill_billme $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill/examples
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0644 examples/example_pq.html examples/printbillrc examples/printcap $(DESTDIR)$(TOPLEVEL_HOME)/share/doc/printbill/examples

	@echo
	@echo Installation completed. Assuming you didn\'t see any horrible error messages,
	@echo you should now configure your printbillrc and printcap files using 
	@echo "printbill_configure". Once this has been done you can run \"pqm --init\" to
	@echo initialise the databases. If you want to use the web interface it is suggested 
	@echo that you run \"pqm --updateprinters\" after you have configured your printcap 
	@echo file, so that they will be listed properly \(if you don\'t, printers will only 
	@echo be listed after something has been printed on them\). Also note that before 
	@echo anyone may print they must be added to the system and given some quota via the
	@echo commands \"pqm --add username\" and \"pqm --inc username --amount 10\" \(e.g. 
	@echo -e for \\04410 worth of quota\).
	@echo

install-init: install-init-script install-init-link

install-init-script:
	$(INSTALL) -d -m 0755 -o $(OWNER) -g $(GROUP) $(DESTDIR)$(INITDIR)
	$(INSTALL) -o $(OWNER) -g $(GROUP) -m 0755 $(INITSCRIPTDIR)/$(INITSCRIPT) $(DESTDIR)$(INITDIR)

# This one should be absolute - RedHat & Debian use the same

install-init-link:
	if [ ${INITSTYLE} = "sysv" ] ; then\
		$(INSTALL) -d -m 0755 -o $(OWNER) -g $(GROUP) $(DESTDIR)$(SYSVLINKDIR) ; \
		ln -sf ../init.d/$(INITSCRIPT) $(DESTDIR)$(SYSVLINKDIR)/$(SYSVLINK) ; \
	fi;
