#! /bin/sh


# ----------------------------------------------------------------------- #
# Source common functions used here.
# ----------------------------------------------------------------------- #

. "${EF_ROOT}/plugins/ef/lib/xmlfuncs"



# ----------------------------------------------------------------------- #
# Define URI using this strategy:
# 1) use _spooler if defined
# 2) use uri if defined (backward compat)
# 3) use first parameter if defined
# 4) use current spooler URI (i.e., EF_SPOOLER_URI)
# ----------------------------------------------------------------------- #

if [ -n "${_spooler}" ] ; then
    URI="${_spooler}"
elif [ -n "${uri}" ] ; then
    URI="${uri}"
elif [ -n "$1" ] ; then
    URI="$1"
fi
if [ -z "${URI}" ]; then
    URI="${EF_SPOOLER_URI}"
fi
export URI



# --------------------------------------------------------------------------- #
# EF_reset_spooler
# ================
#
# Reset an EnginFrame spooler.
#
#   ef_reset_spooler --uri <spooler-uri> --ttl <ttl> [--name <name>]
#   ef_reset_spooler --uri <spooler-uri> --expiration-time <time> [--name <name>]
#   ef_reset_spooler --uri <spooler-uri> --name <name>
#
# Options:
# --uri <spooler-uri>:
#     The spooler to reset
# --ttl <ttl>:
#     Time-to-live to extend (or reduce) the spooler life.
# --expiration-time <time>:
#     The absolute time when the spooler will expire in the form defined
#     in RFC3339 (See http://www.ietf.org/rfc/rfc3339.txt).
#     Example: 2011-05-18T16:20:34+02:00
# --name <name>:
#     The new name to assign to the spooler
# Returns:
# 0 in case of success
# 1 in case of error
#
# XML Produced is:
#   <ef:reset-spooler uri="[spooler-uri]" ttl="[ttl]"/>
# or
#   <ef:reset-spooler uri="[spooler-uri]">
#     <ef:name>[new name]</ef:name>
#     <ef:expiration-time>[date-time in the form: 2011-05-18T16:20:34+02:00]</ef:expiration-time>
#   </ef:reset-spooler>
#
# --------------------------------------------------------------------------- #

ef_reset_spooler() {

    _ef_reset_spooler_uri=""
    _ef_reset_spooler_ttl=""
    _ef_reset_spooler_name=""
    _ef_reset_spooler_expiration_time=""

    while [ -n "$1" ]; do
        case "$1" in

            --uri)
                _ef_reset_spooler_uri="$2"
                shift
                ;;

            --ttl)
                _ef_reset_spooler_ttl="$2"
                shift
                ;;

            --name)
                _ef_reset_spooler_name="$2"
                shift
                ;;

            --expiration-time)
                _ef_reset_spooler_expiration_time="$2"
                shift
                ;;

            *)
                shift
                ;;

        esac
        shift
    done

  # URI must exist
  if [ -z "$_ef_reset_spooler_uri" ]; then
    echo "ef_reset_spooler: The uri parameter must be specified" >&2
    return 1
  fi

  # TTL and expiration-time cannot coexist
  if [ -n "$_ef_reset_spooler_ttl" -a -n "$_ef_reset_spooler_expiration_time" ] ; then
    echo "ef_reset_spooler: Cannot specify ttl and expiration-time together" >&2
    return 1
  fi

  # Emit reset spooler tag with ns and uri
  _ef_reset_spooler_uri=`ef_xml_escape -a -i "$_ef_reset_spooler_uri"`
  printf '<ef:reset-spooler xmlns:ef="%s" uri="%s"' "${EF_XMLNS}" "$_ef_reset_spooler_uri"

  # Emit ttl
  if [ -n "$_ef_reset_spooler_ttl" ] ; then
    _ef_reset_spooler_ttl=`ef_xml_escape -i "$_ef_reset_spooler_ttl"`
    printf ' ttl="%s"' "$_ef_reset_spooler_ttl"
  fi

  # Close reset spooler start tag
  printf '>\n'

  # Emit spooler name
  if [ -n "$_ef_reset_spooler_name" ] ; then
    _ef_reset_spooler_name=`ef_xml_escape -i "$_ef_reset_spooler_name"`
    printf '  <ef:name>%s</ef:name>\n' "$_ef_reset_spooler_name"
  fi

  # Emit expiration time
  if [ -n "$_ef_reset_spooler_expiration_time" ] ; then
    _ef_reset_spooler_expiration_time=`ef_xml_escape -i "$_ef_reset_spooler_expiration_time"`
    printf '  <ef:expiration-time>%s</ef:expiration-time>\n' "$_ef_reset_spooler_expiration_time"
  fi

  printf '</ef:reset-spooler>\n'

  return 0
}
