1. 개요

자작 스크립트이니 사용시 유의하여 주시기 바랍니다.


2. 스크립트

#!/bin/ksh
# |
# | FILENAME
# |   mtz.sh.sh
# |
# | DESCRIPTION
# |   Multi Time Zone Configurator
# |
# | USAGE
# |   mtz.sh {dbTier|cmTier|apTier|dbTierRemove|cmTierRemove|apTierRemove|CloneCfg}
# |
# | PLATFORM
# |   Unix Generic
# |
# | NOTES
# |
# | HISTORY
# |
# +===========================================================================+
#

#


: ${YY=`date +%Y`} ${MM=`date +%m`} ${DD=`date +%d`} ${HH=`date +%H`} ${MI=`date +%M`} ${SS=`date +%S`}

CheckUtilityPath()
{
  CHECK_RESULT=0

  for FILE_NAME in $*
  do
    WHICH_FILE_NAME=`which $FILE_NAME`

    if [ $? -eq 0 ]
    then
      echo "Checking for $FILE_NAME ... found - $WHICH_FILE_NAME"
    else
      echo "Checking for $FILE_NAME ... Unable to locate $FILE_NAME utility in path"
      CHECK_RESULT=1
  fi
  done

  if [ $CHECK_RESULT -eq 1 ]
  then
    echo "\nUnable to locate all utilities with system path."
    echo "\nWARNING: Could not find all the required OS utilities in the \$PATH. Please review the checks above\n"
    exit
  fi
}

DefineMTZInventory()
{
  MTZ_INV=$TZ_ORACLE_HOME/mtzInventory
  MTZ_BACKUP_DIR=$MTZ_INV/${YY}${MM}${DD}${HH}${MI}${SS}
  MTZ_DB_CONN_LOG=$MTZ_INV/db_conn_test.log
  MTZ_CONFIRM_USER_LOG=$MTZ_INV/.confirm_user.log
  MTZ_NEW_MTZ_USER_CREATE=$MTZ_INV/.fnd_user_pkg_create_user.log
  MTZ_NEW_MTZ_USER_RESP=$MTZ_INV/.confirm_user_resp.log

  mkdir $MTZ_INV 2>/dev/null

  MTZ_INV_RETURN=$?

  if [ $MTZ_INV_RETURN -eq 0 ]
  then
    echo "\nMulti Time Zone Inventory ($MTZ_INV) created completed !\n"
    mkdir $MTZ_BACKUP_DIR
  elif [ $MTZ_INV_RETURN -eq 2 ]
  then
    mkdir $MTZ_BACKUP_DIR

    if [ $? -ne 0 ]
    then
      echo "\nERROR: Needs to have to write permissions on $MTZ_INV"
      echo "  Please check directory $MTZ_INV\n"
      exit
    fi

    echo "\nDetected already existing Multi Time Zone Inventory ($MTZ_INV) !\n"
  else
    echo "\nERROR: MTZ Inventory ($MTZ_INV) created failed"
    echo "  Please check directory $MTZ_INV\n"
    exit
  fi
}

DefineCommonServerInfo()
{  
  #####################################################
  ### Define DB Server Node Name
  #####################################################
  TZ_DB_SERVER_TMP=`grep s_dbhost $CONTEXT_FILE | sed 's/^.*s_dbhost[^>.]*>[ ]*\([^<]*\)<.*/\1/g; s/ *$//g'`

  read TZ_DB_SERVER?"Enter the Database Server Node Name [ $TZ_DB_SERVER_TMP ] : "

  if [ -z "$TZ_DB_SERVER" ]
  then
    TZ_DB_SERVER=$TZ_DB_SERVER_TMP
  fi

  #####################################################
  ### Define DB Server Domain Name
  #####################################################
  TZ_SERVER_DOMAIN_TMP=`grep s_domainname $CONTEXT_FILE | sed 's/^.*s_domainname[^>.]*>[ ]*\([^<]*\)<.*/\1/g; s/ *$//g'`

  read TZ_SERVER_DOMAIN?"Enter the Database Server Domain Name [ $TZ_SERVER_DOMAIN_TMP ] : "

  if [ -z "$TZ_SERVER_DOMAIN" ]
  then
    TZ_SERVER_DOMAIN=$TZ_SERVER_DOMAIN_TMP
  fi
}

AppsInfo()
{
  read TZ_APPS_PWD?"Enter the APPS password [ APPS ] : "

  if [ -z "${TZ_APPS_PWD}" ]
  then
    TZ_APPS_PWD=APPS
  fi
}

DatabaseConnTest()
{
  sqlplus apps/${1} << ! > ${2}
  select 'Database Name : '||name||'' from v\$database;

  select 'Session Timeout : '||b.profile_option_value||''
  from fnd_profile_options a, fnd_profile_option_values b
  where a.profile_option_name = 'ICX_SESSION_TIMEOUT'
  and a.profile_option_id = b.profile_option_id;
!

  TZ_SESSION_TIMEOUT=`grep 'Session Timeout' ${2} | cut -c19-25`

  DBNAME=`grep 'Database Name' ${2} | cut -c17-25`
  OKSTR=`grep Connected ${2}`

  if [ $? -eq 0 ]
  then
    if [ "${3}" = "$DBNAME" ]
    then
      echo "\nDatabase connect successfully !!!"
      echo "Your Database Name is [ ${DBNAME} ] !!!\n"
      rm ${2}
    else
      echo "\nERROR: Your Database is not ${3} !!!"
      echo "Please check your database name !!!\n"
      rm ${2}
      exit
    fi
  else
    echo "\nERROR: No Valid database is found "
    echo "Please check your database status or apps password !!!\n"
    rm ${2}
    exit
  fi
}

DatabaseCheckTimeTest()
{
  sqlplus apps/${1}@${2} << ! > $MTZ_DB_CONN_LOG
  alter session set nls_date_format='YYYY/MM/DD HH24:MI:SS';
  select 'Database Time : '||sysdate||'' from dual;
!
}

CompareSystemTime()
{
  DBDATE=`grep 'Database Time' ${1} | cut -c17-40`
  SYSDATE=`export TZ=${2};date +"%Y/%m/%d %H:%M:%S"`

  echo "\n====================================================================================="
  echo "*** Current $TZ_UPPER_DB_SID time (${2})	: $SYSDATE"
  echo "*** Current $TZ_UPPER_NAME time (${3})	: $DBDATE"
  echo "=====================================================================================\n"

  echo "Please compare above system default time ($TZ_UPPER_DB_SID) and $TZ_UPPER_NAME time !!"

  rm ${1}
}

BackupTNSADMINConfFile()
{
  for FILE_NAME in $*
  do
    cp $FILE_NAME $MTZ_BACKUP_DIR 2>/dev/null

    if [ $? -eq 1 ]
    then
      echo "No $FILE_NAME"
      echo "  or cannot make backup of $FILE_NAME in TNS_ADMIN directory !"
      echo "  $TZ_TNS_ADMIN"
      echo "  Please check your TNS_ADMIN directory ...\n"
      exit
    fi
  done
}

BackupEtcParameterFile()
{
  for FILE_NAME in $*
  do
    cp $FILE_NAME $MTZ_BACKUP_DIR 2>/dev/null

    if [ $? -eq 1 ]
    then
      echo "No $FILE_NAME"
      echo "  or cannot make backup of $FILE_NAME !"
      exit
    fi
  done
}

BackupDBMTZScriptsFile()
{
  for FILE_NAME in $*
  do
    cp $FILE_NAME $MTZ_BACKUP_DIR 2>/dev/null

    if [ $? -eq 1 ]
    then
      echo "No $FILE_NAME or cannot make backup of it" 
      echo "  in $TNS_ADMIN directory !"
      echo "  Will make new Multi Time Zone Scripts later !\n"
    fi
  done
}

CheckNetworkPort()
{
  echo " "
  for TZ_CHECKPORT in ${1}
  do
    OKSTR=`netstat -an | grep LISTEN | awk {'print $4'} | grep .$TZ_CHECKPORT`

    if [ $? -eq 0 ]
    then
      TZ_CHECK_PORT_RESULT=`lsof -i:${TZ_CHECKPORT} | grep LISTEN | awk {'print $1" of "$3", Process ID is "$2'}`
      TZ_CHECK_PORT_PROCESS=`lsof -i:${TZ_CHECKPORT} | grep LISTEN | awk {'print $2'}`

      echo "\nERROR: Time Zone Configuration Port ${TZ_CHECKPORT} is already used by ${TZ_CHECK_PORT_RESULT} !!\n"
      ps $TZ_CHECK_PORT_PROCESS
      echo "\nPlease check Time Zone or Port Pool !!!\n"

      exit
    else
      echo "Checking Time Zone Configuration Port ${TZ_CHECKPORT}..."
      echo "${TZ_CHECKPORT} is free !!\n"
    fi
  done
  echo " "
}

CheckNetworkAliasInTNSADMIN()
{
  for FILE_NAME in ${4} ${5}
  do
    if [ -z $FILE_NAME ]
    then
      exit
    fi

    sed 's/ //g' $FILE_NAME > ${2}

    OKSTR=`grep -i ^${1}= ${2}`

    if [ $? -eq 0 ]
    then
      echo "\nERROR: Time Zone Name Alias $TZ_NAME is not valid "
      echo "$FILE_NAME"
      echo "Database Name is ${3}"
      echo "Please check Time Zone or port pool !!!\n"
      exit
    else
      echo "\nChecking the Port Alias Name $TZ_NAME"
      echo "Port Alias $TZ_NAME is valid"
      echo "$FILE_NAME"
      echo "Database Name is ${3}\n"
    fi

    rm ${2}
 done
}

CheckNetworkAliasInTNSADMINFalse()
{
  for FILE_NAME in ${4} ${5}
  do
    if [ -z $FILE_NAME ]
    then
      exit
    fi

    sed 's/ //g' $FILE_NAME > ${2}

    OKSTR=`grep -i ^${1}= ${2}`

    if [ $? -eq 0 ]
    then
      echo "\nChecking the Port Alias Name $TZ_NAME"
      echo "Port Alias $TZ_NAME is valid"
      echo "$FILE_NAME"
      echo "Database Name is ${3}\n"
    else
      echo "\nERROR: Time Zone Name Alias $TZ_NAME is not valid"
      echo "$FILE_NAME"
      echo "Database Name is ${3}"
      echo "Please check Time Zone or port pool !!!\n"
      exit
    fi

    rm ${2}
 done
}

