#!/bin/bash
#
# $Id: vocal_html_lib,v 1.2 2002/05/15 07:33:55 icahoon Exp $
#
# ====================================================================
# The Vovida Software License, Version 1.0 
# 
# Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. The names "VOCAL", "Vovida Open Communication Application Library",
#    and "Vovida Open Communication Application Library (VOCAL)" must
#    not be used to endorse or promote products derived from this
#    software without prior written permission. For written
#    permission, please contact vocal\@vovida.org.
# 
# 4. Products derived from this software may not be called "VOCAL", nor
#    may "VOCAL" appear in their name, without prior written
#    permission of Vovida Networks, Inc.
# 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
# NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
# IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR
# PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# 
# ====================================================================
# 
# This software consists of voluntary contributions made by Vovida
# Networks, Inc. and many individuals on behalf of Vovida Networks,
# Inc.  For more information on Vovida Networks, Inc., please see
# <http://www.vovida.org/>.
#


##################################################################
#
#    Vocal HTML Library 
#
#   	Required variables:
#   	    
#   	    image_dir:	    	directory of images to be used
#   	    background_image:	defaults to "$image_dir/bg.jpg"
#   	    title:  	    	page title, formatted for window title bar
#   	    title_html:     	page title, formatted for html
#   	    brand_image:    	defaults to "$image_dir/vovida.gif"
#   	    brand_name:     	defaults to "Vovida"
#   	    brand_link:     	defaults to "http://www.vovida.org"
#
##################################################################

if [ "$version" = "" ]; then
    version="\$Id: vocal_html_lib,v 1.2 2002/05/15 07:33:55 icahoon Exp $"
fi

VOCAL_HTML_LIB=1

if [ -z "$VOCAL_UTIL_LIB" ];
then
    lib_path="${0%/$(basename $0)}"
    . $lib_path/vocal_util_lib
fi

#
#   Image directory
#

function    image_dir()
{
    if [ -z "$image_dir" ]; then
        image_dir="./images"
    fi
    $print "$image_dir"
}
image_dir=$(image_dir)

#
#   Background image
#

function    background_image()
{
    if [ -z "$background_image" ]; then
        background_image="$image_dir/bg.jpg"
    fi
    $print "$background_image"
}
background_image=$(background_image)

function    background_parameters()
{
    $print "background=\"$(background_image)\""
}

#
#   Indents
#

content_indent="\t    "
content_indent_2="\t\t"

#
#   Branding variables
#

function    brand_image()
{
    if [ -z "$brand_image" ]; then
    	brand_image="$image_dir/vovida.gif"
    fi
    $print "$brand_image"
}
brand_image=$(brand_image)

function    brand_image_span()
{
    if [ -z "$brand_image_span" ]; then
    	brand_image_span="2"
    fi
    $print "$brand_image_span"
}
brand_image_span=$(brand_image_span)

function    brand_image_align()
{
    if [ -z "$brand_image_align" ]; then
    	brand_image_align="left"
    fi
    $print "$brand_image_align"
}
brand_image_align=$(brand_image_align)

function    brand_name()
{
    if [ -z "$brand_name" ]; then
        brand_name="Vovida"
    fi
    $print "$brand_name"
}
brand_name=$(brand_name)

function    brand_link()
{
    if [ -z "$brand_link" ]; then
        brand_link="http://www.vovida.org"
    fi
    $print "$brand_link"
}
brand_link=$(brand_link)

function    branding()
{
    $print "$content_indent<a href=\"$(brand_link)\">\n ${content_indent_2}<img src=\"$(brand_image)\" alt=\"$(brand_name)\" border=\"0\"></a>"
}


#
#   Title variables
#
#		Uses the gatekeeper_identifier, endpoint_idnetifier and the start_time.
#

function    title()
{
    if [ -z "$title" ]; then
        title="Unknown"
    fi
    $print "$title"
}
title=$(title)

function    title_html()
{
    if [ -z "$title_html" ]; then
    	title_html=$title
    fi
    $print "$title_html"
}
title_html=$(title_html)

#
#   Html definition
#

function html_start()
{
    $print "<html>\n\n<!-- \$Id\$ -->\n\n<head>\n    <title> $title\n    </title>\n</head>\n\n<body $(background_parameters)>\n"
}

html_end="</body>\n</html>\n"

#
#   Table html
#

table_begin="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">"
table_end="</table>"


#
#   Cell begin
#
#   	$1: width in percentage
#   	$2: column span, 0 indicates none
#   	$3: row span, 0 indicates none
#   	$4: horizontal alignment
#   	$5: vertical alignment
#   	$6: background color
#   	$7: height
#

cell_indent="\t"

