#!/usr/bin/ksh

#--------------------------------------------------------------------
#
# Program: R3saptp
#
# Functionality: Checks the responding of the 
#                database of the R3 system <SID>
#                "tp connect <SID>"
#
# Hewlett-Packard GmbH
# PSO-LPO
# March '97
#--------------------------------------------------------------------

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

logoMsg()
{
  clear
  echo "___________________________________________________________________"
  echo "          HP OpenView SMART Plug-In for SAP R/3                    "
  echo "___________________________________________________________________"
  echo ""
  echo "       Check the repsonding of the database of the SAP R/3       "
  echo ""
  echo "       Executing in the directory $TPDIR as <SID>adm:             "
  echo ""
  echo "       tp connect <SID>                                          "
  echo "       tp getdbinfo <SID>                                        "
  echo "                                                                 "
  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
}


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`
    SAPLIST="$SAPLIST $SAPNAME"
  done
}


SAPgetR3()
{
  if [ "$SAPLIST" = "$SAPDEFAULT" ]
        then
        SAPSYSTEMNAME=$SAPDEFAULT
  else
        PS3="Please select a SAP system: "
        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
}

R3runTP()
{
  lSAPSYSTEMNAME=`echo $SAPSYSTEMNAME | dd conv=lcase 2>/dev/null`
  SIDLOGIN="${lSAPSYSTEMNAME}adm"
  logoMsg
  echo " "
  echo "        "$bold"NOTE:$plain Please analyse the output of the check."
  echo "              Either the result is:                               "
  echo "                  Connection to Database $SAPSYSTEMNAME was successful     "
  echo "              or" 
  echo "                  Connection to $SAPSYSTEMNAME failed.	                    "
  echo "              " 
  echo "              Possible status: 0 (OK), 4 (warning), 8 (error) "

  echo " "
  echo "        "$bold"NOTE:$plain Executing "tp connect $SAPSYSTEMNAME" "
  echo " "
  echo "        Press return to continue"
  echo " "
  read tmp
  echo " "
  echo " "
  su - $SIDLOGIN -c "cd $TPDIR; tp connect $SAPSYSTEMNAME; tp getdbinfo $SAPSYSTEMNAME"
  echo " "
  echo
}

logoMsg
R3exist
SAPlistdbHost
SAPgetR3
R3runTP

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