#!/bin/sh

# Create the GPC WWW home page. This is a very special-purpose
# script.
#
# 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.

# This script requires bash. Since bash cannot be assumed to be in
# /bin, /usr/bin or any other certain place, we cannot use it in the
# first line. So we use /bin/sh, which can be assumed to exist. Then
# we check if it's actually bash, and if not, try to re-run the
# script with bash.
if [ x"$BASH" = x ]; then
  if [ x"$RERUN_BASH_TRIED" != x ]; then
    echo "`basename "$0"`: cannot run, \`bash' is either not bash or a very old version" >&2
    exit 1
  else
    RERUN_BASH_TRIED=1; export RERUN_BASH_TRIED
    exec bash "$0" "$@"
    echo "`basename "$0"`: cannot run bash" >&2
    exit 1
  fi
fi

if [ $# != 3 ]; then
  echo "Usage: `basename "$0"` GPCVersion src-dir dest-dir" >&2
  exit 1
fi

# This should work even with a crippled sed...
dir="`echo "$0" | sed -e 's,\(.\)/*[^/]*$,\1,'`" || exit 1
sed="`"$dir"/find-sed`" || exit 1

GPCVersion="$1"
srcdir="$2"
destdir="$3"

table ()
{
cat << EOF || exit 1
home     home.html.in     Home     GNU Pascal
about    @Highlights      About    About GNU Pascal
news     @News            News     New features in GNU Pascal
faq      @FAQ             FAQ      Frequently Asked Questions
download @Download        Download Where to get GNU Pascal
support  @Support         Support  How to get support for GNU Pascal
team     @Contributors    Team     The GNU Pascal development team
gnu      @GNU             GNU      The GNU project
todo     @To\ Do          To\ Do   Things still to do for GNU Pascal
misc     misc.html.in     Misc     Miscellaneous topics about GNU Pascal
EOF
}

home=""

table | while read output input short title; do

exec > "$destdir/$output.html" || exit 1

cat << EOF || exit 1
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<!-- Generated automatically by `basename "$0" | "$sed" -e 's,.*/,,'`
     DO NOT CHANGE THIS FILE MANUALLY! -->

  <title>$title</title>

  <link rev="made" title="Peter Gerwinski" href="mailto:peter@gerwinski.de">

</head>

<body background="gradient.jpg" bgcolor="#FFCF7F"
      text="#000000" link="#CF0000" vlink="#7F0000" alink="#FF0000">

<center><small>
EOF

separator="  "
table | while read output2 input2 short2 title2; do
  if [ x"$output2" = x"$output" ]; then
    echo " $separator $short2"
  else
    echo " $separator <a href="'"'"$output2.html"'">'"$short2</a>"
  fi
  separator=" -"
done || exit 1

cat << EOF || exit 1
</small></center>

<hr>

EOF

if [ x"$home" != x ]; then
cat << EOF || exit 1
<h2><a name="$output" href="$home#${output}_toc">$title</a></h2>

EOF
fi

if echo "$input" | grep "^@" > /dev/null; then

  href="$("`echo "$0" | "$sed" -e 's,\(.\)/*[^/]*$,\1,'`"/get-href \
    "`echo "$input" | "$sed" -e 's/^@//'`" "$destdir"/gpc*.html)" || exit 1
  href_file="`echo "$href" | "$sed" -e 's/#.*//'`"
  href_anchor="`echo "$href" | "$sed" -e 's/^[^#]*//;s/^#//'`"
cat << EOF
<p>
This page is a direct extract from the GPC Manual.
If you want to browse the manual, you can start at the
<a href="gpc-doc.html">top of the manual</a> or at the
<a href="$href">counterpart of this page within the manual</a>.</p>
EOF
  "$sed" -e '1,/<[Aa] [^>]*[Nn][Aa][Mm][Ee]="'"$href_anchor"'"/d' < "$destdir/$href_file" |
    "$sed" -e '
      1,/<!--docid/d;
      /<[Hh][Rr] /{N;N;N;};
      /<[Hh][Rr] .*<[Tt][Dd] [^>]*>\[<[^>]*> *&lt;/,$d;
      /This document was generated/{N;N;};
      /.*\n.*using.*[Tt]exi2html/,$d;
      /<!--MAKE-HOMEPAGE-CUT-->/,$d;
    '
  echo "</p><hr>"
else
  "$sed" -e "s/@GPCVersion@/$GPCVersion/" < "$srcdir/$input" || exit 1
fi

if [ x"$home" != x ]; then
cat << EOF || exit 1

<ul>
EOF

first="yes"
table | while read output2 input2 short2 title2; do
  if [ x"$first" != x ]; then
    first=""
  elif [ x"$output2" = x"$output" ]; then
    echo "  <li> $title2"
  else
    echo '  <li> <a href="'"$output2.html"'">'"$title2</a>"
  fi
done || exit 1

cat << EOF || exit 1
</ul>
<ul>
  <li> <a href="$home">Return to top page</a>
</ul>

<hr>
EOF
else
  home="$output.html"
fi

cat << EOF || exit 1

<small>
<p>Copyright &copy; 1996-2000 GNU Pascal development team</p>

<p>Verbatim copying and distribution is permitted in any medium,
provided that this notice and the disclaimer below are
preserved.</p>

<p>This information is provided in the hope that it will be useful,
but without any warranty. We disclaim any liability for the accuracy
of this information.</p>

<p>We are not responsible for the contents of web pages referenced
by this site.</p>
</small>

</body>

</html>
EOF

done || exit 1
