#!/bin/sh
#set -xv
#==============================================================================
#
#  The script will update the values of the following variables:
#  1.  G1XXXDB in $G1XXX/bin/setup and $G1XXX/bin/setup.csh
#  2.  G1XXXDB in $G1SD4/bin/setupg1prod and $G1SD4/bin/setupg1prod.csh.
#
#  For the setupg1prod update, the script will first search for existence of 
#  the workflow server and update the setupg1prod scripts accordingly.
#
#==============================================================================

#==============================================================================
# Function to update the setupg1prod file with the new definition/location
#==============================================================================
update_env()
{
### Update the definition in the specified script
# updates TGTFILE if it exists
if test -f ${TGTFILE}
then
   if [ "$CSHFLG" -eq "1" ]
   then
      `cat $TGTFILE | \
       awk "!/^setenv ${G1ENV}/{print} \
             /^setenv ${G1ENV}/{print \"setenv ${G1ENV} '$G1DEF'\"}" > \
            $TGTFILE.tmp`
   else
      G1STR=`$G1ECHO $G1ENV|sed 's/=//g'`
      `cat $TGTFILE | \
       awk "!/^${G1ENV}/{print} \
             /^${G1ENV}/{print \"${G1STR}='$G1DEF';   export ${G1STR}\"}" > \
            $TGTFILE.tmp`
   fi
   `mv $TGTFILE.tmp $TGTFILE`
   $G1ECHO "$TGTFILE is updated."

else
   $G1ECHO "WARNING: $TGTFILE does not exist."
fi
}

G1DEF=$1
if [ "$G1PROD" = "msn" ]
then
   G1PRODU="MSP"
else
   G1PRODU=`$G1ECHO $G1PROD|tr -s '[:lower:]' '[:upper:]'`
fi
G1ENV="G1${G1PRODU}DB"
#==============================================================================
# update the products setup script in the product's /bin area
#==============================================================================
TGTFILE="${G1DIR}/${G1PROD}/bin/setup"
CSHFLG=0 
update_env;
TGTFILE="${G1DIR}/${G1PROD}/bin/setup.csh"
CSHFLG=1 
update_env;
#==============================================================================
# Update the setupg1prod script if D4 exists
#==============================================================================
if [ -f /var/group1/.group1sd4 ]
then
   G1D4DIR=`head /var/group1/.group1sd4`
   G1D4DIR="${G1D4DIR}/sd4"   
   if [ -f $G1D4DIR/lib/sd4rls ]
   then
      TGTFILE="${G1D4DIR}/bin/setupg1prod"
      CSHFLG=0 
      update_env;
      TGTFILE="${G1D4DIR}/bin/setupg1prod.csh"
      CSHFLG=1 
      update_env;
   else
      $G1ECHO "WARNING: $G1D4DIR/lib/sd4rls does not exist."
   fi
else
   $G1ECHO "INFO: /var/group1/.group1sd4 does not exist."
fi
