#!/bin/sh

# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2005-2008 Sun Microsystems, Inc. All rights reserved. 
#
# The contents of this file are subject to the terms of the GNU General Public
# License Version 3("GPL")(the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the License at
# https://common-agent-container.dev.java.net/bootstrap/legal/license.txt or
# www/bootstrap/legal/license.txt.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# When distributing the software, include this License Header Notice in each
# file and include the License file at www/bootstrap/legal/license.txt.
#
# Sun designates this particular file as subject to the "Classpath" exception
# as provided by Sun in the GPL Version 3 section of the License file that
# accompanied this code.
#
# If applicable, add the following below the License Header, with the fields
# enclosed by brackets [] replaced by your own identifying information:
#     "Portions Copyrighted [year] [name of copyright owner]"
#

# Must be able to determine target OS so can set up appropriate
# command environment.
OS=`uname -s`
OS_VERSION=`uname -r`

# This script is used as a startup script for initd.

case "${OS}" in
    "SunOS")
	CACAOADM="`dirname $0`/../../../bin/cacaoadm"
	CAT="/usr/bin/cat"
        LS="/usr/bin/ls"
	;;
    *)
	echo "Not supported on detected OS \"${OS}\"."
	exit 1
	;;
esac 

LOGGER="/usr/bin/logger"


# PARAMETERS for localization
TEXTDOMAIN="cacao"

case "$1" in
start)
    
    
    # This subcommand is invoked by the SMF framework passing the name of
    # service instance.
    # The instance name is passed as argument
    
   
    #robustness
    if [ ! -f "${CACAOADM}" ]
    then
        MSG="Can't find cacaoadm, exit"
        # Log message on syslog
        ${LOGGER} -p daemon.err -t cacao "${MSG}"
        # Display message on terminal if any
        echo "${MSG}" >&2
        exit 1
    fi

     # Do the start
    instancename=$2
    instance="-i ${instancename}"

    ${CACAOADM} smf_start ${instance} 2>&1
    if [ $? -ne 0 ]; then
        MSG="Error: Fail to start cacao agent. ${instancename}"
        # Log message on syslog
        ${LOGGER} -p daemon.err -t cacao "${MSG}"
        # Display message on terminal if any
        echo "${MSG}" >&2
        exit 1            
    fi        
    ;;

stop)
    

    # This subcommand is invoked by the SMF framework passing the name of
    # service instance.
    # The instance name is passed as argument
    
    #robustness
    if [ ! -f "${CACAOADM}" ]
    then
        MSG="Can't find cacaoadm, exit"
        # Log message on syslog
        ${LOGGER} -p daemon.err -t cacao "${MSG}"
        # Display message on terminal if any
        echo "${MSG}" >&2
        exit 1
    fi

    # Do the stop
    forced_stop=""
    if [ "$2" = "-f" ]
    then
        forced_stop=-f
        shift
    fi
    instancename=$2
    instance="-i ${instancename}"

    ${CACAOADM} smf_stop ${forced_stop} ${instance} 2>&1
    if [ $? -ne 0 ]; then
        MSG="Error: Can't stop cacao agent. ${instancename}"
        # Log message on syslog
        ${LOGGER} -p daemon.err -t cacao "${MSG}"
        # Display message on terminal if any
        echo "${MSG}" >&2
        exit 1
    fi
    ;;

*)
    echo "Usage: $0 {start|stop} [instance name]}" >&2
    ;;
esac

exit 0
