# Makefile for building and uploading website. 
# Karl Mowatt-Wilson - http://mowson.org/karl
# October 2007.
#
#----------------------------------------------------------------------------
# make all      = refresh the wiki.
# make refresh  = refresh the wiki.
# make rebuild  = rebuild the wiki.
# make clean    = clean out the target directory completely.
# make preview  = start local web server (webfsd).
# make upload   = upload to web server.
# make test     = do some sort of test!
#----------------------------------------------------------------------------
# Define programs and commands.
SHELL   = sh
COPY    = cp
REMOVE  = rm -f
REMOVEDIR = rm -rf
MKDIR   = mkdir
IKIWIKI = /usr/bin/ikiwiki
WEBFSD  = /usr/bin/webfsd 
WEBCHECK= /usr/bin/webcheck

#----------------------------------------------------------------------------
# define where I keep my source and destination directories for wiki building
PRJ = $(HOME)/prj
SRC = $(PRJ)/web/src.mowson.org
DST = $(PRJ)/web/mowson.org

# web host details for unison/ssh upload
HOST      = example.net
HOST_USER = MyUserName
HOST_DIR  = /html/example.net
HOST_PORT = 22
HOST_KEY  = $(HOME)/.ssh/private_key
HOST_KEY_FINGERPRINT = 00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd:ee:ff

# BASESITE gets copied verbatim to output dir when wiki is rebuilt
BASESITE    = $(SRC)/_basesite_
IKISRC      = $(SRC)
WEBCHECKDIR = $(SRC)/_webcheck_
IKIDST      = $(DST)

# WIPDIRS are WorkInProgress dirs which aren't linked from anywhere public, 
# so need to be manually specified for webcheck
WIPDIRS = $(shell find "$(IKIDST)" -type d -name 'wip-*' -printf "\"%p\" " )

# Ikiwiki setup
IKICFG  = --setup $(SRC)/_cfg_/ikiwiki.setup
# 
# these two arguments may have to be last!  (I think)
IKICFG += $(IKISRC)
IKICFG += $(IKIDST)
# add no more arguments after src/dst ?

WEBFSCFG = -r $(DST) -f index.html -e 10 

WEBCHECKCFG = --output $(WEBCHECKDIR)

#=========================================================================
all: refresh

cfg: $(IKIWIKI) $(PRJ) $(IKISRC) $(BASESITE)

rebuild: cfg clean
	$(MKDIR) -p $(DST)
	$(COPY) -ap $(BASESITE)/* $(DST)/.
	$(MKDIR) -p $(IKIDST)
	$(IKIWIKI) $(IKICFG) --rebuild 

refresh: cfg 
	$(IKIWIKI) $(IKICFG) --refresh

preview: $(WEBFSD) $(DST)
	@echo "Serving pages on localhost:8000"
	$(WEBFSD) $(WEBFSCFG)

clean: clean_list

clean_list:
	@echo
	@echo "Cleaning project:"
	$(REMOVEDIR) $(IKIDST)
	$(REMOVEDIR) $(DST)
	$(MKDIR) -p $(DST)

upload:
	##### Check/setup private key 
	@ssh-add -l | egrep '^[0-9]+ $(HOST_KEY_FINGERPRINT) ' || { \
	   ssh-add "$(HOST_KEY)" || { \
	      echo "ERROR: problem loading key into agent"; \
	      exit 1; \
	   }; \
	} 
	##### Check for index.html in all uploadable dirs
	[ -z "$(shell find $(DST) -type d ! -regex '.*/[._].*' ! -execdir [ -e "{}/index.html" ] \; -exec echo FAILED {} \;)" ]
	##### Fixing directory permissions so all can read
	@find $(DST) -type d     \
	    ! -regex '.*/[._].*' \
	         -exec chmod -c a+rx "{}" \;
	##### Fixing file permissions so all can read
	@find $(DST) -type f      \
	    ! -regex '.*/[._].*'  \
	    ! -regex '.*~'        \
	         -exec chmod -c a+r,go-w "{}" \;
	
	##### Upload
	@unison $(DST) ssh://$(HOST_USER)@$(HOST)/$(HOST_DIR) \
	 -sshargs '-p $(HOST_PORT)'          \
	 -sortnewfirst=true          \
	 -terse=true                 \
	 -log=true                   \
	 -auto=true                  \
	 -ignore 'Path */_*'         \
	 -ignore 'Path */.*'         \
	 -ignore 'Name _*'           \
	 -ignore 'Name .*'           \
	 -ignore 'Name *~'           
	#============ FINITO! ============#

webcheck: $(IKIDST) $(WEBCHECK)
	$(WEBCHECK) $(WEBCHECKCFG) --avoid-external $(WIPDIRS) $(IKIDST)

webcheckall: $(IKIDST) $(WEBCHECK)
	$(WEBCHECK) $(WEBCHECKCFG) $(WIPDIRS) $(IKIDST)

webchecklive: $(WEBCHECK)
	$(WEBCHECK) $(WEBCHECKCFG) http://www.$(HOST)

test:
	@echo "WIPDIRS: $(WIPDIRS)"

	
#=========================================================================
# Listing of phony targets.
.PHONY : all cfg clean preview rebuild refresh test upload webcheck webcheckall webchecklive 