CheckFindNetworkPortInFile()
{
  for FILE_NAME in ${2} ${3}
  do
    if [ -z $FILE_NAME ]
    then
      exit
    fi

    OKSTR=`grep ${1} $FILE_NAME`

    if [ $? -eq 0 ]
    then
      echo "\nChecking the Port Pool ${1}"
      echo "done: Port Pool ${1} is valid"
      echo "$FILE_NAME"
      echo "Database Port is ${1}\n"
    else
      echo "\nERROR: Time Zone Database Port ${1} is not exist"
      echo "$FILE_NAME"
      echo "Database Port is ${1}"
      echo "Please check your Time Zone or port pool !!!\n"
      exit
    fi
  done
}

CheckFindNetworkPortInFileFalse()
{
  for FILE_NAME in ${2} ${3}
  do
    if [ -z $FILE_NAME ]
    then
      exit
    fi

    OKSTR=`grep ${1} $FILE_NAME`

    if [ $? -eq 0 ]
    then
      echo "\nERROR: Time Zone Database Port ${1} is not exist"
      echo "$FILE_NAME"
      echo "Database Port is ${1}"
      echo "Please check your Time Zone or port pool !!!\n"
      exit
    else
      echo "\nChecking the Port Pool ${1}"
      echo "done: Port Pool ${1} is valid"
      echo "$FILE_NAME"
      echo "Database Port is ${1}\n"
    fi
  done
}

AppendToDBMTZScripts()
{
  echo "
lsnrctl start $TZ_UPPER_NAME " >> $ADMTZSRT_FILE

  echo "
lsnrctl stop $TZ_UPPER_NAME " >> $ADMTZSTP_FILE
}

AppendToAPMTZScripts()
{
  echo "
./adapcctl_${TZ_LOWER_NAME} start " >> $ADMTZSRT_FILE

  echo "
./adapcctl_${TZ_LOWER_NAME} stop " >> $ADMTZSTP_FILE
}

DisplayTimeZoneNoDayLightSaving()
{
  echo "
  (CUT0)           Coordinated Universal Time   (CUT)
  (GMT0)           United Kingdom               (CUT)
  (AZOREST1)       Azores; Cape Verde        (CUT -1)
  (FALKST2)        Falkland Islands          (CUT -2)
  (GRNLNDST3)      Greenland; East Brazil    (CUT -3)
  (AST4)           Central Brazil            (CUT -4)
  (EST5)           Eastern U.S.; Colombia    (CUT -5)
  (CST6)           Central U.S.; Honduras    (CUT -6)
  (MST7)           Mountain U.S.             (CUT -7)
  (PST8)           Pacific U.S.; Yukon       (CUT -8)
  (AST9)           Alaska                    (CUT -9)
  (HST10)          Hawaii; Aleutian          (CUT-10)
  (BST11)          Bering Straits            (CUT-11)
  (NZST-12)        New Zealand               (CUT+12)
  (MET-11)         Solomon Islands           (CUT+11)
  (EET-10)         Eastern Australia         (CUT+10)
  (JST-9)          Japan                     (CUT +9)
  (KORST-9)        Korea                     (CUT +9)
  (WAUST-8)        Western Australia         (CUT +8)
  (TAIST-8)        Taiwan                    (CUT +8)
  (BEIST-8)        Peoples Rep. China        (CUT +8)
  (THAIST-7)       Thailand                  (CUT +7)
  (NOVST-6)        Novosibirsk, Russia       (CUT +6)
  (PAKST-5)        Pakistan                  (CUT +5)
  (WST-4)          Gorki; Central Asia; Oman (CUT +4)
  (MEST-3)         Turkey                    (CUT +3)
  (SAUST-3)        Saudi Arabia              (CUT +3)
  (WET-2)          Finland                   (CUT +2)
  (USAST-2)        South Africa              (CUT +2)
  (NFT-1)          Norway; France            (CUT +1)
	"
}

DisplayTimeZoneDayLightSaving()
{
  echo "
  (CUT0GDT)           Coordinated Universal Time   (CUT)
  (GMT0BST)           United Kingdom               (CUT)
  (AZOREST1AZOREDT)   Azores; Cape Verde        (CUT -1)
  (FALKST2FALKDT)     Falkland Islands          (CUT -2)
  (GRNLNDST3GRNLNDDT) Greenland; East Brazil    (CUT -3)
  (AST4ADT)           Central Brazil            (CUT -4)
  (EST5EDT)           Eastern U.S.; Colombia    (CUT -5)
  (CST6CDT)           Central U.S.; Honduras    (CUT -6)
  (MST7MDT)           Mountain U.S.             (CUT -7)
  (PST8PDT)           Pacific U.S.; Yukon       (CUT -8)
  (AST9ADT)           Alaska                    (CUT -9)
  (HST10HDT)          Hawaii; Aleutian          (CUT-10)
  (BST11BDT)          Bering Straits            (CUT-11)
  (NZST-12NZDT)       New Zealand               (CUT+12)
  (MET-11METDT)       Solomon Islands           (CUT+11)
  (EET-10EETDT)       Eastern Australia         (CUT+10)
  (JST-9JDT)          Japan                     (CUT +9)
  (KORST-9KORDT)      Korea                     (CUT +9)
  (WAUST-8WAUDT)      Western Australia         (CUT +8)
  (TAIST-8TAIDT)      Taiwan                    (CUT +8)
  (BEIST-8BEIDT)      Peoples Rep. China        (CUT +8)
  (THAIST-7THAIDT)    Thailand                  (CUT +7)
  (NOVST-6NOVDT)      Novosibirsk, Russia       (CUT +6)
  (PAKST-5PAKDT)      Pakistan                  (CUT +5)
  (WST-4WDT)          Gorki; Central Asia; Oman (CUT +4)
  (MEST-3MEDT)        Turkey                    (CUT +3)
  (SAUST-3SAUDT)      Saudi Arabia              (CUT +3)
  (WET-2WET)          Finland                   (CUT +2)
  (USAST-2USADT)      South Africa              (CUT +2)
  (NFT-1DFT)          Norway; France            (CUT +1)
	"
}

DisplayCommonSetting()
{
  echo "* Database Server Node Name : ${TZ_DB_SERVER}"
  echo "* Target System database name : ${TZ_DB_SID}"

  if [ "$TZ_BASE" = "apTier" ]
  then
    echo "* Application Server Node Name : ${TZ_AP_SERVER}"
  fi

  echo "* Default port pool number : ${TZ_DTZ_PORT}"
  echo "* Default database port : ${TZ_DTZ_DB_TNS_PORT}"

  echo "* Multi Time Zone name : ${TZ_NAME}"
  echo "* Multi Time Zone port pool number : ${TZ_MTZ_PORT}"
  echo "* Multi Time Zone database port : ${TZ_MTZ_DB_TNS_PORT}"
}

DisplayDBTierSetting()
{
  echo "* RDBMS ORACLE_HOME directory : ${TZ_ORACLE_HOME}"
  echo "* RDBMS TNS_ADMIN directoryy : ${TZ_TNS_ADMIN}"
  echo "\n* Script will modify following files"
  echo "  ${TZ_TNS_ADMIN}/listener.ora"
  echo "  ${TZ_TNS_ADMIN}/tnsnames.ora"
  echo "  (Database Listener Port : ${TZ_MTZ_DB_TNS_PORT})"
}

DisplayCMTierSetting()
{
  echo "* 8.0.6 ORACLE_HOME directory : ${TZ_ORACLE_HOME}"
  echo "* 8.0.6 TNS_ADMIN directory : ${TZ_TNS_ADMIN}"
  echo "* DBC file directory : ${FND_SECURE}"
  echo "\n* Script will make following files"
  echo "  ${FND_SECURE}/${TZ_LOWER_NAME}.dbc"
  echo "\n* Script will modify following files"
  echo "  ${TZ_TNS_ADMIN}/tnsnames.ora"
  echo "  (Database Listener Port : ${TZ_MTZ_DB_TNS_PORT})"

  if [ "$TZ_FS" = "SHARED" ]
  then
    echo "* Instance using Shared File System (Shared APPL_TOP)"
  fi
}

DisplayAPTierSetting()
{
  echo "* Multi Time Zone Apache (HTTP) port : ${TZ_MTZ_HTTP_PORT}"
  echo "* 8.0.6 ORACLE_HOME directory : ${TZ_ORACLE_HOME}"
  echo "* 8.0.6 TNS_ADMIN directory : ${TZ_TNS_ADMIN}"
  echo "* iAS TNS_ADMIN directory : ${TZ_IAS_TNS_ADMIN}"
  echo "* DBC file directory : ${FND_SECURE}"
  echo "\n* Script will make following files"
  echo "  ${FND_SECURE}/${TZ_LOWER_NAME}.dbc"
  echo "  ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd.conf"
  echo "  ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd_pls.conf"
  echo "  ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oracle_apache.conf"
  echo "  ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oprocmgr.conf"
  echo "  ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/apps.conf"
  echo "  ${TZ_APACHE_TOP}/modplsql/cfg/plsql_${TZ_LOWER_NAME}.conf"
  echo "  ${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl_${TZ_LOWER_NAME}.sh"
  echo "  ${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.conf"
  echo "  ${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.properties"
  echo "  ${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/zone.properties"
  echo "  ${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/ssp_init.txt"
  echo "\n* Script will modify following files"
  echo "  ${TZ_TNS_ADMIN}/tnsnames.ora"
  echo "  ${TZ_IAS_TNS_ADMIN}/tnsnames.ora"
  echo "  ${TZ_PLSQLDAD_PATH}/wdbsvr.app"
  echo "  (Database Listener Port : ${TZ_MTZ_DB_TNS_PORT})"

  if [ "$TZ_FS" = "SHARED" ]
  then
    echo "* Instance using Shared File System (Shared APPL_TOP)"
  fi
}

MakeDirectory()
{
  mkdir -p ${1}

  if [ $? -eq 0 ]
  then
    echo "Creating directory ${1} ..."
  fi
}

ExecModifyFunc()
{
  for MODIFY_FUNCTION in $*
  do 
    echo "Present value   : $ORIFILE"

    cp $ORIFILE $TMPFILE
    ${MODIFY_FUNCTION} $ORIFILE $TMPFILE $CFGFILE
    rm $TMPFILE

    echo "New value       : $CFGFILE\n"
  done
}

