#!/bin/bash -
#
# Package:    NodeM
# File:       environ
# Summary:    Nodem environment script
# Maintainer: David Wicksell <dlw@linux.com>
#
# Written by David Wicksell <dlw@linux.com>
# Copyright © 2012-2023 Fourth Watch Software LC
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License (AGPL) as published
# by the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
#
# This is just an example script to get started, if you don't already have a
# YottaDB/GT.M environment configured.
#
# *** NOTE ***
# This environment script will only work if you are running the Bash shell
#
# In order to use this environment script, you would typically add a line to
# your ~/.profile or ~/.bash_profile login script, to source it in your current
# environment, e.g.
#
#     source $HOME/node_modules/nodem/resources/environ
#
# You want to make sure that path is correct, based on where you installed Nodem
# on your system. No matter where you installed it, the following environment
# variables should be set up correctly, without change, to be able to use it.
#
# You need to have the $ydb_dist/$gtm_dist environment variable set correctly,
# in order to compile, link, and use Nodem. You also need the
# $ydb_gbldir/$gtmgbldir environment variable set correctly, pointing to your
# global directory file, in order to use the full Nodem API. The following
# environment variables are also needed; $ydb_ci/$GTMCI points to the Call-in
# table, and $ydb_routines/$gtmroutines must contain the path to the v4wNode.m
# source file, or v4wNode.o object file.
#
# Since $ydb_dist/$gtm_dist needs to correctly point to your YottaDB/GT.M
# installation root directory, in order to compile Nodem in the first place,
# you likely will not need to set up $LD_LIBRARY_PATH, as the path in
# $ydb_dist/$gtm_dist is embedded as a RUNPATH directly in the Nodem library,
# so the dynamic linker should be able to load the YottaDB/GT.M shared library
# at run-time without having to set it. However, if you compile Nodem in one
# environment, and want to run it in a different environment, which has YottaDB
# or GT.M installed in a different location, you will need to either set
# $LD_LIBRARY_PATH to the distribution root, or else add that directory to the
# ldcache, as discussed in the README.md file.

PKG=$(dirname $(dirname ${BASH_SOURCE[0]}))
[[ $PKG =~ $(pwd) ]] || PKG=$(pwd)/$PKG
PKG=${PKG%/.}
[[ $(basename $PKG) == resources ]] && PKG=${PKG%/resources}

# YottaDB/GT.M Call-in table
export ydb_ci=$PKG/resources/nodem.ci
export GTMCI=$ydb_ci

# YottaDB/GT.M routines path
if [[ -n $ydb_routines ]]
then
    DIR=${ydb_routines%% *}

    if [[ $DIR =~ "(" ]]
    then
        export ydb_routines="${DIR/\(*/}($PKG/src ${DIR/*\(/}${ydb_routines/${DIR/\*/\\*}/}"
    else
        export ydb_routines="$DIR($PKG/src)${ydb_routines/${DIR/\*/\\*}/}"
    fi

    export gtmroutines="$ydb_routines"
elif [[ -n $gtmroutines ]]
then
    DIR=${gtmroutines%% *}

    if [[ $DIR =~ "(" ]]
    then
        export gtmroutines="${DIR/\(*/}($PKG/src ${DIR/*\(/}${gtmroutines/${DIR/\*/\\*}/}"
    else
        export gtmroutines="$DIR($PKG/src)${gtmroutines/${DIR/\*/\\*}/}"
    fi
else
    export ydb_routines=$PKG/src
    export gtmroutines=$ydb_routines
fi

# Runtime loader path for YottaDB/GT.M shared library, if necessary (see README.md)
# Uncomment the following lines of code, if you are running Nodem in a different environment than it was compiled in
#if [[ -n $LD_LIBRARY_PATH ]]
#then
#    export LD_LIBRARY_PATH=${ydb_dist:-${gtm_dist:-.}}:${LD_LIBRARY_PATH}
#else
#    export LD_LIBRARY_PATH=${ydb_dist:-${gtm_dist:-.}}
#fi
