#!/usr/bin/ksh
# -----------------------------------------------------------------
# FileName: checkPubsImpactDefectOpen1
#
# Usage:    This is to be used as a CMVC UserExit for the
#           DefectOpen action with an exitID of: 1
#           At this stage the defect number has been already assigned.
#
# Purpose:  If PubsImpact is 'yes' or 'maybe' then send a note to 
#           the ID contact to notify this event.
#
# How to invoke it:
#           Add the following line at the bottom of the file
#           config/userExits.
#           Please note that a comment mark is added in the line
#           to avoid problems in this shell script during runtime:
#
# DefectOpen    1    checkPubsImpactDefectOpen1
#
# Changes in config.ld:
#           In order for this shell script to be useful, it is
#           necessary to add appropriate fields in the config.ld
#           file.
#           Please note that a comment mark is added in the lines
#           to avoid problems in this shell script during runtime:
# 
# pubsImpact|no|no|0|0|There is NO impact to the publications
# pubsImpact|yes|no|0|0|There is impact to the publications
# pubsImpact|maybe|no|0|0|There might be impact to the publications
# pubsImpact|done|no|0|0|The changes to the publications have been done
#
# Changes in the configurable fields:
#           In order for this shell script to be useful, it is
#           necessary to add the configurable fields for defects:
#
#              chfield -object Defect
#
#           Please note that a comment mark is added in the line
#           to avoid problems in this shell script during runtime:
#
# Attribute Name  DB Column Name  Create/Required Type
# --------------- --------------- --------------- ---------------
# pubsImpact      pubsImpact         yes/yes      pubsImpact   
#
# NOTE FOR CUSTOMIZATION:
#  See the Customization Section if you want to modify: 
#    * the text of the message
#    * the name of the person to receive the message
# ------------------------------------------------------------------
# 5765-039 (C) COPYRIGHT International Business Machines Corp. 1991,1993
# 5765-069 (C) COPYRIGHT International Business Machines Corp. 1991,1993
# 5765-207 (C) COPYRIGHT International Business Machines Corp. 1993,1996
# 5765-202 (C) COPYRIGHT International Business Machines Corp. 1993,1996
# 5622-063 (C) COPYRIGHT International Business Machines Corp. 1993,1996
# 5765-397 (C) COPYRIGHT International Business Machines Corp. 1994,1996
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
#           NOTICE TO USERS OF THE SOURCE CODE EXAMPLES
#
# INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THE SOURCE CODE
# EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS, "AS IS" WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
# OF THE SOURCE CODE EXAMPLES, BOTH INDIVIDUALLY AND AS ONE OR MORE GROUPS,
# IS WITH YOU.  SHOULD ANY PART OF THE SOURCE CODE EXAMPLES PROVE
# DEFECTIVE, YOU (AND NOT IBM OR AN AUTHORIZED RISC System/6000* WORKSTATION
# DEALER) ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING, REPAIR OR
# CORRECTION.
#
# * RISC System/6000 is a trademark of International Business Machines
#   Corporation.
#
# /usr/lpp/cmvc/samples/checkPubsImpactDefectOpen1
#
#---------------------------------------------------------------------+


#---------------------------------------------------------------------+
# Subroutine name:  substringLocation
#
# Purpose:
#    This routine returns the location of a substring in the string, in
#    terms of substrings.
#    It returns 0 for the first location, 1 for the second location, and so on.
#    It returns -1 if the substring is NOT found.
#
# Input:
#    string: set of substrings
#    substring: specific substring to locate
#
# Output:
#    indexFound: index where the substring was found (if not found: -1)
#---------------------------------------------------------------------+
substringLocation()
{
   found=no
   let "index=-1"
   let "indexFound=-1"
   for item in `echo $string`
   do
      let "index = index + 1"
      if [ "$found" = "no" ]
      then
         if [ "$item" = "$substring" ]
         then
            found="yes"
            indexFound=$index
         fi
      fi
   done

export indexFound
}

# ------------------------------------------------------------------
# Main section
# ------------------------------------------------------------------
# echo "debug: checkPubsImpactDefectOpen1: begin "

# ------------------------------------------------------------------
# Initialization section.
# ------------------------------------------------------------------
# Return Code for this shell script
rc=0

# ------------------------------------------------------------------
# Giving legible names to the input parameters 
# It is nice to list ALL the paramenters, it helps with the
# debugging and overall understanding.
# NOTE: It is important to deal with the "shift" command to 
#       handle the input positional parameters greater than 9
# ------------------------------------------------------------------
# echo "parameters passed: "
# echo "1 is component name: " $1
# echo "2 is prefix: " $2
prefix=$2
# echo "3 is severity ID: " $3
# echo "4 is reference: " $4
# echo "5 is environment name: " $5
# echo "6 is remarks: " $6
# echo "7 is level name: " $7
# echo "8 is abstract: " $8
# echo "9 is release name: " $9
shift 9
# echo "10 is configurable field string: " $1
string=$1
# echo "11 is defect number: " $2
defectNumber=$2
# echo "12 is effective CMVC user id: " $3
# echo "13 is verbose flag: " $4

# ------------------------------------------------------------------
# Customization section.
# ------------------------------------------------------------------
# Specify the configurable field DB column name
configField="pubsImpact"

# Specify the subject to be used in the mail note"
subject="Defect $defectNumber was opened with value yes/maybe in PubsImpact" 

# Specify the message to be used inside the mail note 
# (it could be longer than the subject)
message="The defect $defectNumber was opened with a value of 'yes' or 'maybe' in the configurable field PubsImpact." 

# Specify the email address of the person to receive the message
userid="bmartin@carvm3"

# ------------------------------------------------------------------
# Store all the labels and values of the string in an array
# to facilitate the look up
# ------------------------------------------------------------------
let "index=0"
for item in $string
do
   array[$index]=$item
# echo "debug: array[$index] =|${array[$index]}|"
   let "index = index + 1"
done

# ------------------------------------------------------------------
# Main processing 
#
# If the value for PubsImpact is 'yes' or 'maybe', then send a note
# to the ID contact.
# ------------------------------------------------------------------

substring="$configField"
substringLocation
# echo "debug: substring=|$substring| was found in position=|$indexFound|"
if [ "$indexFound" != "-1" ]
then
      pubsImpact=${array[$indexFound+1]}
# echo "debug: pubsImpact=|$pubsImpact|"
      if [ "$pubsImpact" = "yes" ] || [ "$pubsImpact" = "maybe" ]
      then
# echo "debug: sending message to $userid"
         echo $message > /tmp/userExit.tmp
         mail -s "$subject" $userid < /tmp/userExit.tmp
         rm /tmp/userExit.tmp
      fi
fi

# echo "debug: checkCustomerNameDefectOpen0: end with rc=$rc"
 
exit $rc

# end of file
