NOW!= TZ=UTC date +%Y%m%D.%H%M%S
THISYEAR:= ${NOW:C/(....).*/\1/}

# Some targets test that THISHOST has an expected value,
# and we sometimes use different directory names
# depending on which host is running "make".
THISHOST!= hostname
THISDIR!= pwd
WWWHOST= noc.ws.afnog.org
CDMASTERHOST= noc.ws.afnog.org

#PROXY= --proxy http://127.0.0.1:3128/

# If ${hostname} is not the local host, then
#	RSYNCHOST.${hostname} is "${hostname}:"
#	SSHCMD.${hostname} is "ssh ${hostname}"
.for hostname in ${WWWHOST} ${CDMASTERHOST}
.  if ${THISHOST} == ${hostname}
RSYNCHOST.${hostname} := #empty
SSHCMD.${hostname} := #empty
.  else
RSYNCHOST.${hostname} := ${hostname:Q}:
SSHCMD.${hostname} := ssh ${hostname:Q}
.  endif
.endfor

# The master files live in WWWDIR on WWWHOST.
WWWDIR= /vol/www/vhosts/www.ws.afnog.org/data

# Things that make or use ISO images work from CDMASTERDIR on CDMASTERHOST
CDMASTERPARENT= /u3/cdprep
CDMASTERDIR= ${CDMASTERPARENT}/wwwcopy
CDIMAGE= ${CDMASTERPARENT}/afnog${THISYEAR}.iso
MKISOFSLOG= ${CDMASTERPARENT}/mkisofs.log

# For testing, we like to mount the CDIMAGE on the MOUNTPOINT on CDMASTERHOST
MOUNTPOINT:= /vol/www/vhosts/cdrom.ws.afnog.org/data
MOUNTPOINT!= ( cd ${MOUNTPOINT:Q} ; /bin/pwd -P )

# TOPDIR is either WWWDIR or CDMASTERDIR, depending on which host
# we are on.
.if ${WWWHOST} == ${CDMASTERHOST}
# Guess whether to use WWWDIR or CDMASTERDIR
.  if defined(MODE) && ${MODE} == www
TOPDIR:= ${WWWDIR}
.  elif defined(MODE) && ${MODE} == cd
TOPDIR:= ${CDMASTERDIR}
.  else
.    if !empty(THISDIR:M*/vol/www*)
TOPDIR:= ${WWWDIR}
.    elif !empty(THISDIR:M*u*/cdprep*)
TOPDIR:= ${CDMASTERDIR}
.    else
TOPDIR:= /error
.    endif
.  endif
.else # WWWHOST != CDMASTERHOST
TOPDIR.${WWWHOST} = ${WWWDIR}
TOPDIR.${CDMASTERHOST} = ${CDMASTERDIR}
TOPDIR:= ${TOPDIR.${THISHOST}}
.endif

# CDPREPDIR is a subdirectory of WWWDIR on WWWHOST.
# There's also a copy in CDMASTERPARENT on CDMASTERHOST.
CDPREPDIR.www= ${WWWDIR}/setup/cdprep
CDPREPDIR.cdmastercopy = ${CDMASTERPARENT}
CDPREPDIR.localcopy = ${TOPDIR}/setup/cdprep

# The "copy" target in this Makefile copies stuff from COPYSRC to COPYDEST,
# under control of COPYINCLUDELIST,
# and touches the COPYTIMESTAMP.
SRCHOST= ${WWWHOST}
COPYSRC= ${RSYNCHOST.${WWWHOST}}${WWWDIR}
COPYTIMESTAMP= ${CDPREPDIR.www}/copy.timestamp
COPYDEST= ${RSYNCHOST.${CDMASTERHOST}}${CDMASTERDIR}
COPYINCLUDELIST= ${CDPREPDIR.localcopy}/include-list

# CD-ROM label and related variables used by the "image" target.
#
# VOLI [32 chars]: Volume Identifier.
# (Try to stay within 11 chars for MS-DOS.)
VOLI=AfNOG ${THISYEAR} WS
# PREP [128 chars]: Preparer.
# This defaults to information about the mkisofs program.
PREP=AfNOG ${THISYEAR} Workshop on Network Technology, http://www.ws.afnog.org/
# PUBL [128 chars]: Publisher.
PUBL=AfNOG, African Network Operators Group, http://www.afnog.org/
# COPY [37 chars]: Copyright info, often a file name.
COPY=00readme.html

# PRUNEDIRS and friends are used with the "find" command to exclude
# things that don't need to be checked by the various check targets
# in this Makefile
PRUNEDIRS= \( -path ./previous -o -path ./local \
		-o -path ./inet2000 \
		-o -path ./pre-SANOG-VI \
		-o -path './afnog200[012345]' \
		-o -path ./mrtg \
		\)
