#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This script is used to copy the environment variables
#  from <ORA_DIR>/<SID>.env to <ORA_DIR>/<SID>_ha.env
#
# This file "<ORA_DIR>/<SID>_ha.env" can then be used by
#  the Resource Type SUNW.oracle_listener via the
#  User_env extension property for the Oracle Listener
#  and Concurrent Manager Listener resoucres.
#
# The Oracle eBusiness Suite installation generates the
#  original <ORA_DIR>/<SID>.env file together with 
#  startup/shutdown scripts for various components of
#  the Oracle eBusiness Suite.
#
# The script requires that you supply the following
#       <ORA_DIR> as parameter 1
#           <SID> as parameter 2
#
# Typically, <ORA_DIR> is the value of 
#
# <SID>.DBS_ORA816 - For the Oracle Listener
# <SID>.CON_ORA806 - For the Concurrent Manager Listener
# 
# Whereas, <SID> is the name of the system determined
#  through Rapid Install at time of installation, ie PROD.
#

ORA_DIR=$1
SID=$2

check_usage()
{
        if [ -z "$ORA_DIR" ]; then
                print_usage
        else
                if [ -d $ORA_DIR ]; then
                        continue
                else
                        echo "\nError:\t$ORA_DIR directory does not exist\n"
                        print_usage
                fi
        fi

        if [ -z "$SID" ]; then
                print_usage
        else
                if [ -f ${ORA_DIR}/${SID}.env ]; then
                        continue
                else
                        echo "\nError:\t${ORA_DIR}/${SID}.env does not exist\n"
                        print_usage
                fi
        fi
}

print_usage()
{
        echo "Usage for copy_env script\n"
        echo " copy_env <ORA_DIR> <SID>\n"

        exit 1
}

copy_env_file()
{
        ENV_FILE=${ORA_DIR}/${SID}_ha.env
        skip_line=no

        cat <<-EOF > $ENV_FILE
	#
	# This file was generated by /opt/SUNWscebs/cmg/util/copy_env
	#  Please do not delete the last blank line
	#
	EOF

        while read aa_line
        do
                if [[ `echo "$aa_line" | awk '{print $1}'` == "case" ]]; then
                        skip_line=yes
                        continue
                fi

                if [[ `echo "$aa_line" | awk '{print $1}'` == "esac" ]]; then
                        skip_line=no
                        continue
                fi

                if [[ "$skip_line" == "yes" ]]; then
                        continue
                fi

                [ -z ${aa_line:-""} ] && continue


                if [[ "$aa_line" == \#* ]]; then
                        continue
                fi

                if [[ "$aa_line" == "case" ]]; then
                        skip_line=yes
                        continue
                fi

                if [[ "$aa_line" == "esac" ]]; then
                        skip_line=no
                        continue
                fi

                # Now ensure that the line read is of the form
                # VARIABLE=VALUE

                if [[ "$aa_line" != *\=* ]]; then
                        continue
                fi

                val="${aa_line#*=}"
                param="${aa_line%%=*}"

                #
                # Do not allow Environment_file to override PATH
                #

                if [[ "$param" == "PATH" ]]; then
                        continue
                fi

		# Omit UNAME= "variable"

		if [[ "$param" == "UNAME" ]]; then
			continue
		fi

                # Ensure there aren't any funny characters in val

                val=`echo $val | tr -d ";"`
                val=`echo $val | tr -d '"'`

                # Ensure that Variable is a valid KSH variable

                if [[ "$param" != @([A-Z]|[a-z]|_)*([A-Z]|[a-z]|[0-9]|_) ]];then
                        continue
                fi

                echo ${param}=\"${val}\" >> $ENV_FILE

        done < ${ORA_DIR}/${SID}.env

        echo " " >> $ENV_FILE

        echo "Copied environment variables from ${ORA_DIR}/${SID}.env to ${ORA_DIR}/${SID}_ha.env"
}

check_usage

copy_env_file

exit 0

