#!/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 check if named instances defined in global config file
# are active. The active is ascertained by existance of the data directory of the named instance is mounted
# on the filesystem. That is sufficient to assertain if a named instance is running on this Scalix 
# server. It is a wrapper around omcheckgc command
################################################################

#
# usage help function
#
usage() {
	echo "usage: $0 [ -i name]"
	exit 1
}
#
# Function to check if the named instance sysdata directory is mounted.
#
checkInstanceState () {
	export OMCURRENT=$1
	SYSDATA=`$SXBIN/omrealpath "~/sys/sysdata"`
	if [ $? -eq 0 ]
	then
		if [ -f $SYSDATA ]
		then
			echo "$1=ACTIVE"			
		else
			echo "$1=INACTIVE"			
		fi
	else
		echo "$1=ERROR:UNABLE TO ASCERTAIN"
	fi
}
#
# parse the arguments
#
instance=""
while getopts i: opt
do
	case "$opt" in
		i) instance="$OPTARG";;
		[?]) usage;;
	esac
done
SXBIN="/opt/scalix/bin"
PATH=$SXBIN:$PATH
if [ "$instance" = "" ]
then
	for line in `$SXBIN/omcheckgc -l`
	do
		checkInstanceState $line
	done
else 
		checkInstanceState $instance
fi
