#!/bin/csh -f
######################################################################
# This csh script finds each dir in the website that contains a 
# file named "Makefile" and runs "make -s" in that dir.
# See the comments in 'makeweb' for details about the environment variables
# $FTP_DIR and $HTML_DIR.
#
# Usage:
#
#	% do-makefiles
#
# Here's a typical makefile that I use to keep the .zip archive of the
# html files in a directory up to date. (This particular makefile resides
# in the directory containing the writings of Ajaan Chah 
# (http://www.accesstoinsight.org/lib/thai/chah).)
# FTP_DIR is an environment variable specifying the directory in which the
# zip file lives. (See "makeweb" for details about environment variables.)
#
# lib/thai/chah/Makefile:
#
# -------- start of Makefile --------
#	SHELL = /bin/sh
#	DESTDIR = $(FTP_DIR)/thai
#	ZIP = chah.zip
#	
#	CHUNKS = *.html
#	
#	$(DESTDIR)/$(ZIP): $(CHUNKS)
#	        echo "Updating $(ZIP)"
#	        zip -u $(DESTDIR)/$(ZIP) $(CHUNKS)
#	        chmod a+r $(DESTDIR)/$(ZIP)
#	        echo done.
#	
#	kill:
#	        rm $(DESTDIR)/$(ZIP)
# -------- end of Makefile --------
######################################################################

# Abort if the FTP_DIR and HTML_DIR env variables aren't set 
set me=${0}
if (! ${?FTP_DIR}) then
	echo ${me}: FTP_DIR environment variable not set\!
	exit
else if (! ${?HTML_DIR}) then
	echo ${me}: HTML_DIR environment variable not set\!
	exit
endif

# Build a list of dirs in the website that contain Makefiles
echo -n Finding the makefiles...
set dirs = `find $HTML_DIR -follow -name Makefile -exec echo {} \; | sed "s/\/Makefile//"`
echo done.

echo Executing the makefiles...

#loop through all the dirs
foreach dir ($dirs)
	cd $dir
	make -s
	#echo ""
end

echo Finished executing the makefiles.