function cell_begin()
{
    local width=""
    local colspan=""
    local rowspan=""
    local align=" align=\"center\""
    local valign=" valign=\"center\""
    local bgcolor=" bgcolor=\"#FFFFFF\""
    local height=""

    if [ $# -gt 0 ]; then
    	if [ ! $1 -eq 0 ]; then
    	    width=" width=\"$1%\""
    	fi
    fi
    if [ $# -gt 1 ]; then
    	if [ ! $2 -eq 0 ]; then
    	    colspan=" colspan=\"$2\""
	fi
    fi
    if [ $# -gt 2 ]; then
    	if [ ! $3 -eq 0 ]; then
    	    rowspan=" rowspan=\"$3\""
	fi
    fi
    if [ $# -gt 3  ]; then
    	align=" align=\"$4\""
    fi
    if [ $# -gt 4 ]; then
    	valign=" valign=\"$5\""
    fi
    if [ $# -gt 5 ]; then
    	bgcolor=" bgcolor=\"$6\""
    fi
    if [ $# -gt 6 ]; then
    	height=" height=\"$7\""
    fi

    $print "${cell_indent}<td${colspan}${rowspan}${width}${valign}${align}${bgcolor}${height}>"
}

cell_50_begin=$(cell_begin 50)
cell_46_begin=$(cell_begin 46)
cell_20_begin=$(cell_begin 20)
cell_17_begin=$(cell_begin 17)
cell_16_begin=$(cell_begin 16)
cell_01_begin=$(cell_begin 1)
cell_end="</td>"

#
#   White cell html
#

cell="$cell_01_begin &nbsp; $cell_end"


#
#   Row html
#

row_indent="    "
row_begin="$row_indent<tr>"
row_end="$row_indent</tr>"


#
#   Columns html
#
#   	$1: number rows spaned
#   	$2: color, defaults to white
#   	$3: content, defaults to &nbsp;
#

function    blank_column()
{
    local rowspan=""
    local bgcolor="#FFFFFF"
    local content="&nbsp;"

    if [ $# -gt 0 ]; then
    	rowspan=$1
    fi
    if [ $# -gt 1 ]; then
    	bgcolor="$2"
    fi
    if [ $# -gt 2 ]; then
    	content="$3"
    fi

    $print "$(cell_begin 1 0 $rowspan center center $bgcolor) $content $cell_end"
}


#
#   Blank row
#
#   	$1: number of columns to span
#   	$2: color, defaults to white
#   	$3: content, defaults to &nbsp;
#

function    blank_row()
{
    local colspan=0
    local bgcolor="#FFFFFF"
    local content="&nbsp;"
        
    if [ $# -gt 0 ]; then
    	colspan=$1
    fi
    if [ $# -gt 1 ]; then
    	bgcolor="$2"
    fi
    if [ $# -gt 2 ]; then
    	content="$3"
    fi

    $print "$row_begin
$(cell_begin 0 $colspan 0 center center $bgcolor) $content $cell_end 
$row_end
"
}

#
#   Blank row even
#
#   	$1: number of columns
#   	$2: color, defaults to white
#   	$3: content, defaults to &nbsp;
#

function    blank_row_even()
{
    local   columns=1
    local   width="100"
    local   bgcolor="#FFFFFF"
    local   content="&nbsp;"
    
    if [ $# -gt 0 ]; then
    	if [ $1 -gt 1 ]; then
	    columns=$1
	    let "width = 100 / $columns"
	fi
    fi
    if [ $# -gt 1 ]; then
    	bgcolor="$2"
    fi
    if [ $# -gt 2 ]; then
    	content="$3"
    fi

    $print "$row_begin"
        
    while [ $columns -gt 0 ]
    do
    	$print "$(cell_begin $width 0 0 center center $bgcolor) $content $cell_end"
    	let "columns = columns - 1"
    done
    
    $print "$row_end"
}

#
#   Space
#

space="&nbsp;&nbsp;&nbsp;"

#
#	Brand html
#
#		Branding, yay.
#

function    brand_information()
{
    local max_brand_image_span=8
    
    local brand_image_span=$(brand_image_span)
    
    if [ $brand_image_span -gt $max_brand_image_span ]; then
    	$brand_image_span=$max_brand_image_span
    fi
    
    local brand_image_remain=0
    let "brand_image_remain = 1 + $max_brand_image_span - $brand_image_span"
    
$print "
$table_begin
$(blank_row_even 10)
$row_begin
$(cell_begin) &nbsp; $cell_end
$(cell_begin 0 $brand_image_span 0 $(brand_image_align))
$(branding)
${cell_indent}$cell_end
$(cell_begin 0 $brand_image_remain 0 left)
$content_indent $(title_html)
${cell_indent}$cell_end
$row_end
$(blank_row 10 \#FFFFFF "<hr>")
$(blank_row 10)
$table_end
"
}

#
#   Strong
#

function    strong()
{
    $print "<strong>$*</strong>"
}
