#!/bin/bash

# Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# IBM makes no representations or warranties about the suitability of
# this program, either express or implied, including but not limited to
# the implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement. 
  

# 
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement. 
# 
#


# usage:
#   run_db_cmd.sh [--file=<file_name>] [--var=<variable>] [--disconnect=<yes/no>] [--errorfilter=<yes/no>]

. ${TOP}/bin/get_params.sh

#echo 1
#echo ${_CCD_FILE}
#echo "$_CCD_VAR"
#echo ${_CCD_DISCONNECT}
#echo ${_CCD_ERRORFILTER}
#echo 2

if [ ${CCD_DB} = "db2" ] && [ "${PLATFORM}" != "WINDOWS" ] && [ "${PLATFORM}" != "CYGWIN" ]; then  
    #echo "2"
    if [ "$_CCD_FILE" != "" ] && [ "$_CCD_DISCONNECT" = "yes" ] && [ "$_CCD_ERRORFILTER" = "yes" ]; then
        #echo "2.1"
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE | ${GREP} -vE "$DDL_ERROR_FILTER"
        ${DB_DISCONNECT}
    elif [ "$_CCD_VAR" != "" ] && [ "$_CCD_DISCONNECT" = "yes" ] && [ "$_CCD_ERRORFILTER" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR | ${GREP} -vE "$DDL_ERROR_FILTER"
        ${DB_DISCONNECT}
    elif [ "$_CCD_FILE" != "" ] && [ "$_CCD_DISCONNECT" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
        ${DB_DISCONNECT}
    elif [ "$_CCD_VAR" != "" ] && [ "$_CCD_DISCONNECT" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR
        ${DB_DISCONNECT}
    elif [ "$_CCD_FILE" != "" ] && [ "$_CCD_ERRORFILTER" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION} 
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE | ${GREP} -vE "$DDL_ERROR_FILTER"
    elif [ "$_CCD_VAR" != "" ] && [ "$_CCD_ERRORFILTER" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION} 
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR | ${GREP} -vE "$DDL_ERROR_FILTER"   
    elif [ "$_CCD_FILE" != "" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
    elif [ "$_CCD_VAR" != "" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR
    else
        ${ECHO} "This condition has to be investigated"
    fi     
else    
    if [ "${_CCD_FILE}" != "" ] && [ "${_CCD_DISCONNECT}" = "yes" ] && [ "${_CCD_ERRORFILTER}" = "yes" ]; then
# XXX ST | ${GREP} -vE "$DDL_ERROR_FILTER"
        ${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
        ${DB_DISCONNECT}
EOF
    elif [ "${_CCD_VAR}" != "" ] && [ "${_CCD_DISCONNECT}" = "yes" ] && [ "${_CCD_ERRORFILTER}" = "yes" ]; then
# XXX ST | ${GREP} -vE "$DDL_ERROR_FILTER"
        ${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR
        ${DB_DISCONNECT}
EOF
    elif [ "${_CCD_FILE}" != "" ] && [ "${_CCD_DISCONNECT}" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION} <<EOF
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
        ${DB_DISCONNECT}
EOF
    elif [ "${_CCD_VAR}" != "" ] && [ "${_CCD_DISCONNECT}" = "yes" ]; then
        ${CCD_SQLPLUS} ${CONNECTION}<<EOF
        ${DB_SQLLINE_EXECUTE} $_CCD_VAR
        ${DB_DISCONNECT}
EOF
    elif [ "${_CCD_FILE}" != "" ] && [ "${_CCD_ERRORFILTER}" = "yes" ]; then
# XXX ST | ${GREP} -vE "$DDL_ERROR_FILTER"
        ${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
EOF
    elif [ "${_CCD_VAR}" != "" ] && [ "${_CCD_ERRORFILTER}" = "yes" ]; then
# XXX ST | ${GREP} -vE "$DDL_ERROR_FILTER"
       ${CCD_SQLPLUS} ${CONNECTION} <<EOF 2>&1 
       ${DB_SQLLINE_EXECUTE} $_CCD_VAR
EOF
    elif [ "${_CCD_FILE}" != "" ]; then
        ${CCD_SQLPLUS} ${CONNECTION} <<EOF
        ${DB_SQLFILE_EXECUTE} $_CCD_FILE
EOF
    elif [ "$_CCD_VAR" != "" ]; then
       ${CCD_SQLPLUS} ${CONNECTION} <<EOF
       ${DB_SQLLINE_EXECUTE} $_CCD_VAR
EOF
    else
        ${ECHO} "This condition need to be investigated"
    fi
fi
#echo "end"
