#!/bin/csh -f
#
#
set linkto = `echo ${1} | cut -f1 -d"#"`
# < and > in HTML comments cause problems, so change them:
set mstring = `echo ${2} | tr "<>" "()"`
set comment = "	Text $mstring	marked up as link to	${1}"

cat <<EOF >! sed.tmp
/^<!--Hyperlinks added:/a\
$comment
EOF

foreach doc (*.html)

# don't link .html file to itself
if ( ${doc} == ${linkto} ) continue;

# only link .html files designed for this mark-up
set count = `grep -c '^<\!\-\-Hyperlinks added:$' ${doc}`
if ( ${count} == 0 ) continue;

# check if .html file contains the string
set count = `fgrep -c "${2}" ${doc}`
if ( ${count} == 0 ) continue;

# check that .html file is not already linked
set count = `fgrep -c "$comment" ${doc}`
if ( ${count} != 0 ) continue;

\rm -f old/${doc}
mv ${doc} old/${doc} 

sed -e 's?'"${2}"'?<A href="'${1}'">'"${2}"'</a>?g' \
    -f sed.tmp    old/$doc >! $doc

echo ${2} linked to ${1} in ${doc}

end
