#!/bin/sh

PATH=/bin:/usr/bin:/usr/sbin:$PATH
CLUS_IMPORT_LOG=/tmp/omni_clus_import.log

#
# For debugging, uncomment this
########################################
#exec 2>/tmp/UTIL_`hostname`_$$_OB2DEBUG
#set -x


init_var()
{
  UNAME=`uname -a`
  ARCH=`echo ${UNAME} | awk '{print $1}'`
  umask 022

  if [ -f /lib/libhpnas.so ]
  then
      UNAME=`echo hpnas-${UNAME}`
      ARCH=`echo hpnas-${ARCH}`
  fi

  case "${ARCH}" in
    OSF1)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo  ${UNAME} | awk '{print $3}'`
      case "$OS_REV" in
        V5*)
          SERIES=dec/alpha/osf1-5
          ;;
        *)
          SERIES=dec/alpha/osf1-4
          ;;
      esac
      STRING="dec alpha osf1-${OS_REV}"
      ;;
    HP-UX)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo  ${UNAME} | awk '{print $3}'`
      ;;
    SunOS)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo  ${UNAME} | awk '{print $3}'`
      PATH=$PATH:/usr/ucb:/usr/etc:/usr/sbin
      ;;
    AIX)
      MACHINE=aix
      OS_REV=`echo ${UNAME} | awk '{printf "%s.%s", $4, $3}'`
      case $OS_REV in
        4*)
          SERIES=ibm/rs6000/aix-43
          ;;
        5*|6*|7*)
          SERIES=ibm/rs6000/aix-51
          ;;
      esac
      STRING="ibm rs6000 aix-${OS_REV}"
      ;;
    Darwin)
      MACHINE=`uname -m`
      MACOS_REV=`sw_vers -productVersion`

      case $MACHINE in
        i[3456]86*)
               SERIES="apple/i386/macos-10.4"
               STRING="apple `uname -p` macos-${MACOS_REV}" 
             ;;
        Power*)
               SERIES="apple/ppc/macos-10.4"
               STRING="apple `uname -p` macos-${MACOS_REV}" 
            ;;
        *x86_64*)
               SERIES="apple/i386/macos-10.4"
               STRING="apple `uname -p` macos-${MACOS_REV}"
            ;;    
      esac
     ;; 
    Linux)
      MACHINE=Linux
      OS_REV=`echo ${UNAME} | awk '{print $3}'`
      SERIES=gpl/i386/linux
      STRING="gpl `uname -m` linux-${OS_REV}"
      ;;
    hpnas-Linux)
      MACHINE=hpnas-Linux
      OS_REV=`echo ${UNAME} | awk '{print $3}'`
      SERIES=gpl/i386/hpnas-linux
      STRING="gpl i386 hpnas-linux-${OS_REV}"
      ;;
    IRIX|IRIX6*)
      MACHINE=irix
      OS_REV=`echo ${UNAME} | awk '{print $3}'`
      SERIES=sgi/mips/irix
      STRING="sgi mips irix-${OS_REV}"
      PATH=$PATH:/usr/bsd:/usr/sbin
      ;;
    ULTRIX)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $3}'`
      SERIES=dec/risc/ultrix
      STRING="dec risc ultrix-${OS_REV}"
      ;;
    SINIX*|Reliant*)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $3}'`
          SERIES=siemens/r400/sinix
          STRING="siemens r400 sinix-${OS_REV}"
      PATH=$PATH:/usr/ucb:/usr/sbin
      ;;
        DYNIX/ptx)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $4}'`
      SERIES=sequent/i386/DYNIX
      STRING="sequent i386 DYNIX-${OS_REV}"
      ;;
        SCO_SV)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $4}'`
      SERIES=sco/i386/sco_sv
      STRING="sco i386 sco_sv-${OS_REV}"
      ;;
    UnixWare)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $4}'`
      SERIES=sco/i386/unixware
      STRING="sco i386 unixware-${OS_REV}"
      ;;
        UNIX_SV)
      MACHINE=`echo ${UNAME} | awk '{print $5}'`
      OS_REV=`echo ${UNAME} | awk '{print $4}'`
      SERIES="ncr/i386/mp-ras"
      STRING="NCR Pentium UNIX-SVR4-${OS_REV}"
      PATH=$PATH:/usr/ucb
      ;;
        MPE/iX)
      MACHINE=`echo ${UNAME} | awk '{print $6}'`
      OS_REV=`echo ${UNAME} | awk '{printf "%s-%s",$3,$4}'`
      SERIES=`echo ${UNAME} | awk '{print $6}'`
      SERIES="hp/hp3000/mpeix"
      STRING="MPE/iX ${OS_REV}"
      ;;

    *)
      return 1
      ;;
  esac

  case "${MACHINE}" in
    *9000/[34]*)
        SERIES=hp/s400/hp-ux
        ADD=`echo ${OS_REV} | sed 's/^..0*//'`
        STRING="hp s300 hp-ux-${ADD}"
        ;;
    *9000/7*)
        ADD=`echo ${OS_REV} | sed 's/^..0*//'`
        STRING="hp s700 hp-ux-${ADD}"
        case "${OS_REV}" in
          *\.11\.*) SERIES="hp/s800/hp-ux-11"
                    ;;
          *\.10\.*) SERIES="hp/s800/hp-ux-10"
                    ;;
          *)        SERIES="hp/s800/hp-ux"
                    ;;
        esac
        ;;
    *9000/8*)
        ADD=`echo ${OS_REV} | sed 's/^..0*//'`
        STRING="hp s800 hp-ux-${ADD}"
        case "${OS_REV}" in
          *\.11\.*) SERIES="hp/s800/hp-ux-11"
                    ;;
          *\.10\.*) SERIES="hp/s800/hp-ux-10"
                    ;;
          *)        SERIES="hp/s800/hp-ux"
                    ;;
        esac
        ;;
    *ia64*)
        ADD=`echo ${OS_REV} | sed 's/^..0*//'`
        STRING="hp s800 hp-ux-${ADD}"
        case "${OS_REV}" in
          B.11.22 | B.11.23 | B.11.31 ) STRING="hp ia64 hp-ux-${ADD}"
                 SERIES="hp/sia64/hp-ux-11"
                 ;;
          *\.11\.*) SERIES="hp/s800/hp-ux-11"
                    ;;
          *)        SERIES="hp/s800/hp-ux"
                    ;;
        esac
        ;;
     i86pc)
        SERIES="sun/i86pc/solaris"
        STRING="sun i86pc solaris-${OS_REV}"
        ;;
     sun4*)
        case "${OS_REV}" in
          5.7 | 5.8) SERIES="sun/sparc/solaris-7"
                   STRING="sun sparc solaris-${OS_REV}"
                   ;;
          5.*)     SERIES="sun/sparc/solaris-26"
                   STRING="sun sparc solaris-${OS_REV}"
                   ;;
          4.*)     SERIES="sun/sparc/sunos"
                   STRING="sun sparc sunos-${OS_REV}"
                   ;;
        esac
        ;;
  esac

  case "${SERIES}" in
    hp/s800/hp-ux-10|hp/s800/hp-ux-11|hp/sia64/hp-ux-11)
        OMNIHOME=/opt/omni
        OMNICONFIG=/etc/${OMNIHOME}/server
        OMNICONFIGCLIENT=/etc/${OMNIHOME}/client
        OMNIINFO=${OMNICONFIGCLIENT}/omni_info
        OMNILBIN=/opt/omni/lbin
                DFS_MNTTAB=${OMNICONFIG}/omni_dfstab
                DCEBIN=/opt/dce/bin
                DCESERVER=`hostname | tr "[A-Z]" "[a-z]"`
                DCEPRINCIPAL=root
        ;;

    ncr/i386/mp-ras | sun/sparc/solaris* | gpl/*/linux* | sun/i86pc/solaris*)
        OMNIHOME=/opt/omni
        OMNICONFIG=/etc/${OMNIHOME}/server
        OMNICONFIGCLIENT=/etc/${OMNIHOME}/client
        OMNIINFO=${OMNICONFIGCLIENT}/omni_info
        OMNILBIN=/opt/omni/lbin
                ;;

    *)
        OMNIHOME=/usr/omni
        OMNICONFIG=${OMNIHOME}/config/server
        OMNICONFIGCLIENT=${OMNIHOME}/config/client
        OMNIINFO=${OMNICONFIGCLIENT}/omni_info
        OMNILBIN=/usr/omni/bin
        ;;
  esac

  CELL_SERVER=${OMNICONFIGCLIENT}/cell_server
  USERLIST=${OMNICONFIG}/users/UserList
  TMP=/tmp/inst.tmp.`date +%d%M%y%H%m%S`$$
  SERVICE=omni
}

NumberOfSapSet ()
{
        case $OS_REV in
        ?.10.??|?.11.??) FILENAME="/var/opt/omni/tmp/BAL_$1_0:*"        ;;
                  *) FILENAME="/usr/omni/tmp/BAL_$1_0:*"        ;;
        esac

        SETNUMBER=`ls ${FILENAME} |wc -l 2>/dev/null`
}

getUser ()
{
  if [ -f ${USERLIST} ]
  then
     OPC_EXIST=`cat ${USERLIST} | grep opc_op`
     case $OPC_EXIST in
         "") echo " " 2>/dev/null;;
          *) echo opc_op 2>/dev/null;;
     esac
  fi
}
getOs ()
{
   case $OS_REV in
     ?.10.??|?.11.??) echo 10 2>/dev/null;;
           *) echo 00 2>/dev/null;;
   esac
}

addUser ()
{
   echo '"OpC default Operator" opc_op * * admin' >> ${USERLIST}
}
getCell ()
{
    if [ -f "${CELL_SERVER}" ]; then
      awk  '{print $1;exit}' ${CELL_SERVER} | tr "[A-Z]" "[a-z]" 2>/dev/null
    fi
}

getDfsFilesets ()
{
        [ ! -s ${DFS_MNTTAB} ] && return
        [ ! -x ${DCEBIN}/fts ] && return
#
#       Get local fileset list using DCE/DFS command fts lsheader
#       Get fileset info from user-supplied file $DFS_MNTTAB
#       Filter out local filesets only
#
        $OMNILBIN/ob2dcecmd "$DCEBIN/fts lsheader -server $DCESERVER" $DCEPRINCIPAL 2>/dev/null |\
        awk '
                BEGIN { while (getline < "'"$DFS_MNTTAB"'" > 0 )
                                        filesets[$1]=$2 }
                !/^Total|^$/ { if ( filesets[$1] )
                                        print $1, filesets[$1] }' | \
#
#       Check if specified mountpoints valid
#
        while read FSET MNT
        do
                $OMNILBIN/ob2dcecmd "$DCEBIN/fts lsmount $MNT" $DCEPRINCIPAL 2>/dev/null >/dev/null
                if [ $? -eq 0 ] ; then
                        echo $MNT
                fi
        done
}

getBdf()
{
  case ${SERIES} in
    ibm/rs6000/aix*)
        #mount 2>/dev/null | sed -e 's/^ /.local/' | awk '$4 !~ /nfs/ { printf("%s\n", $3) }' | tail -n +3

       rm -f /tmp/OMNI_RESULT

       # Get all file systems with vfs attribute set to autofs and namefs

       mount 2>/dev/null | \
       sed -e 's/^ /.local/' | \
       awk '{ if (($4 ~ "autofs") || ($4 ~ "namefs")) print $3}' \
       > /tmp/OMNI_AUTOFS.TMP

       # Get all file systems except those with vfs attribute set to:
       # nfs, autofs, procfs and namefs

       mount 2>/dev/null | \
       sed -e 's/^ /.local/' | \
       tail -n +3 | \
       awk '{
        if (($4 !~ "nfs") && ($4 !~ "autofs") && ($4 !~ "procfs") && ($4 !~ "namefs"))
           print $3
            }' \
       > /tmp/OMNI_FS.TMP

       cat /tmp/OMNI_FS.TMP | \
       while read line
       do
         cat /tmp/OMNI_AUTOFS.TMP | \
         awk -v string=$line 'BEGIN {found = 0}
                              {
                                 if ($0 == string)
                                    found = 1
                              }
                              END {
                                     if (found == 0)
                                        print string
                                  }' >> /tmp/OMNI_RESULT
       done

       cat /tmp/OMNI_RESULT

       rm -f /tmp/OMNI_AUTOFS.TMP
       rm -f /tmp/OMNI_FS.TMP
       ;;
    hp/s[48]00/hp-ux)
           /etc/mount -l -p 2>/dev/null | awk '$3 ~ /^hfs|^vbfs/ {printf("%s\n", $2)}'
       ;;
    hp/s800/hp-ux-10|hp/s800/hp-ux-11|hp/sia64/hp-ux-11)
       /sbin/mount  -l -p 2>/dev/null | awk '$3 ~ /^vxfs$|^hfs|^lofs$|^hsmfs$/ {printf("%s\n", $2)}'
       getDfsFilesets
       ;;
    gpl/i386/linux)
       configuration=/CONFIGURATION
       command=`/bin/df -P -t psfs -t ext2 -t SFS -t ext -t minix -t xiafs -t vmfs -t reiserfs -t ext3 -t vfat -t vxfs -t xfs -t hsmfs -t gfs -t lustre_lite -t ocfs -t ocfs2 -t gfs2 -t ext4 -t gpfs -t ibrix -t acfs -t btrfs 2>/dev/null | sed '1d'`
       # Check if ADRM entry exists in omni_info file. If it exists, /CONFIGURATION mntpt should be listed
       adrm=`awk ' /autodr/ { print 1 }' ${OMNIINFO}`
       if [ "$adrm" == "1" ]
       then
         found=0;
         for mntpt in `echo "$command" | awk -F ' ' '{print $6}'`
         
         # if "/CONFIGURATION" mntpt exists on the system, don't list it;
         # this prevents a double listing of "/CONFIGURATION" mntpt
         do
            echo $mntpt;
            if [ "$mntpt" == "$configuration" ]
            then
               found=1;
            fi
         done       
         if [ "$found" == "0" ]
            then
            echo "$configuration";
         fi
       else
         echo "$command" | awk '{ tmp=""; for(i=6;i<=NF;i++){if ( i==6 ) tmp = $i; else tmp=tmp " " $i }print tmp }'
       fi
       ;;
    apple/*)
       # Using sed instead of awk allows us to list and backup mountpoints with blanks 
       /bin/df -P -t hfs,ufs 2>/dev/null | sed '1d' | cut -d% -f2 |cut -d/ -f2-256 |sed 's/^\(.*\)/\/\1/'
       ;;
    gpl/i386/hpnas-linux)
        /usr/omni/bin/getVSList -user_volume_names
        ;;
    dec/alpha/osf1*)
       /bin/df -t ufs 2>/dev/null | awk '$6 ~ /\/.*/ {print $6}'
       /bin/df -t advfs 2>/dev/null | awk '$6 ~ /\/.*/ {print $6}'
       ;;
    dec/risc/ultrix)
       /etc/mount 2>/dev/null | awk '$5 ~ /ufs/ {printf("%s\n", $3)}'
       ;;
    sun/sparc/solaris | sun/i86pc/solaris  | sun/sparc/solaris-7 | sun/sparc/solaris-26)
       mount -p 2>/dev/null | awk '$4 ~ /ufs|tmpfs|vxfs|lofs|zfs/ { printf("%s\n", $3) }'
       ;;
    sun/sparc/sunos)
       mount -p 2>/dev/null | awk '$3 ~ /ufs|4.2/ { printf("%s\n", $2) }'
       ;;
    sgi/mips/irix)
       /bin/df -l 2>/dev/null | awk '$2 ~ /efs|xfs/ { printf("%s\n", $7)}'
       ;;
    siemens/r400/sinix)
       mount -p 2>/dev/null | awk '$4 ~ /ufs|vxfs/ { printf("%s\n", $3) }'
       ;;
    sequent/i386/DYNIX)
       /etc/mount -p 2>/dev/null | awk '$4 ~ /ufs|efs/ { printf("%s\n", $3) }'
       ;;
    sco/i386/sco_sv)
        for fs in `/etc/mount | awk '{print $3}'`
                do
                        /etc/fstyp $fs >/dev/null 2>&1 && /bin/df $fs | awk '{print $1}'
                done
                echo >/dev/null
        ;;
    sco/i386/unixware)
       mount -p 2>/dev/null | awk '$4 ~ /memfs|s5|sfs|ufs|bfs|vxfs/ { printf("%s\n", $3) }'
       ;;
        ncr/i386/mp-ras)
                /bin/df -l | awk '{print $1}'
                ;;
        hp/hp3000/mpeix)
                callci "dstat all" | awk -v num=1 'END {for (i=1; i<num; i++) printf "/%s\n",arr[i]} NR>2 {n=split($NF,a,"-");if ((n==2) && (a[n] ~ /[0-9]/)) for (i=1; i<num; i++) if (arr[i] == a[1]) next; arr[num++]=a[1]}'
                ;;
  esac
}

