#!/bin/bash

shopt -s nullglob

BINDIR=$(dirname $0)
PROC=$1

### built-ins

function usage {
    echo "Usage $(basename $0): command [options] ..."
    echo "General purpose commands are:"

    (
    declare -F | grep fdb_builtin_ | sed 's/.*fdb_builtin_/   /'
    for n in info list purge schema stats where wipe
    do
        echo "   $n"
    done
    ) | sort
}

function fdb_builtin_version {
    echo @fdb5_VERSION_STR@
}

function fdb_builtin_home {
    echo $($BINDIR/fdb-info --home)
}

function fdb_builtin_help {
    usage
}

###

declare -f "fdb_builtin_$PROC" > /dev/null
if [[ $? -eq 0  ]]
then
    "fdb_builtin_$PROC"
    exit
fi

CMD=$BINDIR/fdb-$PROC

if [[ ! -x $CMD ]]
then
    usage 1>&2
    exit 1
fi

shift
exec $CMD "$@"
