#!/bin/sh
# Copyright 12/29/04 Sun Microsystems, Inc. All Rights Reserved.
# pragma ident  "@(#)es-db	1.58 04/12/29 Sun Microsystems"

# Main function to install DB
# $1 - DB source directory
# $2 - target directory
# $3 - dbf directory
# $4 - relocated_root
installDB() {

    RELOCATED_ROOT="$4"
    PKGADD_PARAM=""
    if [ -z "$RELOCATED_ROOT" -o "$RELOCATED_ROOT" = "/" ]; then
	RELOC_PARAM=""
	[ `${CMD_UNAME} -r` = "5.10" ] && PKGADD_PARAM="-G"
    else
	RELOC_PARAM="-R $RELOCATED_ROOT"
    fi
    
    #
    # set the logfile if not already set
    #
    if [ -z "$LOGFILE" ] ; then
	check_logfile install
    fi

    echologverbose ""
    echologverbose "-------------------------------------------------------"
    echologverbose '       Sun Management Center Database Installation     '
    echologverbose "-------------------------------------------------------"
    echologverbose ""

    if [ "$1" = "" ]; then
	echolog 'Invalid parameters for installDB\\\(\\\)'
	echolog "\$1"
	$CMD_SLEEP 5
	exit_SunMC "$DB_TABLES_INSTALLATION_ERROR"
    fi

    if [ "$2" = "" ]; then
	echolog 'Invalid parameters for installDB\\\(\\\)'
	echolog "\$2"
        $CMD_SLEEP 5
	exit_SunMC "$DB_TABLES_INSTALLATION_ERROR"
    fi

    if [ "$3" = "" ]; then
	echolog 'Invalid parameters for installDB\\\(\\\)'
	echolog "\$3"
        $CMD_SLEEP 5
	exit_SunMC "$DB_TABLES_INSTALLATION_ERROR"
    fi

    # SunMCDB particular parameters and constants
    # may or may not be of future use constants


    SUNMCDB_PKG_DIR="$1"
    SUNMCDB_TARGET_DIR="$2"
    SUNMCDB_DBF_DIR="$3"
    SUNMCDB_PKGS="SUNWesora SUNWestbl"


    #
    # check if the packages have been installed alreadly
    #

    $CMD_PKGINFO -q $RELOC_PARAM "SUNWesora"  >/dev/null 2>&1 # Disallow partial installs and mismatches
    if [ $? -eq 0 ] ; then
        echolog "The SUNWesora Sun Management Center database package is already installed on your system:"        
        
        #check if version matches
        src_ora_vers=`$CMD_PKGPARAM  -f ${SUNMCDB_PKG_DIR}/SUNWesora/pkginfo SUNW_PRODVERS`
        installed_ora_vers=`$CMD_PKGPARAM $RELOC_PARAM SUNWesora SUNW_PRODVERS`
        
        [ "$src_ora_vers" = "$installed_ora_vers" ] && return 1 
        
        echolog 'The installed version of SUNWesora differs from the one required for Sun Management Center 3.5 Update 1' 
        echolog 'Installation cannot continue.'     
	return 0	
    fi

    # For security reasons, no temp executable files be placed under /tmp dir for Solaris 8 and above.
    # However, On Solaris 6 and 7 /tmp should be used.
    # This has been copied from es-common.sh's set_basedir function because could not
    # Create the temp directory if it doesn't exist.
    # Remove the HARDCODING to tmp directory to tmp.SunMCInstall
    # and instead use the es-common.sh function called create_temp_directory.,
    # Since this script is not being called by the user directly, no need to 
    # remove the temp directory as it couldbe having files which are required
    # by the script which is calling the es-db.

    create_temp_directory

    SMCDB_DEFAULT=${TMP_DIR}/smcdb_db1$$
    SMCDB_CHANGED=${TMP_DIR}/smcdb_db2$$
    SMCDB_CHANGED_DBF=${TMP_DIR}/smcdb_db3$$
    echologverbose ""

$CMD_CAT << !EOF > "$SMCDB_DEFAULT"
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nochange
action=nocheck
basedir=default
!EOF

$CMD_CAT << !EOF > "$SMCDB_CHANGED"
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nochange
action=nocheck
basedir="$SUNMCDB_TARGET_DIR"
!EOF

$CMD_CAT << !EOF > "$SMCDB_CHANGED_DBF"
mail=
instance=overwrite
partial=nocheck
runlevel=nocheck
idepend=quit
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nochange
action=nocheck
basedir="$SUNMCDB_DBF_DIR"
!EOF


    # tries to create oracle and db mountpoints.
    echologverbose "Creating the mount points..."

    trailslash=`echo $SUNMCDB_TARGET_DIR | $CMD_GREP -i "/$" | $CMD_WC -l`

    if [ $trailslash = 0 ]
    then
	MOUNTPOINTLOC1="${RELOCATED_ROOT}${SUNMCDB_TARGET_DIR}/oracle"
	MOUNTPOINT1="${RELOCATED_ROOT}$SUNMCDB_TARGET_DIR/oracle"
    else
	MOUNTPOINTLOC1="${RELOCATED_ROOT}${SUNMCDB_TARGET_DIR}oracle"
	MOUNTPOINT1="${RELOCATED_ROOT}${SUNMCDB_TARGET_DIR}oracle"
    fi

    trailslash=`echo $SUNMCDB_DBF_DIR | $CMD_GREP -i "/$" | $CMD_WC -l`
    if [ $trailslash = 0 ]
    then
	MOUNTPOINTLOC2="${RELOCATED_ROOT}${SUNMCDB_DBF_DIR}/db"
	MOUNTPOINT2="${RELOCATED_ROOT}/var/opt/SUNWsymon/db"
    else
	MOUNTPOINTLOC2="${RELOCATED_ROOT}${SUNMCDB_DBF_DIR}db"
	MOUNTPOINT2="${RELOCATED_ROOT}/var/opt/SUNWsymon/db"
    fi

    umask 022

    pkg_base1=`$CMD_PKGPARAM  -f ${SUNMCDB_PKG_DIR}/SUNWesora/pkginfo BASEDIR`
    pkg_base2=`$CMD_PKGPARAM  -f ${SUNMCDB_PKG_DIR}/SUNWestbl/pkginfo BASEDIR`
    
    $CMD_PKGINFO $RELOC_PARAM -qi SUNWesora
    if [ $? -ne 0 ] ; then
	if [ "$pkg_base1" != "$SUNMCDB_TARGET_DIR" ]; then
	    $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_CHANGED" SUNWesora 2>&1 | $CMD_TEE -a "$LOGFILE"
	    retcode=$?
	else
	    $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_DEFAULT" SUNWesora 2>&1 | $CMD_TEE -a "$LOGFILE"
	    retcode=$?
	fi

	if [ $retcode -ne 0 ] ; then
	    echolog 'Error installing SUNWesora.  Installation cannot continue.' 
	    return 0
	fi
    fi

    $CMD_PKGINFO $RELOC_PARAM -qi SUNWestbl
    if [ $? -ne 0 ] ; then
	if [ "$pkg_base2" != "$SUNMCDB_DBF_DIR" ]; then
	    $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_CHANGED_DBF" SUNWestbl 2>&1 | $CMD_TEE -a "$LOGFILE"
	    retcode=$?
	else
	    $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_DEFAULT" SUNWestbl 2>&1 | $CMD_TEE -a "$LOGFILE"
	    retcode=$?
	fi

	if [ $retcode -ne 0 ] ; then
	    echolog 'Error installing SUNWestbl.  Installation cannot continue.' 
	    return 0
	fi
    fi


     #  Only install package if not already installed on machine

	$CMD_PKGINFO $RELOC_PARAM -q "SUNWestbl"  >/dev/null 2>&1 # Disallow partial installs
	if [ $? -gt 0 ] ; then
         if [ "$pkg_base2" != "$SUNMCDB_DBF_DIR" ]; then
           $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_CHANGED_DBF" SUNWestbl 2>&1 | $CMD_TEE -a "$LOGFILE"
           retcode=$?
         else
           $CMD_PKGADD $PKGADD_PARAM $RELOC_PARAM -d "$SUNMCDB_PKG_DIR" -a "$SMCDB_DEFAULT" SUNWestbl 2>&1 | $CMD_TEE -a "$LOGFILE"
           retcode=$?
         fi
      fi

    echologverbose ""
    echolog "Sun Management Center DB packages installed successfully"
    echolog "es-setup will perform the remainder of the config"
    return 1
}


