#!/bin/bash

XML_USR='/opt/VAIxml'
XML_ETC='/opt/VAIxml/etc'
XML_DOCS='/opt/VAIxml/share/doc'
XML_DOCS_URL='/VAIxml'
XML_PUBLIC_URL='http://www.mekwin.com/vaisala/xml'
COMMON_DIR='/opt/VAIxml/share/vaisala/xml/common'
BUILD_ROOT='/tmp/xml/keyval/install'
SOURCEFORGE_MIRROR='http://umn.dl.sourceforge.net/sourceforge'
CONFIG_FILE='no'
TAG='keyval'
SRCDIR='/tmp/xml/keyval'
DTD='keyval_1_0_0.dtd'
URIROOT='http://www.mekwin.com/vaisala/xml'
URIDTD='http://www.mekwin.com/vaisala/xml/dtd/keyval/keyval_1_0_0.dtd'
PUBLIC_ID='-//VAISALA//DTD KeyValue XML V1.0.0//EN'
PKG_PREFIX='vaisala'
COMMON_DIR='/opt/VAIxml/share/vaisala/xml/common'
TAG='keyval'
VERSION='1.0.0'
VERSION_RELEASE='5'
RPM_REDHAT_ARGS='--config no --usr /usr --etc /etc --docs /var/www/html/vaisala/xml --docs-url http://www.mekwin.com/vaisala/xml --java "java -DproxyHost=172.26.0.232 -DproxyPort=3128" --docbook-xsl "search"'
CATALOG='/opt/VAIxml/share/vaisala/xml/keyval/catalog/keyval_catalog.xml'
CATALOG_OASIS='/opt/VAIxml/share/vaisala/xml/keyval/catalog/keyval_catalog.oasis'
SYSROOT='/opt/VAIxml/share/vaisala/xml/keyval'
XSLROOT='/opt/VAIxml/share/vaisala/xml/keyval/xsl'
BASENAME='/usr/bin/basename'
BASH='/bin/bash'
CAT='/usr/bin/cat'
CHMOD='/usr/bin/chmod'
CP='/usr/bin/cp'
DIRNAME='/usr/bin/dirname'
FIND='/usr/bin/find'
GREP='/usr/bin/grep'
JAVA='java -DproxyHost=172.26.0.232 -DproxyPort=3128'
MKDIR='/usr/bin/mkdir'
RMDIR='/usr/bin/rmdir'
TOUCH='/usr/bin/touch'
TR='/usr/bin/tr'
#
# $Id: support.sh,v 1.6 2006/02/24 19:50:25 pkb Exp $
#
#   Adds supporting script functions - assuming that configuration variables
#   are already defined

# log TEXT
#
#   If the LOG_FILE environment variable is not set to "no" and is
#   pointing at a file which exists, this method will append TEXT
#   information to the end of the file.

log() {

  if [ "${LOG_FILE}" != "no" -a -f "${LOG_FILE}" ]; then
    printf "$1\n" >> "${LOG_FILE}"
  fi

}

# error TEXT [EXIT_CODE]
#
#   Reports error message and optionally exits if a exit_code is passed
#

error() {

  log "***ERROR*** ${1}"

  printf "***ERROR*** ${1}\n"
  if [ "$2" != "" ]; then
    exit "${2}"
  fi

}

# message TEXT
#
#   Reports TEXT message to user (unless QUIET is set to "true") and
#   log file (if enabled).

message() {

  log "$1"
  if [ "$QUIET" != "true" ]; then
    printf "$1\n"
  fi

}

# verify_files [FILE0 [FILE1 ...]]
#
# Verifies that ALL files specified exist and exits with error if not

verify_files() {
  for f in $*; do
    if [ ! -f "$f" ]; then
      error "could not find file: $f"
      exit 2
    fi
  done
}


