#!/bin/csh -f

set pr1="--------------------------------------------------------------------------------"
set pr2="================================================================================"
set pr3="------------------------------------------- EOF --------------------------------"

if ( -f precheck.ini ) then 
    source ./precheck.ini
endif
if ( -f param_csh.ini ) then 
    source ./param_csh.ini
endif
alias straight 'printf "%-60s %s" \!*'
alias straightln 'printf "%-60s %s\n" \!*'

rm $log >& /dev/null
touch $log
echo "" >>$log
echo "Pre Installation Check Engine." >> $log
echo "The additional log file: ./log/pre_checks_info_file.log" >> $log
################################################################################
#open info log file.
rm $info >& /dev/null
touch $info
echo $pr1 >> $info  
echo "Pre-installation checks INFO file.">> $info
echo "" >> $info
################################################################################
bash -f serverinfo.sh | tee -a $log
echo "Machine type `uname -m`" | tee -a $log
csh -f precheck_net.csh | tee -a $log

################################################################################
#check OS
set RH4=N
set RH5=N
grep "release 4" /etc/redhat-release
if ( "$status" !=  "0" ) then
	grep "release 5" /etc/redhat-release
	if ( "$status" !=  "0" ) then
		straightln "Checking the linux Release." "[FAULT]" >> $log
	else
		straightln "Checking the linux Release." "[OK]" >> $log
		set  RH5=Y
	endif
else
        straightln "Checking the linux Release." "[OK]" >> $log  
	set  RH4=Y
endif
uname -m | grep -q _64
if ( "$status" == "0" ) then
    if ( "$RH4" == "Y" ) then
        echo "***************************************************" | tee -a $log
        echo "    RH4 64 bit is not supported"  | tee -a $log
        echo "***************************************************" | tee -a $log
        straightln "Checking OS Architecture." "[FAULT]" >> $log
    else
        if ( "$RH5" == "Y" ) then
            straightln "Checking OS Architecture." "[OK]" >> $log
        else
            echo "***************************************************" | tee -a $log
            echo "   Usupported OS VERSION"  | tee -a $log
            echo "***************************************************" | tee -a $log
            straightln "Checking OS Architecture." "[FAULT]" >> $log
        endif
    endif
else
    if ( "$RH5" == "Y" ) then
        echo "***************************************************" | tee -a $log
        echo "    RH5 32 bit is not supported"  | tee -a $log
        echo "***************************************************" | tee -a $log
        straightln "Checking OS Architecture." "[FAULT]" >> $log
    else
        if ( "${MAIN_PRODUCT}" == "PRIMO" || "${MAIN_PRODUCT}" == "DPS" ) then
            echo "***************************************************" | tee -a $log
            echo "    RH5 32 bit is not supported"  | tee -a $log
            echo "***************************************************" | tee -a $log
            straightln "Checking OS Architecture." "[FAULT]" >> $log
        else
            if ( "$RH4" == "Y" ) then
                straightln "Checking OS Architecture." "[OK]" >> $log
            else
                echo "***************************************************" | tee -a $log 
                echo "    Usupported OS VERSION"  | tee -a $log
                echo "***************************************************" | tee -a $log
                straightln "Checking OS Architecture." "[FAULT]" >> $log
            endif
        endif
    endif
endif

echo "OS version ---> `cat /etc/redhat-release`" >> $info
echo "">> $info
################################################################################
echo $pr2 >> $info
#/etc/hosts
echo "Hosts table." >> $info 
cat /etc/hosts >> $info
echo "">> $info

################################################################################
#memory size  
echo "Memory size." >> $info
cat /proc/meminfo | grep MemTotal >> $info
set memory_size = `cat /proc/meminfo | grep MemTotal | awk '{print $2}'`
if ( "$memory_size" < "$MEMORY_SIZE_MIN" ) then
                  straightln "Checking the memory size." "[FAULT]" >> $log
else
                  straightln "Checking the memory size." "[OK]" >> $log
endif
echo $pr2 >> $info
################################################################################
echo "Swap size." >> $info
/sbin/swapon -s  >> $info
#swap size
set swap_size = `/sbin/swapon -s | awk '{SUM+=$3} END {print SUM}'`
if ( "$swap_size" < "$SWAP_SIZE_MIN" ) then
    straightln "Checking the swap size." "[FAULT]" >> $log
else
    straightln "Checking the swap size." "[OK]" >> $log
endif
echo $pr2 >> $info
################################################################################
#/exlibris directory existing + size
echo "" >> $info
echo "Disk size." >> $info
df -k >> $info
echo "" >> $info

df -k /exlibris >&/dev/null
if ( "$status" != "0" ) then
    straightln  "Checking whether /exlibris directory exists." "[FAULT]" >> $log
