#!/bin/sh
#
# Copyright 02/22/02 Sun Microsystems, Inc. All Rights Reserved.
#
# pragma ident  "@(#)es-backup-common	1.1 02/02/22 Sun Microsystems"
#

get_package_param() {
    pkg=$1
    param=$2
    /usr/bin/pkginfo -qi $pkg >/dev/null 2>&1
    if [ $? -eq 0 ] ; then
        value=`/usr/bin/pkgparam $pkg $param`
        return 0
    fi
    value=""
    return 1
}

#
# initialize the following variables:
#
# - SUNMC_VERSION
# - PRM_VERSION
# - SUNMC_DBFILE
# - ESCOM_VERSION
# - ESCOM_BASEDIR
# - ESTBL_BASEDIR
# - ESDRG_VERSION
#
# - SYS_LOGIN
# - SYS_PASSWD
# - ORA_DATADIR
# - ORA_INITFILE
#
# - METAFILE
# - CFGFILE
# - DUMPFILE
# - DBBAKFILE
#
set_backup_common() {

    #
    # get the SunMC version (i.e. 3.0 or 4.0)
    #
    get_package_param SUNWescom VERSION
    if [ $? -ne 0 ] ; then
        echolog "Sun Management Center package (SUNWescom) is not installed"
        return 1
    fi

    ESCOM_VERSION=$value
    SUNMC_VERSION=`/bin/echo "$value" | /usr/bin/awk '{ print substr($1,0,3) }'`

    get_package_param SUNWescom BASEDIR
    ESCOM_BASEDIR=$value

    get_package_param SUNWestbl BASEDIR
    if [ $? -ne 0 ] ; then
        echolog "Sun Management Center package (SUNWestbl) is not installed"
        return 1
    fi
    ESTBL_BASEDIR=$value

    get_package_param SUNWesdrg VERSION
    ESDRG_VERSION=$value
    PRM_VERSION=`/bin/echo "$value" | /usr/bin/cut -f1 -d_`
    if [ "$PRM_VERSION" != "" ] ; then
        PRM_EXISTS=1
        PRM_SCHEMA_OWNER=report
    fi

    #
    # set various version specific variables
    #
    if [ "$SUNMC_VERSION" = "3.0" ]; then
        SYS_LOGIN=sys
        SYS_PASSWD="change_on_install"
        DB_USERS="sunmc sunmcu"
        DB_SCHEMA_OWNER=sunmc

        ORA_INITFILE=$ORACLE_HOME/admin/$ORACLE_SID/pfile/initSunMC30.ora
        ORA_DATADIR=SUNWsymon/SunMCDB02
        ORA_CTRLFILES="$ORA_DATADIR/oradata/SunMC30/*.ctl $ORA_DATADIR/oradata/SunMC30/redo*"
        ORA_EVENTDIR=$ORA_DATADIR/eventmgr

        SUNMC_DBFILE=$VAROPTDIR/cfg/dbase.cfg 
        DB_CRINST_SH=$ESCOM_BASEDIR/SUNWsymon/sbin/db-crinst.sh
        SQLPLUS=$ORACLE_HOME/bin/sqlplus
        DBVERIFY=$ORACLE_HOME/bin/dbv

    elif [ "$SUNMC_VERSION" = "4.0" ]; then
        SYS_LOGIN=sys
        SYS_PASSWD=`/usr/bin/egrep -i "SYSPWD=" ${BASEDIR}/db/build/adminpasswd|/usr/bin/sed 's/.*=//'`
        if [ -z "$SYS_PASSWD" ]; then
            echolog "Failed to get sys password"
            return 1
        fi

        DB_USERS="sunmc sunmc_owner"
        DB_SCHEMA_OWNER=sunmc_owner

        ORA_INITFILE=$ORACLE_HOME/admin/$ORACLE_SID/pfile/initSunMC.ora
        ORA_DATADIR=db
        ORA_CTRLFILES="$ORA_DATADIR/oradata/SunMC/*.ctl $ORA_DATADIR/oradata/SunMC/redo*"
        ORA_EVENTDIR=$ORA_DATADIR/eventmgr

        SUNMC_DBFILE=$BASEDIR/db/cfg/dbadmin.cfg
        DB_CRINST_SH=$ESCOM_BASEDIR/SUNWsymon/lib/sbin/db-crinst.sh
        SQLPLUS=$ORACLE_HOME/bin/sqlplus
        DBVERIFY=$ORACLE_HOME/bin/dbv

    else
        echolog 'Unsupported Sun Management Center version: $2' "$SUNMC_VERSION"
        return 1
    fi

    if [ ! -f "$SUNMC_DBFILE" ]; then
        echolog 'Database configuration $2 file not found' "$SUNMC_DBFILE"
        return 1
    fi

    METAFILE=SUNMC_BAK.meta

    return 0
}