# query_program PROG [DIR [DIR ... ] ]
#
#  Finds fully qualified path of a executable program.
#
#  On success, the environment variable PROG is set to full location of
#  desired program and we return 0, otherwise, we return 1.
#
#  PROG - name of program to find (we always search user PATH and some
#  other common locations)
#
#  DIR [ DIR ... ] - Optional list of other directories to try

query_program() {
  local FILE="$1"
  shift

  for d in ${PATH//:/ } $* \
    /usr/local/bin /opt/csw/bin /opt/sfw/bin /opt/bin /usr/bin /bin /lan/bin; do
    PROG="${d}/${FILE}";
    if [ -x "$PROG" ]; then
      return 0;
    fi
  done
  PROG=
  return 1;
}


# find_program PROG [URL] [DIR [DIR ... ] ]
#
#  Finds fully qualified path of a executable program, or prints error message
#  and terminates.
#
#  On success, the environment variable PROG is set to full location of
#  desired program.
#
#  PROG - name of program to find (we always search user PATH)
#
#  URL  - Optional URL to refer user to if we don't find PROG
#
#  DIR [ DIR ... ] - Optional list of other directories to try

find_program() {
  local FILE="$1"
  shift
  local URL="$1"
  shift

  if query_program "${FILE}" $* ; then
    return 0;
  fi

  MISSING=true;
  error "unable to find executable program '${FILE}' on system" "${URL}"
  return 1;
}

# require_program ENV PROG [URL] [DIR [DIR ... ] ]
#
#  Finds fully qualified path of a executable program, or prints error message
#  and terminates.
#
#  On success, the environment variable ENV will have the full location of
#  desired program.
#
#  If ENV already points to a executable program, no search is done (we assume
#  it was already found and set by a prior call to this function.
#
#  ENV - Environment variable to be set
#
#  PROG - name of program to find (we always search user PATH)
#
#  URL  - Optional URL to refer user to if we don't find PROG
#
#  DIR [ DIR ... ] - Optional list of other directories to try

require_program() {
  local ENV="${1}";

  # Nothing to do if already set
  if [ -x "${!ENV}" ]; then
    return 0;
  fi

  shift;

  if find_program $*; then
    eval "export ${ENV}=\"${PROG}\";";
    return 0;
  fi

  eval "export ${ENV}=\"did_not_find_${1}\";";
  return 1;
}

# find_class CLASS [URL] [JAR_URL]
#
#   Determines if CLASS can be found in the current Java environment.
#   Requires that CLASS not implement the static void main(String[]) method
#   as we look for the "NoSuchMethod" error in the output of trying to invoke
#   CLASS (which indicates class does exist). Returns 0 if class exists,
#   1 if not (unless RC is passes as well). NOTE: If you pass the JAR_URL
#   parameter, we will attempt to download the JAR file from the JAR_URL
#   specified using wget and make a second try.
#
#   CLASS   - name of class to look for (like:
#             org.apache.xml.resolver.tools.CatalogResolver)
#
#   URL     - Optional, if passed, we will display a error message pointing
#             user to this URL if we are unable to find the class.
#
#   JAR_URL - Optional, if passed, we will attempt to download the necessary
#             JAR file from this URL. 

find_class() {
  local CLASS="${1}";
  local URL="${2}";
  local JARURL="${3}";

  if ! require_program JAVA java ||
     ! require_program GREP grep ||
     ! require_program BASENAME basename ||
     ! require_program MKDIR mkdir; then
    return 1;
  fi

  if [ "${JARURL}" != "" ]; then
    local JARNAME="$(${BASENAME} "${JARURL}")";

    # See if global install under /var/vaisala/jars

    local JARDIR="/var/vaisala/jars";
    local JARFILE="${JARDIR}/${JARNAME}";

    if [ ! -f "${JARFILE}" ]; then
      JARDIR="${HOME}/tmp/vaisala/jars";
      JARFILE="${JARDIR}/${JARNAME}";
    fi

    if [ ! -f "${JARFILE}" ]; then
      printf "\nAttempting to download: ${JARURL}\n\n";

      if ! require_program WGET wget; then
	return 1;
      fi
    
      #
      # Setup user's JAR cache (if necessary)
      #
      if [ ! -d "${JARDIR}" ] && ! ${MKDIR} -p "${JARDIR}"; then
        error "could not create JAR directory: ${JARDIR}"
        MISSING=true;
        return 1;
      fi

      #
      # Download JAR
      #
      if ! ${WGET} "-O${JARFILE}" "${JARURL}"; then
        error "failed to download: ${JARURL}";
        printf "You may want to check your proxy settings ~/.wgetrc\n"
        printf "or the global proxy settings in /etc/wgetrc. Look for:\n"
        printf "\nhttp_proxy = http://172.26.0.232:3128/\nuse_proxy=on\n\n";
        MISSING=true;
        return 1;
      fi
    fi

    export XML_COMMON_CLASSPATH="${XML_COMMON_CLASSPATH}:${JARFILE}";
#    printf "\nLooking for ${CLASS} in:\n  ${JARFILE}\n\n";

#    if ${JAVA} -cp "${XML_COMMON_CLASSPATH}" "${CLASS}" 2>&1 < /dev/null | \
#       ${GREP} NoSuchMethod > /dev/null; then
#       return 0;
#    fi
     return 0;
  fi

  error "could not find class: ${CLASS}" "${URL}"
  MISSING=true;
  return 1;
}

# build_xml_common_classpath
#
#   Sets XML_COMMON_CLASSPATH to list of JAR files required to apply
#   XSL transformations.

build_xml_common_classpath() {

  if [ ! -r "${COMMON_DIR}/sh/pkg_url.sh" ]; then
    error "failed to find JAR URL list at: ${COMMON_DIR}/sh/pkg_url.sh";
    return 1;
  fi

  . "${COMMON_DIR}/sh/pkg_url.sh";

  if find_class org.apache.xalan.xslt.SecuritySupport \
        "${apache_xalanj_URL}" "${apache_xalanj_jar_URL}" &&
     find_class org.apache.xml.resolver.tools.CatalogResolver\
      "${apache_resolver_URL}" "${apache_resolver_jar_URL}"; then

    return 0;

  fi

}


# apply_xsl XML_IN XSL OUT [MORE]
#
# Function to apply XSL to XML document and generate OUT
#
#   XML_IN - XML file to process
#
#   XSL    - XSL file to perform translation with.
#
#   OUT    - Where to store the results
#
#   [MORE] - Optional extra arguments for Xalan parser

apply_xsl() {
  if [ ! -f "$1" ]; then
    error "$1 not found" 1
  fi

  if [ ! -f "$2" ]; then
    error "$2 not found" 1
  fi

  if [ "$3" = "" ]; then
    error "output file not specified" 1
  fi

  IN="$1"
  shift
  XSL="$1"
  shift
  OUT="$1"
  shift

  JAVA="${XML_COMMON_JAVA:-java}";

  if [ "${XML_COMMON_CLASSPATH}" == "" ]; then
    if ! build_xml_common_classpath; then
      error "failed to find/download/install 1 or more required JARS" 1;
    fi
  fi

  ${JAVA} -cp \
   "${XML_COMMON_CLASSPATH}:${XML_USR}/share/vaisala/xml/keyval/catalog" \
   org.apache.xalan.xslt.Process \
   -UriResolver org.apache.xml.resolver.tools.CatalogResolver \
   -EntityResolver org.apache.xml.resolver.tools.CatalogResolver \
   -IN "${IN}" -XSL "${XSL}" -OUT "${OUT}"

}

# install_query_check
#
# This option allows a user to specify options like --install xsl to
# retrieve information about the package installation. This might be
# useful for making use of the package files OUTSIDE of the
# script. --install help will list all possible.

install_query_check() {
  choices=(catalog catalog-oasis docs dtd public-id public-url url xsl)
  results=("${CATALOG}"\
 "${CATALOG_OASIS}"\
 "${XML_DOCS}/${TAG}"\
 "${SYSROTO}/dtd/${DTD}"\
 "${PUBLIC_ID}"\
 "${URIDTD}"\
 "${URIROOT}/${TAG}"\
 "${XSLROOT}")

  if [ "$INSTALL_QUERY" = "" ]; then
    return 0;
  fi

  n="${#choices[*]}"
  ((i=0))
  while (( i < n )); do
    if [ "$INSTALL_QUERY" = "${choices[$i]}" ]; then
      echo "${results[$i]}"
      exit 0;
    fi
    ((i=i+1))
  done

  echo "Use '--install-query NAME' where NAME is one of:"
  echo "  ${choices[*]}"

  if [ "$INSTALL_QUERY" = "help" ]; then
    exit 0
  fi
  exit 1;

}

#!/bin/bash

#
# AUTOMATICALLY GENERATED - DO NOT EDIT!
#

#
# Assume command line options are OK
#

OK=true

VARNAME_LIST="NAME TEMPLATES LOG_FILE INSTALL_QUERY "

# print_config
#
# Displays current configuration in format suitable for sourcing

print_config() {
    printf "#\n# configuration options for keyval2java script\n#\n\n"
  for e in ${VARNAME_LIST}; do
    if [ "${e}" != "CONFIG_FILE" ]; then
      printf "${e}=%q\n" "${!e}";
    fi
  done
}

# check_config_file CMD
#
# This function checks the CMD value passed. Following rules are
# imposed:
#
#   "" (unset) or "no"
#     Nothing is done
#   "load" 
#     If file $HOME/.xml.keyval2java.sh exists it is sourced.
#     If not found error message and exit.
#   "auto" 
#     Similar to "load", but does nothing if default config not found.
#   "save" 
#     Settings are saved to $HOME/.xml.keyval2java.sh for future load.
#   "show"
#     Current settings are shown
#   FILE
#     We attempt to load config from FILE specified.

check_config_file() {
  local SCFG="/etc/xml.keyval2java.conf"
  local UCFG="${HOME}/.xml.keyval2java.conf"
  local CMD="$1"

  if [ "$CMD" = "no" -o "$CMD" = "" ]; then
    return 0;
  elif [ "$CMD" = "show" ]; then
    print_config
    exit 0
  elif [ "${CMD}" = "save" ]; then
    print_config > "${UCFG}"
    echo "Saved configuration as: ${UCFG}";
    echo "Will automatically be loaded at next configure invocation."
    exit
  elif [ "${CMD}" = "auto" ]; then
    for f in "${SCFG}" "${UCFG}"; do
      if [ -f "${f}" ]; then
        . "${f}"
      fi
    done
  elif [ "${CMD}" = "load" ]; then
    if [ -f "${UCFG}" ]; then
      for f in "${SCFG}" "${UCFG}"; do
        if [ -f "${f}" ]; then
          . "${f}"
        fi
      done
    else
      error "unable to read config: ${UCFG}"
      exit 1
    fi
  elif [ -f "${CMD}" ]; then
    for f in "${SCFG}" "${CMD}"; do
      if [ -f "${f}" ]; then
        . "${f}"
      fi
    done
  else
    error "for configuration files, use: --config no|save|load|show|FILE"
    exit 1
  fi
}

# list_options
#
# Displays all options on single line

append_line() {
  LLEN=${#LINE}
  NLEN=$((${LLEN}+${#1}+1))
  if [ "$((${NLEN} > 72))" != "0" ]; then
    printf "${LINE}\n";
    LINE="        ";
  fi
  LINE="${LINE} ${1}"
}

list_options() {
  LINE="  $(basename "$0")";
  append_line "[-n|--name TEXT]"
  append_line "[-t|--templates true|false]"
  append_line "[-l|--log-file FILENAME]"
  append_line "[--install-query TEXT]"

  append_line " [-h|--help]";
  printf "${LINE}\n";
}

# show_usage
#
# Displays help screen

show_usage() {
  printf "\nUsage:\n"
  list_options | fold -s 
  printf "\nWhere:\n"
  printf "  -n|--name"
  printf " TEXT  Currently:[$NAME]\n"
  printf "    Specify the name of Java class to hold configuration information.\n"
  printf "  -t|--templates"
  printf " true|false  Currently:[$TEMPLATES]\n"
  printf "    Control whether template file(s) are created.\n"
  printf "  -l|--log-file"
  printf " FILENAME  Currently:[$LOG_FILE]\n"
  printf "    Log of detailed output from processing files.\n"
  printf "  --install-query"
  printf " TEXT  Currently:[$INSTALL_QUERY]\n"
  printf "    Query information about the installed package.\n"
  printf "  -h|--help\n    Displays this usage information.\n"
  printf "\n"
}

# error TEXT
#
# Reports error message to screen and sets OK to false - does NOT exit

error() {
  echo "***ERROR*** $1" 1>&2
  OK=false
}

# error_if_switch TEST
#
# makes sure TEST doesn't start with "-"

error_if_switch() {
  local VAL="${1}";
  if [ "${VAL:0:1}" = "-" ]; then
    error "'${VAL}' is not acceptable after ${2:-switch}";
  fi

}

# get_value_for TYPE FROM CNT OPT [MIN] [MAX]
#
# Sets VALUE to the value parsed from FROM. TYPE indicates the type of value
# we are looking for. CNT is 0 if FROM wasn't available (number of
# arguments left on line). OPT is only used for error reporting (the
# command line switch we are looking for). Current rules (based on
# TYPE):
#
# bool
#   If CNT is 0, then echo "true"
#   If FROM starts with '-', then VALUE="$TRUE"
#   Otherwise, accept and set VALUE=FROM if it is "true" OR "false"
#
# int64/double
#   If CNT is 0, then error
#   Otherwise, we accept anything ('-' might be negative)
#
# OTHER
#   If CNT is 0 or FROM starts with '-' then error
#   Otherwise set VALUE=FROM
#
# returns 1 is FROM was used or 0 if not (use via: shift $?)
#
# Non-Error Examples:
#
#   get_value_for "bool" "" "0" "--enable"   # VALUE=true returns 0
#   get_value_for "bool" "--in" "12" "--enable"  # VALUE=true returns 0
#   get_value_for "bool" "false" "12" "--enable" # VALUE=false returns 1
#   get_value_for "file" "file.txt" "1" "--in" # VALUE=file.txt returns 1
#
# Error Examples:
#
#   get_value_for "bool" "no" "1" "--enable" # no not allowed 
#   get_value_for "file" "" "0" "--in" # missing value (0 count)
#   get_value_for "file" "--out" "12" "--in" # omitted value

get_value_for() {
  local TYPE="${1}"; # Type of value (like "int64")
  local VAL="${2}";  # Value to check (like "7")
  local CNT="${3}";  # Maximum number of values (like "1")
  local OPT="${4}";  # Option name (like "--verbosity")

  if [ "${TYPE}" == "bool" ]; then
                           # if true/false omitted, default to true
    if [ "${CNT}" = "0" -o "${VAL:0:1}" = "-" ]; then
      VALUE="true";
      return 0;
    fi

    if [ "${VAL}" != "true" -a "${VAL}" != "false" ]; then
      error "'${VAL}' is unacceptable (it was not true or false)."
      exit 1;
    fi
    VALUE="${VAL}";
    return 1;
  fi
  
  if [ "${CNT}" = "0" ]; then
    error "missing value for ${OPT} switch"
    exit 1;
  fi

  #
  # Allow leading '-' for number types
  #
  if [ "${TYPE}" = "double" -o "${TYPE}" = "int64" ]; then
    local MIN="${5}";
    local MAX="${6}";
    local VALID="true";

    # We can check min/max values for integers
    if [ "${TYPE}" = "int64" ]; then
      TYPE="integer";
      local IVAL="${VAL}"; # Strip leading "+" if present
      if [ "${VAL:0:1}" = "+" ]; then
        IVAL="${VAL:1}";
      fi

      if [ "$((VAL+0))" != "${IVAL}" ]; then
        VALID="false";
      elif [ "${MIN}" != "" -a "$((VAL < MIN))" == "1" ]; then
        VALID="false";
      elif [ "${MAX}" != "" -a "$((VAL > MAX))" == "1" ]; then
        VALID="false";
      fi
    elif [ "${TYPE}" = "double" ]; then
      TYPE="real number";
      # fix signs for dc
      local DVAL="${VAL//-/_}";
      DVAL="${DVAL//+/}";
      local DMIN="${MIN//-/_}";
      DMIN="${DMIN//+/}";
      local DMAX="${MAX//-/_}";
      DMAX="${DMAX//+/}";

      if [ "$(echo "[0p]sa" "${DVAL}" 0.0 + "${DVAL}" "!=a" | dc 2>&1)" = "0" ]; then
        VALID="false";
      elif [ "${MIN}" != "" -a\
             "$(echo "[0p]sa" "${DVAL}" "${DMIN}" ">a" | dc 2>&1)" = "0" ]; then
        VALID="false";
      elif [ "${MAX}" != "" -a\
             "$(echo "[0p]sa" "${DVAL}" "${DMAX}" "<a" | dc 2>&1)" = "0" ]; then
        VALID="false";
      fi
    fi


    if [ "${VALID}" == "true" ]; then
      VALUE="${VAL}";
      return 1;
    fi

    error "${OPT} requires a ${TYPE} in range [${MIN:--INF},${MAX:-INF}] (${VAL} is bad)."
    exit 1;
  fi

  if [ "${VAL:0:1}" = "-" ]; then
    error "'${VAL}' is not acceptable after ${OPT} switch"
    exit 1;
  fi
  VALUE="${VAL}";
  return 1;
}

#
# Set default values
#

NAME="";
TEMPLATES="true";
LOG_FILE="/dev/null";
INSTALL_QUERY="";

# process_args CMD ARG0 ARG1 ...
#
#   Process command line arguments passed to CMD

process_args() {

  while test $# -gt 0; do

    OPT="$1";
    shift

    case "$OPT" in

    -h | --help)
      check_config_file "$CONFIG_FILE"
      show_usage;
      exit 1;
      ;;

    -n | --name)
      get_value_for std::string "${1}" "$#" "$OPT" "" ""
      shift $?
      NAME="${VALUE}";
      ;;

    -t | --templates)
      get_value_for bool "${1}" "$#" "$OPT" "" ""
      shift $?
      TEMPLATES="${VALUE}";
      ;;

    -l | --log-file)
      get_value_for std::string "${1}" "$#" "$OPT" "" ""
      shift $?
      LOG_FILE="${VALUE}";
      ;;

    --install-query)
      get_value_for std::string "${1}" "$#" "$OPT" "" ""
      shift $?
      INSTALL_QUERY="${VALUE}";
      ;;


    *)
      error "'$OPT' is not a valid option!"
    esac
  done

}

