#!/usr/bin/ksh

#--------------------------------------------------------------------
#
# Program: R3sapdba
#
# Functionality: Check the existance of R3 systems and discovers
#                the hostname of the database server for every
#                R3 system
#
# Hewlett-Packard GmbH
# PSO-LPO
# March '97
#--------------------------------------------------------------------

SAPDIR=/usr/sap
SAPHOST=`hostname`
SAPDBHOST=""
SAPSYSTEMNAME=""
UNIXORACLE=""
bold=`tput smso`
plain=`tput rmso`

logoMsg()
{
  clear
  echo "___________________________________________________________________"
  echo "          HP OpenView SMART Plug-In for SAP R/3                    "
  echo "___________________________________________________________________"
  echo ""
  echo "        "$bold"SAP hostname       $plain = $SAPHOST"
  echo "        "$bold"SAP system name    $plain = $SAPSYSTEMNAME"
  echo "        "$bold"SAP DB hostname    $plain = $SAPDBHOST"
  echo "        "$bold"UNIX ORACLE account$plain = $UNIXORACLE"
  echo ""
}

R3exist()
{
  if [ -a $SAPDIR ]
  then
  {
    SAPRUN="OK"
  }
  else
  {
    echo "        "$bold"ERROR:$plain This program requires the existance of"
    echo "               an R/3 system, in order to execute"
    echo ""
    read tmp
    exit 99
  }
  fi
}


SAPlistSingledbHost()
{

  # echo "----------------------------------------------------------------"
  echo "___________SAP Systems Configured on server $SAPHOST\n"
  # echo "----------------------------------------------------------------"
  for DIR in `ls -d $SAPDIR/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]??`
  do
    SAPNAME=`basename $DIR`
    if [ -a $DIR/SYS/profile/DEFAULT.PFL ]
    then
    {
      SAPDBHOST=`grep SAPDBHOST $DIR/SYS/profile/DEFAULT.PFL | \
                 sed 's/.*=//' `
      echo " Database Server Hostname: $SAPDBHOST"
    }
    else
    {
      echo "  **NO DATABASE SERVER CONFIGURED FOR THIS R/3 SYSTEM"

      # echo "Please, try to start \"sapdba\" on another system"
      # echo "on the OpC hosts desktop"
      
    }
    fi
  done

}

SAPlistdbHost()
{
  SAPHOST=`hostname`
  SAPLIST=""
  SAPDEFAULT=""

  echo "Searching for SAP systems with configured database server..."
  echo ""
  for DIR in `ls -d $SAPDIR/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]??`
  do
    SAPNAME=`basename $DIR`
    if [ -a $DIR/SYS/profile/DEFAULT.PFL ]
    then
    {
      SAPDBHOST=`grep SAPDBHOST $DIR/SYS/profile/DEFAULT.PFL | \
                 sed -e 's/.*=//' -e 's/ //g' -e 's/	//g' `
      if [ "$SAPDEFAULT" = "" ]
	   then
           SAPDEFAULT="$SAPNAME:$SAPDBHOST"
           SAPLIST=$SAPDEFAULT
      else
           SAPLIST="$SAPLIST $SAPNAME:$SAPDBHOST"
      fi
    }
    fi
  done
}


SAPgetR3()
{
  if [ "$SAPLIST" = "$SAPDEFAULT" ]
        then
        SAPSYSTEMNAME=$SAPDEFAULT
  else
        PS3="Please select a SAP system name and DB hostname combination [1]: "
        echo " "
        echo " "
        select i in `echo $SAPLIST`
        do
                case $i in
                $i) SAPSYSTEMNAME=$i; break;;
                esac
        done
        if [ "$SAPSYSTEMNAME" = "" ]
                then
                SAPSYSTEMNAME=$SAPDEFAULT
        fi
  fi

  SAPSYSTEMNAME=`echo $SAPSYSTEMNAME | sed 's/:.*//'`
  logoMsg
}

R3runSapdba()
{
  DBINFOMSG="    To be able to execute sapdba on a remote database server
    you need that your system and  user is decleared on the .rhosts
    file of the oracle dba unix account.
    Otherwise , you will be prompted for a passwd"

  lSAPSYSTEMNAME=`echo $SAPSYSTEMNAME | dd conv=lcase 2>/dev/null`
  UNIXORACLE="ora${lSAPSYSTEMNAME}"
  if [ "$SAPDBHOST" = "$SAPHOST"  ]
  then
  {

    ORA_USER_FOUND=0

# Check /etc/passwd for the oracle dba account

    if grep $UNIXORACLE /etc/passwd
    then
      ORA_USER_FOUND=1
    fi

# Check NIS+

    if [ ${ORA_USER_FOUND} -eq 0 ]
    then
      if niscat passwd.org_dir 2>/dev/null | grep $UNIXORACLE
      then
        ORA_USER_FOUND=1
      fi      
    fi

# Check NIS

    if [ ${ORA_USER_FOUND} -eq 0 ]
    then
      if yycat passwd.byname 2>/dev/null | grep $UNIXORACLE
      then
        ORA_USER_FOUND=1
      fi  
    fi

    if [ ${ORA_USER_FOUND} -eq 1 ]
	then
      	logoMsg
      	echo " "
      	echo "        "$bold"NOTE:$plain To be able to execute sapdba on a remote"
      	echo "              database server you need that your system and user is"
      	echo "              declared on the .rhosts file of the oracle dba unix"
      	echo "              account. Otherwise you will be prompted for a password"
  	
      	echo " "
      	echo "        "$bold"NOTE:$plain Starting DB utility program \"sapdba\""
      	echo " "
      	echo "        Press return to continue"
      	echo " "
      	read tmp
      	su - $UNIXORACLE -c sapdba
      	echo
    else
       logoMsg
       echo " "
       echo "        "$bold"ERROR:$plain User $UNIXORACLE doesn't exist."
       echo "               The tool sapdba is only supported for ORACLE DB."
       echo " "
       read tmp
       exit 99

    fi
  }
  else
  {
    echo " "
    echo "        "$bold"ERROR:$plain Please select $SAPDBHOST in OpC Managed"
    echo "               Nodes window and execute the \"sapdba\" utility again."
    echo " "
    read tmp
    exit 99
  }
  fi
}

logoMsg
R3exist
SAPlistdbHost
SAPgetR3
R3runSapdba

echo " "
echo "        Press return to continue"
echo " "
read tmp
