# DCloud makefile
# Author: Perette Barella <perette@deviousfish.com>
# Copyright 2018 Devious Fish.  All rights reserved.
# $Id: Makefile 35 2019-08-30 13:07:50Z perette $

# Code install locations
PREFIX?=~
BINDIR?=${PREFIX}/bin
MANDIR?=${PREFIX}/man

# Data installation directories
DATAPREFIX?=~
DOCUMENTS?=${DATAPREFIX}/Documents
CLOUD=${DOCUMENTS}/Cloud
WWWDIR?=${DATAPREFIX}/Sites/Cloud
REPODIR?=${DATAPREFIX}/Repositories
REPONAME?=cloud

# Determine version number from SVN.  If we can't, this isn't an error.
# (Script runs on clients, where there is no version number.)
VERSION=$(shell (svn info 2>/dev/null || echo Revision: Unknown) | grep Revision: | awk '{print $$2}')
DISTPATH=dcloud-${VERSION}

default:
	@echo "DCloud installer"
	@echo "No default target.  Useful targets are:"
	@echo "  install_svn       -- create SVN repository and install related utilities."
	@echo "  install_hg        -- create Mercurial repository and install related utilities."
	@echo "  install_standalone-- Install client and web page publisher utilities."
	@echo "  install_client    -- Install only the client utilities."
	@echo "  uninstall         -- Uninstall everything (but not data)."
	@echo "Installations will go to:"
	@echo "  prefix (PREFIX)               --> ${PREFIX}"
	@echo "    utilities (BINDIR)          --> ${BINDIR}"
	@echo "    manual pages (MANDIR)       --> ${MANDIR}"
	@echo "  data prefix (DATAPREFIX)      --> ${DATAPREFIX}"
	@echo "    HTML support (WWWDIR)       --> ${WWWDIR}"
	@echo "    repositories (REPODIR)      --> ${REPODIR}"
	@echo "    documents (DOCUMENTS)       --> ${DOCUMENTS}"
	@echo "      client data               --> ${CLOUD}"
	@echo "  repository name (REPONAME)    --> ${REPONAME}"
	@echo "Locations are based on the environment variables shown in parenthesis."
	@echo "Indenting represents composition."

DOCUMENTATION=README.md INSTALL.md COPYING AUTHORS.md ChangeLog
SERVER_UTILS=src/publish_cloud.py src/queue2ics.py src/tasks.py src/dcloud-post-commit
CLIENT_UTILS=src/pass src/pn src/now src/cloud
WEB_PAGES=web/index.web web/cloud-index.web web/calendar.web
WEB_CSS=web/cloud.css web/documents.css web/calendar.css web/todo.css
WEB_SCRIPTS=web/cloud.js web/calendar.js
WEB_TEMPLATES=web/template.m4 web/calendar-support.m4
WEB_ETC=web/Makefile web/devious-cloud.png
WEB_ALL=${WEB_PAGES} ${WEB_CSS} ${WEB_SCRIPTS} ${WEB_TEMPLATES} ${WEB_ETC}
MAN_1_PAGES=man/cloud.1 man/publish_cloud.1 man/queue2ics.1 man/tasks.1
MAN_5_PAGES=man/queue.5
MAN_PAGES=${MAN_1_PAGES} ${MAN_5_PAGES}
TEST_SCRIPTS=dcloud-test
TEST_DATA=TestData
TEST_GENERIC=$(TEST_DATA)/Generic
TESTS_GENERIC=\
	$(TEST_GENERIC)/queue.txt \
	$(TEST_GENERIC)/test_23_queue_to_html.html \
	$(TEST_GENERIC)/test_24_queue_to_html_with_timezone-east.html \
	$(TEST_GENERIC)/test_24_queue_to_html_with_timezone-west.html \
	$(TEST_GENERIC)/test_26_queue_to_ics_and_back.que

TEST_27=$(TEST_DATA)/test_27_queue_to_ics_with_details_file
TESTS_27=\
	$(TEST_27)/queue.txt \
	$(TEST_27)/detail-map.txt \
	$(TEST_27)/output.html