# check_required_args [EXIT_CODE]
#
#   Checks all arguments which have a default value of "REQUIRED". If
#   these arguments have not been specified by the user, then echos a
#   message (and optionally exits with EXIT_CODE).
#
# EXIT_CODE
#   If this argument is passed, we will exit the script if
#   any required arguments are found to be missing.
#
# RETURNS
#   0 if we were not missing any required arguments, 1 if we were (and
#   we didn't exit).

check_required_args() {

  return 0;
}

process_args "$@"

# If configuration files are enabled (CONFIG_FILE set), then check
# user value. Will need to reload command line arguments if "--config
# auto" specified (as it implies different default settings).

if [ "$CONFIG_FILE" != "" ]; then
  check_config_file "$CONFIG_FILE"

  if [ "$CONFIG_FILE" = "auto" ]; then
    process_args "$@"
  fi
fi

#
# If any obvious errors, show the usage and exit now!
#

if [ "$OK" != "true" ]; then
  show_usage 1>&2
  exit 2
fi

#
# END OF SECTION GENERATED BY keyval_to_sh.xsl
#
#
# $Id: keyval2java.sh,v 1.4 2006/02/24 15:33:41 pkb Exp $
#

#
# Allow user to request information about the install
#

install_query_check

#
# We require the --name option (but that's about all we require)
#

