#!/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 create the SIS index for the specified user
# When completed and mail address is specified it will send mail to the admin with its output.
# it appends the sxmkindex output to /tmp/$0.pid.out; the file is removed after completion.
################################################################
#
# usage help function
#
usage() {
	echo "Usage: $0 -u user_ORN [-t time_in_days] [-m admin_mail_addr]"
	exit 1
}
cd /
tag=$$
prefix="sxcreatesisindex"
suffix="out"
outfile="/tmp/$prefix.$tag.$suffix"
tm="-x"
maildAddr="-x"
#
# check arguments
#
if [ "$#" -lt 2 ]
then
	usage
fi
#
# parse arguments
#
while getopts u:t:m: opt
do
	case "$opt" in
		u) val="$OPTARG"
		     user="$val";;
		t) tm="$OPTARG";;
		m)mailAddr="$OPTARG";;
		[?]) usage;;
	esac
done
#
# construct the sxmkindex command 
#
CMD="/opt/scalix/bin/sxmkindex "$user""
#
# check if time is specified
#
if [ "$tm" != "-x" ]
then
	CMD="$CMD -t $tm"
fi
#
#execute the command and redirect output to a file
#
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 sxmkindex for user $user" $mailAddr
		/bin/rm -f $outfile
	fi
fi
exit 0
