#!/bin/ksh

# **************************** LICENSE START ***********************************
#
# Copyright 2012 ECMWF and INPE. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************


# ---------------------------------------------------------
# Script to run odbsql in a properly set ODB environment.
# ---------------------------------------------------------

# set up the environment


if [ $# -eq 0 ] ; then
    exit 1
fi

if [ "x$MV_ODB1_VERSION" = x ] ; then
    echo "MV_ODB1_VERSION not set - please ask your Metview installer."
    exit 1
fi

set -x
if [ "x$ODB_ROOT" = x ]  # ODB-1 is not set up?
then

    ODB_VERSION=$MV_ODB1_VERSION    # call the setup script
    if [ "x$MV_ODB1_MODULE" = x ]      # module name not given, so use the 'use' script
    then
        . $MV_ODB1_USE_SCRIPT           # call the setup script
    else
        module load $MV_ODB1_MODULE
    fi

else  # ODB-1 is set up - is it the right version?

    if [ "x$ODB_VERSION" != "x$MV_ODB1_VERSION" -a "x$ODB_VERSION" != "xANY" ]
    then
        if [ "x$ODB_VERSION" != "xANY" ]
        then
            ODB_VERSION=$MV_ODB1_VERSION    # call the setup script
            if [ "x$MV_ODB1_MODULE" = x ]      # module name not given, so use the 'use' script
            then
                . $MV_ODB1_USE_SCRIPT       # call the setup script
            else
                module load $MV_ODB1_MODULE
            fi
        fi
    fi
fi



set -x

odbsql "$@"

return  $?