ModifyTnsnamesFile()
{
  echo "

###############################################################################
### Start ${TZ_UPPER_NAME} Time Zone for tnsnames.ora - ${YY}/${MM}/${DD} ${HH}:${MI}:${SS}

${TZ_UPPER_NAME} = (DESCRIPTION=
                (ADDRESS=(PROTOCOL=tcp)(HOST=${TZ_DB_SERVER}.${TZ_SERVER_DOMAIN})(PORT=${TZ_MTZ_DB_TNS_PORT}))
                (CONNECT_DATA=(SID=${TZ_UPPER_DB_SID}))
            )
### End ${TZ_UPPER_NAME} Time Zone for tnsnames.ora
###############################################################################

" >> ${1}
}

ModifyListenerFile()
{
  echo "

###############################################################################
### Start ${TZ_UPPER_NAME} Time Zone for listener.ora - ${YY}/${MM}/${DD} ${HH}:${MI}:${SS}

${TZ_UPPER_NAME} =
  (ADDRESS_LIST =
        (ADDRESS= (PROTOCOL= TCP)(Host= ${TZ_DB_SERVER}.${TZ_SERVER_DOMAIN} )(Port= ${TZ_MTZ_DB_TNS_PORT}))
  )

SID_LIST_${TZ_UPPER_NAME} =
  (SID_LIST =
    (SID_DESC =
      (ORACLE_HOME= ${TZ_ORACLE_HOME})
      (SID_NAME = ${TZ_UPPER_DB_SID})
      (ENVS='TZ=$TZ_CUT_NAME')
    )
  )

STARTUP_WAIT_TIME_${TZ_UPPER_NAME} = 0
CONNECT_TIMEOUT_${TZ_UPPER_NAME} = 10
TRACE_LEVEL_${TZ_UPPER_NAME} = OFF

LOG_DIRECTORY_${TZ_UPPER_NAME} = ${TZ_ORACLE_HOME}/network/admin
LOG_FILE_${TZ_UPPER_NAME} = ${TZ_UPPER_NAME}
TRACE_DIRECTORY_${TZ_UPPER_NAME} = ${TZ_ORACLE_HOME}/network/admin
TRACE_FILE_${TZ_UPPER_NAME} = ${TZ_UPPER_NAME}
ADMIN_RESTRICTIONS_${TZ_UPPER_NAME} = OFF

### End {TZ_UPPER_NAME} Time Zone for listener.ora 
###############################################################################
" >> ${LISTENER_FILE}
}

ModifyPLSQLDAD()
{
  echo "[DAD_${1}]
connect_string  = ${TZ_UPPER_NAME}
password        = APPS
username        = APPS
default_page    = fnd_web.ping
document_table  = APPS.fnd_lobs_document
document_path   = docs
document_proc   =
upload_as_long_raw =
upload_as_blob  = *
reuse           = Yes
connmax         = 10
pathalias       = fndgfm
pathaliasproc   = fnd_gfm.dispatch
enablesso       = No
cgi_env_list=SERVER_NAME=${4}.sarc.io,REQUEST_PROTOCOL=http,SERVER_PORT=${2},HOST=${4}.sarc.io:${2}
input_filtering = Yes
stateful=STATELESS_RESET
;" >> ${3}/wdbsvr.app
}

CreateDBCFile()
{
  OSTR1="=${TZ_DTZ_DB_TNS_PORT}"
  NSTR1="=${TZ_MTZ_DB_TNS_PORT}"

  sed "s/${OSTR1}/${NSTR1}/g" ${2} > ${3}
}

CreateHttpdConf()
{
  OSTR1="${TZ_DTZ_HTTP_PORT}$"
  OSTR2="\/httpd.lock"
  OSTR3="\/httpd.pid"
  OSTR4="\/httpd.scoreboard"
  OSTR5="\/error_log"
  OSTR6="\/access_log"
  OSTR7="\/oracle_apache.conf"
  OSTR8="\/etc\/jserv.conf"
  OSTR9="\/oprocmgr.conf"

  NSTR1="${TZ_MTZ_HTTP_PORT}"
  NSTR2="\/${TZ_LOWER_NAME}\/httpd.lock"
  NSTR3="\/${TZ_LOWER_NAME}\/httpd.pid"
  NSTR4="\/${TZ_LOWER_NAME}\/httpd.scoreboard"
  NSTR5="\/${TZ_LOWER_NAME}\/error_log"
  NSTR6="\/${TZ_LOWER_NAME}\/access_log"
  NSTR7="\/${TZ_LOWER_NAME}\/oracle_apache.conf"
  NSTR8="\/etc\/${TZ_LOWER_NAME}\/jserv.conf"
  NSTR9="\/${TZ_LOWER_NAME}\/oprocmgr.conf"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" -e "s/${OSTR6}/${NSTR6}/g" -e "s/${OSTR7}/${NSTR7}/g" -e "s/${OSTR8}/${NSTR8}/g" -e "s/${OSTR9}/${NSTR9}/g" ${2} > ${3}
}

CreateHttpdPlsConf()
{
  OSTR1="${TZ_DTZ_HTTP_PORT}$"
  OSTR2="${TZ_DTZ_HTTP_PLS_PORT}$"
  OSTR3="\/httpd_pls.lock"
  OSTR4="\/httpd_pls.pid"
  OSTR5="\/httpd_pls.scoreboard"
  OSTR6="\/error_log_pls"
  OSTR7="\/access_log_pls"

  NSTR1="${TZ_MTZ_HTTP_PORT}"
  NSTR2="${TZ_MTZ_HTTP_PLS_PORT}"
  NSTR3="\/${TZ_LOWER_NAME}\/httpd_pls.lock"
  NSTR4="\/${TZ_LOWER_NAME}\/httpd_pls.pid"
  NSTR5="\/${TZ_LOWER_NAME}\/httpd_pls.scoreboard"
  NSTR6="\/${TZ_LOWER_NAME}\/error_log_pls"
  NSTR7="\/${TZ_LOWER_NAME}\/access_log_pls"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" -e "s/${OSTR6}/${NSTR6}/g" -e "s/${OSTR7}/${NSTR7}/g" ${2} > ${3}
}

CreateOracleApacheConf()
{
  OSTR1="\/plsql.conf"
  OSTR2="\/apps.conf"

  NSTR1="\/plsql_${TZ_LOWER_NAME}.conf"
  NSTR2="\/${TZ_LOWER_NAME}\/apps.conf"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" ${2} > ${3}
}

CreateOprocmgrConf()
{
  sed "s/${TZ_DTZ_OPROCMGR_PORT}/${TZ_MTZ_OPROCMGR_PORT}/g" ${2} > ${3}
}

CreateAppsConf()
{
  sed "s/TWO_TASK ${TZ_UPPER_DB_SID}/TWO_TASK ${TZ_UPPER_NAME}/g" ${2} > ${3}
}

CreatePlsqlConf()
{
  sed "s/:${TZ_DTZ_HTTP_PLS_PORT}/:${TZ_MTZ_HTTP_PLS_PORT}/g" ${2} > ${3}
}

CreateAdapcctlSh()
{
  OSTR1="\/httpd.pid"
  OSTR2="\/httpds.pid"
  OSTR3="\/httpd_pls.pid"
  OSTR4="\/httpd.conf"
  OSTR5="\/httpd_pls.conf"

  NSTR1="\/${TZ_LOWER_NAME}\/httpd.pid"
  NSTR2="\/${TZ_LOWER_NAME}\/httpds.pid"
  NSTR3="\/${TZ_LOWER_NAME}\/httpd_pls.pid"
  NSTR4="\/${TZ_LOWER_NAME}\/httpd.conf"
  NSTR5="\/${TZ_LOWER_NAME}\/httpd_pls.conf"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" ${2} > ${3}
 
  chmod +x $TZ_COMMON_TOP/admin/scripts/$CONTEXT_NAME/adapcctl_${TZ_LOWER_NAME}.sh
}

CreateJservConf()
{
  OSTR1="\/jserv.properties$"
  OSTR2="\/mod_jserv.log"
  OSTR3="^ApJServGroup DiscoGroup"
  OSTR4="^ApJServGroup XmlSvcsGrp"
  OSTR5="^ApJServGroupMount \/mobile"
  OSTR6="^ApJServGroupMount \/discoverer4i"
  OSTR7="^ApJServGroupMount \/soap\/servlet"
  OSTR8="^ApJServGroupMount \/webservices"
  OSTR9="^ApJServGroupMount \/dmsDisco"

  NSTR1="\/${TZ_LOWER_NAME}\/jserv.properties"
  NSTR2="\/${TZ_LOWER_NAME}\/mod_jserv.log"
  NSTR3="#ApJServGroup DiscoGroup"
  NSTR4="#ApJServGroup XmlSvcsGrp"
  NSTR5="#ApJServGroupMount \/mobile"
  NSTR6="#ApJServGroupMount \/discoverer4i"
  NSTR7="#ApJServGroupMount \/soap\/servlet"
  NSTR8="#ApJServGroupMount \/webservices"
  NSTR9="#ApJServGroupMount \/dmsDisco"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" -e "s/${OSTR6}/${NSTR6}/g" -e "s/${OSTR7}/${NSTR7}/g" -e "s/${OSTR8}/${NSTR8}/g" -e "s/${OSTR9}/${NSTR9}/g" ${2} > ${3}
}

CreateJservProperties()
{
  OSTR1="\/${TZ_LOWER_DB_SID}.dbc$"
  OSTR2="\/ibe.log$"
  OSTR3="\/fwsys.log$"
  OSTR4="Provider.port=${TZ_DTZ_IASCACHE_PORT}$"
  OSTR5="\/jserv.log$"
  OSTR6="^port=${TZ_DTZ_JSERV1_PORT}-${TZ_DTZ_JSERV2_PORT}$"
  OSTR7="zones=mobile$"
  OSTR8="^mobile.properties="
  OSTR9="^viewer4i.properties="
  OSTRA="etc\/zone.properties$"
  OSTRB="\/mwa\/"

  NSTR1="\/${TZ_LOWER_NAME}.dbc"
  NSTR2="\/${TZ_LOWER_NAME}\/ibe.log"
  NSTR3="\/${TZ_LOWER_NAME}\/fwsys.log"
  NSTR4="Provider.port=${TZ_MTZ_IASCACHE_PORT}"
  NSTR5="\/${TZ_LOWER_NAME}\/jserv.log"
  NSTR6="port=${TZ_MTZ_JSERV1_PORT}-${TZ_MTZ_JSERV2_PORT}"
  NSTR7="#zones=mobile"
  NSTR8="#mobile.properties="
  NSTR9="#viewer4i.properties="
  NSTRA="etc\/${TZ_LOWER_NAME}\/zone.properties"
  NSTRB="\/mwa_${TZ_LOWER_NAME}\/"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" -e "s/${OSTR6}/${NSTR6}/g" -e "s/${OSTR7}/${NSTR7}/g" -e "s/${OSTR8}/${NSTR8}/g" -e "s/${OSTR9}/${NSTR9}/g" -e "s/${OSTRA}/${NSTRA}/g" -e "s/${OSTRB}/${NSTRB}/g" ${2} > ${3}

  OKSTR=`grep mwa_${TZ_LOWER_NAME} ${3}`

  if [ $? -ne 0 ]
  then
    echo "
# For MWA
wrapper.bin.parameters=-Doracle.apps.mwa=${TZ_APPL_TOP}/mwa_${TZ_LOWER_NAME}/11.5.0
" >> ${3}
  fi
}

