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

#==============================================================================
function addr_clear() {
# syntax:	addr_clear
# sample:	addr_clear
# synopsis:	subroutine clears the variables of an addr record

SALUT=''
SALUTATION=''
TITLE=''
FIRST_NAME=''
LAST_NAME=''
FIRMA=''
CO=''
STREET=''
ZIPCODE=''
CITY=''
COUNTRY=''
PHONE_PRIVATE=''
PHONE_OFFICE=''
PHONE_MOBILE=''
FAX=''
EMAIL_EXTERNAL=''
UID_VAT=''
MEMBER_ID=''
MEMBER_CODE=''
MEMBER_ROLE=''
MEMBER_SINCE=''
MEMBER_UNTIL=''

return 0;
}

#==============================================================================
function addr_get() {
# syntax:	addr_get ID
# sample:	addr_get hsh00-xyz
# synopsis:	subroutine reads the data for an address into variables

local rc=1

# initialize record
addr_clear

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

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

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

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

return ${rc}
}

#==============================================================================
function addr_get_by_index () {
# syntax:	addr_get_by_index ID INDEX
# sample:	addr_get_by_index hsh00-xyz 0
# synopsis:	subroutine reads the data for an address into variables, identified by index

local rc=1

# initialize record
addr_clear

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

  hsdb --cmd=addr_get_by_index --arg=$1 --file=${TEMPFILE} --index=$2
  rc=$?

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

  if [ -f ${TEMPFILE} ]; then
    source ${TEMPFILE}
    rm -f ${TEMPFILE}
  fi
else
  echo >&2 "addr_get_by_index: Need exactly two arguments"
fi

return ${rc}
}

#==============================================================================
function addr_print() {
# syntax:	addr_print ID
# sample:	addr_print hsh00-xyz
# synopsis:	subroutine prints the given address record on stdout

local rc=1

if [ $# -eq 1 ]; then
  addr_get $1
  rc=$?

  if [ ${rc} -eq 0 ]; then
    cat <<eof
SHORT_NAME='$1'
SALUT='${SALUT}'
SALUTATION='${SALUTATION}'
TITLE='${TITLE}'
FIRST_NAME='${FIRST_NAME}'
LAST_NAME='${LAST_NAME}'
FIRMA='${FIRMA}'
CO='${CO}'
STREET='${STREET}'
ZIPCODE='${ZIPCODE}'
CITY='${CITY}'
COUNTRY='${COUNTRY}'
PHONE_PRIVATE='${PHONE_PRIVATE}'
PHONE_OFFICE='${PHONE_OFFICE}'
PHONE_MOBILE='${PHONE_MOBILE}'
FAX='${FAX}'
EMAIL_EXTERNAL='${EMAIL_EXTERNAL}'
UID_VAT='${UID_VAT}'
MEMBER_ID='${MEMBER_ID}'
MEMBER_CODE='${MEMBER_CODE}'
MEMBER_ROLE='${MEMBER_ROLE}'
MEMBER_SINCE='${MEMBER_SINCE}'
MEMBER_UNTIL='${MEMBER_UNTIL}'
eof
  fi
else
  echo >&2 "addr_print: Need exactly one argument"
fi

return ${rc}
}

#==============================================================================
function addr_exists() {
# syntax:	addr_exists [-n] ID
# sample:	addr_exists hsh00-xyz
# synopsis:	subroutine checks whether a record exists
# options:	-n -> not exists

local rc=1

# get options
local aNot=""
if [ "$1" = "-n" ]; then
  aNot=!
  shift
fi

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

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

  test ${aNot} ${rc} -eq 0
  rc=$?

else
  echo >&2 "addr_exists: Need exactly one argument"
fi

return ${rc}
}