excludeCell()
{
    # Cell server
    # ###########
    CHECKSERVER=$1

    if [ ! -r "${CELL_SERVER}" ]; then
        return 0
    fi

    # old cell server name in the cell_server
    # #######################################
    SERVER=`awk '{print $1}' ${CELL_SERVER} | tr "[A-Z]" "[a-z]" 2> /dev/null`

    case "${SERIES}" in
      hp/s*|sun/*|*/linux*)
        # on CM platforms, check local hostname
        # #####################################
        HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
        NODE=`/opt/omni/lbin/utilns/hostlookup $HOSTNAME | tr "[A-Z]" "[a-z]"`
        if [ "${NODE}" = "${SERVER}" ]
        then
          # Cell Server cannot be excluded from its own cell
          # ################################################
          echo "Error: Cell Server cannot be excluded from its own cell"
          return 1
        fi
        ;;
      *)
        ;;
    esac

    CHECKSERVER=`echo ${CHECKSERVER} | tr "[A-Z]" "[a-z]"`

    if [ "${CHECKSERVER}" = "${SERVER}" ] || [ -z "${SERVER}" ]
    then

      case "${SERIES}" in
        hp/s[48]00/hp-ux)
          # check for HP-UX diskless client:
          # ################################
          CNODE=`getcontext | grep -c remoteroot 2>/dev/null`
          if [ ${CNODE} -eq 1 ]
          then
            # system equals cluster client, determine cluster server system
            # #############################################################
            CLUST_CLT=${NODE}
            CLUST_SV=`cnodes -rA`
            if [ `grep -v CELL_SERVER ${OMNIINFO} | grep -cv ${NODE}` -gt 0 ]
            then
                # CELL_SERVER won't be deleted from the file
                # user wanted to exclude diskless client, so change will be made
                # just on cell server
                return 0
            fi
          fi
          ;;
        *)
          ;;
      esac

      if [ -r "${OMNIINFO}" ]
      then
        awk  '{if ($1!="CELL_SERVER") {print}}' ${OMNIINFO}  > ${TMP}
        cp ${TMP} ${OMNIINFO}
        rm ${TMP}
      fi

      rm -f ${CELL_SERVER}
      rm -f ${OMNICONFIGCLIENT}/allow_hosts
      return 0 # Host excluded
    else
      echo "Error: Client system belongs to another cell"
      return 1 # Cannot exclude host that belongs to another cell
    fi
}

