#!/bin/sh
#
#
#    Scalix Public License
#
#
#    The contents of this file are subject to the Scalix Public License
#    Version 1.1 ("License"); you may not use this file except in
#    compliance with the License. You may obtain a copy of the License at
#
# 
#    http://www.scalix.com/community/licensing 
#
# 
#    Software distributed under the License is distributed on an "AS IS"
#    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
#    the License for the specific language governing rights and limitations
#    under the License.
#
# 
#    The Original Code is Scalix Server. 
#    The Initial Developer of the Original Code is Scalix Corporation. 
#    Copyright (C) 2006 Scalix Corporation. 
#    All Rights Reserved.
#    Additional Copyrights may apply - see the notice in each file
#
#
#
#
#
#
# Copyright (C) 2005 Scalix Corporation. All rights reserved.
#
################################################################
# This script is used by the RES to prepare or initiate the SmartCache for the specified user
# When completed and mail address is specified it will send mail to the admin with its output.
# it appends the sxmbcprep output to /tmp/$0.pid.out; the file is removed after completion.
################################################################
#
# usage help function
#
usage() {
	echo "Usage: $0 -u user_ORN [-k kilobytes] [-d directory_path] [-m admin_mail_addr] [-p password]"
	exit 1
}
cd /
tag=$$
prefix="sxprepsmartcache"
suffix="out"
outfile=/tmp/$prefix.$tag.$suffix
directory="-x"
kb="-x"
mailAddr="-x"
user="-x"
pass="-x"
if [ "$#" -lt 2 ]
then
	usage
fi
while getopts u:k:d:m:p: opt
do
	case "$opt" in
		u) val="$OPTARG"
		     user="$val";;
		k) kb="$OPTARG";;
		d)directory="$OPTARG";;
		m)mailAddr="$OPTARG";;
		[?]) usage;;
	esac
done
#
# construct the argument list to sxmbcprep
#
CMD="/opt/scalix/bin/sxmbcprep -u "$user""
#
#check for directory path
#
if [ "$directory" != "-x" ]
then
	CMD="$CMD -d $directory"
fi
#
#check for maximum cache size item
#
if [ "$kb" != "-x" ]
then
	CMD="$CMD -k $kb"
fi
#
# check for password
#
if [ $pass != "-x" ]
then
	CMD="$CMD -p $pass"
fi
out=`eval $CMD  2>&1 > $outfile`
result=$?
if [ $result -ne 0 ]
then
	echo $out >> /tmp/$prefix.$tag.$suffix
fi
if [ "$mailAddr" != "-x" ]
then
	if [ -f $outfile ]
	then
		cat $outfile | mail -s "Output from sxmbcprep for user $user" $mailAddr
		/bin/rm -f $outfile
	fi
fi
exit 0
		
	

	



	
