#
#    WWWMaker
#    Copyright (C) 2001  Kai Puolamäki <Kai.Puolamaki@iki.fi>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# The public access directory where the files are installed.
TARGETDIR = $(HOME)/public_html/spam
WMLTARGETDIR = $(HOME)/public_html/wml/spam

# WML files and the HTML files generated from them.
WMLFILES := $(wildcard *.wml)
HTMLFILES := $(WMLFILES:.wml=.html)

# WML files and the PHP files generated from them.
PWMLFILES := $(wildcard *.pwml)
PHPFILES := $(PWMLFILES:.pwml=.php)

# The text files.
TXTFILES := $(wildcard *.txt)

# The installed files.
DISTFILES := $(HTMLFILES) $(PHPFILES) $(TXTFILES) default.css rblook.pl 
# Name of the tarball.
TARBALL := etvi.tar.gz
ZIPFILE := etvi.zip

INSTALL := install
WML := wml
TIDY := tidy -e -q

WMLOPTIONS =
PWMLOPTIONS = $(WMLOPTIONS)

.PHONY: all
all: html php

.PHONY: html
html: $(HTMLFILES)

.PHONY: php
php: $(PHPFILES)

%.html: %.wml page.wmltmpl
	$(WML) $(WMLOPTIONS) -s $< -o $@
	$(TIDY) $@ || mv -f $@ $@.rejected

%.php: %.pwml page.pwmltmpl
	$(WML) $(PWMLOPTIONS) -s $< -o $@

.PHONY: install
install: forceinstall wmlinstall

.PHONY: forceinstall
forceinstall: $(DISTFILES) $(TARBALL) $(ZIPFILE)
	for i in $(HTMLFILES) $(TXTFILES) default.css $(TARBALL) $(ZIPFILE) ; \
		do \
		$(INSTALL) -m 0644 $$i $(TARGETDIR)/$$i ; \
	done
	if [ -n "$(PHPFILES)" ] ; then \
		for i in $(PHPFILES) rblook.pl dummy ; do \
			[ $$i -ne "dummy" ] && \
			$(INSTALL) -m 0755 $$i $(TARGETDIR)/$$i ; \
		done ; \
	fi


.PHONY: wmlinstall
wmlinstall: $(WMLFILES) page.wmltmpl
	for i in $(WMLFILES) page.wmltmpl ; do \
		$(INSTALL) -m 0644 $$i $(WMLTARGETDIR)/$$i ; \
	done 

.PHONY: dist
dist: $(TARBALL) $(ZIPFILE)

$(TARBALL): $(DISTFILES)
	tar -czf $(TARBALL) $(DISTFILES)

$(ZIPFILE): $(DISTFILES)
	rm -f $(ZIPFILE)
	zip -q $(ZIPFILE) $(DISTFILES)

.PHONY: clean
clean:
	rm -f $(HTMLFILES) $(PHPFILES) *~

.PHONY: distclean
distclean: clean
	rm -f $(TARBALL) $(ZIPFILE)

.PHONY: test
test: $(HTMLFILES)
	for i in $(HTMLFILES) ; do \
		echo "Checking $$i: " ; \
		$(TIDY) $$i ; \
	done
