#!/bin/bash
# 
# DISCLAIMER
#   Trigo Technologies, Inc. disclaims all warranties or representations herein,
#   whether express, implied or statutory, to the fullest extent
#   permissible by law, including the implied warranties of title,
#   fitness for a particular purpose and merchantability.
# 
# COPYRIGHTS
#   Copyright 2001, 2002 Trigo Technologies, Inc. All rights reserved.
# 
#   Trigo Technologies, Inc.
#   8000 Marina Blvd, Suite 810
#   Brisbane, CA 94005
#   U.S.A.
#   www.trigo.com
# 
# TRADEMARKS
#   Trigo, Trigo Enterprise, Trigo Content Suite, Trigo Commerce Suite,
#   and Trigo Platform are trademarks of Trigo Technologies, Inc.
#   All other company and product names are trademarks or registered
#   trademarks of their respective owners.
#


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

. ${TOP}/bin/get_params.sh

#echo 1
#echo ${_TRIGO_FILE}
#echo "$_TRIGO_VAR"
#echo ${_TRIGO_DISCONNECT}
#echo ${_TRIGO_ERRORFILTER}
#echo 2

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