#!/usr/bin/ksh
#--------------------------------------------------------------------
#
# Program: R3proclog
#
# Functionality: Check the existance of R3 systems and
#                shows the process log files
#
# Hewlett-Packard GmbH
# PSO-LPO 
# March '97
#--------------------------------------------------------------------

SAPDIR=/usr/sap
SAPHOST=`hostname`
SAPSYSTEMNAME=""
SAPINSTANCENAME=""
SAPSYSTEMNR=""
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 instance     $plain = $SAPINSTANCENAME"
  echo "        "$bold"SAP system number$plain = $SAPSYSTEMNR"
  echo ""
}

R3exist()
{
  if [ -d $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
}

SAPlistR3()
{
  SAPLIST=""
  SAPDEFAULT=""
  for SAPNAMEDIR in `ls -d $SAPDIR/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]??`
  do
    bSAPNAMEDIR=`basename $SAPNAMEDIR`
    if [ "$SAPDEFAULT" = "" ]
	then
	SAPDEFAULT=$bSAPNAMEDIR
        SAPLIST=$bSAPNAMEDIR
    else
        SAPLIST="$SAPLIST $bSAPNAMEDIR"
    fi
  done
}

SAPexecute()
{
   if [ -d $SAPDIR/$SAPSYSTEMNAME/$SAPINSTANCENAME/work ]
          then
          {
            cd $SAPDIR/$SAPSYSTEMNAME/$SAPINSTANCENAME/work
            ERRORFILES=`grep -l ERROR dev*`
            for i in $ERRORFILES
		do
                echo "        "$bold"ERROR$plain found in file '$i'"
  		done
            echo ""
            echo "        Press return to continue"
            read tmp

            ERRORFILES="`ls stdout*` `ls stderr*` $ERRORFILES quit"
            CONTFLAG="TRUE"

            while [ "$CONTFLAG" = "TRUE" ] 
		do
		logoMsg
		PS3="Please select a file for viewing: "
            	select i in `echo $ERRORFILES`
        		do
                		case $i in
				quit)  CONTFLAG="FALSE"; break;;
                		$i) view $i; break;;
                		esac
        		done
		done
          }
    else
    	logoMsg
    	echo "        "$bold"ERROR:$plain The directory '$SAPDIR/$SAPSYSTEMNAME/$SAPINSTANCENAME/work' does not exist."
    	echo ""
	read tmp
    	exit 99
    fi
}

SAPgetInstance()
{
  if [ "$SAPLIST" = "$SAPDEFAULT" ]
	then
	SAPINSTANCENAME=$SAPDEFAULT
  else
  	PS3="Please select a SAP instance [1]: "
  	echo " "
  	echo " "
  	select i in `echo $SAPLIST`
  	do
		case $i in
		$i) SAPINSTANCENAME=$i; break;;
        	esac
  	done
	if [ "$SAPINSTANCENAME" = "" ]
		then
		SAPINSTANCENAME=$SAPDEFAULT
	fi
  fi

  if [ -d $SAPDIR/$SAPSYSTEMNAME/$SAPINSTANCENAME ]
  then
  {
    SAPSYSTEMNR=` echo $SAPINSTANCENAME | \
                awk ' { print substr($0,length($0)-1,2) } ' `
    logoMsg
  }
  else
  {
    logoMsg
    echo "        "$bold"ERROR:$plain This SAP instance does not exist."
    echo ""
    read tmp
    exit 99
  }
  fi
}

SAPlistInstance()
{
  SAPLIST=""
  SAPDEFAULT=""
  STRINGLENGTH=`ls -d $SAPDIR/${SAPSYSTEMNAME}/[DVEBMGS][!Y]* 2>/dev/null | \
                wc -l `
  if [  $STRINGLENGTH != "0" ]
  then 
  {
    for SAPINSTANCE in `ls -d $SAPDIR/${SAPSYSTEMNAME}/[DVEBMGS][!Y]*`
    do
       bSAPINSTANCE=`basename $SAPINSTANCE`
       if [ "$SAPDEFAULT" = "" ]
	   then
	   SAPDEFAULT=$bSAPINSTANCE
           SAPLIST=$bSAPINSTANCE
       else
           SAPLIST="$SAPLIST $bSAPINSTANCE"
       fi
    done
  }
  else
  {
    echo "        "$bold"ERROR:$plain No SAP instance configured"
    echo ""
    read tmp
    exit 99
  }
  fi
}

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

  if [ -d $SAPDIR/$SAPSYSTEMNAME ]
  then
  {
    logoMsg
  }
  else
  {
    logoMsg
    echo "        "$bold"ERROR:$plain This SAP system does not exist."
    echo ""
    read tmp
    exit 99
  }
  fi
}

if [ $# -eq 3 ]
	then
	SAPSYSTEMNAME=$1
     	SAPINSTANCE=$2
	FILENAME=$3
        TRACEFILE=/usr/sap/$SAPSYSTEMNAME/$SAPINSTANCE/work/$FILENAME
	view $TRACEFILE
else
	logoMsg
	R3exist
	SAPlistR3
	SAPgetR3
	SAPlistInstance
	SAPgetInstance
	SAPexecute
fi
