#!/bin/sh

# DM setup script for Bourne-type shells
# This file is typically sourced in user's .bashrc file

myDir=`dirname $BASH_SOURCE`
currentDir=`pwd` && cd $myDir
if [ ! -z "$DM_ROOT_DIR" -a "$DM_ROOT_DIR" != `pwd` ]; then
    echo "WARNING: Resetting DM_ROOT_DIR environment variable (old value: $DM_ROOT_DIR)" 
fi
export DM_ROOT_DIR=`pwd`
export DM_HOST_ARCH=`uname | tr [A-Z] [a-z]`-`uname -m`

if [ -z $DM_INSTALL_DIR ]; then
    export DM_INSTALL_DIR=$DM_ROOT_DIR/..
    if [ -d $DM_INSTALL_DIR ]; then
        cd $DM_INSTALL_DIR
        export DM_INSTALL_DIR=`pwd`
    fi
fi

if [ -z $DM_VAR_DIR ]; then
    export DM_VAR_DIR=$DM_ROOT_DIR/../var
    if [ -d $DM_VAR_DIR ]; then
        cd $DM_VAR_DIR
        export DM_VAR_DIR=`pwd`
    fi
fi

# Check support setup
if [ -z $DM_SUPPORT_DIR ]; then
    export DM_SUPPORT_DIR=$DM_ROOT_DIR/../support 
    if [ -d $DM_SUPPORT_DIR ]; then
        cd $DM_SUPPORT_DIR
        export DM_SUPPORT_DIR=`pwd`
    fi
fi

if [ ! -d $DM_SUPPORT_DIR ]; then
    echo "ERROR: $DM_SUPPORT_DIR directory does not exist. Developers should point DM_SUPPORT_DIR to the desired area." 
    unset DM_SUPPORT_DIR
    return 1
fi
export DM_OPT_DIR=$DM_SUPPORT_DIR/opt
export DM_GLASSFISH_DIR=$DM_OPT_DIR/glassfish


# Add to path only if directory exists.
prependPathIfDirExists() {
    _dir=$1
    if [ -d ${_dir} ]; then
        PATH=${_dir}:$PATH
    fi
}

# Setup path and other variables
prependPathIfDirExists $DM_OPT_DIR/node/$DM_HOST_ARCH/bin
prependPathIfDirExists $DM_OPT_DIR/mongodb/$DM_HOST_ARCH/bin
prependPathIfDirExists $DM_OPT_DIR/postgresql/$DM_HOST_ARCH/bin
prependPathIfDirExists $DM_OPT_DIR/java/$DM_HOST_ARCH/bin
prependPathIfDirExists $DM_OPT_DIR/ant/bin
prependPathIfDirExists $DM_ROOT_DIR/bin
PATH=.:$PATH
export PATH

if [ -z $LD_LIBRARY_PATH ]; then
    LD_LIBRARY_PATH=.
else
    LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
fi
export LD_LIBRARY_PATH

# Setup python path. 
# Check if we have local python
if [ -z $DM_PYTHON_DIR ]; then
    pythonDir=$DM_OPT_DIR/python/$DM_HOST_ARCH
else
    pythonDir=$DM_PYTHON_DIR
fi
if [ -d $pythonDir ]; then
    cd $pythonDir
    pythonDir=`pwd`
    export PATH=`pwd`/bin:$PATH
    export LD_LIBRARY_PATH=`pwd`/lib:$LD_LIBRARY_PATH
    export DM_PYTHON_DIR=$pythonDir
fi

if [ -z $PYTHONPATH ]; then
    PYTHONPATH=$DM_ROOT_DIR/lib/python
else
    PYTHONPATH=$DM_ROOT_DIR/lib/python:$PYTHONPATH
fi
export PYTHONPATH

# Setup postgres. 
if [ -z $DM_POSTGRESQL_DIR ]; then
    postgresqlDir=$DM_OPT_DIR/postgresql/$DM_HOST_ARCH
else
    postgresqlDir=$DM_POSTGRESQL_DIR 
fi
if [ -d $postgresqlDir ]; then
    cd $postgresqlDir
    postgresqlDir=`pwd`
    export PATH=`pwd`/bin:$PATH
    export LD_LIBRARY_PATH=`pwd`/lib:$LD_LIBRARY_PATH
    export DM_POSTGRESQL_DIR=$postgresqlDir
fi

# Setup oracle libraries. 
oracleDir=$DM_OPT_DIR/oracle-client/$DM_HOST_ARCH
if [ -d $oracleDir ]; then
    LD_LIBRARY_PATH=$oracleDir:$LD_LIBRARY_PATH
    PATH=$oracleDir:$PATH
fi

# Get back to where we were before invoking the setup script
cd $currentDir

# Print out user environment
echo 
echo "Your DM environment is defined as follows:"
echo 
env | grep DM_ | grep -v GDM_
echo
echo


