#!/bin/bash
#
# Copyright (C) 2001-2007  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

# Convert from old to current format if required.
for def in header_version build_timestamp engine_type engine_name \
           engine_version blorb_pattern acceptor_offset acceptor_length \
           acceptor_pattern author_name author_email engine_home_url \
           builder_name builder_email engine_description engine_copyright; do
  old_def="IFP_$(tr 'a-z' 'A-Z' <<< $def)"
  eval "$def=\${$def:-\$$old_def}"
done

# Check mandatory definitions.
for def in engine_type engine_name engine_version acceptor_offset \
           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 acceptor_offset 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 "$blorb_pattern" ]; then
  if [ $acceptor_length -eq 0 -o -z "$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 "$header_version" ] && header_version="IFP_HEADER_VERSION"
[ -z "$build_timestamp" ] && 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 "struct ifp_header ifpi_header = {"
echo "  .version = ${header_version},"
echo "  .build_timestamp = ${build_timestamp},"
echo ""
echo "  .engine_type = \"${engine_type}\","
echo "  .engine_name = \"${engine_name}\","
echo "  .engine_version = \"${engine_version}\","
echo ""
if [ -n "${blorb_pattern}" ]; then
  echo "  .blorb_pattern = \"${blorb_pattern}\","
fi
echo ""
echo "  .acceptor_offset = ${acceptor_offset},"
echo "  .acceptor_length = ${acceptor_length},"
echo "  .acceptor_pattern = \"${acceptor_pattern}\","
echo ""
if [ -n "${author_name}" ]; then
  echo "  .author_name = \"${author_name}\","
fi
if [ -n "${author_email}" ]; then
  echo "  .author_email = \"${author_email}\","
fi
if [ -n "${engine_home_url}" ]; then
  echo "  .engine_home_url = \"${engine_home_url}\","
fi
echo ""
if [ -n "${builder_name}" ]; then
  echo "  .builder_name = \"${builder_name}\","
fi
if [ -n "${builder_email}" ]; then
  echo "  .builder_email = \"${builder_email}\","
fi
echo ""
if [ -n "${engine_description}" ]; then
  description=$(tr -d '\n\"' <<< "${engine_description}")
  echo "  .engine_description = \"$description\","
fi
if [ -n "${engine_copyright}" ]; then
  copyright=$(tr -d '\n\"' <<< "${engine_copyright}")
  echo "  .engine_copyright = \"$copyright\""
fi
echo "};"

exit 0
