#! /bin/sh

# Page.sh
#
# Quick and dirty website generator.
#
# Reads a text file from standard input,
# converts it to multi-page web site,
# and writes one page of it, with links to the other pages,
# to standard output.
#
# Each group of consecutive non-blank lines in the input is
# considered a page.  The first line in each page should contain
# the URL (which should not contain spaces) followed by a space,
# followed by the page title (which may contain spaces).
#
# The remaining lines are the contents of that page.
#
# The contents of the selected page are written to standard
# output, with each input line appearing between <P> and </P>
# tags in the output.  The output will also contain a HEAD
# and at the bottom, a UL element with links to the other pages.
#
# Input processing stops at the first 2 consecutive blank lines, or
# end-of-input, whichever comes first.
#
# To update the whole site, use something like:
# for i in *.html; do cat MySite.txt | Page.sh $i > $i; done
# (from within the html/ directory)
# (any new URLs would have to be 'touch'ed first).
#
# For example of a site generated by this script, see
#    http://julianbr.sdf.org
#
# SDF version
# September 15, 2012
# For latest version, see gopher://sdf.org/1/users/julianbr
# or
# http://gopher.floodgap.com/gopher/gw.lite?gopher://sdf.org/1/users/julianbr
#

if [ -z "$1" ] || [ -n "$2" ]; then
       echo >&2 Usage: ${0##*/} \(page Url\)
       echo -e >&2 \\tReads document from standard input
       echo -e >&2 \\tWrite specified page with links to the others
       exit 0
       fi

UrlTitle=
FoundUrl=no
EndOfInput=no
if ! read Line; then
       EndOfInput=
       fi
while [ -n "$EndOfInput" ] && [ -n "$Line" ]; do
       # A new page
       # First line in page is Url Title
       Url=${Line%% *}
       if [ "$1" = "$Url" ]; then
               FoundUrl=
               echo -e \<HTML\>
               echo -e \\t\<HEAD\>
               echo -e \\t\\t\<STYLE\>\<\!\-\-
               echo -e \\t\\t\\tBODY \{
               echo -e \\t\\t\\t\\tbackground-color: rgb\(90%,90%,95%\)\;
               echo -e \\t\\t\\t\\tborder: lightblue solid 15px\;
               echo -e \\t\\t\\t\\tmargin: 0\;
               echo -e \\t\\t\\t\\tpadding-top: 1%\;
               echo -e \\t\\t\\t\\tpadding-left: 1%\;
               echo -e \\t\\t\\t\\tpadding-right: 5%\;
               echo -e \\t\\t\\t\\tpadding-bottom: 10%\;
               echo -e \\t\\t\\t\\t\}
               echo -e \\t\\t\\tIMG \{
               echo -e \\t\\t\\t\\tmargin: 1px\;
               echo -e \\t\\t\\t\\t\}
               echo -e \\t\\t\\t\-\-\>\</STYLE\>
               echo -e \\t\\t\<TITLE\>${Line#* }\</TITLE\>
               echo -e \\t\\t\</HEAD\>
               echo -e \\t\<BODY\>
       else
               if [ -z "$UrlTitle" ]; then
                       UrlTitle=$Line
               else
                       UrlTitle=$UrlTitle\|$Line
                       fi
               fi
       if ! read Line; then
               EndOfInput=
               fi
       while [ -n "$EndOfInput" ] && [ -n "$Line" ]; do
               if [ "$1" = "$Url" ]; then
                       echo -e \\t\\t\<P\>$Line\</P\>
                       fi
               if ! read Line; then
                       EndOfInput=
                       fi
               done
       if [ -n "$EndOfInput" ] && ! read Line; then
               EndOfInput=
               fi
       done
if [ -z "$FoundUrl" ]; then
       echo -e \\t\\t\<UL\>
       echo $UrlTitle | tr \| \\n | while read Line; do
               echo -e \\t\\t\\t\<LI\>\<A HREF=\"${Line%% *}\"\>${Line#* }\</A\>\</LI\>
               done
       echo -e \\t\\t\\t\</UL\>
       echo -e \\t\\t\</BODY\>
       echo -e \\t\</HTML\>
else
       echo >&2 \*\*\*${0##*/}: Url \"$1\" not found.
       fi