TEST_30=$(TEST_DATA)/test_30_now
TESTS_30=\
	$(TEST_30)/queue.txt \
	$(TEST_30)/width-50.stdout \
	$(TEST_30)/width-80.stdout \
	$(TEST_30)/complete.stdout \
	$(TEST_30)/date-range.stdout \
	$(TEST_30)/event-count.stdout \
	$(TEST_30)/incomplete.stdout \
	$(TEST_30)/not-complete.stdout \
	$(TEST_30)/unformatted.stdout \
	$(TEST_30)/matching.stdout \
	$(TEST_30)/tallying.stdout \
	$(TEST_30)/tallying-2.stdout

TEST_31=$(TEST_DATA)/test_31_pn
TESTS_31=\
	$(TEST_31)/full-word.stdout \
	$(TEST_31)/multi-entry.stdout \
	$(TEST_31)/multi-line.stdout \
	$(TEST_31)/multi-word.stdout \
	$(TEST_31)/partial-word.stdout \
	$(TEST_31)/phonelist.txt

TEST_41=$(TEST_DATA)/test_41_text_probe
TESTS_41=\
	$(TEST_41)/index.html \
	$(TEST_41)/plain-text-no-extension.html \
	$(TEST_41)/plain-text-with-extension.html \
	$(TEST_41)/plain-text-with-title.html
TESTS_41_INPUTS=\
	$(TEST_41)/Input/plain-text-no-extension \
	$(TEST_41)/Input/plain-text-with-extension.txt \
	$(TEST_41)/Input/plain-text-with-title.txt

TEST_42=$(TEST_DATA)/test_42_markdown_probe
TESTS_42=\
	$(TEST_42)/index.html \
	$(TEST_42)/filename.html \
	$(TEST_42)/metadata-format.html \
	$(TEST_42)/pound-heading.html \
	$(TEST_42)/underline.html
TESTS_42_INPUTS=\
	$(TEST_42)/Input/filename.md \
	$(TEST_42)/Input/metadata-format.txt \
	$(TEST_42)/Input/pound-heading.txt \
	$(TEST_42)/Input/underline.txt

TEST_43=$(TEST_DATA)/test_43_restructured_probe
TESTS_43=\
	$(TEST_43)/index.html \
	$(TEST_43)/filename.html \
	$(TEST_43)/metadata-format.html \
	$(TEST_43)/overline.html \
	$(TEST_43)/underline.html
TESTS_43_INPUTS=\
	$(TEST_43)/Input/filename.rst \
	$(TEST_43)/Input/metadata-format.txt \
	$(TEST_43)/Input/overline.txt \
	$(TEST_43)/Input/underline.txt \

TEST_47=$(TEST_DATA)/test_47_indexing
TESTS_47=\
	$(TEST_47)/index-name.html \
	$(TEST_47)/index-type.html
		
CONTRIBS=contrib/todone contrib/completed_mover

# Rework filenames to point to installation locations
UN_SERVER_UTILS=$(subst src/,${BINDIR}/,${SERVER_UTILS})
UN_CLIENT_UTILS=$(subst src/,${BINDIR}/,${CLIENT_UTILS})
UN_WEB_PAGES=$(subst web/,${WWWDIR}/,${WEB_ALL})
UN_MAN_1_PAGES=$(subst man/,${MANDIR}/man1/,${MAN_1_PAGES})
UN_MAN_5_PAGES=$(subst man/,${MANDIR}/man5/,${MAN_5_PAGES})
UN_MAN_PAGES=${UN_MAN_1_PAGES} ${UN_MAN_5_PAGES}




CREATE_DIR=mkdir -p
COPY=cp -p
INSTALL_DIRS=install -d -v
INSTALL_DATA=install -p -v -m 644
INSTALL_BINARIES=install -p -v
REMOVE=rm -f
REMOVE_DIR=rmdir
KSHCOMPILE=kshcompile -p 755

#
# INSTALLATION TARGETS
#

install_standalone: install_client install_webpage

# Install client utilities to user's ~/bin
install_client: install_manual
	${INSTALL_DIRS} ${BINDIR}
	${INSTALL_BINARIES} ${CLIENT_UTILS} ${BINDIR}

# Install manual pages
install_manual:
	${INSTALL_DIRS} ${MANDIR}/man1 ${MANDIR}/man5
	${INSTALL_DATA} ${MAN_1_PAGES} ${MANDIR}/man1
	${INSTALL_DATA} ${MAN_5_PAGES} ${MANDIR}/man5
	./src/pn --nroff > ${MANDIR}/man1/pn.1 2>&1 || [ $$? -eq 2 ]
	./src/pass --nroff > ${MANDIR}/man1/pass.1 2>&1 || [ $$? -eq 2 ]
	./src/now --nroff > ${MANDIR}/man1/now.1 2>&1 || [ $$? -eq 2 ]