PRUNERCS= \( -name RCS -type d \)
PRUNESTUFF= \( ${PRUNEDIRS} -o ${PRUNERCS} \)

default:
	@echo no default target

copy: copy-www
copy-all: copy-cdprep copy-www

copy-cdprep:
	[ "${THISHOST}" = "${SRCHOST}" ] || [ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	rsync -av --stats --delete \
		--exclude /wwwcopy --exclude \*.iso --exclude \*.log \
		${RSYNCHOST.${WWWHOST}}${CDPREPDIR.www}/ \
		${RSYNCHOST.${CDHOST}}${CDPREPDIR.cdmastercopy}/

copy-www:
	[ "${THISHOST}" = "${SRCHOST}" ] || [ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	${SSHCMD.${SRCHOST}} touch ${COPYTIMESTAMP}
	rsync -av --stats --include-from ${COPYINCLUDELIST} \
		--delete --delete-excluded \
		 ${COPYSRC}/ ${COPYDEST}/

findrecent:
	[ "${THISHOST}" = "${SRCHOST}" ] || false
	find ${WWWDIR} -newer ${COPYTIMESTAMP} -print

MKISOFS_VERBOSE=-v
MKISOFS_ROCKRIDGE=-r -hide-rr-moved
MKISOFS_JOLIET=-J -hide-joliet-trans-tbl
# -T: use TRANS.TBL files
# -pad: add extra 32k padding
# -f: follow symlinks
# -l: allow long file names (bad for MSDOS, good for Amiga,
#				irrelevant for Rockridge (unix),
#				irrelevant for Joliet (Windows))
MKISOFS_COMPAT=-T -pad -f -l
image:
	[ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	mkisofs ${MKISOFS_VERBOSE} \
		${MKISOFS_ROCKRIDGE} \
		${MKISOFS_JOLIET} \
		${MKISOFS_COMPAT} \
		-V ${VOLI:Q} \
		-p ${PREP:Q} \
		-publisher ${PUBL:Q} \
		-copyright ${COPY:Q} \
		-log-file ${MKISOFSLOG:Q}.tmp \
		-o ${CDIMAGE:Q}.tmp \
		${CDMASTERDIR:Q}
	mv ${CDIMAGE:Q}.tmp ${CDIMAGE:Q}
	mv ${MKISOFSLOG:Q}.tmp ${MKISOFSLOG:Q}
	ls -l ${CDIMAGE:Q}

CDDEV=/dev/acd0c
CDSPEED=8
CDTESTMODE= -t
burncd:
	burncd -f /dev/acd0c -s ${CDSPEED} ${CDTESTMODE} data ${CDIMAGE:Q} fixate

check: findbadnames findbadperms finddotfiles findsymlinks findbakfiles \
	findabslinks finddirslash findfixme
checkall: check checkbot # weblint

findbadperms:
	@echo 'Looking for files with bad permissions'
	@cd ${TOPDIR} && find . \! \( ${PRUNESTUFF} -prune \) \
		\( \( -type d \! -perm -0555 \) \
		       -o \( -type f \! -perm -0444 \) \) \
		-print

findbadnames:
	@echo 'Looking for files with bad characters in their names'
	@cd ${TOPDIR} && find . \! \( ${PRUNESTUFF} -prune \) \
		-name '*[!-._a-zA-Z0-9]*' -print

finddotfiles:
	@echo 'Looking for files with names that start with "."'
	@cd ${TOPDIR} && find . \! \( ${PRUNESTUFF} -prune \) \
		-name '.*' -print

findbakfiles:
	@echo 'Looking for old/bak/core/twiddle/RCS files'
	@cd ${TOPDIR} && find . ${PRUNEDIRS} -prune \
		-o \( -name \*bak -o -name \*old -o -name \*OLD \
		-o -name \*\~\* \
		-o -name RCS -o -name \*,v \
	 	-o -name \*core\* \) -print

#findhtmlfiles:
#	@echo 'Looking for *.html files (please use *.htm instead)'
#	@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
#		-o \( -name \*.html \) -print

findabslinks:
	@echo 'Looking for absolute links to *.ws.afnog.org/* (please use relative links)'
	-@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -name \*.htm -o -name \*.html \) -print \
	 | xargs egrep -i '(href|src)=[^>]*ws.afnog.org/[^"'\'']'
	@echo 'Looking for semi-relative links to "/*" (please use proper relative links)'
	-@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -name \*.htm -o -name \*.html \) -print \
	 | xargs egrep -i '(href|src)=.?/'

