#!/usr/bin/ksh
# -----------------------------------------------------------------
# FileName: checkCustomerNameDefectOpen0
#
# Usage:    This is to be used as a CMVC UserExit for the
#           DefectOpen action with an exitID of: 0
#           At this stage the defect number has not been assigned yet.
#
# Purpose:  If prefix is 'e' then verify that there is something in
#           the configurable field 'customerName'. If not, display
#           message indicating the need to update it.
#
# 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    0    checkCustomerNameDefectOpen0
#
# 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 line 
#           to avoid problems in this shell script during runtime:
#
#defectPrefix|e|no|0|0|"Defect from an external customer -> enter Customer Name"
#
# 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
# --------------- --------------- --------------- ---------------
# customerName    customerName       yes/no                      
#
# ------------------------------------------------------------------
# 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/checkCustomerNameDefectOpen0
#
#---------------------------------------------------------------------+

#---------------------------------------------------------------------+
# 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: checkCustomerNameDefectOpen0: begin "

# ------------------------------------------------------------------
# Customization section.
# ------------------------------------------------------------------
# Specify the name of your configurable field (DB column)
configField="customerName"
# Specify the message to be used"
message="Specify the customerName in order to open a defect with prefix 'e'" 

# ------------------------------------------------------------------
# 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 real UNIX login: " $4
# echo "14 is verbose flag: " $5

# ------------------------------------------------------------------
# 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 prefix is 'e' (defect opened by external customer) then make sure
# that there is information in the configurable field customerName.
# If not, do not open the defect and display a message to the user
# for the need to specify the customer name.
# ------------------------------------------------------------------

if [ "$prefix" = 'e' ]
then
   substring="customerName"
   substringLocation
#   echo "debug: substring=|$substring| was found in position=|$indexFound|"
   if [ "$indexFound" = "-1" ]
   then
      echo "If you specify defect prefix 'e', then you must provide"
      echo "a value for the field 'Customer Name:'."
      echo "Please try again."
      echo "You can disregard the following error message 0010-794."
      rc=-1
#   else
#      echo "debug: customerName=|${array[$indexFound+1]}|"
   fi
fi

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

# end of file
