#-------------------------------------------------------------------------------
# Run any CLP command which does NOT require a database connection; and write the output to a file.
# CLP commands requiring a database connection are handled in function RUN_CONNECTED_DB2CLP_COMMAND().
# Some CLP commands (e.g. db2 get dbm cfg show detail) require an instance attachment.
#
RUN_DB2CLP_COMMAND()
{
 set "$DEBUG_SWITCH"
 OUTPUT_FILE="$1"
 MAX_RETURN_CODE="$2"
 COMPRESS_OPTION="$3"
 DB2_COMMAND="$4"
 typeset -u ATTACH_OPTION="$5" # Convert to UPPER case.
 INIT_OUTPUT_FILE "$OUTPUT_FILE" "$DATE_FORMAT" "$DEBUG_SWITCH"  

 print "\nCommand run:"         >> "$OUTPUT_FILE" 
 print "  db2 ${DB2_COMMAND}\n" >> "$OUTPUT_FILE"

 if [[ "$ATTACH_OPTION" == "ATTACH" ]]; then
    db2 +o "ATTACH TO ${INSTNAME}"
    RETURN_CODE="$?"
    if [[ "$RETURN_CODE" -gt 0 ]]; then
       ERROR_MESSAGE "Problem attaching to instance: \"${INSTNAME}\", return code: \"${RETURN_CODE}\"" $DEBUG_SWITCH 
       ATTACHED_OK="N"
       db2 +o "DETACH"
    else
       ATTACHED_OK="Y"
    fi
 else
    ATTACHED_OK="N/A"
 fi

 if [[ "$ATTACHED_OK" != "N" ]]; then
    db2 +o -v -z "$OUTPUT_FILE" "$DB2_COMMAND"
    RETURN_CODE="$?"
    if [[ "$RETURN_CODE" -gt "$MAX_RETURN_CODE" ]]; then
       ERROR_MESSAGE "Problem running command: \"db2 ${DB2_COMMAND}\", return code: \"${RETURN_CODE}\",\n        View: \"${OUTPUT_FILE}\" for further details" $DEBUG_SWITCH
    else
       case "$COMPRESS_OPTION" in
          compress|COMPRESS)
          ZIP_IT "$OUTPUT_FILE" "$DEBUG_SWITCH"  
          ;;
          *)
          print "Output saved to: \"${OUTPUT_FILE}\""
          ;;
       esac
    fi
    if [[ "ATTACHED_OK" == "Y" ]]; then
       db2 +o "DETACH"
    fi
 fi
}