includeCell()
{
    NEWSERVER=$1
    CHECKSERVER=$1

    if [ ! -r "${OMNIINFO}" ]
    then
#        echo "Error: ${OMNIINFO} file does not exist or is not readable."
        return 1
    fi


    # added bacause of preinstalled software
    # ######################################
    if [ "`grep HOSTNAME ${OMNIINFO} 2>/dev/null | wc -l`" -gt 0 ]; then
        case "${SERIES}" in
        hp/s[48]00/hp-ux)
          HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
          HOST=`/opt/omni/lbin/utilns/hostlookup $HOSTNAME | tr "[A-Z]" "[a-z]"`
          cat ${OMNIINFO} | sed "s/HOSTNAME/${HOST}/" > ${TMP}
          mv ${TMP} ${OMNIINFO}
          ;;
        *)
          HOST=""
          ;;
        esac
    fi

    SERVER=`awk  '{print $1}' ${CELL_SERVER} | tr "[A-Z]" "[a-z]" 2> /dev/null`
    CHECKSERVER=`echo ${CHECKSERVER} | tr "[A-Z]" "[a-z]"`

    if [ "${CHECKSERVER}" != "${SERVER}" ]
    then
        if [ -z "${SERVER}" -o "$2" = "FORCE" ]
        then
            if [ ! -d ${OMNICONFIGCLIENT} ]
            then
              mkdir -p ${OMNICONFIGCLIENT}
            fi
            echo  ${NEWSERVER} > ${CELL_SERVER}
            return 0   # Host included
        else
            echo "Client system already in another cell"
            return 1   # Host belongs to another cell
        fi
    else
        return 0  # Host  already belongs to the required cell
    fi
}


