#!/bin/bash
#
# synapsis:	libaray to read and write the hive database
# copyright:	(C) 2004, Hostsharing eG, Hamburg, Germany
# license:	GPL
#
# $Id$
#


#==============================================================================
function hives_by_name() {
# syntax:	hives_by_name HIVE-WILDCARD
# sample:	hives_by_name 'h*'
#		returns all hives with prefix h
# sample:	hives_by_name '*'
#		returns all hives
# synopsis:	subroutine queries all hives with a name matching the
#		wildcard.

local rc=1

if [ $# -eq 1 ]; then
  hsdb --cmd=hives_by_name --arg=$1
  rc=$?

  while [ ${rc} -eq 255 ]; do
    sleep 15
    hsdb --cmd=hives_by_name --arg=$1
    rc=$?
  done
else
  echo >&2 "hives_by_name: Need exactly one argument"
fi

return ${rc}
}

#==============================================================================
function hive_clear() {
# syntax:	hive_clear
# sample:	hive_clear
# synopsis:     subroutine clears the variables of a hive record

HIVE_NAME=''
HIVE_IPNUMBER=''
HIVE_DESC=''

return 0
}

#==============================================================================
function hive_get() {
# syntax:	hive_get HIVE
# sample:	hive_get h01
# synopsis:	subroutine reads the data for a hive into variables
#		beginning with HIVE_

local rc=1

hive_clear

if [ $# -eq 1 ]; then
  local TEMPFILE=`mktemp -t hsdb.XXXXXX`

  hsdb --cmd=hive_get --arg=$1 --file=${TEMPFILE}
  rc=$?

  while [ ${rc} -eq 255 ]; do
    sleep 15
    hsdb --cmd=hive_get --arg=$1 --file=${TEMPFILE}
    rc=$?
  done

  source ${TEMPFILE}
  rm -f ${TEMPFILE}
else
  echo >&2 "hive_get: Need exactly one argument"
fi

return ${rc}
}
