#!/bin/sh
#

#
#	This function is used to read the "pathToRcpHome" property from /tmp/xpd.config file (created during preinstall) 
#	and construct the $rcp.home variable.  pathToRcpHome is typically:
#		pathToRcpHome="/Applications/Sametime.app/Contents/MacOS/rcp"
#	If it fails to construct $rcp.home, this script will exit. 
#	

# fix permissions
USERNAME=`echo "${HOME}" | cut -d'/' -f3`
DATADIR="${HOME}/Library/Application Support/Lotus Sametime Data"
OLD_DATADIR="${HOME}/Lotus"
logger -t Sametime "DATADIR=${DATADIR}"
logger -t Sametime "USERNAME=${USERNAME}"

chmod -R 700 "${DATADIR}" 
chown -R ${USERNAME}:admin "${DATADIR}" 
chmod -R 700 "${OLD_DATADIR}" 
chown -R ${USERNAME}:admin "${OLD_DATADIR}" 

function global_config()
{
    CONFIGURATION="/tmp/xpd.config"
    KEY_RCPHOME="pathToRcpHome"

    #construct the path to mpkg,$1 is the path to the component package, we need the path to mpkg, we construct $package from $1
    package="$1/../../.."
    
    if [ -f "$CONFIGURATION" -a -r "$CONFIGURATION" ]
    then
		#get path to rcphome from configuration
		rcphome=`cat $CONFIGURATION|grep "$KEY_RCPHOME="|cut -d '=' -f2`
		echo "got rcp home : $rcphome"
    fi

    #If $rcp.home is empty, we will exit the script
    if [ -z "$rcphome" ]
    then
		echo "rcp.home is empty."
		exit
    fi
}

#
#	This function is used to create the ${rcp.home}/deploy directory. If the directory already exists, we will do nothing;
#	otherwise,we will create it.
#
function create_deploy_dir()
{
	deploy="$rcphome/deploy"
	echo "get deploy directory $deploy"
	#if rcp.home/deploy does not exist, we need to make the directory
	if [ ! -d "$deploy" ]
	then
	    echo "Create deploy directory in $rcphome"
	    mkdir -p "$deploy"
	fi
}

#
#	This functions copies files to ${rcp.home}/deploy directory.
#	This function has two paras, first is source file,second is dest file.
#	If the source file does not exist,we will do nothing.
#
function copy_to_deploy()
{
    source_file="$1"
    dest_file="$2"
    
    #if we found the source file,we will copy it;othwise,we will not.
    if [ -f "$source_file" ]
    then
		cp "$source_file" "$dest_file"
    fi
}

#Note: installer app provides the following env vars:
#$1 - the full path to the component installation package (sametime-offering.pkg) 
#$2 - the path to the destination
#$3 - the mountpoint of the destination volume

#call global_config function to set up $package and $rcphome
global_config $1

#Copy community-config.xml to rcphome/deploy/community-config.xml
src="$package/deploy/community-config.xml"
dest="$rcphome/deploy/community-config.xml"
echo "community-config src file: $src"
echo "community-config dest file: $dest"

create_deploy_dir

#if the creation finshes successfully, we copy files    
if [ -d "$rcphome/deploy" ]
then
    copy_to_deploy "$src" "$dest"
fi

    






