#!/bin/sh
#############################################################################
#
# Licensed Materials - Property of IBM
#
# (C) COPYRIGHT International Business Machines Corp. 2011
#
# All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#############################################################################

#
# NAME: db2iexec
#
# FUNCTION: db2iexec - executes instances related commands from DB2 Installer
#
# USAGE: db2iexec [-n] <instance_name> <list_of_shell_commands>
#
#############################################################################
# Options for "set" command
setopts="${setopts:-+x}"
set ${setopts?}

# Current DB2 installation directory

curdir=`/bin/pwd`
DB2DIR=`dirname $0`
cd ${DB2DIR?}
DB2DIR=`/bin/pwd`
cd ${DB2DIR?}
DB2DIR=`dirname ${DB2DIR?}`
cfg_tmp="${DB2DIR?}/cfg/installcopy.cfg"
cmd_db2fupdt_tmp="${DB2DIR?}/bin/db2fupdt"
DB2_KEEP_IN_ORIGINAL_DB2DIR="${DB2_KEEP_IN_ORIGINAL_DB2DIR:-FALSE}"

if [ -f "${cfg_tmp?}" -a -f "${cmd_db2fupdt_tmp?}"  -a "X${DB2_KEEP_IN_ORIGINAL_DB2DIR?}" != "XTRUE" ]; then
   db2dir_tmp=`${cmd_db2fupdt_tmp?} -f ${cfg_tmp?} -p DB2DIR`
   if [ $? -eq 0 ]; then
      cd ${db2dir_tmp?} 2>/dev/null 1>/dev/null
      if [ $? -eq 0 ]; then
         DB2DIR=`echo ${db2dir_tmp?} | sed 's/\/$//'`
      fi
   fi
fi 
cd ${curdir?}
unset curdir cfg_tmp cmd_db2fupdt_tmp db2dir_tmp DB2_KEEP_IN_ORIGINAL_DB2DIR

PROGNAME=`basename $0`       # Program name

# show db2diag.log by default
SHOWLOGFLAG="0"        

# check for command-line option "-n" and set SHOWLOGFLAG to "1" if found
if [ $1 = "-n" ]; then
    SHOWLOGFLAG="1"
    shift
fi

INSTNAME=$1
shift

# Instance home directory
USERHOME=`${DB2DIR?}/bin/db2usrinf -d ${INSTNAME?}`

# Instance shell
USERSHELL=`${DB2DIR?}/bin/db2usrinf -s ${INSTNAME?}`

# sqllib
SQLLIB="sqllib/db2"
if [ -d ${USERHOME?}/das ]
then
    SQLLIB="das/das"
fi

# either source db2profile or db2cshrc depending on which shell the instance is running
case ${USERSHELL?} in
    *csh)
# C shell is running
        if [ -f ${USERHOME?}/${SQLLIB?}cshrc ]; then
           PROFILE="source ${USERHOME?}/${SQLLIB?}cshrc >& /dev/null; $* "
        else
           PROFILE="$* "
        fi
        ;;
    *)
        if [ -f ${USERHOME?}/${SQLLIB?}profile ]; then
           PROFILE=". ${USERHOME?}/${SQLLIB?}profile 1>/dev/null 2>/dev/null; $* "
        else
           PROFILE="$* "
        fi
        ;;
esac

PROCID=$$                                    # Process ID
TMPFILE="/tmp/${PROGNAME?}.tmp.${PROCID?}"   # Temp. file

su - ${INSTNAME?} -c "${PROFILE?}; echo \$? > ${TMPFILE?}"
return_code=`cat ${TMPFILE?} 2>/dev/null`
rm -f ${TMPFILE?} 1>/dev/null 2>/dev/null

if [ ${SHOWLOGFLAG?} -eq 0 ]; then
    if [ ${return_code?} -ne 0 ]; then
        if [ -f ${USERHOME?}/sqllib/db2dump/db2diag.log ]; then
            cat ${USERHOME?}/sqllib/db2dump/db2diag.log >&2
        fi
    fi
fi

exit ${return_code?}

