#!/bin/sh
# Copyright 07/16/02  Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)es-setup	1.144 02/07/16 Sun Microsystems"
#
#
# Options:ACDFHhruvw
#   A <file> : Does an automated setup using <file> configuration file
#   C <file> : Stores configuration details of this installation in <file>
#   D <dir>  : Setup a single addon by force. This will also setup other 
#              products on which this addon depends - if they are not setup.
#   F        : Setup/Resetup everything by force.
#   H        : Setup HA server
#   h        : Prints this usage report
#   r        : ReSetup database 
#   u        : Brings SunMC to a factory default level
#   v        : Setup Sun Management Center in verbose mode
#   w        : Skips ReSetup of database while SunMC Base product is resetup.
#   
#
# Environment of this script:
#
prog_base=`/usr/bin/basename $0`
PROGNAME=$0
PREV_DIR=`/usr/bin/pwd`
[ "$GUI_SETUP" != "1" ] && SCRIPT_DIR=`/usr/bin/dirname $PROGNAME`
[ "$SCRIPT_DIR" = "." ] && SCRIPT_DIR=`pwd`
TOOLS_DIR=${SCRIPT_DIR}/../lib/sbin

#later make it $SCRIPT_DIR/somedirname
#
#
#
# External Files:
# (All placed in $TOOLS_DIR)
#
# common_cmds_globals      : Error codes returned by all functions, command aliases and globals
# es-common.sh             : 
# es-common-private.sh     : 
# db-common.sh             :
# es_setup_functions.sh    :
#
#
# Diagnostics:
#   Exit Status: (TBD)
#-----------------------------------------------------------------
# Exit_status        Valid with      Meaning
#-----------------------------------------------------------------
#1.     0x00[000]    All [ACDHFruv]  NO ERRORS
# 
#2.     0x1*         All             GENERAL ERRORS
#       0x11[017]                    - WRONG ARGUMENTS
#       0x12[018]    All             - LOGFILE WRITE Error 
#       0x13[019]    All             - Registry WRITE Error
#       0x14[020]    All             - Registry READ Error
#
#
#3.     0x3*        -C/-A            Resp file READ/WRITE Error 
#       0x31[049]   -C               - Resp file WRITE Error 
#       0x31[050]   -A               - Resp file READ Error 
# 
#4.     0x6*[097]   -H               HA Related errors
#       0x61[098]   -H               - HA Setup failed    
#    
#5.     0x7*        Default,-ruF     Database setup failed
#                                    because of -
#       0x71[113]                    - Memory not enough
#       0x72[114]                    - Create ORACLE user failed
#       0x73[115]                    - Importing of previous SunMC data failed
#       0x74[116]                    - Database Ownership & grup can 
#                                      not be changed to SunMC user
#                                    - TBD
#   
#6.     0x8*        Default,-Fu      Setup failed for base product
#                                    because of -
#       0x81[129]                    - Licence
#       0x82[130]                    - Locale setup failed
#       0x83[131]                    - Java Setup failed
#       0x84[132]                    - Database not OK
#       0x85[133]                    - MC not rebooted after kernel parameters
#                                      tuned i.e. Kernel parameters not proper
#                                    - TBD  
#                                
#                                    The exact details can be found by parsing
#                                    a file /tmp/..../exit_status_setup
#                                
#                                
#7.     0x9*        Default,-DFu     Setup failed for at least one addon
#                                    product selected for setup.
#                                    because of -
#       0x91[145]                    - BASE PROD not setup               
#       0x92[146]                    - Addon setup script returned error.
#                                    - TBD  
#
#                                    The exact details can be found by parsing
#                                    a file /tmp/..../exit_status_setup
#                                
#
#
# Logs:
#     Setup log :/var/opt/SUNWsymon/install/setup.$TIMESTAMP.$$
#     failed products: /tmp/SunMC3.1/failed_products
# 
# Temporary files:
#   -/var/opt/SUNWsymon/install/adminfile.$TIMESTAMP.$$
#
#
#
# Other sourced scripts :


. ${SCRIPT_DIR}/es-common.sh
. ${TOOLS_DIR}/db-common.sh
. ${TOOLS_DIR}/setup-functions.sh
. ${TOOLS_DIR}/script-errors.sh

############################################################################
# Main Starts here
############################################################################

# set OS,Registry,path related GLOBALS
#
set_setup_environment

# This will exit if arguments are not proper. 
# Otherwise, sets/resets setup flags accordingly
#
process_options_setup "$@"

# Setup variables related to response summary file
# 
setup_response_summary

# Find what is installed & what is setup 
#
get_SunMC_setup_status

