#!/bin/sh

# Given HTML files generated by texi2html, get a HREF to a given
# node. This will probably fail when applied to HTML files not
# generated by texi2html, and produce the same error messsage then
# that it produces when the node does not exist.
#
# Copyright (C) 2000-2002 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Pascal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

if [ $# -lt 2 ]; then
  echo "Usage: `basename "$0"` node-name HTML-file-names" >&2
  exit 1
fi

node="$1"
node_quoted="`echo "$node" | sed -e 's,[][\/*.^$],\&,g;s/[Aa]/[Aa]/g;s/[Bb]/[Bb]/g;s/[Cc]/[Cc]/g;s/[Dd]/[Dd]/g;s/[Ee]/[Ee]/g;s/[Ff]/[Ff]/g;s/[Gg]/[Gg]/g;s/[Hh]/[Hh]/g;s/[Ii]/[Ii]/g;s/[Jj]/[Jj]/g;s/[Kk]/[Kk]/g;s/[Ll]/[Ll]/g;s/[Mm]/[Mm]/g;s/[Nn]/[Nn]/g;s/[Oo]/[Oo]/g;s/[Pp]/[Pp]/g;s/[Qq]/[Qq]/g;s/[Rr]/[Rr]/g;s/[Ss]/[Ss]/g;s/[Tt]/[Tt]/g;s/[Uu]/[Uu]/g;s/[Vv]/[Vv]/g;s/[Ww]/[Ww]/g;s/[Xx]/[Xx]/g;s/[Yy]/[Yy]/g;s/[Zz]/[Zz]/g'`"
shift

# Though sed can do what grep does, using grep is much faster (even
# though it requires a separate process). Given the number of files
# to search, this matters.
href="`grep -i "href.*$node_quoted" "$@" |
       sed -ne 's/[Hh][Rr][Ee][Ff]="[^"]*#[Ii][Dd][Xx]//g;
                /[Hh][Rr][Ee][Ff]="[^"]*#[^"]*">'"$node_quoted"'<\/[Aa]>/\
                s/.*[Hh][Rr][Ee][Ff]="\([^"]*#[^"]*\)">'"$node_quoted"'<\/[Aa]>.*/\1/p' |
       uniq`" || exit 1
if [ x"$href" = x ]; then
  echo "href for \`$node' not found." >&2
  exit 1
fi
if [ `echo "$href" | wc -l` -gt 1 ]; then
  echo "multiple hrefs for \`$node' found:" >&2
  echo "$href" >&2
  exit 1
fi

echo "$href"
