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

source /usr/local/lib/hostsharing/misc
test -f /root/etc/config && source /root/etc/config

#==============================================================================
function pacs_by_name() {
# syntax:   pacs_by_name [--hive HIVE] PACKET-WILDCARD
# sample:   pacs_by_name 'hsh*'
#       returns all packages with prefix hsh
# sample:   pacs_by_name '*'
#       returns all packets
# synopsis: subroutine queries all packets with a name matching the
#       wildcard. Only packets for the local hive are matched.
# options:  --hive HIVE
#           specifies the hives to search, default is THIS_HIVE,
#           "-" can be used to search all hives

if [ -n "$THIS_HIVE" ]; then
    aHive="--hive=$THIS_HIVE"
else
    aHive=""
fi
while [ "${1:0:1}" = "-" ]; do
    case $1 in
        --hive|-h)
            if [ "$2" = '-' -o "$2" = '*' ]; then
                aHive=
            else
                aHive="--hive=$2"
            fi
            shift
            ;;

        *)
            echo "ERROR: invalid option '$1' to pacs_by_name" >&2
            ;;
    esac
    shift
done

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

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

return ${rc}
}

#==============================================================================
function pacs_by_member() {
# syntax:	pacs_by_member MEMBER-REGEXP
# sample:	pacs_by_member 'xyz00-otto'
# synopsis:	subroutine queries all packets with a name matching the
#		pattern.

local rc=1

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

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

return ${rc}
}

#==============================================================================
function pacs_by_type() {
# syntax:	pacs_by_type TYPE
# sample:	pacs_by_type 'DW*'
# synopsis:	subroutine queries all packets of the specified type

local rc=1

if [ -n "$THIS_HIVE" ]; then
	aHive="--hive=$THIS_HIVE"
else
	aHive=""
fi
while [ "${1:0:1}" = "-" ]; do 
	case $1 in
		--hive|-h) 
			if [ "$2" = '-' -o "$2" = '*' ]; then 
				aHive=
			else
				aHive="--hive=$2"
			fi
			shift
			;;

		*)
			echo "ERROR: invalid option '$1' to pacs_by_type" >&2
			;;
	esac
	shift
done


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

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

return ${rc}
}

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

PAC_NAME=''
PAC_MEMBER=''
PAC_OWNER=''
PAC_TYPE=''
PAC_HOST=''
PAC_MULTI=''
PAC_DATE=''
PAC_CANCELLED=''
PAC_QUOTA=''
PAC_STORAGE=''
PAC_TRAFFIC=''
PAC_CPU=''
PAC_RAM=''
PAC_IPNUMBER=''
PAC_IPNUMBER_EX=''

return 0
}

#==============================================================================
function pac_get() {
# syntax:	pac_get	PACKET
# sample:	pac_get xyz00
# synopsis:	subroutine reads the data for a packet into variables
#		beginning with PAC_

local rc=1

pac_clear

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

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

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

  source ${TEMPFILE}
  rm -f ${TEMPFILE}

  # set PAC_OWNER
  if [ "${PAC_MEMBER:1:6}" = "hsh00-" ]; then
    PAC_OWNER=${PAC_MEMBER:6}
  else
    PAC_OWNER=$1
  fi

  # compute some date fields in YMD and DMY
  if [ -n "$PAC_DATE" ]; then
    PAC_SINCE_DMY=$PAC_DATE
    PAC_SINCE_YMD=${PAC_DATE:6:4}-${PAC_DATE:3:2}-${PAC_DATE:0:2}
  else
    PAC_SINCE_DMY=""
    PAC_SINCE_YMD=""
  fi

  if [ -n "$PAC_CANCELLED" ]; then
    PAC_UNTIL_DMY=$PAC_CANCELLED
    PAC_UNTIL_YMD=${PAC_CANCELLED:6:4}-${PAC_CANCELLED:3:2}-${PAC_CANCELLED:0:2}
  else
    PAC_UNTIL_DMY=""
    PAC_UNTIL_YMD=""
  fi

  # determine PAC_BASETYPE
  PAC_BASETYPE=`echo $PAC_TYPE | cut -d'/' -f1`

  # is active (not cancelled yet)
  PAC_ISCANCELLED=
  if [ -n "$PAC_CANCELLED" ]; then
    aSortableCancDate=`convGermanToSortableDate $PAC_CANCELLED`
    nPacSecs=`date -d$aSortableCancDate +%s`
    nNowSecs=`date +%s`
    if [ "$nNowSecs" -gt "$nPacSecs" ]; then
      PAC_ISCANCELLED=1
    fi
  fi
else
  echo >&2 "pac_get: Need exactly one argument"
fi

return ${rc}
}

#==============================================================================
function pac_print() {
# syntax:	pac_print PACKET
# sample:	pac_print xyz00
# synopsis:	subroutine prints the data for a pac

local rc=1

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

  if [ ${rc} -eq 0 ]; then
    cat <<eof
PAC_NAME='${PAC_NAME}'
PAC_MEMBER='${PAC_MEMBER}'
PAC_DATE='${PAC_DATE}'
PAC_CANCELLED='${PAC_CANCELLED}'
PAC_TYPE='${PAC_TYPE}'
PAC_HOST='${PAC_HOST}'
PAC_MULTI='${PAC_MULTI}'
PAC_QUOTA='${PAC_QUOTA}'
PAC_STORAGE='${PAC_STORAGE}'
PAC_TRAFFIC='${PAC_TRAFFIC}'
PAC_CPU='${PAC_CPU}'
PAC_RAM='${PAC_RAM}'
PAC_IPNUMBER='${PAC_IPNUMBER}'
PAC_IPNUMBER_EX='${PAC_IPNUMBER_EX}'
eof
  fi
else
  echo >&2 "pac_print: Need exactly one argument"
fi

return ${rc}
}

#==============================================================================
function pac_exists() {
# syntax:	pac_exists [-n] PACKET
# sample:	pac_exists xyz00
# 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=pac_exists --arg=$1
  rc=$?

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

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

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

return ${rc}
}