setup_olddata_dir

# Check if the setup conditions satisfied when run "es-stup -k"
#
#if [ "$generate_security_keys" = "0" ]; then
#    check_keepseed_conditions     # Should not check in case fresh install
#fi

# backup the old key to check if it has been changed in the coming steps.
backup_existing_key

# 
# Setup Java if required
#
setup_java

# Select products & components to setup & display them
#
select_setup_todo

# Stop All SunMC if required
#
stop_SunMC

[ $do_default -eq 1 ] && bring_SunMC_to_default

# Fetch cluster host name & ESDIR in case of HA setup
#
get_HA_information

# Undo HA related changes done by previous run of setup
# This is required for force run of setup and database recreation
#
[ "$install_on_cluster" = "1" -a "$database_setup_required" = "1" ] && undo_HA

# Import preveous data
#
if [ $database_setup_required -eq 1 ] ; then
      set_basedir
      set_db_env
      relink_sunmc_database
fi

# check for addressing mode
# If user has enabled NAT already using es-config, 
# default it back to "ip" and give user a warning.
#
check_for_addressing_mode

get_ex_args

# Read security seed if required
#
if [ $agent_setup_required -eq 1 -o $server_setup_required -eq 1 ] ;then
   if [ "$generate_security_keys" = "1" ]; then
       get_seed_after_upgrade && get_SNMPV1_community_string 
   else
      [ "$OLDDATADIR" != "" ] && migrate_30_security_files
   fi
fi

# Setup Server if required
#
if [ $server_setup_required -eq 1 ] ;then
   no_DE_EXPORTED=$no_DE     
   export no_DE_EXPORTED           ## fix  4839102, 4850470
                                   ## So that sm_setup_server can use it
                                   ## Not exporting no_DE to avoid regression
   setup_component sm_setup_server.sh Server 
   setup_component sm_setup_service.sh Services 
fi


# Setup Agent if required
#
if [ $agent_setup_required -eq 1 ] ;then
   unset_env_variables
   change_esdir "$AGENT_ESDIR"
   setup_component sm_setup_agent.sh Agent
   unset_env_variables
   change_esdir "$DEFAULT_ESDIR"
   get_SunMC_install_status
   if [ "$no_agent" -eq 0 -a "$no_server" -eq 1 ]; then
      # This means that current install is not a server but an agent installation.
      # Check for the previous agent configuration data.
      migrate_agent_data_only
   fi
   OSREV=`${CMD_UNAME} -r| ${CMD_CUT} -d. -f2`
   # process snmpdx daemon only for Solaris 9 and below
   # and process snmpd daemon for Solaris 10 and above
   if [ $OSREV -lt 10 ]; then
       disable_snmpdx_daemon
    else
	process_sma
    fi
fi

# Setup server
#
if [ $server_setup_required -eq 1 ] ;then
   [ "$no_DE" = "0" ]  && ${TOOLS_DIR}/es-setup-de.sh
   server_setup=1
fi

if [ $database_setup_required -eq 1 ] ; then
   setup_sunmc_database
   install_oracle_patches
   retain_cfgserver_engines
   database_setup=1
   import_sunmc_data
   patch_database_schema core
   if [ "$MIGRATE_SUNMC35" -eq 1 ] ; then
      import_sunmc35_data
   fi
   db_post_database_setup
fi

[ $server_setup_required -eq 1 ] && echolog 'Setup of $2 component is successful.' "Server"

# Setup Console
#
if [ $console_setup_required -eq 1 ]; then
   setup_component sm_setup_console.sh Console
   console_setup=1
fi

# These are the files related to Adv services
#
clear_junk_files

if [ $agent_setup_required -eq 1 ] ; then
   
   #If Agent setup was done then prepare device info file.
   # 
   unset_env_variables
   change_esdir "$AGENT_ESDIR"
   setup_device_type && agent_setup=1
   unset_env_variables
   change_esdir "$DEFAULT_ESDIR"
fi

#
# Save cfginfo file
#
if [ "$force_addon_only_flag" != "1" ] ; then
   save_config_info
fi

#
# Check the resp file and export MIGRATE_DATA variable.
#
export_migrate_data

# This will update L_SETUP_LEFT & echo L_SETUP_FAILED
#
[ "$setup_base_only" != "1" ] && setup_addons "$L_ADDON_SETUP_TODO"

# Update registry for setup
#
update_registry_setup

# call post_setup_processing only after core and all addons are setup
[ "$setup_base_only" != "1" ] && do_post_setup_processing

# This will update Registry, display setup status & exit with proper error status.
#
exit_SunMC_setup "$NO_ERROR"