infoCell()
{
    case "${SERIES}" in
    hp/s[48]00/hp-ux)
      HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
      HOST=`/opt/omni/lbin/utilns/hostlookup $HOSTNAME | tr "[A-Z]" "[a-z]"`
      ;;
    *)
      HOST=""
      ;;
    esac

    if [ ! -r "${OMNIINFO}" ]
    then
        return 1
    fi

        VERS=`awk ' /CELL_CONSOLE_VERSION/ { printf (" -cc %s", $NF) }
                                /DISK_AGENT_VERSION/ { printf (" -da %s", $NF) }
                                /MEDIA_AGENT_VERSION/ { printf (" -ma %s", $NF) }
                                /OMNIBACKI_VERSION/ { printf (" -o1 %s", $NF) }
                                /ORACLE_INTEGRATION_VERSION/ { printf (" -oracle %s", $NF) }
                                /OST_INTEGRATION_VERSION/ { printf (" -omnist %s", $NF) }
                                /SAP_INTEGRATION_VERSION/ { printf (" -sap %s", $NF) }
                                /OPC_INTEGRATION_VERSION/ { printf (" -opc %s", $NF) }
                                /SYBASE_INTEGRATION_VERSION/ { printf (" -sybase %s", $NF) }
                                /EMC_INTEGRATION_VERSION/ { printf (" -emc %s", $NF) }
                                /FASTRAX_MEDIA_AGENT_VERSION/ { printf (" -fxma %s", $NF) }
                                /FASTRAX_INTEGRATION_VERSION/ { printf (" -fastrax %s", $NF) }
                                /ORACLE8_INTEGRATION_VERSION/ { printf (" -oracle8 %s", $NF) }
                                /INFORMIX_INTEGRATION_VERSION/ { printf (" -informix %s", $NF) }
                                /ACS_INTEGRATION_VERSION/ { printf (" -acs %s", $NF) }
                                /DAS_INTEGRATION_VERSION/ { printf (" -das %s", $NF) }
                                /ENABLER_INTEGRATION_VERSION/ { printf (" -oms %s", $NF) }
                                /SSEA_INTEGRATION_VERSION/ { printf (" -ssea %s", $NF) }
                                /64BIT/ { printf (" -64bit") }
                        END     { printf("\n") } ' ${OMNIINFO}`

        STRING="-os \"${STRING}\""
        echo "${STRING} ${VERS}"

    return 0
}



