#! /bin/sh

(   echo "% Site map ${1:+for $1}"
    echo '% mksitemap <http://www.pell.portland.or.us/~orc/mksitemap.sh>'
    echo -n '% '; date
    echo
    echo "#Site Map"
    echo '<style type="text/css"> @import url(/~orc/sitemap.css); </style>'
    echo '<style type="text/css"> #sitemap { display: none; } </style>'

    d=1
    find ./* -name index.html | sed -e 's:/index.html$::' \
			    | sort -f | while read X;do
	depth=`echo $X | tr -dc / | wc -c`

	if [ "$depth" -gt `expr $d + 1` ]; then
	    # there needs to be a continuous tree of indexes going down;
	    # if an index appears separated by a layer or so of directories,
	    # it's probably an exposed index page in an archive directory.
	    continue
	fi
	if [ -r $X/.ignore ]; then
	    # the "please don't add me to the sitemap" flag
	    continue
	fi
	d=$depth
	if [ "$depth" -gt 0 ]; then
	    Pn=`basename $X`
	    Dir=`echo "$X" | sed -e 's:\./::'`
	    FF=
	    if [ -r $X/index.text ]; then
		# index.text is markdown source for index.html ; pick up
		# the % title (if any)
		FF=`head -1 $X/index.text | sed -n -e 's/^% //p'`
	    fi

	    if [ -z "$FF" ]; then
		FF=`sed -n -e 's/.*<title>\(.*\)<\/title>.*/\1/p' $X/index.html`
	    fi

	    jot -b '' -s '    ' -n $depth; echo " *  [${FF:-$Pn}](${Dir} '${Dir}')"
	fi
    done ) | /usr/local/bin/theme -p mksitemap.sh > sitemap.html
