#!/bin/bash

DD=`date +'%y%m%d.%H%M%S'`
cp -p /etc/rc.d/rc.local /etc/rc.d/rc.local.kit
grep -v "echo " /etc/rc.d/rc.local > /etc/rc.d/rc.local.tmp
mv -f /etc/rc.d/rc.local.tmp /etc/rc.d/rc.local
PARAMS=(fs.file-max kernel.shmall kernel.shmmax kernel.shmmni kernel.sem net.ipv4.ip_local_port_range net.core.rmem_default net.core.rmem_max net.core.wmem_default net.core.wmem_max)
VALS=("65536" "2097152" "2147483648" "4096" "250 32000 100 128" "9000 65500" "262144" "4194304" "262144" "1048576") 
i=${#PARAMS[@]}
j=0
test -s ./tmp/newSysParam.lis
if [ "$?" = "0" ] ; then
	rm -rf ./tmp/newSysParam.lis > /dev/null 2>&1
fi
echo "VarName Exl CurrentSys problem" > /tmp/sysval
while [ $j -lt $i ] ; do
	myval=(${VALS[$j]})
	sysval=(`/sbin/sysctl ${PARAMS[$j]} | sed 's/.*= //'`)
	length=${#myval[@]}
	for (( x=0; x<$length ; x++ ))
	do
		if [ "${myval[$x]}" -gt "${sysval[$x]}" ] ; then
			echo "Updating parameter ${PARAMS[$j]}"
			echo "${PARAMS[$j]} = ${VALS[$j]}" >> ./tmp/newSysParam.lis
			good="XXXXX"
		else
			good=""
		fi
		echo "${PARAMS[$j]}[$x] ${myval[$x]} ${sysval[$x]} $good" >> /tmp/sysval
	done
        let j=$j+1
done
update=1
echo ""
cat /tmp/sysval | column -t
echo ""

test -s ./tmp/newSysParam.lis
if [ "$?" = "0" ] ; then
    cp -p /etc/sysctl.conf /etc/sysctl.conf.$DD
    echo "# Exlibris Parameters" >> /etc/sysctl.conf
    cat ./tmp/newSysParam.lis >> /etc/sysctl.conf
    echo "# End of Exlibris Parameters" >> /etc/sysctl.conf
else
    echo "System values are up2date"
    exit 0
fi


/sbin/sysctl -p
if [ "$?" != "0" ] ; then 
	echo "Parameter update failed"
	cp -p  /etc/sysctl.conf.$DD /etc/sysctl.conf
	exit 1
else
	exit 0
fi
