#!/bin/bash
#
# $Id: vocal_util_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 Utility Library 
#
##################################################################


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

VOCAL_UTIL_LIB=1

#
#   Tools definitions
#

print="echo -e"
ln="ln"
ls="/bin/ls"
wc="/usr/bin/wc"
head="/usr/bin/head"
tail="/usr/bin/tail"
egrep="/bin/egrep"
mkdir="/bin/mkdir"
chmod="/bin/chmod"
cd="builtin cd"
cp="/bin/cp"
find="/usr/bin/find"
rm="/bin/rm -f"
cat="/bin/cat"
basename="/bin/basename"

#
#   Debugging info
#

debug=1

log_warning=1
log_info=2
log_debug=3
log_verbose=4

log_file=""

function    log_file()
{
    if [ -z "$log_file" ]; then
    	log_file="$PWD/$(basename $0).log"
    fi
    $print "$log_file"
}
log_file=$(log_file)


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


function    log()
{
    local log_lvl=$log_debug

    if [ ! $# -eq 1 ]; then
	if [ ! $# -ge 2 ]; then
		return
	fi
	log_lvl=$1
	shift
    fi

    if [ $log_lvl -le $(log_level) ]; then
	case $log_lvl in
	    $log_warning )
	    	$print "warning: $*" >> $log_file
	    ;;

	    $log_info )
	    	$print "$*" >> $log_file
	    ;;

	    $log_debug | log_verbose | *)
	        $print "debug: $*" >> $log_file
	    ;;
	esac
    fi
}


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


function    assert()
{
    log $log_verbose "assert: $*"

    if $*; then
    	return 0
    else
	$print "$(usage)"
	exit 1
    fi
}


function    abs_path()
{
    log $log_verbose "absolute_path: $*"

    $print "$*" | $egrep "/\.\./|^\.\./|/\.\.$|^\.\.$" -
    if [ $? -eq 0 ]; then
    	$print "false"
    fi
    $print "true"
}


function    make_dir()
{
    log $log_verbose "make_dir: $*"
    
    local directory="$*"
    
    if [ -e "$directory" ]; then
	assert [ -d "$directory" ]	# Make sure it's a directory
	assert [ -w "$directory" ]	# Make sure it's writable
	assert [ -x "$directory" ]	# Make sure it's executable
    else
	$mkdir "$directory"
	assert [ $? -eq 0 ]

	$chmod 775 "$directory"
	assert [ $? -eq 0 ]
    fi
}


function    make_sym_link()
{
    log $log_verbose "make_sym_link: $*"
    
    assert [ $# -eq 2 -o \( $# -eq 1 \) ]
    
    local   target="$1"
    local   symlink="${target##*/}"
    
    if [ $# -eq 2 ]; then   
    	symlink="$2"
    fi

    log $log_verbose "target: $target, symlink: $symlink"
    
    if [ -e "$symlink" ]; then
    	assert [ -L "$symlink" ]    	# Make sure it's a symbolic link
    else
    	$ln -s $target $symlink 
	assert [ $? -eq 0 ]
    fi
}

function    content_type_value()
{
    if [ -z "$content_type_value" ]; then
    	content_type_value="text/html"
    fi
    $print "$content_type_value"
}
content_type_value="$(content_type_value)"

function    content_type()
{
    $print "Content-type: $(content_type_value)"
}
content_type=$(content_type)

