#!/bin/sh

set -eu

SUPPORTED_VERSIONS="2.5 2.1 2.0 1.5"
SUPPORTED_UTILS="fbstat gbak gdef gfix gpre isql-fb qli gsec nbackup"
BASE="/usr/lib/firebird"

PROG=`basename $0`

FOUND=""
for i in $SUPPORTED_UTILS;
do
    if [ "$i" = "$PROG" ];
    then
        FOUND="$i"
        break
    fi
done

if [ -z "$FOUND" ];
then
    echo "$PROG utility is not known."
    exit 1
fi

if [ -n "${FB_VER:-}" ]; then
    export FIREBIRD="$BASE/$FB_VER"

    if [ -x "/usr/bin/$PROG-$FB_VER" ];
    then
        exec "/usr/bin/$PROG-$FB_VER" "$@"
    else
        echo /usr/bin/$PROG-$FB_VER is not executable. Try installing firebird$FB_VER-super or firebird$FB_VER-classic packages, or specify different version in 'FB_VER' environment variable.
        exit 1
    fi
fi

for v in $SUPPORTED_VERSIONS; do
    if [ -x "/usr/bin/$PROG-$v" ]; then
        export FIREBIRD="$BASE/$v"
        exec "/usr/bin/$PROG-$v" "$@"
    fi
done

echo "No supported version of '$PROG' utility was found."
echo "Please install some of the firebirdVER-FLAVOUR packages"
echo "VER beng one of $SUPPORTED_VERSIONS"
echo "and FLAVOUR being 'super' or 'classic'"