GetVersion()
{
        PDIR=${1}/*/*/*/*
        if [ -f ${PDIR}/packet.Z ]
        then
		# ls -d ${PDIR} | grep -v "require.dat" |cut -f10 -d/ |sort -r | head -1
		for f in ${PDIR}/packet.Z; do
			d=${f%/packet.Z}
			echo ${d##*/}
		done | sort | tail -1
        fi
        return
}


#
#       Return installation packets available on Installation Server
##################################################################
IS_info()
{
    case "${SERIES}" in
		hp/s800/hp-ux-1*)
			ISERVER=`/usr/sbin/swlist DATA-PROTECTOR 2>/dev/null | grep DATA-PROTECTOR.OMNI-CORE-IS`
			PACKETDIR=${OMNIHOME}/databases/vendor
			;;

		sun/sparc/solaris-7)
			ISERVER=`/bin/pkginfo 2>/dev/null | grep OB2-C-IS `
			PACKETDIR=${OMNIHOME}/databases/vendor
			;;

                *)
                  ;;
    esac

        if [ -z "${ISERVER}" ]
        then
                echo "This host does not have the installation packages."
                return 1
        fi
#
# get the last version of installed components (if any)
# and print out merged version string in cell_info format
#

        CC_VERS=`GetVersion             ${PACKETDIR}/cc`
        MOMGUI_VERS=`GetVersion ${PACKETDIR}/momgui`
        DA_VERS=`GetVersion             ${PACKETDIR}/da`
        MA_VERS=`GetVersion             ${PACKETDIR}/ma`
        OB_VERS=`GetVersion             ${PACKETDIR}/ob`
        ORACLE_VERS=`GetVersion ${PACKETDIR}/oracle`
        OST_VERS=`GetVersion    ${PACKETDIR}/ost`
        SAP_VERS=`GetVersion    ${PACKETDIR}/sap`
        OPC_VERS=`GetVersion    ${PACKETDIR}/opc`
        SYBASE_VERS=`GetVersion ${PACKETDIR}/sybase`
        EMC_VERS=`GetVersion    ${PACKETDIR}/emc`
        FXMA_VERS=`GetVersion   ${PACKETDIR}/fxma`
        FASTRAX_VERS=`GetVersion ${PACKETDIR}/fastrax`
        OR8_VERS=`GetVersion    ${PACKETDIR}/oracle8`
        ACS_VERS=`GetVersion    ${PACKETDIR}/acs`
        DAS_VERS=`GetVersion    ${PACKETDIR}/das`
        ENABLER_VERS=`GetVersion ${PACKETDIR}/enabler`
        INFORMIX_VERS=`GetVersion ${PACKETDIR}/informix`
        SSEA_VERS=`GetVersion ${PACKETDIR}/ssea`

        [ -n "${CC_VERS}" ]             && CC_VERS="-cc ${CC_VERS}"
        [ -n "${MOMGUI_VERS}" ]         && CC_VERS="-momgui ${MOMGUI_VERS}"
        [ -n "${DA_VERS}" ]             && DA_VERS="-da ${DA_VERS}"
        [ -n "${MA_VERS}" ]             && MA_VERS="-ma ${MA_VERS}"
        [ -n "${OB_VERS}" ]             && OB_VERS="-o1 ${OB_VERS}"
        [ -n "${ORACLE_VERS}" ]         && ORACLE_VERS="-oracle ${ORACLE_VERS}"
        [ -n "${OST_VERS}" ]            && OST_VERS="-omnist ${OST_VERS}"
        [ -n "${SAP_VERS}" ]            && SAP_VERS="-sap ${SAP_VERS}"
        [ -n "${OPC_VERS}" ]            && OPC_VERS="-opc ${OPC_VERS}"
        [ -n "${SYBASE_VERS}" ]         && SYBASE_VERS="-sybase ${SYBASE_VERS}"
        [ -n "${EMC_VERS}" ]            && EMC_VERS="-emc ${EMC_VERS}"
        [ -n "${FXMA_VERS}" ]       && FXMA_VERS="-fxma ${FXMA_VERS}"
        [ -n "${FASTRAX_VERS}" ]        && FASTRAX_VERS="-fastrax ${FASTRAX_VERS}"
        [ -n "${OR8_VERS}" ]            && OR8_VERS="-oracle8 ${OR8_VERS}"
        [ -n "${ACS_VERS}" ]            && ACS_VERS="-acs ${ACS_VERS}"
        [ -n "${DAS_VERS}" ]            && DAS_VERS="-das ${DAS_VERS}"
        [ -n "${ENABLER_VERS}" ]        && ENABLER_VERS="-oms ${ENABLER_VERS}"
        [ -n "${INFORMIX_VERS}" ]       && INFORMIX_VERS="-informix ${INFORMIX_VERS}"
        [ -n "${SSEA_VERS}" ]           && SSEA_VERS="-ssea ${SSEA_VERS}"

        HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
        HOST="-host \"`/opt/omni/lbin/utilns/hostlookup $HOSTNAME | tr "[A-Z]" "[a-z]"`\""
        STRING="-os \"${STRING}\""

        DIR_64=${PACKETDIR}/*/*/*
        if [ -d ${DIR_64}/hp-ux-11-64 ]
        then
                PACKETS_64="-64bit"
        fi

        echo "${HOST} ${STRING} ${PACKETS_64} ${CC_VERS} ${DA_VERS} ${MA_VERS} ${OB_VERS} ${ORACLE_VERS} ${OST_VERS} ${SAP_VERS} ${OPC_VERS} ${INFORMIX_VERS} ${SYBASE_VERS} ${ENABLER_VERS} ${EMC_VERS} ${FXMA_VERS} ${FASTRAX_VERS} ${OR8_VERS} ${ACS_VERS} ${DAS_VERS} ${SSEA_VERS}"

    return 0
}