finddirslash:
	@echo 'Looking for links to directories with trailing slash (please use */index.html)'
	-@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -name \*.htm -o -name \*.html \) -print \
	 | xargs egrep -i '(href|src)=.[^:">]*/["'\'']'

findsymlinks:
	@echo 'Looking for symlinks (these will not work in an ISO9660 CD-ROM)'
	@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -type l \) -ls | perl -lane 'print join " ", @F[10..12]'

findfixme:
	@echo 'Looking for TODO or <foo class="fixme">'
	-@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -name \*.htm -o -name \*.html \) -print \
	 | xargs egrep -i 'TODO|class=["'\'']?fixme'

findfixmefiles:
	@echo 'Looking for TODO or <foo class="fixme">'
	-@cd ${TOPDIR} && find . ${PRUNESTUFF} -prune \
		-o \( -name \*.htm -o -name \*.html \) -print \
	 | xargs egrep -l -i 'TODO|class=["'\'']?fixme'

CHECKBOTIGNORE= --ignore '^foo:$$|pictures\/.*\/tn|(\/(afnog2006|inet2000)\/)|e2\/misc\/cisco.*htm'
#CHECKBOTIGNORE+= --dontwarn 902
#CHECKBOTURL= ${TOPDIR}/index.html
CHECKBOTURL= http://cdrom.ws.afnog.org/index.html
checkbotinternal:
	[ ! -d checkbot.tmp ] || false # mutual exclusion
	mkdir checkbot.tmp ; \
	cd checkbot.tmp ; \
	checkbot --verbose --internal-only \
		${CHECKBOTIGNORE} \
		${PROXY} \
		--url ${CHECKBOTURL} ; \
	cd .. ; \
	rm -rf checkbot.out ; \
	mv checkbot.tmp checkbot.out

checkbot:
	[ ! -d checkbot.tmp ] || false # mutual exclusion
	mkdir checkbot.tmp ; \
	cd checkbot.tmp ; \
	checkbot --verbose \
		${CHECKBOTIGNORE} \
		${PROXY} \
		--url ${CHECKBOTURL} ; \
	cd .. ; \
	rm -rf checkbot.out ; \
	mv checkbot.tmp checkbot.out

weblint:
	weblint ${TOPDIR} \
	| egrep -v '(slide|sld)[0-9][0-9][0-9]*.htm\([0-9]*\):'

# If there is not already an md* device for the image, configure one
mdconfig-image:
	[ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	found=false ; \
	for n in $$( mdconfig -l -n ) ; do \
	    if mdconfig -l -u $${n} | fgrep -q ${CDIMAGE:Q} ; then \
		found=true ; \
		break ; \
	    fi ; \
	done ; \
	if $${found} ; then \
	    echo md$${n} was already configured ; \
	else \
	    mddev=$$( mdconfig -a -t vnode -f ${CDIMAGE:Q} ) \
	    && echo configured $${mddev} ; \
	fi

# If the image is not mounted, mount it
mount-image: mdconfig-image
	[ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	found=false ; \
	for n in $$( mdconfig -l -n ) ; do \
	    if mdconfig -l -u $${n} | fgrep -q ${CDIMAGE:Q} ; then \
		found=true ; \
		break ; \
	    fi ; \
	done ; \
	if $${found} ; then \
	    if ! mount | fgrep -q ${MOUNTPOINT:Q} ; then \
		mount -t cd9660 -r /dev/md$${n} ${MOUNTPOINT:Q} \
		&& echo Mounted md$${n} on ${MOUNTPOINT:Q} ; \
	    else \
		echo Already mounted ; \
	    fi ; \
	else \
	    echo md device not found ; \
	    false ; \
	fi

# If the image is mounted, unmount it
unmount-image:
	[ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	if mount | fgrep -q ${MOUNTPOINT:Q} ; then \
	    umount -f ${MOUNTPOINT:Q} \
	    && echo Unmounted ${MOUNTPOINT:Q} ; \
	else \
	    echo Not mounted ; \
	fi

# If there is an md* device for the image, unconfigure it
unmdconfig-image: unmount-image
	[ "${THISHOST}" = "${CDMASTERHOST}" ] || false
	found=false ; \
	for n in $$( mdconfig -l -n ) ; do \
	    if mdconfig -l -u $${n} | fgrep -q ${CDIMAGE:Q} ; then \
		found=true ; \
		break ; \
	    fi ; \
	done ; \
	if $${found} ; then \
	    mdconfig -d -u $${n} \
	    && echo unconfigured md$${n} ; \
	else \
	    echo md device not found ; \
	fi

# Shortcuts
mount: mdconfig-image mount-image
unmount: unmount-image unmdconfig-image