else 
    straightln  "Checking whether /exlibris directory exists." "[OK]" >> $log
endif
echo $pr2 >> $info
################################################################################
set exlibris_size = ` df -Pk /exlibris | awk '{print $4}' `
if ( "$exlibris_size[2]" < "$EXLIBRIS_SIZE_MIN" ) then
           straightln "Checking the /exlibris directory size." "[WARNING]" >> $log
else
           straightln "Checking the /exlibris directory size." "[OK]" >> $log
endif 	   
################################################################################
#check /tmp directory exist + size
echo "/tmp directory." >> $info
if (! -d /tmp ) then 
           straightln "Checking whether /tmp directory exists." "[FAULT]" >>$log
	   echo "The /tmp directory does not exists." >> $info
else
           straightln "Checking whether /tmp directory exists." "[OK]"  >> $log 
	   ls -l / | grep tmp >> $info	
endif	      
echo $pr2 >> $info
################################################################################
set tmp_size = ` df -Pk /tmp | awk '{print $4}' `
if ("$tmp_size[2]" < "$TMP_SIZE_MIN" ) then
           straightln "Checking the /tmp directory size." "[FAULT]" >> $log
else 
           straightln "Checking the /tmp directory size." "[OK]" >> $log
endif	   	      

################################################################################
#check $ORACLE_VER dir exist
echo "" >> $info
if ( -d /exlibris/app/oracle/product/$ORACLE_VER ) then
         echo "The /exlibris/app/oracle/product/$ORACLE_VER directory exists. " >> $info  
         echo "">> $info
else
         echo "The /exlibris/app/oracle/product/$ORACLE_VER directory does not exists. " >> $info
endif
################################################################################
#exlibris users oracle in the server.
echo "" >> $info
echo "Exlibris users information." >> $info
echo $pr1 >> $info
echo "Users:" >> $info
echo "" >> $info
grep metalib: /etc/passwd >> $info
grep aleph: /etc/passwd >> $info
grep dtl: /etc/passwd >> $info
grep oracle /etc/passwd >> $info
grep verde /etc/passwd >> $info
grep crn /etc/passwd >> $info
grep dps /etc/passwd >> $info
grep primo /etc/passwd >> $info
echo $pr1 >> $info
echo "Groups:" >> $info
echo "">> $info
grep "^dba:" /etc/group >> $info
grep "^exlibris:" /etc/group >> $info
echo $pr2 >> $info
################################################################################
#check gzip,su,tar,sftp,make,ar,ld,nm
set comlist = "gzip su tar sftp make ar ld nm gcc"
foreach arg ( $comlist )
if ( -X ${arg}  ) then
       straightln "Checking whether ${arg} exists in the path." "[OK]" >> $log
       echo "The ${arg} exists in the path." >> $info
   else
       straightln "Checking whether ${arg} exists in the path." "[FAULT]" >> $log
       echo "The ${arg} does not exist in the path." >> $info
   endif
end
echo $pr2 >> $info
################################################################################
echo "">> $info
echo "System library checks" >> $info
echo $pr1 >> $info
echo  "On the server:" >> $info

if ( "$RH4" == "Y" ) then
    set rpm_list=RH4_rpm_list
else
    set rpm_list=RH5_rpm_list
endif

foreach LINE  (`cat $rpm_list | grep -v '^#'`)
    set rpm=`echo $LINE | awk -F, '{print $1}'`
    set ver=`echo $LINE | awk -F, '{print $2}'`
    echo "Search $arg package or higher" >> $info
    set exist=`rpm -q $rpm --queryformat "%{NAME}\t%{VERSION}\t%{ARCH}\t%{RELEASE}\n" | grep -v "not installed" | tee logfile | awk '{print $2}' | cut -d'.' -f1`
    if ( "$exist" != "" && "$exist" == "$ver" ) then
        straightln "Checking if the $rpm exist." "[OK]" >> $log
        straightln "Checking if the $rpm exist." "[OK]" >> $info
    else
        straightln "Checking if the $rpm exist." "[FAULT]" >> $log
        straightln "Checking if the $rpm exist." "[FAULT]" >> $info
    endif
    echo "" >> $info
end


echo >> $info
rm -f tmp/rpm_list >& /dev/null
rm -f tmp/rpmlist.64 >& /dev/null
rm -f tmp/rpmlist.32 >& /dev/null

echo $pr2 >> $info
echo "" >> $info
echo "" >> $info
echo "               RESULTS" >>$info
echo $pr1 >> $info
echo "" >> $info
cat $log >> $info
echo "" >> $info
echo $pr3 >> $info
##############EOF###############################################################
