#!/bin/bash

set -e

name=`basename "$0"`
dir=`dirname "$0"`
src=`cd "$dir/.." && pwd`
bin="$src/bin"

die () {
        echo >&2 "$1"
        exit 1
}

usage () {
        die "usage: $name"
}

case "$#" in
0)
;;
*)
	usage
;;
esac

TIMEFORMAT='  Took %lR.'

base='http://mlton.org'
index='.index'
script='.script'
tmp='.tmp'
useLocal='false'
version=`date +%Y%m%d`

cd "$src/doc/guide"

rm -f *

drop='\(Download\|Experimental\|.*MoinEditorBackup\|OldPages\|Preferences\|RecentChanges\|TemporaryUpload\|WikiSandBox\)'

if $useLocal; then 
	(cd "$HOME/mlton/guide-in" && tar.write .) | tar.read;
else
echo "Getting index:" 
lynx -dump "$base/Index?action=titleindex" | \
    grep -v >"$index" -e '^$' -e "^$drop$"

echo "Getting pages:" 
wget -B "$base" -nv -i "$index"

echo "Getting CSS:"
for f in common screen print; do
	wget "$base/wiki/mlton/css/$f.css"
done

echo "Getting images:"
for f in bottom email ftp news top www; do
	wget "$base/wiki/mlton/img/moin-$f.png"
done
fi

echo "Fixing pages:" 

# Eliminate ungrabbed pages from Index.
grep -v ">$drop<" Index >"$tmp"
mv "$tmp" Index

wwwImg='<img src="moin-www.png" alt="[WWW]" height="11" width="11">'

# Write sed script to file.
cat >"$script" <<EOF
# Delete search box.
/<form .* action = "http:\/\/www.google.com\/custom">/,+6d
# Replace highlight actions with no-op.
s;"\([^"]*\).action=highlight[^"]*";"\1";g
# Delete Index actions.
/<a href *= *"Index?.*"/d
# Delete unused link rel.
/<link rel="\(Glossary\|Help\|Index\|Search\)"/d
# Delete <a> and <link> actions.
s;<a[^>]* href *= *"[^"]*?action[^"]*">.*</a>;;g
s;<link[^>]* href *= *"[^"]*?action[^"]*">;;g
# Add version stamp in upper left.
s;<a class = mltona\(.*\)>MLton</a>;<a class = mltona\1>MLton MLTONWIKIVERSION</a>;
# Replace ViewSVN references to trunk with references to release tag.
s;\(<a href = \"http://mlton.org/cgi-bin/viewsvn\.cgi[^>]*/mlton/\)trunk\([^>]*\)>;\1tags/on-MLTONWIKIVERSION-release\2>;g
# Fix wiki hrefs and srcs
s; \(href\|src\) *= *"\(/pages/[^"]*\)"; \1="$base\2";g
s; \(href\|src\) *= *"/\([^"]\+\)"; \1="\2";g
s; href *= *"/"; href="Home";g
s; href *= *"$drop"; href="$base/\1";g
s; \(href\|src\) *= *"wiki/mlton/[^/]\+/\([^"]*\)"; \1="\2";g
s;\(<a href *= *"http://mlton.org[^>]*>\);\1$wwwImg;g
EOF

for f in $(cat "$index"); do
	echo "$f"
        l=`wc -l < "$f"`
        let l-=19
	head -n $l <"$f" >"$tmp"
	(
		"$bin/msed" -f "$script" <"$tmp"
		echo '</body></html>'
	) >"$f"
done

rm -f "$tmp" "$index" "$script"

cp Home index.html

echo $version > MLTONWIKIVERSION
