# There are plans to move to auto tools, this Makefile will work for now.

DocumentRoot = /should/be/set/or/autodetected
UseSymlinks = 0

all: check-dependencies
	@echo ""
	@echo "Please run one of the following commands:"
	@echo ""
	@echo "	make install  # autodetects DocumentRoot if possible"
	@echo "	make install DocumentRoot=/home/apache/html"
	@echo "	make install UseSymlinks=1  # don't copy anything, use symlinks"
	@echo ""
	@echo "This copies one file archzoom.cgi and creates one directory archzoom-data"
	@echo "inside the specified or autodetected DocumentRoot dir of your web server."
	@echo "Alternativelly, symlinks are used instead of copying."
	@echo ""

install: check-dependencies
	@DocumentRoot=$(DocumentRoot); \
	if [ ! -d "$$DocumentRoot" ]; then \
		for i in \
			/etc/httpd/conf/httpd.conf \
			/etc/apache/httpd.conf \
			/etc/apache2/sites-available/default \
			/var/www/conf/httpd.conf \
		; do \
			if [ -f $$i ]; then \
				DocumentRoot="`grep '^DocumentRoot' $$i | head -1 | cut -d' ' -f2- | tr -d '\"'`"; \
			fi; \
		done; \
	fi; \
	if [ ! -d "$$DocumentRoot" ]; then \
		echo "Can't autodetect web server's DocumentRoot dir, specify explicitely"; \
		exit 1; \
	fi; \
	echo "=============================================================="; \
	echo "Installing into $$DocumentRoot"; \
	echo ""; \
	rm -f "$$DocumentRoot/archzoom.cgi"; \
	rm -rf "$$DocumentRoot/archzoom-data"; \
	if [ $(UseSymlinks) = 0 ]; then \
		cp bin/archzoom.cgi "$$DocumentRoot/"; \
		mkdir "$$DocumentRoot/archzoom-data"; \
		cp -RL templates perllib conf var "$$DocumentRoot/archzoom-data/"; \
	else \
		ln -s "`pwd`/bin/archzoom.cgi" "$$DocumentRoot/archzoom.cgi"; \
		ln -s "`pwd`" "$$DocumentRoot/archzoom-data"; \
	fi; \
	echo "=============================================================="; \
	echo "$$DocumentRoot/archzoom.cgi is installed, define it as cgi-script in httpd.conf"; \
	echo ""

check-dependencies: 

# needed for development only; released tarballs include perllib/Arch inline
../arch-perl/perllib/Arch:
	@echo "No arch-perl found in the parent directory, needed for this project."
	@echo "Please execute the following command:"
	@echo "	tla get migo@homemail.com--Perl-GPL/arch-perl--devel--0 ../arch-perl"
	@exit 1

# needed for development only; released tarballs include the required revision
check-required-arch-perl-revision:
	@revision=`cat ./.required-arch-perl-revision`; \
	if [ ! -f ../arch-perl/\{arch\}/arch-perl/arch-perl--devel/arch-perl--devel--0/migo@homemail.com--Perl-GPL/patch-log/$$revision ]; then \
		echo "Your arch-perl tree is not up to date, at least $$revision is required"; \
		echo "Please execute the following command:"; \
		echo "	tla update -d ../arch-perl"; \
		exit 1; \
	fi
