#!/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:
#*        <store proxy credentials into iFolder store>
#*
#*
#*******************************************************************************/


# Usage:  ./iFolder_update_proxy_cred_store proxydn proxypwd

MONO_CMD=""
if [ $# -lt 1 ]; then
        echo "Usage: ./iFolder_update_proxy_cred_store <proxydn> <ldapport>"
        exit -1
fi

pwd=$OES_IFOLDER_DATA
if [ -z $pwd ]; then
	echo "Set proxy password in environment variable OES_IFOLDER_DATA"
        echo "Usage: ./iFolder_update_proxy_cred_store <proxydn> <ldapport>"
        exit -1
fi


OS_ARCH=`uname -m | grep -c x86_64`
if [ $OS_ARCH -gt 0 ]
then
        export OS_ARCH=`uname -m`
fi

rpm -q novell-ifolder-mono > /dev/null 2>&1
if [ $? -gt 0 ]
then

        export MONO_PATH=/usr/lib/simias/web/bin:/usr/bin
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/simias/web/bin
        export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/lib/simias/web/bin
        cd /usr/bin

	MONO_CMD=mono

else
        MONO_RUNTIME_PATH=/usr/bin/../mono
        export MONO_PATH=$MONO_RUNTIME_PATH/lib/mono/:$MONO_RUNTIME_PATH/lib/mono/2.0:/usr/lib/simias/web/bin:/usr/bin
        source $MONO_RUNTIME_PATH/bin/novell-ifolder-mono-environment.sh
        export MONO_CFG_DIR=$MONO_RUNTIME_PATH/etc
        export IFOLDER_MOD_MONO_SERVER2_PATH=/usr/bin
        export IFOLDER_MONO_PATH=$MONO_RUNTIME_PATH
        cd /usr/bin
	MONO_CMD=$MONO_RUNTIME_PATH/bin/mono
fi

# get the value of datapath
datapath=`grep SimiasDataDir /etc/apache2/conf.d/simias.conf | gawk 'BEGIN { RS=";|\"" } { if (/^SimiasDataDir/) print gensub(/SimiasDataDir=/, "", 1) }'`
touch $datapath/proxydetails
echo update_proxy_cred_store>>$datapath/proxydetails
echo "$1">>$datapath/proxydetails
echo $pwd>>$datapath/proxydetails

`chown wwwrun:www $datapath/proxydetails`
`chmod 600 $datapath/proxydetails`
$MONO_CMD iFolderProxySetup.exe $datapath update_proxy_cred_store $3
EXIT_CODE=$?
rm $datapath/proxydetails
exit $EXIT_CODE