# ----------------------------------------------------------------------
#
#  Function gets a list of node names and virtual names from a cluster
#  that this host belongs to.
#
# ----------------------------------------------------------------------
infoClus()
{
    sgNodes=""             # string with physical nodes
    sgVirtual=""           # string with virtual servers (from packages)
    
    sgClusName=""          # latest cluster found
    sgLocalClusName=""     # cluster that local host belongs to
    localhost=`hostname`   # hostname of the local machine
    
    SGBIN_DIR="/usr/sbin"

    #
    # copy the output of command for further processing
    #
    
    $SGBIN_DIR/cmquerycl > /tmp/.obsgtmp 2>> /dev/null

    if [ $? != 0 ]; then
        #
        # cmquerycl failed: clustering is most probably not installed
        #
        rm /tmp/.obsgtmp 2> /dev/null
        return 1
    fi

    # Format of the output of cmquerycl is:
    # Cluster Name Node Name
    # Cluster Name
    # Node1
    # Node2
    #
    # Cluster Name 2
    # Node3
    # Node4
    #
    
    #
    # STEP 1:
    # Find the cluster name that local host belongs to by scanning the file
    # line by line.
    #
    
    sgLoopState="none"

    while read line
    do
        if [ "$sgLoopState" = "none" ]; then
            #
            # Skip everything in front of the header
            #
            (echo "$line" | grep "Cluster " 2>&1 | grep "Node " 2>&1 ) > /dev/null
            
            if [ $? = 0 ]; then
                sgLoopState="go"
            fi
        else
            if [ "$sgClusName" = "" ]; then
                sgClusName=$line
            else
                if [ "$line" = "$localhost" ]; then
                    sgLocalClusName=$sgClusName
                else
                    if [ "$line" = "" ]; then
                        #
                        # Empty line is delimiter between clusters
                        #
                        sgClusName=""
                    fi
                fi
            fi
        fi
    done < /tmp/.obsgtmp
    
    if [ "$sgLocalClusName" = "" ]; then
        #
        # this node is not in a cluster (clustering sw is installed)
        #
        rm /tmp/.obsgtmp 2> /dev/null
        return 2 # local host not in cluster
    fi
    
    #
    # STEP 2: second pass -> get the node names
    # Extract node names from the same file once you have the cluster name by
    # scanning the file again.
    #
    sgLoopState="clus"
    
    while read line
    do
        case "$sgLoopState" in
        "clus")
            if [ "$sgLocalClusName" = "$line" ]; then
                sgLoopState="node"
            fi
            ;;
        "node")
            if [ "$line" = "" ]; then
                sgLoopState="done"
            else
                sgNodes=$sgNodes"1 \"$line\""
            fi
            ;;
        *)
            ;;
        esac
    done < /tmp/.obsgtmp
    
    #
    # We have the list of nodes.
    # Now get the virtual server network names.
    #
    # cmviewcl -l package
    #    PACKAGE      STATUS       STATE        PKG_SWITCH   NODE
    #    Informix731  starting     starting     disabled     dodge.hermes
    #
    
    $SGBIN_DIR/cmviewcl -l package > /tmp/.obsgtmp2 2> /dev/null
    
    if [ $? != 0 ]; then
        rm /tmp/.obsgtmp2 2> /dev/null
        return 3 # error getting virtual names (cluster not up or similar)
    fi
    
    #
    # extract package names from the output of cmviewcl
    #
    grep -v "PACKAGE" /tmp/.obsgtmp2 | sed -e "s/^[ ]*//" | cut -d" " -f1 > /tmp/.obsgtmp 2> /dev/null
    
    if [ $? != 0 ]; then
        rm /tmp/.obsgtmp 2> /dev/null
        rm /tmp/.obsgtmp2 2> /dev/null
        return 3 # error getting virtual names
    fi

    rm /tmp/.obsgtmp2 2> /dev/null
    
    #
    # transform each package name to its network name
    #

    while read line
    do
        if [ "$line" != "" ]; then
            export __LINE=`cmviewconf | grep -e "package name:" -e "package run script:" | awk ' { if ($2=="name:" && $3==PKGN ) PK=$3; else if ($3=="script:" && PK!="") {printf "%s ", $4; PK=""}  } ' PKGN=$line`
            if [ -f $__LINE ]; then
                 pkgIP=`grep "^IP\[" $__LINE | tr -d "\""`

                 # Does package has its own address?
                 #
                 if [ $? = 0 ]; then
                     validIP=`echo $pkgIP | wc -m`
                     if [ $validIP -gt 3 ]; then
                         pkgIP=`echo $pkgIP | cut -d"=" -f2 | head -1`
                         pkgNetName=`${OMNIHOME}/bin/oblookup $pkgIP | awk '{ if($1=="Name:"){print$2}}' | tr "[A-Z]" "[a-z]"`
                         sgVirtual=$sgVirtual"8 \"$pkgNetName\""
                     fi
                 fi
            fi
        fi
    done < /tmp/.obsgtmp

    rm /tmp/.obsgtmp 2> /dev/null

    echo "$sgNodes$sgVirtual"
    echo "$sgNodes$sgVirtual" >> /opt/omni/lbin/trace.log
    #
    # Output is:
    #
    # 0 "audi.hermes.si"0 "dodge.hermes.si"0 "sgipr.hermes.si"
    #
}

