#!/bin/bash

. date-config

function replace
{
global-replace-y "$1" "$2" $F
}

function xmlsub
{
F=Overview.xml
replace '<!ENTITY base.uri ".*?">' "<!ENTITY base.uri \"http://www.w3.org/TR/\">"
replace '<!ENTITY maturity.level ".*?">' "<!ENTITY maturity.level \"$M\">"
replace '<!ENTITY draft.year ".*?">' "<!ENTITY draft.year \"$YEAR\">"
replace '<!ENTITY draft.month.name ".*?">' "<!ENTITY draft.month.name \"November\">"
replace '<!ENTITY draft.month ".*?">' "<!ENTITY draft.month \"11\">"
replace '<!ENTITY draft.day ".*?">' "<!ENTITY draft.day \"$DAY2\">"
replace '<w3c-doctype>W3C .*</w3c-doctype>' "<w3c-doctype>W3C $MNAME</w3c-doctype>"
replace '<prevlocs>.*</prevlocs>' "<prevlocs><loc href=\"$PREV\">$PREV</loc></prevlocs>"
replace '<status>.*</status>' "<status>$STATUS</status>"
replace '@@previousURL' "$PREV"
replace '@@thisURL' "$U"
replace "<!-- $SHORTNAME -->" "  (this document)"
# replace '<!ENTITY this.version "http://www.w3.org/2009.*?">' '<!ENTITY this.version "&base.uri;&doc.ident;">'
replace '<!ENTITY this.version ".*?">' '<!ENTITY this.version "&base.uri;&draft.year;/&doc.ident;">'
replace '<spec w3c-doctype=".*?" xml:lang="en">' "<spec w3c-doctype=\"$LCM\" xml:lang=\"en\">"

replace '<!-- begin cvs history -->.*<!-- end cvs history -->' '<!-- cvs history removed for publication -->'
}

function htmlsub
{
F=Overview.html

replace '<h2 id="status">.*<div class="toc">' "<h2 id=\"status\">Status of This Document</h2>$STATUS</div><hr /><div class=\"toc\">"
replace '<!-- begin status section -->.*<!-- end status section -->' "<h2 id=\"status\">Status of This Document</h2>$STATUS<hr />"

replace 'http://www.w3.org/StyleSheets/TR/W3C-[^"]*' "http://www.w3.org/StyleSheets/TR/W3C-$M"
replace "<h2>Editor'?s? Draft</h2>" "<h2>W3C $MNAME $DAY1 $MONTH_NAME $YEAR</h2>"
replace "<h2>(W3C )?Working Draft.*?</h2>" "<h2>W3C $MNAME $DAY1 $MONTH_NAME $YEAR</h2>"

replace '@@previousURL' "$PREV"
replace '@@thisURL' "$U"

replace "<span id=\"this-version-url\">.*?</span>" "<span id=\"this-version-url\"><a href=\"$U/\">$U/</a></span>"
replace "<span id=\"previous-version-url\">.*?</span>" "<span id=\"previous-version-url\"><a href=\"$PREV/\">$PREV/</a></span>"

replace '<!-- begin cvs history -->.*<!-- end cvs history -->' '<!-- cvs history removed for publication -->'
}

function refs
{
for ref in ../.ref-*; do
  refname=`echo $ref | sed 's/^....//'`
  reftext=`cat $ref`
  replace "@@$refname" "$reftext"
done
for ref in ../.refx-*; do
  refname=`echo $ref | sed 's/^....//'`
  echo REFNAME $refname
  reftext=`cat $ref`
  replace "@@$refname" "$reftext"
  # global-replace "<dd><a href=\"http://www.w3.org/TR/$K.*?cite.*?</dd>" "$reftext" $F
done
}

for x in d[0-9][0-9]; do
  echo -n '=== ' $x
  cd $x
  M=`cat .maturity`
  LCM=`cat .maturity | tr [A-Z] [a-z]`
  MNAME=`cat .maturity-name`
  PREV=`cat .prev`
  STATUS=`cat ../status-$M`
  SHORTNAME=`cat .shortname`
  U=http://www.w3.org/TR/$YEAR/$M-$SHORTNAME-$DT

  if [ -f Overview.xml ]; then
    echo
    xmlsub
    xsltproc Overview.xml > Overview.html
  else
    echo 
    htmlsub
  fi
  F=Overview.html
  replace "@import.url..../shared/local.css..;" " "
  LOG="$"Log
  replace "$LOG:" ""
  refs
  cd ..
done
