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

# Usage:  ./iFolder_retrieve_proxy_creds.sh password /opt/novell/Proxy/myproxycreds

MONO_CMD=""
function getProxy(){
	datapath=`grep SimiasDataDir /etc/apache2/conf.d/simias.conf | gawk 'BEGIN { RS=";|\"" } { if (/^SimiasDataDir/) print gensub(/SimiasDataDir=/, "", 1) }'`
	if [ $1 = "username" ]; then
		$MONO_CMD iFolderProxySetup.exe $datapath retrieve_proxy_creds username $2 $3
		exit $?
	elif [ $1 = "password" ]; then
		$MONO_CMD iFolderProxySetup.exe $datapath retrieve_proxy_creds password $2 $3
		exit $?
	fi
}

if [ $# -lt 2 ]; then
        echo "Usage: ./iFolder_retrieve_proxy_creds password <proxucredentialspath> <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

if [ $1 = "username" ]; then
	getProxy "username" $2 $3 
elif [ $1 = "password" ]; then
	getProxy "password" $2 $3
fi