[ "$NAME" = "" ] && error "the --name NAME option was not set properly" 2

#
# Create a template file if it doesn't exist
#

XML_FILE="${NAME}_config.xml"

if [ ! -f "${XML_FILE}" ]; then
  [ "$TEMPLATES" != "true" ] && error "missing file: ${XML_FILE}" 2

  message "creating template file: ${XML_FILE}"

  cat >| "${XML_FILE}" <<EOF
<?xml version="1.0"?>

<!-- $(printf "\$%s\$" "Id")

  Initial template definition of configuration for ${NAME}.hh

-->

<!DOCTYPE keyval PUBLIC
   "-//VAISALA//DTD KeyValue XML V1.0.0//EN"
   "http://www.mekwin.com/vaisala/xml/dtd/keyval/keyval_1_0_0.dtd">

<keyval class="${NAME}" namespace="${NAMESPACE}">

  &verbosity;

  <integer default="0" min="0" max="1000000000" varname="Count">
    <key>${NAMESPACE}.${NAME}.count</key>

    <summary>How many times we should do something.</summary>

    <description>If this option is set to a value larger than 0, then
we stop simulating data at the count specified. This can be useful
during the verification process (if you set this option to
<literal>100</literal>, then you should get <literal>100</literal>
solutions out of your system if things are working correctly. If
you set this value to <literal>0</literal>, then no count limit
will be imposed (we will keep simulating data until you kill the
process).</description>
  </integer>

</keyval>
EOF
fi

# generate TYPE POSTFIX [SUBDIR]
#
# Generate from XML source using XSL TYPE to create file ${NAME}${POSTFIX}
# in subdirectory $SUBDIR (or current directory if omitted). Sets OK="false"
# if a failure occurs.

generate() {
  TYPE="${1}"
  POSTFIX="${2}"
  verify_files "${XSLROOT}/keyval2${TYPE}.xsl"

  SDIR="${3:-.}"
  if [ ! -d "$SDIR" ]; then
    mkdir -p "$SDIR"
  fi

  OFILE="${SDIR}/${NAME}${POSTFIX}"
  CMD="apply_xsl ${XML_FILE} ${XSLROOT}/keyval2${TYPE}.xsl ${OFILE}"
  message "\nGenerating: ${OFILE} with:\n${CMD}\n"
  $CMD >> "${LOG_FILE:-/dev/null}" 2>&1
  if [ "$?" != "0" ]; then 
    OK="false";
  fi
}

#
# Generate files by applying XSL templates to XML document
#

OK="true"
generate java .java
generate java_config ValuesConfig.java
generate java_editor ValuesEditor.java
generate java_editor_properties .properties msgs
generate java_help .html help

if [ "$OK" != "true" ]; then
  error "one or more XSL translations failed (rc:${OK}) - stopping!";
  exit 1;
fi

#
# If templates have been disabled, then we're done, otherwise stub in
# some starter code
#

if [ "$TEMPLATES" != "true" ]; then
  exit 0;
fi

#
# Finally done
#

message "Finished - finally!\n"\
"STEP 1: Add \"${NAME}_config.cc\" to your makefile.\n"\
"STEP 2: Add the following line to your applications .man.m4 file:\n"\
"\tm4_include([\"${NAME}_config.m4i\"])\n"\
"STEP 3: As new options are required, edit the configuration file:\n"\
"\t${NAME}_config.xml\n"
