SHELL=/bin/sh


# Load in machine-specific environment settings from environment_config.make.
# This include file MUST be created and MUST define the following variables:
#
# Used in Makefile
# EPRINTS_ROOT: /path/to/eprints/installation
# EPRINTS_USER: the user that owns the EPrints directory
# EPRINTS_GROUP: the group that owns the EPrints directory
# APACHE_RESTART: the command to restart Apache
#
# Used in archive XML configuration files
# EPRINTS_HOST: eprints host name
# EPRINTS_ALIASES: eprints host aliases (if any, in full XML form)
# EPRINTS_PORT: appropriate HTTP port
# EPRINTS_ADMIN_EMAIL: administrator email address (quote "@" with \)
# EPRINTS_ARCHIVE_NAME: full archive name as it appears in web pages
# EPRINTS_DB_NAME: name of archive database
# EPRINTS_DB_USER: user for archive database
# EPRINTS_DB_PASS: password for archive database
#
# Used in apache .conf files
# EPRINTS_ALLOWED_DOMAINS: domains that web registration is allowed from
# EPRINTSSTATS: /path/to/EPrintsStats/installation
# PHPSURVEYOR: /path/to/phpSurveyor/installation (deprecated)
#
# Utilities:
# SUDO: command to run sudo as the eprints user
# GREP: command to run a grep that supports the --quiet switch
#
# Required commands:
# pgrep
#
include environment_config.make


XML_PL_SUBS:=EPRINTS_HOST EPRINTS_PORT EPRINTS_ADMIN_EMAIL \
	EPRINTS_ARCHIVE_NAME EPRINTS_DB_NAME EPRINTS_DB_USER EPRINTS_DB_PASS
CONF_SUBS:=EPRINTS_ROOT EPRINTS_ALLOWED_DOMAINS EPRINTSSTATS #PHPSURVEYOR

ARCHIVE_NAME:=otago_eprints

BINDIR:=$(EPRINTS_ROOT)/bin

ARCHIVE_ROOT:=$(EPRINTS_ROOT)/archives/$(ARCHIVE_NAME)
ARCHIVE_CONFIG:=$(ARCHIVE_NAME)/cfg/cfg.d

SOURCE_PL:=$(shell find . -name "*_src.pl" -type f)
SOURCE_CONF:=$(shell find . -name "*_src.conf" -type f)
GENERATED_XML:=$(subst _src,,$(SOURCE_PL))
GENERATED_CONF:=$(subst _src,,$(SOURCE_CONF))
GENERATED_FILES:=$(GENERATED_XML) $(GENERATED_CONF)


.PHONY: deploy generate_apacheconf import_subjects generate_static \
	generate_abstracts generate_views reload_config restart_apache \
	index #stats


deploy: $(GENERATED_FILES)
	@announce "Copying files..."
	@$(SUDO) rsync --verbose --cvs-exclude --exclude=Makefile \
		--exclude='*.make' --exclude='*_src.*' \
		--recursive --times . $(EPRINTS_ROOT)/archives > .del-rsync-output
	@-$(GREP) $(ARCHIVE_NAME) .del-rsync-output
	@-$(GREP) --quiet "cfg\\.d/\\(10_core\\)\\|\\(20_baseurls\\)" .del-rsync-output && $(MAKE) generate_apacheconf
	@-$(GREP) --quiet "cfg/subjects" .del-rsync-output && $(MAKE) import_subjects
	@-$(GREP) --quiet "cfg/\\(static\\|lang\\)/" .del-rsync-output && $(MAKE) generate_static
	@-$(GREP) --quiet "\\(cfg/subjects\\)\\|\\(cfg/citations\\)" .del-rsync-output && $(MAKE) generate_views
	@-$(GREP) --quiet "\\(cfg\\.d/eprint_render\\)\\|\\(cfg/citations\\)\\|\\(document_formats\\)" .del-rsync-output && $(MAKE) generate_abstracts
	@-$(GREP) --quiet "\\(\\.conf\\)\\|\\(\\.pl\\)\\|\\(\\.xml\\)" .del-rsync-output && $(MAKE) reload_config
	@rm -f .del-rsync-output


%.pl: %_src.pl environment_config.make
	@announce "Creating $@"
	@perl -p $(foreach SUB,$(XML_PL_SUBS),-e "s|##$(SUB)##|$($(SUB))|g;") \
		-e "s|##EPRINTS_ALIASES##|$(foreach A,$(EPRINTS_ALIASES),\n                  {\n                    'redirect' => 'yes',\n                    'name' => '$(A)'\n                  },)|g;" $< > $@

%.conf: %_src.conf environment_config.make
	@announce "Creating $@"
	@perl -p $(foreach SUB,$(CONF_SUBS),-e "s|##$(SUB)##|$($(SUB))|g;") $< > $@

# $(ARCHIVE_NAME).xml: $(ARCHIVE_NAME)_src.xml environment_config.make
# 	@announce "Creating $@"
# 	perl -p $(foreach SUB,$(CONFIG_SUBS),-e "s|##$(SUB)##|$($(SUB))|g;") \
# 		-e 's|##EPRINTS_ALIASES##|$(foreach A,$(EPRINTS_ALIASES),\n    <alias redirect="yes">$(A)</alias>)|g;' $< > $@
# 
# 
# $(ARCHIVE_NAME)/cfg/apachevhost.conf: $(ARCHIVE_NAME)/cfg/apachevhost_src.conf environment_config.make
# 	@announce "Creating $@"
# 	perl -p $(foreach SUB,$(VHOST_SUBS),-e "s|##$(SUB)##|$($(SUB))|g;") $< > $@


generate_apacheconf:
	@announce "generate_apacheconf"
	@$(SUDO) $(BINDIR)/$@


import_subjects:
	@announce "$@ $(ARCHIVE_NAME)"
	@$(SUDO) $(BINDIR)/$@ $(ARCHIVE_NAME) --xml $(ARCHIVE_NAME)/cfg/subjects.xml
# 	@$(SUDO) $(BINDIR)/import $(ARCHIVE_NAME) subject XML


generate_static generate_abstracts generate_views:
	@announce "$@ $(ARCHIVE_NAME)"
	@$(SUDO) $(BINDIR)/$@ $(ARCHIVE_NAME)


reload_config:
	@announce "Reloading configuration for $(ARCHIVE_NAME)..."
	@$(SUDO) $(BINDIR)/epadmin reload $(ARCHIVE_NAME)


restart_apache:
	@announce "Restarting Apache..."
	@sudo $(APACHE_RESTART)


index:
	@announce "Rebuilding indexes..."
	@$(SUDO) $(BINDIR)/indexer start --once --notdaemon


# stats:
# 	@announce "Generating statistics..."
# 	@php $(EPRINTS_ROOT)/ePrintsStats/scripts/eprints-usage.php
# #	@$(SUDO) perl $(EPRINTS_ROOT)/ePrintsStats/scripts/add_country.pl


debug:
	@echo "ARCHIVE_NAME = [$(ARCHIVE_NAME)]"
	@echo "ARCHIVE_ROOT = [$(ARCHIVE_ROOT)]"
	@echo "ARCHIVE_CONFIG = [$(ARCHIVE_CONFIG)]"
	@echo "GENERATED_FILES = [$(GENERATED_FILES)]"