CreateZoneProperties()
{
  OSTR1="_pages$"
  OSTR2="=weboam.log$"
  OSTR3="=weboamLocal.log$"
  OSTR4="\/${TZ_LOWER_DB_SID}.dbc$"
  OSTR5="registry_port=${TZ_DTZ_REGISTRY_PORT}"
  OSTR6="etc\/ssp_init.txt$"

  NSTR1="_pages_${TZ_LOWER_NAME}"
  NSTR2="=weboam_${TZ_LOWER_NAME}.log"
  NSTR3="=weboamLocal_${TZ_LOWER_NAME}.log"
  NSTR4="\/${TZ_LOWER_NAME}.dbc"
  NSTR5="registry_port=${TZ_MTZ_REGISTRY_PORT}"
  NSTR6="etc\/${TZ_LOWER_NAME}\/ssp_init.txt"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" -e "s/${OSTR5}/${NSTR5}/g" -e "s/${OSTR6}/${NSTR6}/g" ${2} > ${3}
}

CreateSspInitTxt()
{
  OSTR1="\/${TZ_UPPER_DB_SID}$"
  OSTR4="\/${TZ_LOWER_DB_SID}.dbc$"
  OSTR3="${TZ_DTZ_DB_TNS_PORT}"
  OSTR1="\/debug.log"

  NSTR1="\/${TZ_UPPER_NAME}"
  NSTR2="\/${TZ_LOWER_NAME}.dbc"
  NSTR3="${TZ_MTZ_DB_TNS_PORT}"
  NSTR4="\/${TZ_LOWER_NAME}\/debug.log"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" ${2} > ${3}

  echo "DB_NAME=${TZ_UPPER_DB_SID}" >> ${3}
}

CreateMwaCfg()
{
  echo " "
  read MWA_TELNET_PORT?"* Enter the MWA Telnet Port Number : "
  read MWA_DISPATCHER_PORT?"* Enter the MWA Dispatcher Port Number : "
  echo " "

  OSTR1="mwa.DbcFile=${TZ_LOWER_DB_SID}.dbc$"
  OSTR2="\/log\/${CONTEXT_NAME}$"
  OSTR3="=10200$" 
  OSTR4=":$TZ_DTZ_DISPATCHER1_PORT-$TZ_DTZ_DISPATCHER2_PORT"

  NSTR1="mwa.DbcFile=${TZ_LOWER_NAME}.dbc"
  NSTR2="\/log\/${CONTEXT_NAME}\/${TZ_LOWER_NAME}"
  NSTR3="=$MWA_TELNET_PORT"
  NSTR4=":$MWA_DISPATCHER_PORT"

  sed -e "s/${OSTR1}/${NSTR1}/g" -e "s/${OSTR2}/${NSTR2}/g" -e "s/${OSTR3}/${NSTR3}/g" -e "s/${OSTR4}/${NSTR4}/g" ${2} > ${3}
}

MakeTimeZoneName()
{
  TZ_LOWER_NAME=`echo ${TZ_NAME} | awk {'print tolower($1)'}`
  TZ_UPPER_NAME=`echo ${TZ_NAME} | awk {'print toupper($1)'}`
}

RemoveCommon()
{
  until [ $TZ_NAME ]
  do
    read TZ_NAME?"* Enter the alias of Time Zone : "
  done

  MakeTimeZoneName

  read TZ_MTZ_PORT?"* Enter the port pool number of ${TZ_NAME} [0-99] : "
}

RemoveDBTier()
{
  RemoveCommon

  $ORACLE_HOME/bin/lsnrctl stop ${TZ_UPPER_NAME}

  exit
}

RemoveCMTier()
{
  RemoveCommon

  RMFILE1=$FND_SECURE/${TZ_LOWER_NAME}.dbc

  for FILENAME in $RMFILE1
  do
    echo "\nDeleting $FILENAME\n"

    rm $FILENAME

    if [ $? -eq 0 ]
    then
      echo "  Deletion Successful\n"
    fi
  done

  exit
}

RemoveAPTier()
{
  RemoveCommon

  $COMMON_TOP/admin/scripts/$CONTEXT_NAME/adapcctl_${TZ_LOWER_NAME}.sh stop

  RMFILE1=$APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}/httpd.conf
  RMFILE2=$APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}/httpd_pls.conf
  RMFILE3=$APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}/oracle_apache.conf
  RMFILE4=$APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}/oprocmgr.conf
  RMFILE5=$APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}/apps.conf
  RMFILE6=$APACHE_TOP/Jserv/etc/${TZ_LOWER_NAME}/jserv.conf
  RMFILE7=$APACHE_TOP/Jserv/etc/${TZ_LOWER_NAME}/jserv.properties
  RMFILE8=$APACHE_TOP/Jserv/etc/${TZ_LOWER_NAME}/zone.properties
  RMFILE9=$APACHE_TOP/Jserv/etc/${TZ_LOWER_NAME}/ssp_init.txt
  RMFILE10=$APACHE_TOP/modplsql/cfg/plsql_${TZ_LOWER_NAME}.conf
  RMFILE11=$APACHE_TOP/Apache/logs/${TZ_LOWER_NAME}/access_log_pls.*
  RMFILE12=$APACHE_TOP/Apache/logs/${TZ_LOWER_NAME}/httpd.pid
  RMFILE13=$APACHE_TOP/Apache/logs/${TZ_LOWER_NAME}/error_log_pls
  RMFILE14=$APACHE_TOP/Apache/logs/${TZ_LOWER_NAME}/error_log
  RMFILE15=$APACHE_TOP/Jserv/logs/${TZ_LOWER_NAME}/jserv.log
  RMFILE16=$FND_SECURE/${TZ_LOWER_NAME}.dbc

  for FILENAME in $RMFILE1 $RMFILE2 $RMFILE3 $RMFILE4 $RMFILE5 $RMFILE6 $RMFILE7 $RMFILE8 $RMFILE9 $RMFILE10 $RMFILE11 $RMFILE12 $RMFILE13 $RMFILE14 $RMFILE15 $RMFILE16
  do
    echo "\nDeleting $FILENAME\n"

    rm $FILENAME

    if [ $? -eq 0 ]
    then
      echo "  Deletion Successful\n"
    fi
  done

  rmdir $APACHE_TOP/Apache/conf/${TZ_LOWER_NAME}
  rmdir $APACHE_TOP/Jserv/etc/${TZ_LOWER_NAME}
  rm -rf $APPL_TOP/mwa_${TZ_LOWER_NAME}
  exit
}

CloneCfgMakeQuery()
{
  echo "select substr(d.user_profile_option_name,1,29) user_profile_option_name,
         substr(c.profile_option_value,1,64) profile_option_value,
         d.profile_option_id
  from fnd_profile_option_values c,
       ( select b.profile_option_id, a.user_profile_option_name
         from fnd_profile_options_tl a, fnd_profile_options b
         where a.profile_option_name = b.profile_option_name
       ) d
  where c.profile_option_id = d.profile_option_id
  and c.profile_option_value like '%${1}%';" >> ${2}
}

CreateUser()
{
  #####################################################
  ### Create ERP Applicatiion Users
  #####################################################
  read TZ_ORACLE_HOME?"Target system ORACLE_HOME directory [ $ORACLE_HOME ] : "

  if [ -z "$TZ_ORACLE_HOME" ]
  then
    TZ_ORACLE_HOME=$ORACLE_HOME
  fi

  ###
  ### Get APPS information 
  ###

  AppsInfo

  ###
  ### Configure MTZ Inventory Directory 
  ###

  DefineMTZInventory

  until [ $TZ_NEW_MTZ_USER_TMP ]
  do
    read TZ_NEW_MTZ_USER_TMP?"Enter the new application username : "
  done 

  TZ_NEW_MTZ_USER=`echo $TZ_NEW_MTZ_USER_TMP | awk {'print toupper($1)'}`

  ###
  ### Create Scripts for User ($MTZ_INV)
  ###

  CreateScriptForUser

  ConfirmMTZUser
  
  OKSTR=`grep $TZ_NEW_MTZ_USER $MTZ_CONFIRM_USER_LOG`

  if [ $? -eq 0 ]
  then
    echo "\n[$TZ_NEW_MTZ_USER] is already existing !!!"
    echo " "
    read CONTINUE?"Enter any key to add special User-Level Profile for MTZ to user [$TZ_NEW_MTZ_USER] or [Q] to quit ... "

    if [ \( "$CONTINUE" = "Q" \) -o \( "$CONTINUE" = "q" \) ]
    then
      echo "\nspecial User-Level Profile configuration for MTZ canceled !!!\n"
      exit
    fi
    
    exit
  else
    echo " "
    read CONTINUE?"Enter any key to create user [$TZ_NEW_MTZ_USER] or [Q] to quit ... "
   
    if [ \( "$CONTINUE" = "Q" \) -o \( "$CONTINUE" = "q" \) ]
    then
      echo "\nUser [$TZ_NEW_MTZ_USER] creation canceled !!!\n"
      exit
    else
      sqlplus -s apps/$TZ_APPS_PWD @$MTZ_INV/.fnd_user_pkg_create_user.sql $TZ_NEW_MTZ_USER > $MTZ_NEW_MTZ_USER_CREATE

      ConfirmMTZUser
 
      OKSTR=`grep $TZ_NEW_MTZ_USER $MTZ_CONFIRM_USER_LOG`

      if [ $? -eq 0 ]
      then
        echo "\nUser [$TZ_NEW_MTZ_USER] created successfully !!!"
        echo "\nPlease check following responsibility to use [$TZ_NEW_MTZ_USER] ... \n"
        
        ConfirmMTZUserResp

        echo "***********************************************************************"
        cat $MTZ_NEW_MTZ_USER_RESP
        echo "***********************************************************************"

        read CONTINUE?"Enter any key to add special User-Level Profile for MTZ to user [$TZ_NEW_MTZ_USER] or [Q] to quit ... "

        if [ \( "$CONTINUE" = "Q" \) -o \( "$CONTINUE" = "q" \) ]
        then
          echo "\nspecial User-Level Profile configuration for MTZ canceled !!!\n"
          exit
        fi

        exit    
      else
        echo "\nUser $TZ_NEW_MTZ_USER failed to create !!\n"
        cat $MTZ_NEW_MTZ_USER_CREATE
        exit 
      fi
      exit
    fi
  fi

  exit
}

