#!/bin/sh
#
# ablog - the ACME Blogmaker
#
# This script assembles some template files with a sequence of numbered item
# files to create HTML and RSS2 output files.
#
# You initialize a new blog using the 'ablog_init' script.  Then you use
# the 'ablog_item' script to create a new item file.  When 'ablog_item'
# is done, it runs this script automatically to update the blog; you can
# also run this script manually if you change something.

if [ ! -d .ablog_items ] ; then
    echo "$0: .ablog_items directory not found" >&2
    exit 1
fi
if [ ! -d .ablog_templates ] ; then
    echo "$0: .ablog_templates directory not found" >&2
    exit 1
fi
. .ablog_templates/setup.sh

# Tests whether the args contain any HTML/XML markup chars.
contains_markup ( )
    {
    echo "$@" | egrep '&|<|>' > /dev/null
    }

# Echoes back its arguments enclosed in an XML CDATA container.
cdata ( )
    {
    echo "<![CDATA[$@]]>"
    }

# Converts text to CDATA if necessary.
xmlize ( )
    {
    if contains_markup "$@" ; then
	cdata "$@"
    else
	echo "$@"
    fi
    }

subst ( )
    {
    sedfile=/tmp/subst_sed.$$
    rm -f $sedfile
    (
	cat << EOF
s${dollar}blog_title$blog_titleg
s${dollar}xml_blog_title$xml_blog_titleg
s${dollar}blog_author$blog_authorg
s${dollar}xml_blog_author$xml_blog_authorg
s${dollar}blog_url$blog_urlg
s${dollar}blog_description$blog_descriptiong
s${dollar}xml_blog_description$xml_blog_descriptiong
s${dollar}language$languageg
s${dollar}max_xml_items$max_xml_itemsg
s${dollar}html_filename$html_filenameg
s${dollar}rss2_filename$rss2_filenameg
s${dollar}webmaster$webmasterg
s${dollar}date$dateg
s${dollar}item_title$item_titleg
s${dollar}xml_item_title$xml_item_titleg
s${dollar}item_author$item_authorg
s${dollar}xml_item_author$xml_item_authorg
s${dollar}item_date$item_dateg
s${dollar}item_day$item_dayg
s${dollar}item_num$item_numg
s${dollar}permalink$permalinkg
s${dollar}item_description$item_descriptiong
s${dollar}xml_item_description$xml_item_descriptiong
EOF
    ) |
      sed -e 's/&/\\\&/g' -e '/g$/!s/$/\\/' > $sedfile
    sed -f $sedfile -f $sedfile -f $sedfile
    rm -f $sedfile
    }

date=`date -u "+$rfc822_format"`

# Initialize.
cat /dev/null > "$html_filename"
cat /dev/null > "$rss2_filename"

# Generate headers.
xml_blog_title=`xmlize "$blog_title"`
xml_blog_author=`xmlize "$blog_author"`
xml_blog_description=`xmlize "$blog_description"`
subst < .ablog_templates/header.html >> "$html_filename"
subst < .ablog_templates/header_rss2.xml >> "$rss2_filename"

# Generate items.
n_xml_items=0
for item_num in `ls .ablog_items | egrep '^[1-9][0-9]*$' | sort -nr` ; do
    item_file=".ablog_items/$item_num"

    item_title=""
    item_author="$blog_author"
    item_date=`file_date -u -f "$rfc822_format" "$item_file"`
    item_day=`file_date -u -f "%d%b%Y" "$item_file"`
    permalink="${blog_url}archive/${item_num}.html"

    varfile=/tmp/subst_var.$$
    rm -f $varfile
    (
	egrep '^[A-Za-z0-9_][A-Za-z0-9_]*=' < "$item_file" |
	  subst |
	  while read line ; do
	      var=`echo "$line" | sed -e 's/=.*$//'`
	      val=`echo "$line" | sed -e 's/^[A-Za-z0-9_][A-Za-z0-9_]*=//' -e "s/^'//" -e "s/'$dollar//"`
	      case "$var" in
		  item_title|item_author|item_date|item_day|permalink)
		  echo "$var='$val'"
		  ;;
		  *) echo "$0: unrecognized item variable in item $item_num - '$var'" >&2
	      esac
	  done
    ) > $varfile
    . $varfile
    rm -f $varfile

    xml_item_title=`xmlize "$item_title"`
    xml_item_author=`xmlize "$item_author"`

    item_description=`egrep -v '^[A-Za-z0-9_][A-Za-z0-9_]*=' < "$item_file" | subst`
    xml_item_description=`xmlize "$item_description"`

    subst < .ablog_templates/item.html >> "$html_filename"
    subst < .ablog_templates/archive.html > "archive/$item_num.html"
    if [ "$n_xml_items" -lt "$max_xml_items" ] ; then
	subst < .ablog_templates/item_rss2.xml >> "$rss2_filename"
	n_xml_items=`expr "$n_xml_items" + 1`
    fi
done

# Generate trailers.
subst < .ablog_templates/trailer.html >> "$html_filename"
subst < .ablog_templates/trailer_rss2.xml >> "$rss2_filename"
