function genname # format a person's name in bold
	{
	sed -e 's/\([^,]*\)/<b>\1<\/b>/' -e 's/$/<br>/'
	}

function genliststart
	{
	echo "<dl>"
	}

function genlistend
	{
	echo "</dl>"
	}

function gencomment
	{
	echo "<!-- $* -->"
	}

function genlistitem
	{
	echo "<dt>$1<dd>$2"
	}

function gentablestart
	{
	echo "<table border=2>"
	}

function gentableend
	{
	echo "</table>"
	}

function gensiglinks
	{
	acm=http://www.acm.org
	label=$1; shift
	if test "$*"
	then
		echo "<i>$label:</i>"
		echo $* | sed "
			s_SIGART_<a href=$acm/sigart/>&</a>_
			s_SIGART_<a href=$acm/sigart/>&</a>_
			s_SIGCAPH_<a href=$acm/sigcaph/>&</a>_
			s_SIGCHI_<a href=$acm/sigchi/>&</a>_
			s_SIGDOC_<a href=$acm/sigdoc/>&</a>_
			s_SIGGRAPH_<a href=$acm/siggraph/>&</a>_
			s_SIGIR_<a href=$acm/sigir/>&</a>_
			s_SIGLINK_<a href=$acm/siglink/>&</a>_
			s_SIGMM_<a href=$acm/sigmm/>&</a>_
			s_SIGOIS_<a href=$acm/sigois/>&</a>_
			s_SIGSOFT_<a href=$acm/sigsoft/>&</a>_
			s_SIGSOUND_<a href=$acm/sigsound/>&</a>_
			s_AAAI_<a href=http://www.aaai.org/>&</a>_
			s_ACM_<a href=$acm/>&</a>_
			"
	fi
	}

function gendesc # category [codename]
	{
	if test "$1" = "-v" # verbose
	then
		verbose=true
		shift
	fi
	category=$1
	codename=$2
	if test "$codename" -a -r $category.meta
	then
		descrec=`sssearch "codename=$codename" $category.meta`

		longname=`echo "$descrec" | ssformat -t %longname`
		url=`echo "$descrec" | ssformat -t %url`
		if test "$longname"
		then
			echo "<b>"
			if test "$url"
			then
				echo "<a href=$url>"
			fi
			echo "$longname"
			if test "$url"
			then
				echo "</a>"
			fi
			echo "</b>"
			echo "<br>"
		fi
		sponsor=`echo "$descrec" | ssformat -t %sponsor`
		incoop=`echo "$descrec" | ssformat -t %incoop`
		gensiglinks Sponsoring $sponsor
		gensiglinks Cooperating $incoop
		test "$sponsor" -o "$incoop" &&  echo "<br>"
		if test "$verbose" = true
		then
			imagesmall=`echo "$descrec" | ssformat -t %imagesmall`
			imagelarge=`echo "$descrec" | ssformat -t %imagelarge`
			if test "$imagesmall"
			then
				echo "<table border=1>"
				echo "<tr><td>"
				if test "$imagelarge"
				then
					echo "<a href=$imagelarge>"
				fi
				echo "<img src=$imagesmall>"
				if test "$imagelarge"
				then
					echo "</a>"
				fi
				echo "<td>"
			fi
		fi

		desc=`echo "$descrec" | ssformat -t %desc`
		if test "$desc"
		then
			echo "$desc"
		fi
		if test "$verbose" = true
		then
			charter=`echo "$descrec" | ssformat -t %charter`
			if test "$charter"
			then
				echo "<p>$charter"
			fi
			if test "$imagesmall"
			then
				echo "</table>"
			fi
		fi
	else # only the category
		sssearch category=$1 pubs.db |
			ssformat -t %desc
	fi
	}

function genpubtab # category [codename] # generate pub list
	{
	category=$1
	codename=$2
	catrec=`sssearch "category=$category" pubs.db`
	sortkeys=`echo "$catrec" | ssformat -t %sortkeys`
	tabent=`echo "$catrec" | ssformat -t %tabent`
	gentablestart
	if test "$codename"
	then
		sssearch codename=$codename $category.db
	else
		cat $category.db
	fi | sssort -f "$sortkeys" |
		genrecord |
			ssformat -t "$tabent"
	gentableend
	}