CreateScriptForUser()
{
  echo "set heading off
set verify off
select user_name 
from fnd_user 
where user_name = '&1';
exit" > $MTZ_INV/.confirm_user.sql

  echo "exec fnd_user_pkg.createuser( x_user_name => '&1', x_owner => 'SEED', x_unencrypted_password => 'welcome', x_password_Date => sysdate );
exec fnd_user_pkg.addresp( username => '&1', resp_app => 'SYSADMIN', resp_key => 'SYSTEM_ADMINISTRATOR', security_group => 'STANDARD', description => null, start_date => sysdate, end_date => null);
exec fnd_user_pkg.addresp( username => '&1', resp_app => 'ICX', resp_key => 'SYSTEM_ADMINISTRATION', security_group => 'STANDARD', description => null, start_date => sysdate, end_date => null);
commit;
exit;" > $MTZ_INV/.fnd_user_pkg_create_user.sql

  echo "select c.responsibility_key 
from fnd_user a, fnd_user_resp_groups_direct b, fnd_responsibility c
where a.user_id = b.user_id and b.responsibility_id = c.responsibility_id and user_name='&1';
exit;" > $MTZ_INV/.confirm_user_resp.sql
}

ConfirmMTZUser()
{
  sqlplus -s apps/$TZ_APPS_PWD @$MTZ_INV/.confirm_user.sql $TZ_NEW_MTZ_USER > $MTZ_CONFIRM_USER_LOG
}

ConfirmMTZUserResp()
{
  sqlplus -s apps/$TZ_APPS_PWD @$MTZ_INV/.confirm_user_resp.sql $TZ_NEW_MTZ_USER > $MTZ_NEW_MTZ_USER_RESP
}




CloneCfg()
{
  #####################################################
  ### Define RDBMS ORACLE_HOME
  #####################################################
  read TZ_ORACLE_HOME?"Target system ORACLE_HOME directory [ $ORACLE_HOME ] : "

  if [ -z "$TZ_ORACLE_HOME" ]
  then
    TZ_ORACLE_HOME=$ORACLE_HOME
  fi

  DefineMTZInventory

  ###
  ### Define OLD Instance Name
  ###
 
  until [ $TZ_OLD_DB_SID ]
  do
    read TZ_OLD_DB_SID?"Target System old database name : "
  done

  TZ_UPPER_OLD_DB_SID=`echo $TZ_OLD_DB_SID | awk {'print toupper($1)'}`
  TZ_LOWER_OLD_DB_SID=`echo $TZ_OLD_DB_SID | awk {'print tolower($1)'}`

  ###
  ### Define New Instance Name
  ###

  read TZ_DB_NEW_SID?"Target System new database name [ $ORACLE_SID ] : "

  if [ -z "$TZ_DB_NEW_SID" ]
  then
    TZ_DB_NEW_SID=$ORACLE_SID
  fi

  TZ_UPPER_NEW_DB_SID=`echo $TZ_NEW_DB_SID | awk {'print toupper($1)'}`
  TZ_LOWER_NEW_DB_SID=`echo $TZ_NEW_DB_SID | awk {'print tolower($1)'}`

  ###
  ### Define OLD Instance Port
  ###

  until [ $TZ_MTZ_OLD_PORT ]
  do
    read TZ_MTZ_OLD_PORT?"Enter the old port pool number of ${TZ_UPPER_OLD_DB_SID} [0-99] : "
  done

  ###
  ### Define New Instance Port
  ###

  TZ_MTZ_NEW_DB_PORT=`grep s_dbport $CONTEXT_FILE | sed 's/^.*PORT[^>.]*>[ ]*\([^<]*\)<.*/\1/g; s/ *$//g'`
  (( TZ_MTZ_NEW_PORT_TMP = TZ_MTZ_NEW_DB_PORT - 1521 ))

  read TZ_MTZ_NEW_PORT?"Enter the new port pool number of ${TZ_UPPER_NEW_DB_SID} [ $TZ_MTZ_NEW_PORT_TMP ] : "

  if [ -z "$TZ_MTZ_NEW_PORT_TMP" ]
  then
    TZ_MTZ_NEW_PORT=$ORACLE_SID
  fi

  (( TZ_MTZ_HTTP_PORT = ${TZ_MTZ_OLD_PORT} + 8000 )) 

  AppsInfo

  QUERY1=$MTZ_BACKUP_DIR/mtz_query1.sql
  QUERY2=$MTZ_BACKUP_DIR/mtz_query2.sql
  QUERY3=$MTZ_BACKUP_DIR/mtz_query3.sql

  RESULT1_TMP=$MTZ_BACKUP_DIR/mtz_query_result1.tmp
  RESULT2_TMP=$MTZ_BACKUP_DIR/mtz_query_result2.tmp
  RESULT3_TMP=$MTZ_BACKUP_DIR/mtz_query_result3.tmp

  RESULT1=$MTZ_BACKUP_DIR/mtz_query_result1.log
  RESULT2=$MTZ_BACKUP_DIR/mtz_query_result2.log
  RESULT3=$MTZ_BACKUP_DIR/mtz_query_result3.log

  CloneCfgMakeQuery ${TZ_UPPER_OLD_DB_SID} $QUERY1
  CloneCfgMakeQuery ${TZ_LOWER_OLD_DB_SID} $QUERY2
  CloneCfgMakeQuery ${TZ_MTZ_HTTP_PORT} $QUERY3

  sqlplus apps/${TZ_APPS_PWD}@${TZ_UPPER_NEW_DB_SID} << ! > $MTZ_DB_CONN_LOG

  set linesize 120
  set pagesize 50
  set echo off
  set verify off
  set feedback off
 
  column user_profile_option_name for a30
  column profile_option_value for a65 
  column profile_option_id for 99999999

  spool $RESULT1_TMP
  @$QUERY1
  spool off

  spool $RESULT2_TMP
  @$QUERY2
  spool off

  spool $RESULT3_TMP
  @$QUERY3
  spool off
!

  sed '/SQL>/d' $RESULT1_TMP > $RESULT1
  sed '/SQL>/d' $RESULT2_TMP > $RESULT2
  sed '/SQL>/d' $RESULT3_TMP > $RESULT3
 
  rm $QUERY1
  rm $QUERY2
  rm $QUERY3

  rm $RESULT1_TMP
  rm $RESULT2_TMP
  rm $RESULT3_TMP

  echo "\nPlease check following result and have to update some tables\n"

  ls -l $MTZ_BACKUP_DIR/mtz_query_result*.log | awk {'print $9'}
 
  echo " "

  exit
}

#####################################################
### Start Scripts
#####################################################

echo ""

TZ_BASE=${1}

case "$TZ_BASE" in
'dbTier')
  TierName="DB"
  banner "$TierName Tier"
  ;;
'cmTier')
  TierName="CM"
  banner "$TierName Tier"
  ;;
'apTier')
  TierName="AP"
  banner "$TierName Tier"
  ;;
'dbTierRemove')
  TierName="DB"
  banner "$TierName Tier"
  RemoveDBTier
  ;;
'cmTierRemove')
  TierName="CM"
  banner "$TierName Tier"
  RemoveCMTier
  ;;
'apTierRemove')
  TierName="AP"
  banner "$TierName Tier"
  RemoveAPTier
  ;;
'crUser')
  banner "Create"
  banner "MTZ User"
  CreateUser
  ;;
'CloneCfg')
  CloneCfg
  banner "Cloning"
  ;;
*)
  echo "  $0 $1: unrecognized action specified\n"
  echo "  USAGE:

    $0 {dbTier|cmTier|apTier|dbTierRemove|cmTierRemove|apTierRemove|crUser|CloneCfg}\n"
  exit 1
  ;;
esac

case "$TZ_BASE" in
'dbTier'|'cmTier'|'apTier')
  banner "MTZ"
  banner "INSTALL"
  ;;
esac

echo "\nOracle Multi Time Zone Installer version 0.9.1.0"

#####################################################
### Check Required Utilities
#####################################################

if [ "$TZ_BASE" = "dbTier" ]
then
  CheckUtilityPath netstat lsof sqlplus lsnrctl
else
  CheckUtilityPath netstat lsof sqlplus
fi

echo " "

#####################################################
### Check Shared File System (APPL_TOP)
#####################################################

case "$TZ_BASE" in
'cmTier'|'apTier')
  read TZ_FS?"Do you enable a shared file system for this instance? [Y/n] : "

  if [ -z "$TZ_FS" -o "$TZ_FS" = "Y" -o "$TZ_FS" = "y" ]
  then
    TZ_FS="SHARED"

    TZ_TNS_ADMIN_TMP=$TOOLS_CONFIG_HOME/network/admin/$CONTEXT_NAME
    TZ_IAS_TNS_ADMIN_TMP=$IAS_CONFIG_HOME/network/admin/$CONTEXT_NAME
  else
    TZ_FS="NONSHARED"

    TZ_TNS_ADMIN_TMP=$TNS_ADMIN
    TZ_IAS_TNS_ADMIN_TMP=`echo $TNS_ADMIN | sed "s/8.0.6/iAS/g"`
  fi
  ;;
esac

echo " "

#####################################################
### Define DB SID
#####################################################

read TZ_DB_SID?"Target System database name [ $ORACLE_SID ] : "

if [ -z "$TZ_DB_SID" ]
then
  TZ_DB_SID=$ORACLE_SID
fi

TZ_UPPER_DB_SID=`echo $TZ_DB_SID | awk {'print toupper($1)'}`
TZ_LOWER_DB_SID=`echo $TZ_DB_SID | awk {'print tolower($1)'}`

#####################################################
### Define RDBMS ORACLE_HOME, TNS_ADMIN
#####################################################

case "$TZ_BASE" in
'dbTier')

  ###
  ### Define DB Server and Domain Name
  ###

  DefineCommonServerInfo

  #####################################################
  ### Define RDBMS ORACLE_HOME
  #####################################################
  read TZ_ORACLE_HOME?"Target system RDBMS ORACLE_HOME directory [ $ORACLE_HOME ] : "

  if [ -z "$TZ_ORACLE_HOME" ]
  then
    TZ_ORACLE_HOME=$ORACLE_HOME
  fi

  #####################################################
  ### Define RDBMS TNS_ADMIN
  #####################################################
  read TZ_TNS_ADMIN?"Target system RDBMS TNS_ADMIN directory [ $TNS_ADMIN ] : "

  if [ -z "$TZ_TNS_ADMIN" ]
  then
    TZ_TNS_ADMIN=$TNS_ADMIN
  fi
  ;;
