#!/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>
#*
#*
#*******************************************************************************/

CONF_FILE_PATH=/etc/apache2/conf.d
IFOLDER32BIT="/opt/novell/ifolder3/lib/simias"
IFOLDER64BIT="/opt/novell/ifolder3/lib64/simias"
OS_ARCH=`uname -m | grep -c x86_64`
CLUSTER_CONF_DIR=/simias/ClusterConf/
INVOKE_YAST=0

processFile(){
		if [ -f $1/32bit/simias.conf -o -f $1/64bit/simias.conf ]; then
			INVOKE_YAST=1
			if [ $OS_ARCH -gt 0 ]; then
				if [ -f $1/64bit/simias.conf ]; then
#					rm $CONF_FILE_PATH/simias.conf
#					ln -s $1/64bit/simias.conf $CONF_FILE_PATH/simias.conf
					cp -f $1/64bit/simias.conf $CONF_FILE_PATH/simias.conf
				else
					echo ""	
					echo "\n\nUnable find configuration file $1/64bit/simias.conf, configuration failed\n\n"	
					echo ""	
					exit
				fi
			else
				if [ -f $1/32bit/simias.conf ]; then
#					rm $CONF_FILE_PATH/simias.conf
#					ln -s $1/32bit/simias.conf $CONF_FILE_PATH/simias.conf
					cp -f $1/32bit/simias.conf $CONF_FILE_PATH/simias.conf
				else
					echo ""	
					echo "\n\nUnable find configuration file $1/64bit/simias.conf, configuration failed\n\n"	
					echo ""	
					exit
				fi
			fi
		else
			if [ -f $CONF_FILE_PATH/simias.conf ]; then
				mkdir -p $1/32bit
				mkdir -p $1/64bit
			
				if [ $OS_ARCH -gt 0 ]; then
					cp $CONF_FILE_PATH/simias.conf $1/64bit/simias.conf
					if [ $? -eq 0 ]; then
						sed -e 's,'$IFOLDER64BIT','$IFOLDER32BIT',g'  $CONF_FILE_PATH/simias.conf > $1/32bit/simias.conf	
#						rm $CONF_FILE_PATH/simias.conf
#						ln -s $1/64bit/simias.conf $CONF_FILE_PATH/simias.conf  
					else
						echo ""	
						echo "\n\nFailed to copy $CONF_FILE_PATH/simias.conf to $1/64bit/simias.conf, configuration failed \n\n"	
						echo ""	
					fi
				else
					cp $CONF_FILE_PATH/simias.conf $1/32bit/simias.conf
					if [ $? -eq 0 ]; then
						sed -e 's,'$IFOLDER32BIT','$IFOLDER64BIT',g'  $CONF_FILE_PATH/simias.conf > $1/64bit/simias.conf	
#						rm $CONF_FILE_PATH/simias.conf
#						ln -s $1/32bit/simias.conf $CONF_FILE_PATH/simias.conf  
					else
						echo ""	
						echo "\n\nFailed to copy $CONF_FILE_PATH/simias.conf $1/32bit/simias.conf, configuration failed \n\n"	
						echo ""	
					fi
				
				fi
			else
				echo ""	
				echo "Unable find configuration file $CONF_FILE_PATH/simias.conf, configuration failed"	
				echo ""	
				exit
			fi
		fi
		if [ -f $INPUT_STRING/simias/Simias.config ]; then
			newline=`grep LdapUri $INPUT_STRING/simias/Simias.config`  
			ipval=`echo $newline | awk '{print $3}'`
			ldapaddress=${ipval:7:${#ipval}-8}
			if [ ${ldapaddress:0:5}=="ldaps" ]; then
				newldapaddr=$ldapaddress
				if [ ${ldapaddress:${#ldapaddress}-1:1}=="/" ]; then
					newldapaddr=${ldapaddress:0:${#ldapaddress}-1}
					PORT=${newldapaddr#*:*:}
					ldapaddport=$newldapaddr
					if [ -z $PORT ]; then
						echo "Previous commands succeeded, but problem occured while running certmgr command"
						echo "please run: certmgr -ssl -m ldaps://<LDAP_IP>:636"
					else
						if [ `expr index $PORT ldap` -gt 0 ]; then
							ldapaddport=$newldapaddr":636"	
						fi
					fi
				fi
				certmgr -ssl -m $ldapaddport
			fi
		else
			echo "Could not find the config file Simias.config. Please check that shared resource path is mounted to this node."
			exit
		fi
		if [ $INVOKE_YAST -ne 0 ]; then 
			echo "SuccessFull!!! Symbolic link for simias.conf is created for this node. Please configure admin and web setup using yast"
			yast2 novell-ifolder3
		else
			echo "SuccessFull!!! Symbolic link for simias.conf is created for this node."
		fi
}


usage()
{
    echo "Usage: $0 <Shared Resource Path>"
    echo "(The shared resource path must be mounted and accessible on this node)"
    echo ""
    echo "Example Usage: $0 /media/nss/IFVOL "
    exit $USAGE_ERROR
}

if [ $# -ne 1 ]; then
    usage
fi


INPUT_STRING="$1"
#if [ -f $CONF_FILE_PATH/simias.conf ]; then
#	ISDPATHCORRECT=`grep -c SimiasDataDir=$INPUT_STRING/simias $CONF_FILE_PATH/simias.conf` 
#	if [ $ISDPATHCORRECT -eq 0 ]; then
#		DPATHINCONFIFILE=`grep -in MonoSetEnv $CONF_FILE_PATH/simias.conf`
#		echo ""	
#		echo "DATPATH configured in the existing $CONF_FILE_PATH/simias.conf file"	
#		echo "is pointing to \"$DPATHINCONFIFILE\"."	
#		echo "Please verify this and make sure DATA path provided is correct"
#		echo "If Datapath $INPUT_STRING mentioned in the command line correct, then remove $CONF_FILE_PATH/simias.conf and rerun this script."
#		echo ""	
#		exit
#	fi
#fi
if [ -d $INPUT_STRING/simias ]; then 
	processFile $INPUT_STRING$CLUSTER_CONF_DIR
else
    	echo "Usage: $0 <Shared Resource Path>"
	echo "Error: Could not find Shared Resource Path. Either path is wrong or shared resource is not migrated/mounted to this node."
	echo "Shared Resource Path should be the path where iFolder server stores data. e.g. /media/nss/IFVOL"
fi
