BASENAME := HAppSHSPQuickStart

MAIN := Main.lhs 
DEPS := State.lhs pages/Interface.lhs pages/Index.lhs pages/UACCT.lhs
EXTRA_DEPS := SimpleExample.lhs 
SRC := $(MAIN) $(DEPS)

all: $(BASENAME).html

$(BASENAME).html: Main.html
	tidy -q -o $@ Main.html 
	@echo ""
	@echo "********************************************************************************************"
	@echo "Open the following file in your browser, or run 'make open' to automatically open in firefox"
	@(echo file://$$(realpath $@))
	@echo "********************************************************************************************"

# open the .html file in firefox
# probably will not work if path contains spaces, etc
open: $(BASENAME).html
	(firefox -new-tab "file://$$(realpath $<)")

Main.html : $(SRC) $(EXTRA_DEPS)

EXTRA := template/App.cabal template/Setup.hs

template/% : extra/%
	cp $^ $@

TEMPLATE_FILES := $(subst .lhs,.hs,$(addprefix template/,$(SRC))) $(EXTRA)
template: $(TEMPLATE_FILES)

template/%.hs: %.lhs
	(mkdir -p `dirname $@`)
	cpp -DExtract -traditional-cpp -P $^ | grep -v '{-# OPTIONS_GHC -cpp #-}' | sed -n 's/^> \?//p' > $@

template/%: %
	cp $^ $@

test:
	runhaskell -cpp -ipages Main.lhs

%.html: %.cpphs
	HsColour -css $< -o$@ -lit
	validate -w --verbose --emacs $@


%.cpphs: %.lhs
	cpp -traditional-cpp -DHsColour -P $< $@

%.hs: %.lhs
	sed -n 's/^> \?//p' $^ > $@

clean:
	-rm -f $(BASENAME).html Main.html
	-rm -f $(TEMPLATE_FILES)
	(if test -d template/pages ; then rmdir template/pages ; fi)
	(if test -d template ; then rmdir template ; fi)

.PHONY: test all clean
