PACKAGE=pianod-client
DOCUMENTS=README INSTALL COPYING
IMAGES = no-art.jpeg pianod-icon.gif pianod-button.gif
JQUERY = jquery-2.1.0.min.js
CSSFILES=viewer.css client.css

VIEWS = activityview.m4 adminview.m4 loginview.m4 seedview.m4 stationview.m4 \
	trackview.m4 userview.m4
JAVASCRIPTS = communication.js viewmanager.js $(VIEWS:.m4=.js)
SOURCES=index.web ${VIEWS} html-min.m4

VIEWERSOURCES=viewer.web
VIEWERJS = viewer.js

ALLJS = ${JAVASCRIPTS} ${VIEWERJS} ${JQUERY}
ALLFILES=${DOCUMENTS} ${SOURCES} ${VIEWERSOURCES} ${ALLJS} ${CSSFILES} ${IMAGES} Makefile
HOST=$(shell uname -n | cut -d. -f1)

# Compile programs
M4 = m4 -P
TIDY=tidy -q -utf8 --doctype auto --preserve-entities yes --anchor-as-name no
MINIFY=uglifyjs

# Unless you're also running your own validator.nu, you'll want to use
# the W3C's validator service.
VALIDATOR=validator.w3.org/check
VALIDATE=curl -s -F uploaded_file=@index.html -F output=soap12 ${VALIDATOR}
VIEWERVALIDATE=curl -s -F uploaded_file=@viewer.html -F output=soap12 ${VALIDATOR}
# Be polite and use my local validator
ifeq (${HOST},stefanie)
VALIDATE=validate index.html
VIEWERVALIDATE=validate viewer.html
endif

all:	index.html viewer.html

index.html: ${SOURCES}
	${M4} index.web > index.mid
	${TIDY} index.mid > index.html
	rm -f index.mid
	${VALIDATE}

viewer.html: ${VIEWERSOURCES}
	${M4} viewer.web > viewer.mid
	${TIDY} viewer.mid > viewer.html
	rm -f viewer.mid
	${VIEWERVALIDATE}

compress: compressclient compressviewer

compressclient:
	cat ${JAVASCRIPTS} > client-engine.js
	${MINIFY} client-engine.js > client-engine.min.js
	${M4} -D_COMPRESSED index.web > index.mid
	${TIDY} index.mid > index.html
	rm -f index.mid client-engine.js
	${VALIDATE}

compressviewer:
	cat communication.js ${VIEWERJS} > viewer-engine.js
	${MINIFY} viewer-engine.js > viewer-engine.min.js
	${M4} -D_COMPRESSED viewer.web > viewer.mid
	${TIDY} viewer.mid > viewer.html
	rm -f viewer.mid viewer-engine.js
	${VIEWERVALIDATE}

clean:
	rm -f index.html index.mid client-engine.js client-engine.min.js
	rm -f viewer.html viewer.mid viewer-engine.js viewer-engine.min.js
	rm -f ${PACKAGE}-[0-9]*.tar.gz

dist:
	VERSION=$$(svn info http://svn.deviousfish.com/websites | grep Revision: | awk '{print $$2}') && \
	rm -rf ${PACKAGE}-$${VERSION} && \
	mkdir ${PACKAGE}-$${VERSION} && \
	cp ${ALLFILES} ${PACKAGE}-$${VERSION} && \
	tar cfvz ${PACKAGE}-$${VERSION}.tar.gz ${PACKAGE}-$${VERSION} && \
	rm -rf ${PACKAGE}-$${VERSION}

distpkg: compress
	VERSION=$$(svn info http://svn.deviousfish.com/websites | grep Revision: | awk '{print $$2}') && \
	rm -rf ${PACKAGE}-compiled-$${VERSION} && \
	mkdir ${PACKAGE}-compiled-$${VERSION} && \
	cp ${DOCUMENTS} ${IMAGES} ${CSSFILES} \
	   index.html client-engine.min.js \
	   viewer.html viewer-engine.min.js ${PACKAGE}-compiled-$${VERSION} && \
	tar cfvz ${PACKAGE}-compiled-$${VERSION}.tar.gz ${PACKAGE}-compiled-$${VERSION} && \
	rm -rf ${PACKAGE}-compiled-$${VERSION}

distcheck:
	VERSION=$$(svn info http://svn.deviousfish.com/websites | grep Revision: | awk '{print $$2}') && \
	tar xfvz ${PACKAGE}-$${VERSION}.tar.gz && \
	cd ${PACKAGE}-$${VERSION} && make && cd .. && \
	rm -rf ${PACKAGE}-$${VERSION} && \
	echo "${PACKAGE}-$${VERSION} appears ready for distribution."