function genheader # title [url] [category] # generate the header of a page
	{
	title="$1"
	echo "<html><head><title>$title</title></head>"
	echo "<body bgcolor=ffffff>"

	echo "<h2>"
	echo "<a href=http://www.acm.org/>ACM</a>"
	echo "<a href=http://www.acm.org/sigchi/>SIGCHI</a>"
	if test "$3"
	then
		echo "$3<br>"
	fi
	if test "$2"
	then
		echo "<a href=$url>"
	fi
	echo "$title"
	if test "$2"
	then
		echo "</a>"
	fi
	echo "</h2>"
	}

function genfooter # generate the footer of a page
	{
	echo "<hr>"
	echo "Go to: "
	echo "<a order=0 href=http://www.acm.org/catalog>"
	echo "<img order=0 src=http://www.acm.org/images/catalog/toc_banner.gif alt=\"[ACM catalog]\" align=center height=50>"
	echo "</a>"
	#echo "<a href=http://www.acm.org/sigchi/publications>[SIGCHI Pubs]</a>"
	if test "$1"
	then
		name=`echo "$1" | tr a-z A-Z`
		echo "<a href=$1.html>[$name]</a>"
	fi
	echo "<br>"
	email=perlman@acm.org
	echo "<address>"
	echo "Send comments to: <a href=mailto:$email>$email</a>"
	echo "</address>"
	echo "</body></html>"
	}

function gentabent # generate a paired value table
	{
	eval `echo value='$'$1` # turn $1 into a variable and interpolate
	name=$1
	if test "$value"
	then
		shift
		echo "<tr><td align=right> $*:"
		echo "<td align=left>"
		if test "$name" = url
		then
			echo "<a href=$value>[details]</a>"
		else
			echo "$value"
		fi
		# echo "<br>" # why is this here? it makes the cells too high!
	fi
	}

function genfields # generate assignments of db fields to shell variables
	{
	DB=$1
	tmp=/tmp/pub$$
	fields=`grep "^%" < $DB |
		sed -e 's/[ 	].*$//' -e 's/^%//' |
			sort -u`
	for field in $fields
	do
		echo "$field"'=`echo "$record" | ssformat -t %'"$field"'`'
	done > $tmp
	echo $tmp
	}

function genrecord #prepend a documenting record to the standard input
	{
	echo "
%type TYPE
%title TITLE
%editor EDITOR
%freq FREQ
%author AUTHOR
%desc DESC
%year YEAR
%pages PAGES
%format FORMAT
%ISBN ISBN
%pricemember MEMBER
%pricenonmember NONMEMBER
%avail AVAIL
%pricestudent STUDENT
%acmordernumber ACM#
%imagesmall THUMBNAIL
%imagelarge IMAGE
%issue ISSUE
%contents CONTENTS

"

cat
}

function gencontact # print contact information
{
echo "<hr>
<h3>Contact Information</h3>
<table>
<tr>
	<td valign=top>Mailing Address:<td>
		ACM<br>
		PO Box 12105<br>
		Church St. Station<br>
		New York, NY 10257 USA
<tr>
	<td valign=top>Email:<td>
		<a href=mailto:acmhelp@acm.org
			>acmhelp@acm.org</a>
<tr>
	<td valign=top>Fax:<td>
		+1-212-944-1318
<tr>
	<td valign=top>Phone Orders:<td>
		1-800-342-6626 (USA and Canada)<br>
		+1-212-626-0500 (metro NY and outside USA)
<tr>
<td><b>In Europe:</b>
<tr>
	<td valign=top>Mailing Adress:<td>
		ACM European Serice Centre<br>
		108 Cowley Road<br>
		Oxford, OX4 1JF, UK
<tr>
	<td valign=top>Email:<td>
		<a href=mailto:acm_europe@acm.org
			>acm_europe@acm.org</a>
<tr>
	<td valign=top>Fax:<td>
		+44-1865-381338
<tr>
	<td valign=top>Phone Orders:<td>
		+44-1865-382338
</table>
"
}

function debug # echo parameters to the screen
	{
	echo "$*" > /dev/tty
	}