# ----------------------------------------------------------------------
#
#  Function checks for a hostname if it has OmniInet in cluster mode
#  installed.
#
#  The criteria is: If the host has MC/SG installed, then also
#  OmniInet is in cluster mode.
#
# ----------------------------------------------------------------------
infoClus1()
{
    #
    # On MC/Service Guard we suppose that the node is in cluster if it has
    # software installed.
    #

    /usr/sbin/cmquerycl 2>&1 > /dev/null

    localhost=`hostname`   # hostname of the local machine

    if [ $? != 0 ]; then
        #
        # An error: we suppose that the MC/SG is not installed.
        #
        echo "0 \"NONE\""
        echo "0 \"NONE\"" >> /opt/omni/lbin/trace.log
    else
        #
        # 49 means: E_CLUS_NODE (1) + E_CLUS_OB_INST (16) +
        #           E_CLUS_OB_CLUS (32)
        #
        echo "49 \"$localhost\""
        echo "49 \"$localhost\"" >> /opt/omni/lbin/trace.log
    fi
}


checkCluster()
{
    OB2CONF=/etc/opt/omni/server/sg
    CONFIG_SG=${OB2CONF}/sg.conf
    test -r $CONFIG_SG

    if [ $? = 0 ]
    then
        . $CONFIG_SG
        VALUE=`eval 'echo $SHARED_DISK_ROOT'`

        test -n "$VALUE"

        if [ $? != 0 ]
        then
            CLUSTER="NO"
        else
            CLUSTER="YES"
            SHARED_PATH=${VALUE}
        fi
    else
        CLUSTER="NO"
    fi
}


# ----------------------------------------------------------------------
#
#  Construct and print a FQDN from uname and domain name info.
#  This is called in case DNS lookup fails on Linux and Solaris.
#
# ----------------------------------------------------------------------
printFQDN()
{
    tmpHost=`uname -a | awk '{print $2}'`

    case "${ARCH}" in
        Linux*)
            if [ -f "/bin/dnsdomainname" ]
            then
                # get info from dnsdomainname
                tmpDomain=`/bin/dnsdomainname`
            else
                # parse resolve conf
                # CHECKME: is it okay to parse the "search" lines?
                if [ -f "/etc/resolv.conf" ]
                then
                    tmpDomain=`cat /etc/resolv.conf 2>/dev/null | grep ^search | awk '{print $2}'`
                    if [ "$tmpDomain" = "" ]
                    then
                        tmpDomain=`cat /etc/resolv.conf 2>/dev/null | grep ^domain | awk '{print $2}'`
                    fi
                fi
            fi
            ;;
        SunOS*)
            if [ -f "/etc/defaultdomain" ]
            then
                tmpDomain=`cat /etc/defaultdomain 2>/dev/null`
            fi
            ;;
    esac

    if [ "$tmpDomain" != "" ]
    then
        tmpDomain=".$tmpDomain"
    fi
    echo ${tmpHost}${tmpDomain} | tr "[A-Z]" "[a-z]"

    return 0
}



# main part of the script

COMMAND=$1
shift
PARAMS="$*"


init_var $PARAMS