'cmTier')

  ###
  ### Define DB Server and Domain Name
  ###

  DefineCommonServerInfo

  #####################################################
  ### Define cmTier ORACLE_HOME
  #####################################################
  read TZ_ORACLE_HOME?"Target system 8.0.6 ORACLE_HOME directory [ $ORACLE_HOME ] : "

  if [ -z "$TZ_ORACLE_HOME" ]
  then
    TZ_ORACLE_HOME=$ORACLE_HOME
  fi

  #####################################################
  ### Define cmTier TNS_ADMIN
  #####################################################
  read TZ_TNS_ADMIN?"Target system 8.0.6 TNS_ADMIN directory [ $TZ_TNS_ADMIN_TMP ] : "

  if [ -z "${TZ_TNS_ADMIN}" ]
  then
    TZ_TNS_ADMIN=$TZ_TNS_ADMIN_TMP
  fi
  ;;
'apTier')

  ###
  ### Define DB Server and Domain Name
  ###

  DefineCommonServerInfo

  #####################################################
  ### Define AP Server Node Name
  #####################################################
  TZ_AP_SERVER_TMP=`grep s_hostname $CONTEXT_FILE | sed 's/^.*s_hostname[^>.]*>[ ]*\([^<]*\)<.*/\1/g; s/ *$//g'`
  read TZ_AP_SERVER?"Target system forms / web server node [ $TZ_AP_SERVER_TMP ] : "

  if [ -z "${TZ_AP_SERVER}" ]
  then
        TZ_AP_SERVER=$TZ_AP_SERVER_TMP
  fi

  TZ_LOWER_AP_SVR=`echo ${TZ_AP_SERVER} | awk {'print tolower($1)'}`

  #####################################################
  ### Define apTier ORACLE_HOME
  #####################################################
  read TZ_ORACLE_HOME?"Target system 8.0.6 ORACLE_HOME directory [ $ORACLE_HOME ] : "

  if [ -z "$TZ_ORACLE_HOME" ]
  then
    TZ_ORACLE_HOME=$ORACLE_HOME
  fi

  #####################################################
  ### Define apTier TNS_ADMIN
  #####################################################
  read TZ_TNS_ADMIN?"Target system 8.0.6 TNS_ADMIN directory [ $TZ_TNS_ADMIN_TMP ] : "

  if [ -z "${TZ_TNS_ADMIN}" ]
  then
    TZ_TNS_ADMIN=$TZ_TNS_ADMIN_TMP
  fi

  #####################################################
  ### Define apTier iAS TNS_ADMIN
  #####################################################
  read TZ_IAS_TNS_ADMIN?"Target system iAS TNS_ADMIN directory [ $TZ_IAS_TNS_ADMIN_TMP ] : "

  if [ -z "${TZ_IAS_TNS_ADMIN}" ]
  then
    TZ_IAS_TNS_ADMIN=$TZ_IAS_TNS_ADMIN_TMP
  fi

  #####################################################
  ### Define apTier APPL_TOP
  #####################################################
  read TZ_APPL_TOP?"Target system APPL_TOP mount point [ ${APPL_TOP} ] : "

  if [ -z "${TZ_APPL_TOP}" ]
  then
    TZ_APPL_TOP=$APPL_TOP
  fi

  #####################################################
  ### Define apTier COMMON_TOP
  #####################################################
  read TZ_COMMON_TOP?"Target system COMMON_TOP directory [ ${COMMON_TOP} ] : "

  if [ -z "${TZ_COMMON_TOP}" ]
  then
    TZ_COMMON_TOP=$COMMON_TOP
  fi
  ;;
esac

#####################################################
### Define MTZ Inventory
#####################################################

DefineMTZInventory

#####################################################
### Define APPS info
#####################################################

AppsInfo

#####################################################
### Define Customized Directory
#####################################################

if [ "$TZ_FS" = "SHARED" ]
then
  TZ_PLSQLDAD_PATH=$IAS_CONFIG_HOME/Apache/modplsql/cfg
  TZ_APACHE_TOP=$IAS_CONFIG_HOME/Apache
  TZ_APACHE_HOME_CONF=$IAS_CONFIG_HOME/Apache/Apache/conf
  TZ_APACHE_HOME_LOG=$IAS_CONFIG_HOME/Apache/Apache/logs
  TZ_JSERV_HOME=$IAS_CONFIG_HOME/Apache/Jserv
  TZ_JSERV_HOME_CONF=$IAS_CONFIG_HOME/Apache/Jserv/etc
  TZ_JSERV_HOME_LOG=$IAS_CONFIG_HOME/Apache/Jserv/logs
else
  TZ_PLSQLDAD_PATH=${APACHE_TOP}/modplsql/cfg
  TZ_APACHE_TOP=${APACHE_TOP}
  TZ_APACHE_HOME_CONF=${APACHE_TOP}/Apache/conf
  TZ_APACHE_HOME_LOG=${APACHE_TOP}/Apache/logs
  TZ_JSERV_HOME=${APACHE_TOP}/Jserv
  TZ_JSERV_HOME_CONF=${APACHE_TOP}/Jserv/etc
  TZ_JSERV_HOME_LOG=${APACHE_TOP}/Jserv/logs
fi

#####################################################
### DB connection test
#####################################################

DatabaseConnTest $TZ_APPS_PWD $MTZ_DB_CONN_LOG $TZ_UPPER_DB_SID

LISTENER_FILE=$TZ_TNS_ADMIN/listener.ora
TNSNAMES_FILE=$TZ_TNS_ADMIN/tnsnames.ora
LISTENER_IAS_FILE=$TZ_IAS_TNS_ADMIN/listener.ora
TNSNAMES_IAS_FILE=$TZ_IAS_TNS_ADMIN/tnsnames.ora

case "$TZ_BASE" in
'dbTier')
  ADMTZSRT_FILE=$TZ_TNS_ADMIN/lsnralsrt.sh
  ADMTZSTP_FILE=$TZ_TNS_ADMIN/lsnralstp.sh
  MTZTAB=$MTZ_INV/mtztab_dbTier
  MTZLOG=$MTZ_INV/mtzlog_dbTier_${YY}${MM}${DD}${HH}${MI}${SS}.log
  ;;
'cmTier')
  MTZTAB=$MTZ_INV/mtztab_cmTier
  MTZLOG=$MTZ_INV/mtzlog_cmTier_${YY}${MM}${DD}${HH}${MI}${SS}.log
  ;;
'apTier')
  ADMTZSRT_FILE=${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcsrt.sh
  ADMTZSTP_FILE=${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcstp.sh
  MTZTAB=$MTZ_INV/mtztab_apTier
  MTZLOG=$MTZ_INV/mtzlog_apTier_${YY}${MM}${DD}${HH}${MI}${SS}.log
  ;;
esac

#####################################################
### Backup listener.ora (dbTier only)
###        tnsnames.ora
###        Multi Listener Start & Stop Scripts
#####################################################

case "$TZ_BASE" in
'dbTier')
  echo "\nBacking up listener.ora and tnsnames.ora files. This might take a while...\n"
  BackupTNSADMINConfFile $LISTENER_FILE 
  BackupTNSADMINConfFile $TNSNAMES_FILE 
  BackupDBMTZScriptsFile $ADMTZSRT_FILE
  BackupDBMTZScriptsFile $ADMTZSTP_FILE
  ;;
'cmTier')
  echo "\nBacking up 8.0.6 tnsnames.ora files. This might take a while...\n"
  BackupTNSADMINConfFile $TNSNAMES_FILE
  BackupEtcParameterFile ${FND_SECURE}/${TZ_LOWER_DB_SID}.dbc
  ;;
'apTier')
  echo "\nBacking up 8.0.6 and iAS tnsnames.ora files. This might take a while..."
  BackupTNSADMINConfFile $TNSNAMES_FILE
  BackupTNSADMINConfFile $TNSNAMES_IAS_FILE
  echo "Backing up configuration files affected by the MTZ Script. This might take a while...\n"
  BackupEtcParameterFile ${FND_SECURE}/${TZ_LOWER_DB_SID}.dbc 
  BackupEtcParameterFile ${TZ_PLSQLDAD_PATH}/wdbsvr.app
  BackupEtcParameterFile ${TZ_APACHE_HOME_CONF}/httpd.conf
  BackupEtcParameterFile ${TZ_APACHE_HOME_CONF}/httpd_pls.conf
  BackupEtcParameterFile ${TZ_APACHE_HOME_CONF}/oprocmgr.conf
  BackupEtcParameterFile ${TZ_APACHE_HOME_CONF}/oracle_apache.conf
  BackupEtcParameterFile ${TZ_APACHE_HOME_CONF}/apps.conf
  BackupEtcParameterFile ${TZ_APACHE_TOP}/modplsql/cfg/plsql.conf
  BackupEtcParameterFile ${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl.sh
  BackupEtcParameterFile ${TZ_JSERV_HOME_CONF}/jserv.conf
  BackupEtcParameterFile ${TZ_JSERV_HOME_CONF}/jserv.properties
  BackupEtcParameterFile ${TZ_JSERV_HOME_CONF}/zone.properties
  BackupEtcParameterFile ${TZ_JSERV_HOME_CONF}/ssp_init.txt
  ;;
esac

#####################################################
### Define Deault Port Pool
#####################################################

TZ_DTZ_DB_TNS_PORT_TMP=`grep s_dbport $CONTEXT_FILE | sed 's/^.*PORT[^>.]*>[ ]*\([^<]*\)<.*/\1/g; s/ *$//g'`
(( TZ_DTZ_PORT_TMP = TZ_DTZ_DB_TNS_PORT_TMP - 1521 ))

until [ $TZ_DTZ_PORT ]
do
  read TZ_DTZ_PORT?"Enter the default port pool number of $TZ_UPPER_DB_SID [ $TZ_DTZ_PORT_TMP ] : "
done

(( TZ_DTZ_DB_TNS_PORT = TZ_DTZ_PORT + 1521 ))
(( TZ_DTZ_HTTP_PORT = TZ_DTZ_PORT + 8000 ))

CheckFindNetworkPortInFile $TZ_DTZ_DB_TNS_PORT $TNSNAMES_FILE 

case "$TZ_BASE" in
'cmTier'|'apTier')
  CheckFindNetworkPortInFile $TZ_DTZ_DB_TNS_PORT ${FND_SECURE}/${TZ_LOWER_DB_SID}.dbc
  ;;
