#!/bin/sh
#/*****************************************************************************
#*
#* Copyright (c) [2009] Novell, Inc.
#* All Rights Reserved.
#*
#* This program is free software; you can redistribute it and/or
#* modify it under the terms of version 2 of the GNU General Public License as
#* published by the Free Software Foundation.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, contact Novell, Inc.
#*
#* To contact Novell about this file by physical or electronic mail,
#* you may find current contact information at www.novell.com
#*
#*-----------------------------------------------------------------------------
#*
#*                 Novell iFolder Enterprise
#*
#*-----------------------------------------------------------------------------
#*
#*                 $Author: Anil Kumar (kuanil@novell.com)
#*                 $Modified by: <Modifier>
#*                 $Mod Date: <Date Modified>
#*                 $Revision: 0.1
#*-----------------------------------------------------------------------------
#* This module is used to:
#*        <iFolder mono environment setup script>
#*
#*
#*******************************************************************************/

iFPrefix=/usr/bin/../
CONF_FILE_PATH=/etc/apache2/conf.d
MOD_MONO_CONF_FILE_PATH="/etc/apache2/mod_mono.conf"
IFOLDER32BIT="/opt/novell/ifolder3/lib/simias"
IFOLDER64BIT="/opt/novell/ifolder3/lib64/simias"
OS_ARCH=`uname -m | grep -c x86_64`

processFile(){
	if [ -f $1 ]; then
		sed -e 's,'$MOD_MONO_CONF_FILE_PATH','$iFPrefix/mono/etc/apache2/conf.d/mod_mono.conf',g' -i $1
		if [ $OS_ARCH -gt 0 ]; then
			sed -e 's,'$IFOLDER32BIT','$IFOLDER64BIT',g' -i $1	
		fi
		if [ $1 == $CONF_FILE_PATH/simias.conf ]; then
			newline=`grep -n MonoSetEnv $1`
			lineno=${newline:0:`expr index "$newline" \:`-1}

			if [ `grep -c -E MONO_THREADS_PER_CPU $1` -eq 0 ]; then
				sed -e ''$lineno' s/\"$/;MONO_THREADS_PER_CPU=2000\"/g' -i $1 
			fi
			
			if [ `grep -c -E MonoServerPath $1` -eq 0 ]; then
				sed '6i MonoServerPath simias10 '$iFPrefix/bin/ifolder-mod-mono-server2'' -i $1
			else
				sed -e 's,'/mod-mono-server2','/ifolder-mod-mono-server2',g' -i $1
			fi

			if [ `grep -c -E MonoMaxActiveRequests $1` -eq 0 ]; then
				sed '7i MonoMaxActiveRequests simias10 150' -i $1
			fi

			if [ `grep -c -E MonoMaxWaitingRequests $1` -eq 0 ]; then
				sed '8i MonoMaxWaitingRequests simias10 250' -i $1
			fi

		elif [ $1 == $CONF_FILE_PATH/ifolder_admin.conf ]; then
			if [ `grep -c -E MonoServerPath $1` -eq 0 ]; then
				sed '5i MonoServerPath admin '$iFPrefix/bin/ifolder-mod-mono-server2'' -i $1
			else
				sed -e 's,'/mod-mono-server2','/ifolder-mod-mono-server2',g' -i $1
			fi
		elif [ $1 == $CONF_FILE_PATH/ifolder_web.conf ]; then
			if [ `grep -c -E MonoSetEnv $1` -eq 0 ]; then
				sed '5i MonoSetEnv ifolder MONO_THREADS_PER_CPU=2000' -i $1
			fi
			if [ `grep -c -E MonoServerPath $1` -eq 0 ]; then
				sed '6i MonoServerPath ifolder '$iFPrefix/bin/ifolder-mod-mono-server2'' -i $1
			else
				sed -e 's,'/mod-mono-server2','/ifolder-mod-mono-server2',g' -i $1
			fi
			if [ `grep -c -E MonoMaxActiveRequests $1` -eq 0 ]; then
				sed '7i MonoMaxActiveRequests ifolder 150' -i $1
			fi
			if [ `grep -c -E MonoMaxWaitingRequests $1` -eq 0 ]; then
				sed '8i MonoMaxWaitingRequests ifolder 250' -i $1
			fi
		elif [ $1 == $iFPrefix/lib/simias/web/web.config ] || [ $1 == $iFPrefix/lib64/simias/web/web.config ]; then
			if [ `grep -c -E AnyHttpSoap $1` -eq 0 ]; then
				newline=`grep -n authentication\ mode $1`
				lineno=${newline:0:`expr index "$newline" \:`-1}
				sed ''$lineno'i \\t<webServices>\n\t\t<protocols>\n\t\t\t<add name="HttpSoap"/>\n\t\t\t<add name="HttpGet"/>\n\t\t\t<add name="HttpPost"/>\n\t\t\t<add name="AnyHttpSoap"/>\n\t\t</protocols>\n\t</webServices>\n' -i $1
			fi
			sed -e 's,maxconnection=\"10\", maxconnection=\"24\",g' -i $1
		fi
	fi
}


rpm -q novell-ifolder-mono > /dev/null 2>&1
if [ $? -eq 0 ]
then
	processFile $CONF_FILE_PATH/simias.conf
	processFile $CONF_FILE_PATH/ifolder_admin.conf
	processFile $CONF_FILE_PATH/ifolder_web.conf
	if [ $OS_ARCH -gt 0 ]; then
		if [ -f $iFPrefix/lib/simias/web/web.config.rpmsave ]; then
			cp $iFPrefix/lib/simias/web/web.config.rpmsave $iFPrefix/lib64/simias/web/web.config
			mv $iFPrefix/lib/simias/web/web.config.rpmsave $iFPrefix/lib64/simias/web/
		fi
		processFile $iFPrefix/lib64/simias/web/web.config
		if [ -f $iFPrefix/lib/simias/admin/Web.config.rpmsave ]; then
			cp $iFPrefix/lib/simias/admin/Web.config.rpmsave $iFPrefix/lib64/simias/admin/Web.config
			mv $iFPrefix/lib/simias/admin/Web.config.rpmsave $iFPrefix/lib64/simias/admin/
		fi
		if [ -f $iFPrefix/lib/simias/webaccess/Web.config.rpmsave ]; then
			cp $iFPrefix/lib/simias/webaccess/Web.config.rpmsave $iFPrefix/lib64/simias/webaccess/Web.config
			mv $iFPrefix/lib/simias/webaccess/Web.config.rpmsave $iFPrefix/lib64/simias/webaccess/
		fi
	else
		processFile $iFPrefix/lib/simias/web/web.config
	fi
	if [ ! -d /opt/novell/ifolder3/mono/lib/mono/gac/log4net ]; then
		if [ -f /usr/lib/mono/log4net/log4net.dll ]; then
			/usr/bin/gacutil -i /usr/lib/mono/log4net/log4net.dll -root /opt/novell/ifolder3/mono/lib
		fi
	fi
fi