# Utilities that convert cloud documents to HTML
install_html_utils:
	${INSTALL_DIRS} ${BINDIR}
	${INSTALL_BINARIES} ${SERVER_UTILS} ${BINDIR}

# Install the web page components
install_webpage: install_html_utils
	${INSTALL_DIRS} ${WWWDIR}
	${INSTALL_DIRS} ${WWWDIR}/Documents
	${INSTALL_DATA} ${WEB_ALL} ${WWWDIR}


# SVN support
install_svn: install_webpage install_repo_svn install_manual


install_repo_svn:
	${INSTALL_DIRS} ${REPODIR} ${DOCUMENTS}
	[ -d ${REPODIR}/${REPONAME} ] || (cd ${REPODIR} && svnadmin create ${REPONAME})
	[ -f ${REPODIR}/${REPONAME}/hooks/post-commit ] || (echo "#!/bin/ksh"; echo "export HOME='$$HOME'"; echo "export PATH='$$PATH'"; echo "export USER='$$USER'"; echo '~/bin/dcloud-post-commit "$$@"') > ${REPODIR}/${REPONAME}/hooks/post-commit
	chmod u+x ${REPODIR}/${REPONAME}/hooks/post-commit


# Mercurial support
install_hg: install_webpage install_repo_hg install_manual

install_repo_hg:
	${INSTALL_DIRS} ${REPODIR} ${DOCUMENTS}
	[ -d ${REPODIR}/${REPONAME} ] || (cd ${REPODIR} && hg init ${REPONAME})
	[ -f ${REPODIR}/${REPONAME}/.hg/hgrc ] || (echo "[hooks]"; echo "commit = ${BINDIR}/dcloud-post-commit --hg \"\$$HG_NODE\""; echo "incoming = ${BINDIR}/dcloud-post-commit --hg \"\$$HG_NODE\""; echo "[ui]"; echo "debug = false") > ${REPODIR}/${REPONAME}/.hg/hgrc
	[ -f $$HOME/.dcloudrc ] || echo "export PATH='$$PATH'" > $$HOME/.dcloudrc
	[ -d ${CLOUD} ] || hg clone ${REPODIR}/${REPONAME} ${CLOUD}


# Uninstall whatever has been installed (except user data)
uninstall:
	${REMOVE} ${UN_SERVER_UTILS}
	${REMOVE} ${UN_CLIENT_UTILS}
	${REMOVE} ${UN_WEB_PAGES}
	${REMOVE} ${UN_MAN_PAGES}
	${REMOVE} ${BINDIR}/dcloud-post-commit
	${REMOVE_DIR} ${BINDIR} || true
	${REMOVE_DIR} ${WWWDIR} || true
	${REMOVE} ${MANDIR}/man1/pn.1 ${MANDIR}/man1/pass.1 ${MANDIR}/man1/now.1
	${REMOVE_DIR} ${MANDIR}/man1 ${MANDIR}/man5 ${MANDIR} || true

#
# DEVELOPMENT TARGETS
#


# Bring together components for Cloud
gather: srcs webs contribs

srcs: src ${SERVER_UTILS} ${CLIENT_UTILS}


src:
	mkdir src
	
src/publish_cloud.py: ~/bin/publish_cloud.py
	${COPY} "$<" "$@"

src/queue2ics.py: ~/bin/queue2ics.py
	${COPY} "$<" "$@"

src/tasks.py: ~/bin/tasks.py
	${COPY} "$<" "$@"

src/pass: ~/bin/pass
	${KSHCOMPILE} -o "$@" "$<"

src/pn: ~/bin/pn
	${KSHCOMPILE} -o "$@" "$<"

src/now: ~/bin/now
	${KSHCOMPILE} -o "$@" "$<"

src/cloud: ~/bin/cloud
	${KSHCOMPILE} -o "$@" "$<"

src/dcloud-post-commit: ~/bin/dcloud-post-commit
	${KSHCOMPILE} -o "$@" "$<"

contribs: contrib ${CONTRIBS}

contrib:
	mkdir contrib

contrib/todone: ~/bin/todone
	${KSHCOMPILE} -o "$@" "$<"

