#!/bin/sh
#* ----------------------------------------------------------------------
#*               Proprietary and Confidential Information
#*
#* This material contains, and is part of a computer software program
#* which is, proprietary and confidential information owned by COMPUTER
#* ASSOCIATES INTERNATIONAL, INC.  The program, including this material,
#* may not be duplicated, disclosed or reproduced in whole or in part for
#* any purpose without the express written authorization of COMPUTER
#* ASSOCIATES. All authorized reproductions must be marked with this
#* legend.
#*
#*                          Copyright (c) 1992-2004
#*             Computer Associates International, Incorporated
#*                          All rights reserved
#*
#* ----------------------------------------------------------------------
#*  SCCS Module Control Stamp
#* ----------------------------------------------------------------------
#
#* "@(#)ca_read	1.5  09/24/04 11:10:58"
#* "Copyright (C) 1992-2004 Computer Associates International, Inc.  All rights reserved."
#

#+----------------------------------------------------------------------+
#| Function: ca_read                                                    |
#|                                                                      |
#| Purpose: To allow for a response to be retrieved from an answer file |
#|          instead of from the console. This technique allows the      |
#|          possibility of an unattended install.                       |
#|                                                                      |
#| Syntax:                                                              |
#| $1 - The variable to store the "read" input.                         |
#| $2 - The .lcl file key (or any key that is supposed to be in resp.   |
#|      file) associated with the prompt.                               |
#| $3 - Default value - not required for enter or blank.                |
#|                                                                      | 
#| Usage: Example:                                                      |
#| ca_read     Local_Node     Node_Name    "`uname -n`"                 |
#|                                                                      |
#| Above esample checks the response file to see if Node_Name exists    |
#| and sets Local_Node=$Node_Name; if  Node_Name doesn't exist then     |
#| it sets  Local_Node="`uname -n`" (which is $3).                      |
#+----------------------------------------------------------------------+

ca_read()
{
ans_file=$PIF_RESPONSE_FILE

# Make sure that ans_file is set or the grep commands will hang install

   if [ "$ans_file" = "" -o ! -r "$ans_file" ];then
	eval $1="$3"	
	return
   fi

   #+-------------------------------------------------------------------+
   #| If there is an answer file then we are executing an unattended    |
   #| install. Get the correct answer associated with the key in the    |
   #| file.                                                             |
   #+-------------------------------------------------------------------+
   if [ -r "$ans_file" ] && [ -n "`grep \^$2= $ans_file`" ]; then

	field=2

	eval value="`grep \^$2= $ans_file | cut -d= -f $field `"

	latest_val=""

	for param in $value
	do
		latest_val=$param
	done

	eval $1=$latest_val

   else
      #+----------------------------------------------------------------+
      #| Either there is no answer in the file 
      #| not set. First, read the "input". If we are generating the     |
      #| answer file, put a heading in it, if needed, then add the tag. |
      #| If not generating the answer file, exit.                       |
      #+----------------------------------------------------------------+

      # OR the particular key is missing from the answer file
      if [ -r "$ans_file" ] && [ -z "`grep \^$2= $ans_file`" ]; then
			eval $1="$3"
			return
      fi
   fi
}

