#!/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 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'`"
shift

href="`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"