esac

#####################################################
### Define Number of Multi Time Zone
#####################################################

echo " "
TZ_COUNT_TEMP=1

until [ $TZ_COUNT_TEMP -eq 0 ]
do
  case "$TZ_BASE" in
  'dbTier'|'cmTier'|'apTier')
    read TZ_COUNT?"Enter number of Time Zone to install (except. default Time Zone) [1-9] : "
    ;;
   esac

  if [[ -z $TZ_COUNT ]]
  then
    TZ_COUNT=1
    TZ_COUNT_TEMP=0
  elif [[ $TZ_COUNT = @(1|2|3|4|5|6|7|8|9) ]]
  then
    TZ_COUNT_TEMP=0
  else
    echo "$TZ_COUNT is incorrect number ! Enter number [1-9] Time Zone !!\n"
  fi
done

MTZ_COUNT=1
MTZ_PROBLEM=0

echo "\nWill setup number of $TZ_COUNT extra Time Zone ..."

while [ $TZ_COUNT -ne 0 ]                               
do       
  echo "\n====================================================================================="
  echo "*** Multi Time Zone Configuration ( Remain number of Time Zone : ${TZ_COUNT} )         "
  echo "=====================================================================================\n"              

  echo "Start $MTZ_COUNT Time Zone configuration !!\n"
  
  if [ -f $MTZTAB ]
  then
    echo "*** $TZ_UPPER_DB_SID System's Current Multi Time Zone Configuration *** "
    
    case "$TZ_BASE" in
    'dbTier')
      echo "+----------------------------------------------------------------+"
      echo "|       Name       Port    DB Port        TZ Value"
      echo "+----------------------------------------------------------------+"
      cat $MTZTAB | awk 'BEGIN { FS=":::" };{ print $2 " " $1 " " $3 " " $4 }' | \
	sort -u | awk {'printf("| %10s %10s %10s %15s\n", $2, $1, $3, $4)'}
      ;;
    'cmTier'|'apTier')
      echo "+----------------------------------------------------------------+"
      echo "|       Name       Port    DB Port"
      echo "+----------------------------------------------------------------+"
      cat $MTZTAB | awk 'BEGIN { FS=":::" };{ print $2 " " $1 " " $3 }' | \
	sort -u | awk {'printf("| %10s %10s %10s\n", $2, $1, $3)'}
      ;;
    esac
    echo "+----------------------------------------------------------------+\n"
  fi

  read TZ_NAME?"* Enter the alias of Time Zone : "

  if [ -z $TZ_NAME ]  
  then
    until [ $TZ_NAME ]
    do
      read TZ_NAME?"* Enter the alias of Time Zone : "
    done
  fi

  read TZ_MTZ_PORT?"* Enter the new port pool number of ${TZ_NAME} [0-99] : "

  #####################################################
  ### Get Time Zone Variable (dbTier only)
  #####################################################
  
  if [ "$TZ_BASE" = "dbTier" ]
  then
    read TZ_DAY_LIST_SAVING?"* Does this Time Zone go on DAYLIGHT SAVINGS TIME ? [y/N] : "

    if [ \( "$TZ_DAY_LIST_SAVING" = "Y" \) -o \( "$TZ_DAY_LIST_SAVING" = "y" \) ]
    then
      DisplayTimeZoneDayLightSaving
    else
      DisplayTimeZoneNoDayLightSaving
    fi
    read TZ_CUT_NAME?"* Enter the ${TZ_NAME}'s CUT (Coordinated Universal Time) Name (ex. KORST-9) : "
  fi
 
  ###
  ### Make Lower / Upper Name 
  ###

  MakeTimeZoneName

  #####################################################
  ### Configure Database Port
  #####################################################

  (( TZ_MTZ_DB_TNS_PORT = TZ_MTZ_PORT + 1521 ))

  #####################################################
  ### Configure All Application Port
  #####################################################

  (( TZ_MTZ_DB_TNS_PORT = ${TZ_MTZ_PORT} + 1521 ))                ### Multi Time Zone Database Listener Port
  (( TZ_MTZ_HTTP_PORT = ${TZ_MTZ_PORT} + 8000 ))                  ### Multi Time Zone Apache Port

  (( TZ_DTZ_HTTP_PLS_PORT = ${TZ_DTZ_PORT} + 8000 + 200 ))        ### httpd_pls.conf, plsql.conf, DTZ PLS Listener Port
  (( TZ_MTZ_HTTP_PLS_PORT = ${TZ_MTZ_PORT} + 8000 + 200 ))        ### httpd_pls.conf, plsql.conf, MTZ PLS Listener Port

  (( TZ_DTZ_OPROCMGR_PORT = ${TZ_DTZ_PORT} + 8000 + 100 ))        ### oprocmgr.conf, DTZ oprocmgr Port
  (( TZ_MTZ_OPROCMGR_PORT = ${TZ_MTZ_PORT} + 8000 + 100 ))        ### oprocmgr.conf, MTZ oprocmgr Port

  (( TZ_DTZ_IASCACHE_PORT = ${TZ_DTZ_PORT} + 12345 ))             ### jserv.properties, DTZ iASCache Port
  (( TZ_MTZ_IASCACHE_PORT = ${TZ_MTZ_PORT} + 12345 ))             ### jserv.properties, MTZ iASCache Port

  (( TZ_DTZ_JSERV1_PORT =  ${TZ_DTZ_PORT} * 10 + 16000 ))         ### jserv.properties, DTZ Jserv Start Port
  (( TZ_DTZ_JSERV2_PORT =  ${TZ_DTZ_PORT} * 10 + 16009 ))         ### jserv.properties, DTZ Jserv End Port
  (( TZ_MTZ_JSERV1_PORT =  ${TZ_MTZ_PORT} * 10 + 16000 ))         ### jserv.properties, MTZ Jserv Start Port
  (( TZ_MTZ_JSERV2_PORT =  ${TZ_MTZ_PORT} * 10 + 16009 ))         ### jserv.properties, MTZ Jserv End Port

  (( TZ_DTZ_REGISTRY_PORT = ${TZ_DTZ_PORT} + 9800 ))              ### zone.properties, DTZ Registry Port
  (( TZ_MTZ_REGISTRY_PORT = ${TZ_MTZ_PORT} + 9800 ))              ### zone.properties, MTZ Registry Port

  (( TZ_DTZ_DISPATCHER1_PORT = ${TZ_DTZ_PORT} * 4 + 10300 ))      ### mwa.cfg, DTZ Dispatcher Start Port 
  (( TZ_DTZ_DISPATCHER2_PORT = ${TZ_DTZ_PORT} * 4 + 10303 ))      ### mwa.cfg, DTZ Dispatcher End Port 

  #####################################################
  ### Check Port
  #####################################################

  case "$TZ_BASE" in
  'dbTier')
    CheckNetworkPort $TZ_MTZ_DB_TNS_PORT
    ;;
  'apTier')
    CheckNetworkPort $TZ_MTZ_HTTP_PORT
    CheckNetworkPort $TZ_MTZ_HTTP_PLS_PORT
    CheckNetworkPort $TZ_MTZ_OPROCMGR_PORT
    CheckNetworkPort $TZ_MTZ_IASCACHE_PORT
    CheckNetworkPort $TZ_MTZ_REGISTRY_PORT
    ;;
  esac
    
  #####################################################
  ### Check Port Number (listener.ora, tnsnames.ora)
  #####################################################

  case "$TZ_BASE" in
  'dbTier')
    CheckFindNetworkPortInFileFalse $TZ_MTZ_DB_TNS_PORT $LISTENER_FILE 
    CheckFindNetworkPortInFileFalse $TZ_MTZ_DB_TNS_PORT $TNSNAMES_FILE
    ;;
  'cmTier')
    CheckFindNetworkPortInFileFalse $TZ_MTZ_DB_TNS_PORT $TNSNAMES_FILE
    ;;
  'apTier')
    CheckFindNetworkPortInFileFalse $TZ_MTZ_DB_TNS_PORT $TNSNAMES_FILE
    CheckFindNetworkPortInFileFalse $TZ_MTZ_DB_TNS_PORT $TNSNAMES_IAS_FILE
    ;;
  esac
	
  #####################################################
  ### Check Port Alias (listener.ora, tnsnames.ora)
  #####################################################

  case "$TZ_BASE" in
  'dbTier')
    CheckNetworkAliasInTNSADMIN $TZ_NAME $MTZ_DB_CONN_LOG $TZ_UPPER_NAME $LISTENER_FILE 
    CheckNetworkAliasInTNSADMIN $TZ_NAME $MTZ_DB_CONN_LOG $TZ_UPPER_NAME $TNSNAMES_FILE
    ;;
  'cmTier')
    CheckNetworkAliasInTNSADMIN $TZ_NAME $MTZ_DB_CONN_LOG $TZ_UPPER_NAME $TNSNAMES_FILE
    ;;
  'apTier')
    CheckNetworkAliasInTNSADMIN $TZ_NAME $MTZ_DB_CONN_LOG $TZ_UPPER_NAME $TNSNAMES_FILE
    CheckNetworkAliasInTNSADMIN $TZ_NAME $MTZ_DB_CONN_LOG $TZ_UPPER_NAME $TNSNAMES_IAS_FILE
    ;;
  esac

  #####################################################
  ### Display Configuration
  #####################################################

  echo "\n====================================================================================="
  echo "*** USER FEEDBACK ***"
  echo "=====================================================================================\n"

  DisplayCommonSetting

  case "$TZ_BASE" in
  'dbTier')
    DisplayDBTierSetting 
    ;;
  'cmTier')
    DisplayCMTierSetting 
    ;;
  'apTier')
    DisplayAPTierSetting
    ;;
  esac

  echo " "
  read CONTINUE?"Enter any key to continue or [Q] to quit ... "

  if [ \( "$CONTINUE" = "Q" \) -o \( "$CONTINUE" = "q" \) ]
  then
    echo "\n${TZ_NAME} Time Zone Configuration canceled !!!\n"
    exit
  fi

  echo "\nRunning $TZ_UPPER_NAME installation - `date`\n"

  #####################################################
  ### Modify tnsnames.ora 
  #####################################################

  ModifyTnsnamesFile $TNSNAMES_FILE
  echo "Configuring $TNSNAMES_FILE...COMPLETED"

  #####################################################
  ### AppsTier Configuration
  #####################################################

  echo " "

  if [ "$TZ_BASE" = "cmTier" ]
  then

    ###
    ### Generate dbc File
    ###

    ORIFILE=$FND_SECURE/${TZ_LOWER_DB_SID}.dbc
    TMPFILE=$FND_SECURE/${TZ_LOWER_NAME}.dbc.tmp
    CFGFILE=$FND_SECURE/${TZ_LOWER_NAME}.dbc
   
    ExecModifyFunc CreateDBCFile
  fi

  if [ "$TZ_BASE" = "apTier" ]
  then
    ModifyTnsnamesFile $TNSNAMES_IAS_FILE
    echo "Configuring $TNSNAMES_IAS_FILE...COMPLETED"

    ModifyPLSQLDAD $TZ_UPPER_NAME $TZ_MTZ_HTTP_PORT $TZ_PLSQLDAD_PATH $TZ_LOWER_AP_SVR
    echo "Configuring $TZ_PLSQLDAD_PATH/wdbsvr.app...COMPLETED\n"

    ###
    ### Make WAS Time Zone Directory
    ###

    MakeDirectory ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}
    MakeDirectory ${TZ_APACHE_HOME_LOG}/${TZ_LOWER_NAME}
    MakeDirectory ${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}
    MakeDirectory ${TZ_JSERV_HOME_LOG}/${TZ_LOWER_NAME}
    MakeDirectory ${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}
 
    cp -Rp $APPL_TOP/mwa $APPL_TOP/mwa_${TZ_LOWER_NAME}
    echo "Creating directory $APPL_TOP/mwa_${TZ_LOWER_NAME} ...\n"

    ###
    ### Generate dbc File
    ###

    ORIFILE=$FND_SECURE/${TZ_LOWER_DB_SID}.dbc
    TMPFILE=$FND_SECURE/${TZ_LOWER_NAME}.dbc.tmp
    CFGFILE=$FND_SECURE/${TZ_LOWER_NAME}.dbc

    ExecModifyFunc CreateDBCFile

    ###
    ### Generate httpd.conf
    ###

    ORIFILE=${TZ_APACHE_HOME_CONF}/httpd.conf
    TMPFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd.conf.tmp
    CFGFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd.conf

    ExecModifyFunc CreateHttpdConf

    ###
    ### Generate httpd_pls.conf
    ###

    ORIFILE=${TZ_APACHE_HOME_CONF}/httpd_pls.conf
    TMPFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd_pls.conf.tmp
    CFGFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/httpd_pls.conf

    ExecModifyFunc CreateHttpdPlsConf

    ###
    ### Generate oracle_apache.conf
    ###

    ORIFILE=${TZ_APACHE_HOME_CONF}/oracle_apache.conf
    TMPFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oracle_apache.conf.tmp
    CFGFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oracle_apache.conf

    ExecModifyFunc CreateOracleApacheConf

    ###
    ### Generate oprocmgr.conf
    ###

    ORIFILE=${TZ_APACHE_HOME_CONF}/oprocmgr.conf
    TMPFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oprocmgr.conf.tmp
    CFGFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/oprocmgr.conf

    ExecModifyFunc CreateOprocmgrConf

    ###
    ### Generate apps.conf
    ###

    ORIFILE=${TZ_APACHE_HOME_CONF}/apps.conf
    TMPFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/apps.conf.tmp
    CFGFILE=${TZ_APACHE_HOME_CONF}/${TZ_LOWER_NAME}/apps.conf

    ExecModifyFunc CreateAppsConf

    ###
    ### Generate plsql.conf
    ###

    ORIFILE=${TZ_APACHE_TOP}/modplsql/cfg/plsql.conf
    TMPFILE=${TZ_APACHE_TOP}/modplsql/cfg/plsql_${TZ_LOWER_NAME}.conf.tmp
    CFGFILE=${TZ_APACHE_TOP}/modplsql/cfg/plsql_${TZ_LOWER_NAME}.conf

    ExecModifyFunc CreatePlsqlConf

    ###
    ### Generate adapcctl.sh
    ###

    ORIFILE=${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl.sh
    TMPFILE=${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl_${TZ_LOWER_NAME}.sh.tmp
    CFGFILE=${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl_${TZ_LOWER_NAME}.sh

    ExecModifyFunc CreateAdapcctlSh

    ###
    ### Generate jserv.conf
    ###

    ORIFILE=${TZ_JSERV_HOME_CONF}/jserv.conf
    TMPFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.conf.tmp
    CFGFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.conf

    ExecModifyFunc CreateJservConf

    ###
    ### Generate jserv.properties
    ###

    ORIFILE=${TZ_JSERV_HOME_CONF}/jserv.properties
    TMPFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.properties.tmp
    CFGFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/jserv.properties

    ExecModifyFunc CreateJservProperties

    ###
    ### Generate zone.properties
    ###

    ORIFILE=${TZ_JSERV_HOME_CONF}/zone.properties
    TMPFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/zone.properties.tmp
    CFGFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/zone.properties

    ExecModifyFunc CreateZoneProperties

    ###
    ### Generate ssp_init.txt
    ###

    ORIFILE=${TZ_JSERV_HOME_CONF}/ssp_init.txt
    TMPFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/ssp_init.txt.tmp
    CFGFILE=${TZ_JSERV_HOME_CONF}/${TZ_LOWER_NAME}/ssp_init.txt

    ExecModifyFunc CreateSspInitTxt

    ###
    ### Generate mwa.cfg
    ###

    ORIFILE=${TZ_APPL_TOP}/mwa/11.5.0/secure/mwa.cfg
    TMPFILE=${TZ_APPL_TOP}/mwa_${TZ_LOWER_NAME}/11.5.0/secure/mwa.cfg.tmp
    CFGFILE=${TZ_APPL_TOP}/mwa_${TZ_LOWER_NAME}/11.5.0/secure/mwa.cfg

    ExecModifyFunc CreateMwaCfg

  fi

  #####################################################
  ### Modify listener.ora (dbTier only)
  #####################################################

  if [ "$TZ_BASE" = "dbTier" ]
  then
    echo "Configuring $LISTENER_FILE...COMPLETED\n"
    ModifyListenerFile
  fi

  #####################################################
  ### Modify MTZ Scripts
  #####################################################

  case "$TZ_BASE" in
  'dbTier')
    AppendToDBMTZScripts
    ;;
  'apTier')
    AppendToAPMTZScripts
    ;;
  esac

  echo "Completed installation - `date`\n"
   
  #####################################################
  ### Listener Test & Start (dbTier only)
  #####################################################

  if [ "$TZ_BASE" = "dbTier" ]
  then
    read DB_CONN_TEST?"Enter [Y] to start $TZ_UPPER_NAME listener and perform connection test [y/N] : "

    if [ \( "$DB_CONN_TEST" = "Y" \) -o \( "$DB_CONN_TEST" = "y" \) ]
    then
      export DEFAULT_TZ=$TZ
      lsnrctl start $TZ_UPPER_NAME >> $MTZLOG

      if [ $? -eq 0 ]
      then
	echo "\n$TZ_UPPER_NAME Time Zone database configuration ended, and $TZ_UPPER_NAME listener started successfully !" 
      else
	echo "\n$TZ_UPPER_NAME Time Zone database configuration ended, but $TZ_UPPER_NAME listener failed to start !"
	MTZ_PROBLEM=1
      fi
    fi

    DatabaseCheckTimeTest $TZ_APPS_PWD $TZ_UPPER_NAME
    CompareSystemTime $MTZ_DB_CONN_LOG $DEFAULT_TZ $TZ_CUT_NAME $TZ_UPPER_NAME
  fi

  #####################################################
  ### Apache Test & Start (apTier only)
  #####################################################

  if [ "$TZ_BASE" = "apTier" ]
  then
    read APACHE_TEST?"Enter [Y] to start $TZ_UPPER_NAME Apache process [y/N] : "

    if [ \( "$APACHE_TEST" = "Y" \) -o \( "$APACHE_TEST" = "y" \) ]
    then
      ${TZ_COMMON_TOP}/admin/scripts/${CONTEXT_NAME}/adapcctl_${TZ_LOWER_NAME}.sh start >> $MTZLOG
      
      if [ $? -eq 0 ]
      then
        echo "\n$TZ_UPPER_NAME Application (Web) Tier configuration ended, and $TZ_UPPER_NAME Apache started successfully !"
      else
        echo "\n$TZ_UPPER_NAME Application (Web) Tier configuration ended, but $TZ_UPPER_NAME Apache failed to start !"
        MTZ_PROBLEM=1
      fi
    fi
  fi

  #####################################################
  ### Register MTZTAB
  #####################################################
 
  if [ -e $MTZTAB ]
  then
    echo " "
  else
    touch $MTZTAB
  fi
   
  OKSTR=`grep '$TZ_UPPER_NAME:::$TZ_MTZ_PORT:::$TZ_MTZ_DB_TNS_PORT:::' $MTZTAB`
  
  if [ $? -ne 0 ]
  then
    case "$TZ_BASE" in
    'dbTier')
      echo "$TZ_UPPER_NAME:::$TZ_MTZ_PORT:::$TZ_MTZ_DB_TNS_PORT:::$TZ_CUT_NAME:::`date`" >> $MTZTAB
      echo "MTZ Inventory Registration... success\n"
      ;;
    'cmTier'|'apTier')
      echo "$TZ_UPPER_NAME:::$TZ_MTZ_PORT:::$TZ_MTZ_DB_TNS_PORT:::`date`" >> $MTZTAB
      echo "MTZ Inventory Registration... success\n"
      ;;
    esac
  fi

  (( TZ_COUNT = TZ_COUNT - 1 ))
  (( MTZ_COUNT = MTZ_COUNT + 1 ))

done                                     

if [ "$TZ_BASE" = "apTier" -a "$TZ_SESSION_TIMEOUT" != "" ]
then
  echo "$TWO_TASK instance's ICX_SESSION_TIMEOUT is $TZ_SESSION_TIMEOUT. Please modify its value."
  echo "(Multi Time Zone architecture has to have null value of ICX_SESSION_TIMEOUT!)"
fi

if [ $TZ_COUNT -eq 0 ]
then
  if [ $MTZ_PROBLEM -eq 0 ]
  then
    echo "\nAll Multi Time Zone database configuration completed successfully !!\n"
  else
    echo "\nERROR: All Multi Time Zone database configuration not completed successfully !!\n"
  fi
else
  echo "\nERROR: All Multi Time Zone database configuration not completed successfully !!\n"
fi