#!/bin/sh
# -----------------------------------------------------------------------------
# File Name : setmqenv
# Descriptive File Name : Set the environment for WebSphere MQ
# -----------------------------------------------------------------------------
#   <copyright 
#   notice="lm-source-program" 
#   pids="5724-H72," 
#   years="2010,2015" 
#   crc="436476459" > 
#   Licensed Materials - Property of IBM  
#    
#   5724-H72, 
#    
#   (C) Copyright IBM Corp. 2010, 2015 All Rights Reserved.  
#    
#   US Government Users Restricted Rights - Use, duplication or  
#   disclosure restricted by GSA ADP Schedule Contract with  
#   IBM Corp.  
#   </copyright> 
# -----------------------------------------------------------------------------
# @(#) MQMBID sn=p800-003-150615.2 su=_O7DtFhOPEeWRp7-5NNeHyQ pn=cmd/tools/setmqenv/setmqenv.sh
# -----------------------------------------------------------------------------
# File Description :
#
# This script is used to set the environment for WebSphere MQ. The arguments
# specified are passed directly to crtmqenv whose usage is as follows. At
# least one of -m, -n, -p, -r or -s must be specified. Use -s to set up the
# environment for the installation that this script comes from.
#
# crtmqenv usage:
# -m name            : Set up the environment for the specified queue manager
# -n name            : Set up the environment for the specified installation
# -p name            : Set up the environment for the installation with the
#                      specified path
# -r                 : Remove WebSphere MQ from the environment
# -s                 : Set up the environment for the installation that this
#                      script comes from
# -l                 : Set up the library path, appending to the current value
# -k                 : Set up the library path at the start of the current
#                      value
# -x 32|64           : Set up either a 32 or 64-bit environment
#
# -----------------------------------------------------------------------------

MQ_RETVAL=0

crtmqenv_platform=`uname`
case $crtmqenv_platform in
AIX)
    MQ_DEFAULT_INSTALLATION_PATH=/usr/mqm
    MQ_LD_LIBRARY_PATH=${LIBPATH:-""}
    unset LIBPATH
    ;;

OS400)
    MQ_DEFAULT_INSTALLATION_PATH=/QIBM/ProdData/mqm
    MQ_LD_LIBRARY_PATH=${LIBPATH:-""}
    unset LIBPATH
    ;;

*)
    MQ_DEFAULT_INSTALLATION_PATH=/opt/mqm
    MQ_LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-""}
    unset LD_LIBRARY_PATH
    LCL_MQ_INSTALLATION_PATH=$MQ_DEFAULT_INSTALLATION_PATH
    ;;
esac

# LCL_MQ_INSTALLATION_PATH is modified at install time to be the correct
# directory for the installation if MQ is not installed in the default
# directory.
LCL_MQ_INSTALLATION_PATH=$MQ_DEFAULT_INSTALLATION_PATH

# Because this script wants to set environment variables in the current
# shell, it needs to be sourced in order to succeed, otherwise all changes
# will be lost when the script ends.
case $0 in
-*)
    # $0 starts with dash so we can assume it is not setmqenv
    ;;
 *)
    # If $0 is setmqenv, the script has not been sourced
    if [ `basename $0` = "setmqenv" ]
    then
        case $crtmqenv_platform in
        OS400)
            /QSYS.LIB/QMQM.LIB/MQRC.PGM -b amq8595 1>&2
            ;;

        *)
            $LCL_MQ_INSTALLATION_PATH/bin/mqrc -b amq8595 1>&2
            ;;
        esac
        exit 20
    fi
    ;;
esac

# The MQ_ENV_OPTIONS environment variable is used to specify options to
# crtmqenv, taking precedence over any positional parameters passed to the
# script.
crtmqenv_options=${MQ_ENV_OPTIONS:-""}
if [ -z "$crtmqenv_options" ]
then
    # If MQ_ENV_OPTIONS isn't set and no positional parameters were specified,
    # either the script was called incorrectly or the shell being used does not
    # pass parameters when a script is being sourced. It is assumed that the
    # environment for the installation that this script comes from should be
    # set up, but a message is displayed to indicate this.
    if [ $# -eq 0 ]
    then
        case $crtmqenv_platform in
        OS400)
            /QSYS.LIB/QMQM.LIB/MQRC.PGM -b amq8588 1>&2
            ;;

        *)
            $LCL_MQ_INSTALLATION_PATH/bin/mqrc -b amq8588 1>&2
            ;;
        esac
        crtmqenv_options=-s
        MQ_RETVAL=10
    # Pass the positional parameters onto crtmqenv.
    else
        crtmqenv_options=$*
    fi
fi

# Ensure that our private LD_LIBRARY_PATH value is visible to crtmqenv
# note: This is subsequently unset at the end of the script
export MQ_LD_LIBRARY_PATH

case $crtmqenv_platform in
OS400)
    crtmqenv_out=`/QSYS.LIB/QMQM.LIB/CRTMQENV.PGM -z $crtmqenv_options`
    crtmqenv_rc=$?
    ;;

*)
    crtmqenv_out=`$LCL_MQ_INSTALLATION_PATH/bin/crtmqenv -z $crtmqenv_options`
    crtmqenv_rc=$?
    ;;
esac

if [ $crtmqenv_rc -eq 0 ]
then
    for crtmqenv_line in $crtmqenv_out
    do
        crtmqenv_var=`echo $crtmqenv_line | cut -d '=' -f1`
        crtmqenv_value=`echo $crtmqenv_line | cut -d '=' -f2-`
        if [ -z "$crtmqenv_value" ]
        then
            eval "unset $crtmqenv_var"
        else
            eval "$crtmqenv_line"
            eval "export $crtmqenv_var"
        fi
    done
else
    echo "$crtmqenv_out"
    if [ -n "$MQ_LD_LIBRARY_PATH" ]
    then
        case $crtmqenv_platform in
        AIX)
            LIBPATH=$MQ_LD_LIBRARY_PATH
            export LIBPATH
            ;;

        *)
            LD_LIBRARY_PATH=$MQ_LD_LIBRARY_PATH
            export LD_LIBRARY_PATH
            ;;
        esac
    fi

    MQ_RETVAL=$crtmqenv_rc
fi

unset crtmqenv_var
unset crtmqenv_value
unset crtmqenv_line
unset crtmqenv_rc
unset crtmqenv_out
unset crtmqenv_options
unset LCL_MQ_INSTALLATION_PATH
unset MQ_LD_LIBRARY_PATH
unset MQ_DEFAULT_INSTALLATION_PATH
unset crtmqenv_platform

sh -c "exit $MQ_RETVAL"
