#!/bin/ksh93

###########################################################################################
# This script need to be run with option either with a or b
# option a - it is used when user wants to check prerequisites before SAP installation
# option b - it is used when user wants to check prerequisties before adding SAP application to PowerHA SystemMirror using SAP Smart Assist
#
###########################################################################################


################################################
#function :precheck_install                     #
#                                              #
#Explaination :This function will verify whether# 
#basic requirements to install SAP on           #
#AIX machines is met or not.                    #
#                                              #
#Input : none                                   #
                                               #
                                               #
                                               #
                                               #
################################################
function precheck_install {
  typeset var
 # AIX filesets to be installed?
        ##  bos.net
           var=$(lslpp -l bos.net.*)
            if [[ $? != 0 ]];then
             echo "Base fileset bos.net is not present"
            fi


##  X11.Dt
           var=$(lslpp -l X11.Dt.*)
            if [[ $? != 0 ]];then 
             echo "X11.Dt is not installed"
            fi

##  openssh file sets
         var=$(lslpp -l openssh.*)
         if [[ $? != 0 ]];then
           echo " openssh files are not installed properly"
         fi

##  mozilla
        var=$(lslpp -l Mozilla.*)
         if [[ $? != 0 ]];then
             echo "mozilla files are not installed properly"
         fi


##  swap space
         a=`lsps -s | grep  MB | awk ' {print $1}'`
         test="MB"
         i=`awk -v a="$a" -v b="$test" 'BEGIN{print index(a,b)}'`
         typeset -i k
         k=${a:0:$i-1}
         echo ${a:0:$i-1}
         if (( $k < 7000 ));then
            echo "It is recommended that swap space need to be greater than 7GB"
         fi

## java
         var=$(lslpp -l Java14_64.*)
         if [[ $? != 0 ]];then
            echo "java files not installed properly"
         fi

## hostname / NFS domain
         a=`chnfsdom`
         if [[ $a == "N/A" ]];then
            echo "nfs domain is not set "
         fi

## check for ulimits
if [[ `ulimit -t` == "unlimited" && `ulimit -f` == "unlimited" && `ulimit -d` == "unlimited" && `ulimit -s` == "unlimited" &&`ulimit -m` == "unlimited" && `ulimit -c` == "unlimit\
ed" && `ulimit -n` == "unlimited" &&  `ulimit -u` == "unlimited" ]];then
echo "ulimits are set to unlimited"
else
echo " ulimits are not set to unlimited "
fi

# Max processes
typeset -i var1
var1=$(lsattr -El sys0 | grep maxuproc | awk -F "=" '{print $2}')
if (( $var1 != 1024 ));then
chdev -l sys0 -a maxuproc=1024  
fi
chdev -l sys0 -a maxpout=8193 -a minpout=4096
}

################################################
#function :precheck_smartassist                 #
                                               #
#Explaination :This function will verify whether# 
#basic requirements to configure SAP with PowerHA#
#SAP Smart Assist and give needed changes to be #
#done                                           #
#Input : none                                   #
                                               #
                                               #
                                               #
                                               #
################################################
function precheck_smartassist
{
typeset nodes
typeset SID
nodes=$(clmgr q nodes)
typeset localnode=$(hostname)

## Verification of existence of sapservices file and profile files
for node in $nodes
do
typeset sapservices_output
sapservices_output=$(/usr/es/sbin/cluster/utilities/cl_rsh -n $node "ls -l /usr/sap/sapservices")
if [[ -z $sapservices_output ]];then
      echo "/usr/sap/sapservices is not present in $node"
      echo "Please make sure of existence of the file in the specified location and run the script again \n"
      exit 1
else
      /usr/es/sbin/cluster/utilities/cl_rsh -n $node "cd /usr/sap ; /usr/sbin/clrcp sapservices $localnode:/tmp/sapservices.$node"
      /usr/bin/diff /usr/sap/sapservices /tmp/sapservices.$node
      if [[ $? != 0 ]];then
          echo "/usr/sap/sapservices is not same on nodes $localnode and $node \n" 
          exit 1
      fi
fi	  
done

## Verification whether Instance directories are mounted on any of the nodes
typeset found
for SID in $(cat /usr/sap/sapservices | grep -w "^LIBPATH" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
while read line;do
if [[ $(echo $line | grep $SID) ]];then
    var=$(echo "$line" | awk '{print $5}' | awk -F "=" '{print $2}')
    for node in $nodes
    do
       found=0
	typeset output
       output=$(/usr/es/sbin/cluster/utilities/cl_rsh -n $node "ls -l $var  ")
       if [[ -n $output ]];then
          found=1
          break
       fi
    done
       if [[ $found == 0 ]];then
          echo "Profile file $var is not available on any of the cluster nodes \n"
		  echo "Please make sure that profile file $var is mounted on one of the nodes and run the script again \n"
          exit 1
       fi
 fi
done < /usr/sap/sapservices
done

## ERS Profile changes
echo "If you want to configure ERS instance with PowerHA, Follow below steps"
for SID in $(cat /usr/sap/sapservices | grep -w "^LIBPATH" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
     var=$( cat /usr/sap/sapservices | grep $SID | grep ERS | awk '{print $5}' | awk -F "=" '{print $2}')
    echo "If ERS need to configured with PowerHA , Edit the file $var with below changes \n"
     echo "1.Comment the line containing Autostart = 1 \n"
      echo "2. Edit the line Restart_Program_00 by replacing Restart_Program_00 as Start_Program_00 "
done

## Configuration of NFS server with Smart Assist"
echo "\n \n"
echo "If you want to configure NFS server with SAP Smart Assist, Follow below steps "
 typeset SAPADMNUSR
 for SID in $(cat /usr/sap/sapservices | grep -w "^LIBPATH" | awk -F"/" '{ print $4 }' | sort | uniq) ; do
 SAPADMNUSR=$(echo $SID | tr '[:upper:]' '[:lower:]')adm
  typeset var1
  var1=$(su - $SAPADMNUSR -c 'setenv LANG C ; echo $LIBPATH')
   while read line;do
        if [[ $(echo $line | grep $SID) ]];then
		    typeset var2=$(echo $line | grep $SID | awk ' {print $1}' | awk -F "=" ' {print $2}' | awk -F ":" ' {print $1}')
                    typeset var3=$(echo "$var1" | grep -w "$var2")
			if [[ -z $var3 ]];then
			echo "The directory $var2 is not present in LIBPATH. Please execute below command "
                        echo "su - $SAPADMNUSR "
                        echo " echo \"setenv LIBPATH \${LIBPATH}:"$var2" \" >> .cshrc  \n" 
			fi
	    fi
	done < /usr/sap/sapservices
  done
}

### MAIN ####

while getopts ab option; do
case $option in
                a) #call precheck_install function
				    precheck_install
					;;
			    b) #call precheck_smartassist function
				    precheck_smartassist
					;;
esac
done

					