# setup_backup_format $formatnum
setup_backup_format () {
    format=$1
    if [ "$format" -eq 1 ] ; then
        CFGFILE=data/cfgfiles.tar
        DBBAKFILE=data/db.tar
    elif [ "$format" -eq 2 ] ; then
        CFGFILE=data/cfgfiles.tar
        DUMPFILE=data/sunmc.dmp
    else
        echolog 'Internal error: unknown backup format $2' "$format"
        exit 1
    fi
    return 0
}

stop_SunMC() {
    $BASEDIR/sbin/es-stop -A
    if [ $? -ne 0 ] ; then
        echolog "Failed to stop Sun Management Center"
        return 1
    fi
    return 0
}

start_SunMC() {
    $BASEDIR/sbin/es-start -A
    if [ $? -ne 0 ] ; then
        echolog "Failed to start Sun Management Center"
        return 1
    fi
    return 0
}

start_db() {
    if [ -z "$DB_UP" -o "$DB_UP" -eq 0 ]; then
        echolog 'Starting database...'
        /usr/bin/su - $SMCORAU -c $BASEDIR/sbin/db-start
        if [ $? -ne 0 ] ; then
            echolog "Failed to start Sun Management Center database"
            print_failure_and_exit 1
        fi
        DB_UP=1
    fi
    return 0
}

shutdown_db() {
    if [ -z "$DB_UP" -o "$DB_UP" -eq 1 ]; then
        echolog 'Stopping database...'
        /usr/bin/su - $SMCORAU -c $BASEDIR/sbin/db-stop
        if [ $? -ne 0 ] ; then
            echolog "Failed to stop Sun Management Center database"
            print_failure_and_exit 1
        fi
        DB_UP=0
    fi
    return 0
}

clean_subdirs() {
    dir=$1
    if [ "$dir" = "/" ] ; then
        echolog 'Cannot remove /.'
        return 1
    fi
    /usr/bin/rm -rf $dir/*
}

#
# NOTE: should improve this check to account for more than just the first 8
# characters of the process name.
#
check_process_conflicts () {
    name=$1

    pids=`/usr/bin/ps -ef -o pid,args | /usr/bin/egrep $name | /usr/bin/egrep -v $$ | /usr/bin/awk '{print $1}' `
    if [ "$pids" != "" ]; then
        name8=`/bin/echo $name | /usr/bin/awk '{print substr($1,0,7)}'`
        count=`/usr/bin/ps -p "$pids" -o fname | /usr/bin/egrep $name8 | /usr/bin/wc -l`
        if [ "$count" -gt 0 ]; then
            return 1
        fi
    fi

    return 0
}

#
# inputs: $1 - base name
#         $2 - number of elements
#
# output: names - list of names, i.e. "baseaa baseab baseac ..."
#
get_split_names () {

    name=$1
    num=$2

    ALPHABET="a b c d e f g h i j k l m n o p q r s t u v w x y z"

    names=""
    n=1
    for i in $ALPHABET
    do
        for j in $ALPHABET
        do
            for k in $ALPHABET
            do
                if [ "$n" -gt "$num" ]; then
                    break 3
                fi
                names="$names ${name}${i}${j}${k}"
                n=`expr $n + 1`
            done
        done
    done
    return 0
}

# create_pipe pipename
create_pipe () {
    pipe=$1
    /usr/bin/rm -f $pipe     2>/dev/null 1>&2
    /usr/sbin/mknod $pipe p  2>/dev/null 1>&2
    if [ $? -ne 0 ] ; then
        /usr/bin/rm -f $pipe 2>/dev/null 1>&2
        echolog 'Could not create pipe'
        return 1
    fi

    return 0
}

# scan_dbv_log $logfile $associated_dbf_file
# Scan DBVERIFY logfile to see if verification was successful.
scan_dbv_log () {
    dbvlog=$1
    dbf_file=$2

    /usr/bin/egrep '^DBV-[0-9]+:' $dbvlog >/dev/null 2>&1
    if [ $? -ne 1 ] ; then
        echolog 'Database verification utility errors occurred:'
        /usr/bin/egrep '^DBV-[0-9]+:' $dbvlog | /usr/bin/tee -a $LOGFILE 2>&1
        echolog 'Database verification utility failed.'
        return 1
    fi

    num_errors=`/usr/bin/egrep '^(Total Pages Failing|Total Pages Marked Corrupt)' $dbvlog | /usr/bin/egrep -v ':[ ]*0[ ]*$' | /usr/bin/wc -l`
    if [ "$num_errors" -gt 0 ] ; then
        echolog 'Problems were found in the database file $2.' "$dbf_file"
        return 1
    fi

    return 0
}


