#!/bin/sh
#
# Copyright (C) 2001-2003  Simon Baldwin (simon_baldwin@yahoo.com)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
ME="`basename $0`"

# Check usage.
if [ $# -ne 1 -a $# -ne 2 ]; then
	echo "Usage: $ME defs_file [ output_file ]" >&2
	exit 1
fi
if [ ! -r $1 ]; then
	echo "$ME: can't find or read $1" >&2
	exit 1
fi
if [ $# -eq 2 ]; then
	if [ -f $2 ]; then
		echo "$ME: file $2 already exists" >&2
		exit 1
	fi
	exec 1>$2
fi

# Source in the definitions file.
. $1

# Check mandatory definitions.
for def in IFP_ENGINE_TYPE IFP_ENGINE_NAME IFP_ENGINE_VERSION \
	    IFP_ACCEPTOR_OFFSET IFP_ACCEPTOR_LENGTH; do
	cmd="echo \${$def}"
	if [ -z "`eval $cmd`" ]; then
		echo "$ME: missing mandatory definition of $def" >&2
		exit 1
	fi
done

# Check some numerical values and combinations.
for def in IFP_ACCEPTOR_OFFSET IFP_ACCEPTOR_LENGTH; do
	cmd="expr \"\${$def}\" + 0"
	eval $cmd >/dev/null 2>/dev/null
	if [ $? -eq 2 ]; then
		echo "$ME: $def must be numeric" >&2
		exit 1
	fi
	cmd="echo \${$def}"
	if [ "`eval $cmd`" -lt 0 ]; then
		echo "$ME: $def cannot be less than zero" >&2
		exit 1
	fi
done
if [ -z "$IFP_BLORB_PATTERN" ]; then
	if [ $IFP_ACCEPTOR_LENGTH -eq 0 -o -z "$IFP_ACCEPTOR_PATTERN" ]; then
		echo "$ME: neither Blorb nor normal acceptor specified" >&2
		echo "$ME: to be useful, you need one or the other, or both" >&2
		exit 1
	fi
fi


# Default a couple of options that may be unset.
[ -z "$IFP_HEADER_VERSION" ]	&& IFP_HEADER_VERSION="IFP_HEADER_VERSION_0_2_0"
[ -z "$IFP_BUILD_TIMESTAMP" ]	&& IFP_BUILD_TIMESTAMP="__DATE__\", \"__TIME__"

# Write the output file
echo "/*"
echo " * DO NOT EDIT THIS FILE."
echo " *"
echo " * IFP plugin header definition file.  Generated automatically by"
echo " * $ME from input file $1."
echo " *"
echo " * `date`"
echo " */"
echo "#define IFP_HEADER_ONLY"
echo "#include \"ifp_internal.h\""
echo ""
echo "#define NULL      ((void *)0)"
echo ""
echo "struct ifp_header ifpi_header = {"
echo "/* IFP header     */ ${IFP_HEADER_VERSION},"
echo "/* Timestamp      */ ${IFP_BUILD_TIMESTAMP},"
echo ""
echo "/* Engine type    */ \"${IFP_ENGINE_TYPE}\","
echo "/* Engine name    */ \"${IFP_ENGINE_NAME}\","
echo "/* Version        */ \"${IFP_ENGINE_VERSION}\","
echo ""
if [ -z "${IFP_BLORB_PATTERN}" ]; then
	echo "/* Blorb pattern  */ NULL,"
else
	echo "/* Blorb pattern  */ \"${IFP_BLORB_PATTERN}\","
fi
echo ""
echo "/* Accept offset  */ ${IFP_ACCEPTOR_OFFSET},"
echo "/* Accept length  */ ${IFP_ACCEPTOR_LENGTH},"
echo "/* Accept pattern */ \"${IFP_ACCEPTOR_PATTERN}\","
echo ""
if [ -z "${IFP_AUTHOR_NAME}" ]; then
	echo "/* Author         */ NULL,"
else
	echo "/* Author         */ \"${IFP_AUTHOR_NAME}\","
fi
if [ -z "${IFP_AUTHOR_EMAIL}" ]; then
	echo "/* Author email   */ NULL,"
else
	echo "/* Author email   */ \"${IFP_AUTHOR_EMAIL}\","
fi
if [ -z "${IFP_ENGINE_HOME_URL}" ]; then
	echo "/* Engine URL     */ NULL,"
else
	echo "/* Engine URL     */ \"${IFP_ENGINE_HOME_URL}\","
fi
echo ""
if [ -z "${IFP_BUILDER_NAME}" ]; then
	echo "/* Builder        */ NULL,"
else
	echo "/* Builder        */ \"${IFP_BUILDER_NAME}\","
fi
if [ -z "${IFP_BUILDER_EMAIL}" ]; then
	echo "/* Builder email  */ NULL,"
else
	echo "/* Builder email  */ \"${IFP_BUILDER_EMAIL}\","
fi
echo ""
if [ -z "${IFP_ENGINE_DESCRIPTION}" ]; then
	echo "/* Description    */ NULL,"
else
	description="`echo "${IFP_ENGINE_DESCRIPTION}" | tr -d '\n\"'`"
	echo "/* Description    */ \"$description\","
fi
if [ -z "${IFP_ENGINE_COPYRIGHT}" ]; then
	echo "/* Copyright      */ NULL"
else
	copyright="`echo "${IFP_ENGINE_COPYRIGHT}" | tr -d '\n\"'`"
	echo "/* Copyright      */ \"$copyright\""
fi
echo "};"

# Done successfully.
exit 0