webs:	web ${WEB_ALL}

web:
	mkdir web

WEBSRC=~/Sites/Cloud
web/Makefile: $(WEBSRC)/Makefile
	${COPY} "$<" "$@"

web/template.m4: $(WEBSRC)/template.m4
	${COPY} "$<" "$@"

web/cloud.css: $(WEBSRC)/cloud.css
	${COPY} "$<" "$@"

web/index.web: $(WEBSRC)/index.web
	${COPY} "$<" "$@"

web/cloud-index.web: $(WEBSRC)/cloud-index.web
	${COPY} "$<" "$@"

web/cloud.js: $(WEBSRC)/cloud.js
	${COPY} "$<" "$@"

web/calendar.js: $(WEBSRC)/calendar.js
	${COPY} "$<" "$@"

web/documents.css: $(WEBSRC)/documents.css
	${COPY} "$<" "$@"

web/calendar.web: $(WEBSRC)/calendar.web
	${COPY} "$<" "$@"

web/calendar.css: $(WEBSRC)/calendar.css
	${COPY} "$<" "$@"

web/todo.css: $(WEBSRC)/todo.css
	${COPY} "$<" "$@"

web/calendar-support.m4: $(WEBSRC)/calendar-support.m4
	${COPY} "$<" "$@"

web/devious-cloud.png: $(WEBSRC)/devious-cloud.png
	${COPY} "$<" "$@"

show_version:
	echo ${VERSION}

dist: gather
	rm -rf ${DISTPATH}
	${CREATE_DIR} ${DISTPATH}/src ${DISTPATH}/web ${DISTPATH}/man \
		${DISTPATH}/contrib \
		${DISTPATH}/${TEST_DATA} \
		${DISTPATH}/${TEST_GENERIC} \
		${DISTPATH}/${TEST_27} \
		${DISTPATH}/${TEST_30} \
		${DISTPATH}/${TEST_31} \
		${DISTPATH}/${TEST_41} ${DISTPATH}/${TEST_41}/Input\
		${DISTPATH}/${TEST_42} ${DISTPATH}/${TEST_42}/Input\
		${DISTPATH}/${TEST_43} ${DISTPATH}/${TEST_43}/Input\
		${DISTPATH}/${TEST_47}
	${COPY} Makefile ${DOCUMENTATION} ${DISTPATH}
	${COPY} ${SERVER_UTILS} ${CLIENT_UTILS} ${DISTPATH}/src
	${COPY} ${WEB_ALL} ${DISTPATH}/web
	${COPY} ${MAN_PAGES} ${DISTPATH}/man
	${COPY} ${CONTRIBS} ${DISTPATH}/contrib
	${COPY} ${TEST_SCRIPTS} ${DISTPATH}
	${COPY} ${TESTS_GENERIC} ${DISTPATH}/${TEST_GENERIC}
	${COPY} ${TESTS_27} ${DISTPATH}/${TEST_27}
	${COPY} ${TESTS_30} ${DISTPATH}/${TEST_30}
	${COPY} ${TESTS_31} ${DISTPATH}/${TEST_31}
	${COPY} ${TESTS_41} ${DISTPATH}/${TEST_41}
	${COPY} ${TESTS_41_INPUTS} ${DISTPATH}/${TEST_41}/Input
	${COPY} ${TESTS_42} ${DISTPATH}/${TEST_42}
	${COPY} ${TESTS_42_INPUTS} ${DISTPATH}/${TEST_42}/Input
	${COPY} ${TESTS_43} ${DISTPATH}/${TEST_43}
	${COPY} ${TESTS_43_INPUTS} ${DISTPATH}/${TEST_43}/Input
	${COPY} ${TESTS_47} ${DISTPATH}/${TEST_47}
	tar cfvz ${DISTPATH}.tar.gz ${DISTPATH}
	rm -rf ${DISTPATH}

distcheck: dist
	tar xf ${DISTPATH}.tar.gz
	cd ${DISTPATH} && ./dcloud-test
	rm -rf ${DISTPATH}
	

check:
	./dcloud-test

# Get rid of collected stuff
clean:
	${REMOVE} ${SERVER_UTILS} ${CLIENT_UTILS} ${WEB_ALL} ${CONTRIBS}
	${REMOVE_DIR} web contrib >/dev/null 2>&1 || true