case $COMMAND in
     sap_set)    NumberOfSapSet $PARAMS ;;
     adduser)    addUser $PARAMS;;
     USER   )    getUser $PARAMS;;
     OS     )    getOs $PARAMS;;
     CELL   )    getCell $PARAMS;;
     BDF    )    getBdf $PARAMS;;
     EXCLUDE)    excludeCell $PARAMS
                 exit $?
                 ;;
     INCLUDE)    includeCell $PARAMS
                 exit $?
                 ;;
     INFO   )    infoCell $PARAMS
                 exit $?
                 ;;
     ISINFO )    IS_info $PARAMS
                 exit $?
                 ;;
     OSINFO )    echo "${STRING}"
                 exit $?
                 ;;
     SECURE )   VAROPTOMNIDIR="/var/opt/omni"
                ENHINCRDB="${VAROPTOMNIDIR}/enhincrdb"
                LOGDIR="${VAROPTOMNIDIR}/log"
                SERVERDIR="${VAROPTOMNIDIR}/server"
                TMPDIR="${VAROPTOMNIDIR}/tmp"
                WINDUDIR="${VAROPTOMNIDIR}/windu"
                SERVERLOGDIR="${SERVERDIR}/log"
                NEWCONFIG="/opt/omni/newconfig"


                HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
                chmod 1777 ${ENHINCRDB}
                chmod 1777 ${LOGDIR}
                chmod 1777 ${TMPDIR}
                chmod 1777 ${WINDUDIR}

                if [ -d ${NEWCONFIG} ]
                then
                    chmod 1777 ${NEWCONFIG}${ENHINCRDB}
                    chmod 1777 ${NEWCONFIG}${LOGDIR}
                    chmod 1777 ${NEWCONFIG}${TMPDIR}
                fi

                CLUSTER="NO"
                SHARED_PATH=""

                checkCluster

                if [ ${CLUSTER} = "YES" ]; then
                    chmod 1777 ${VAROPTOMNIDIR}/server.save
                    chmod 1777 ${SHARED_PATH}/var_opt_omni_server
                    chmod 1777 ${SHARED_PATH}/var_opt_omni_server/log
                else
                    chmod 1777 ${SERVERDIR}
                    chmod 1777 ${SERVERLOGDIR}
                fi

                echo "Successfully Secured the Client $HOSTNAME"
                exit 0
                ;;

    RMSECURE )  VAROPTOMNIDIR="/var/opt/omni"
                ENHINCRDB="${VAROPTOMNIDIR}/enhincrdb"
                LOGDIR="${VAROPTOMNIDIR}/log"
                SERVERDIR="${VAROPTOMNIDIR}/server"
                TMPDIR="${VAROPTOMNIDIR}/tmp"
                WINDUDIR="${VAROPTOMNIDIR}/windu"
                SERVERLOGDIR="${SERVERDIR}/log"
                NEWCONFIG="/opt/omni/newconfig"

                HOSTNAME=`hostname | tr "[A-Z]" "[a-z]"`
                chmod 777 ${ENHINCRDB}
                chmod 777 ${LOGDIR}
                chmod 777 ${TMPDIR}
                chmod 777 ${WINDUDIR}
                if [ -d ${NEWCONFIG} ]
                then
                    chmod 777 ${NEWCONFIG}${ENHINCRDB}
                    chmod 777 ${NEWCONFIG}${LOGDIR}
                    chmod 777 ${NEWCONFIG}${TMPDIR}
                fi

                CLUSTER="NO"
                SHARED_PATH=""

                checkCluster

                if [ ${CLUSTER} = "YES" ]; then
                    chmod 777 ${VAROPTOMNIDIR}/server.save
                    chmod 777 ${SHARED_PATH}/var_opt_omni_server
                    chmod 777 ${SHARED_PATH}/var_opt_omni_server/log
                else
                    chmod 777 ${SERVERDIR}
                    chmod 777 ${SERVERLOGDIR}
                fi

                echo "Successfully Unsecured the Client $HOSTNAME"
                exit 0
                ;;


     HOSTNAME)   case "${ARCH}" in
                   MPE*)
                     uname -a | awk '{print $2}'
                     exit $?
                     ;;
                   Linux*|SunOS*)
                     tmpHost=`${OMNIHOME}/bin/oblookup \`hostname\` 2> /dev/null | awk '/^Name:/ {print $2}' | tr "[A-Z]" "[a-z]"`
                     if [ "$tmpHost" = "" ]
                     then
                         printFQDN
                     else
                        echo $tmpHost
                     fi
                     exit $?
                     ;;
                   *)
                     ${OMNIHOME}/bin/oblookup `hostname` 2> /dev/null | awk '/^Name:/ {print $2}' | tr "[A-Z]" "[a-z]"
                     exit $?
                     ;;
                 esac
                 ;;

    UNAME)      echo ${UNAME}
                exit $?
                ;;

    OMNIACS   ) case $SERIES in
                    hp*) while true ; do
                             PAR="${PAR} '$1'"
                             shift
                             if [ $# = 0 ] ; then
                                 break
                             fi
                         done
                         eval "$OMNILBIN/omniacs $PAR"
                         exit $?
                         ;;
                    *)   echo Platform not supported!
                         exit 1;
                         ;;
                 esac
                 ;;

    OMNIDAS   ) case $SERIES in
                    hp*) while true ; do
                             PAR="${PAR} '$1'"
                             shift
                             if [ $# = 0 ] ; then
                                 break
                             fi
                         done
                         eval "$OMNILBIN/omnidas $PAR"
                         exit $?
                         ;;
                    *)   echo Platform not supported!
                         exit 1;
                         ;;
                 esac
                 ;;

    INFO_CLUS   )
                    infoClus
                ;;

    INFO_CLUS1  )
                    infoClus1
                ;;

esac

