#!/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]"
#

# Script functions list:
#
# - cacao_create_instance_startup         : Create the instance startup smf service
# - cacao_delete_instance_startup         : Delete the instance startup smf service
# - cacao_delete_startup                  : Delete the whole cacao startup smf service
#
# - cacao_update_smf_service_manifest   : Update the SMF service manifest for an
#                                         instance.
# - cacao_register_instance_smf_service : Registers service in SMF registry for
#                                         an instance.
# - cacao_update_instance_smf_service   : update service in SMF registry for
#                                         an instance.
# - cacao_get_highest_install_id    : Print the hihest install id found in startup resources



#------------------------------------------------------------------------------
# cacao_create_instance_startup
#
# DESCRIPTION :
# Create the instance startup smf service
#
# PARAMETERS :
# The instance name
#
# RETURN CODE:
# CACAO_CR_ERROR in case of error
# CACAO_CR_SUCCESS in case of sucess
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_create_instance_startup() {

    # Try to make the handling more generic/robust
    if [ $# -eq 1 ]
    then
	# Create for a given instance
        instances="${1}"
    else
	# Create for all existing instances
	instances="`${LS} -1 ${cacao_etc_instances_dir} 2>/dev/null`"
	if [ ${?} -ne 0 ]
	then
	    instances="default"
	fi
    fi
    for i in ${instances}
    do
        _instance_dir="${cacao_etc_instances_dir}/${i}"
        _property_file="${_instance_dir}/private/cacao.properties"
        if [ ! -f "${_property_file}" ]
	then
            continue
        fi
        cacao_is_embedded_instance ${i}
        # don't startup embedded stuff
        if [ ${?} -eq ${CACAO_FALSE} ]
	then
            # Current user must be root
            if [ ${cacao_current_id} -eq 0 ]
	    then
                cacao_do_create_smf_startup "${i}"
                if [ ${?} -ne ${CACAO_CR_SUCCESS} ]
		then
                    return ${CACAO_CR_ERROR}
                fi
            fi
        fi
    done
    return ${CACAO_CR_SUCCESS}
}

#------------------------------------------------------------------------------
# cacao_do_create_smf_startup
#
# DESCRIPTION :
# Internal create/upgrade smf startup method
#
# PARAMETERS :
# None
#
# RETURN CODE:
# CACAO_CR_ERROR in case of error
# CACAO_CR_SUCCESS in case of sucess
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_do_create_smf_startup() {

    instance_name="$1"
    smf_name=${CACAO_SMF_SVC_SERVICE_NAME_PREFIX}"${cacao_unique_name}"
    service_name="${smf_name}":"${instance_name}"

    _do_create=${CACAO_TRUE}

    cacao_smf_status="SMF Checking|1|${instance_name}|${service_name}"
    # should be svcs -l call
    ${SVCS} -l ${service_name} >/dev/null 2>&1
    if [ ${?} -eq 0 ]
    then
	#service present, we gonna update it
	_do_create=${CACAO_FALSE}
    fi

    # Update service manifest file
    # Register instance service
    cacao_smf_status="SMF Updating manifest|2|${instance_name}|${service_name}"
    cacao_update_smf_service_manifest "${instance_name}"
    if [ ${?} -ne ${CACAO_CR_SUCCESS} ]
    then
        cacao_print_error_message "${CACAO_MSG_ERROR_SMF_MANIFEST_UPDATE}" \
            "${instance_name}"
	cacao_smf_status=""
        return ${CACAO_CR_ERROR}
    fi
    
    if [ ${_do_create} -eq ${CACAO_TRUE} ]
    then
	cacao_smf_status="SMF Registering|3|${instance_name}|${service_name}"
	cacao_register_instance_smf_service "${instance_name}" "${smf_name}"
	if [ ${?} -ne ${CACAO_CR_SUCCESS} ]
	then
            cacao_print_error_message "${CACAO_MSG_ERROR_SMF_REGISTRATION}" \
		"${instance_name}"
	    cacao_smf_status=""
            return ${CACAO_CR_ERROR}
	fi
    else
	cacao_update_instance_smf_service "${instance_name}" "${smf_name}"
	if [ ${?} -ne ${CACAO_CR_SUCCESS} ]
	then
	     cacao_print_error_message "${CACAO_MSG_ERROR_SMF_MANIFEST_UPDATE}" \
		 "${instance_name}"
            cacao_smf_status=""
            return ${CACAO_CR_ERROR}
	fi
    fi
    #enable the service if needed
    cacao_is_enabled_instance "${instance_name}"
    if [ ${?} -eq ${CACAO_TRUE} ]
    then
	cacao_smf_status="SMF Enabling at boot|4|${instance_name}|${service_name}"
        cacao_do_smf_enable_at_boot_internal "${instance_name}"
        if [ ${?} -ne ${CACAO_CR_SUCCESS} ]
        then
            cacao_print_error_message "${CACAO_MSG_ERROR_SMF_ENABLING}" \
                "${instance_name}"
	    cacao_smf_status=""
            return ${CACAO_CR_ERROR}
        fi
    fi
    
    cacao_smf_status=""
    return ${CACAO_CR_SUCCESS}
}

#------------------------------------------------------------------------------
# cacao_delete_instance_startup
#
# DESCRIPTION :
# Delete the instance startup smf service
#
# PARAMETERS :
# The instance name
#
# RETURN CODE:
# CACAO_CR_ERROR in case of error
# CACAO_CR_SUCCESS in case of sucess
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_delete_instance_startup() {
    instance_name=$1
    full_service_name=${CACAO_SMF_SVC_NAME_PREFIX}"${cacao_unique_name}":"${instance_name}"
    service_name=${CACAO_SMF_SVC_SERVICE_NAME_PREFIX}"${cacao_unique_name}":"${instance_name}"

    # should be svcs -l call
    if [ -n "`${SVCCFG} -s ${service_name} list 2>/dev/null`" ]; then
        ${SVCCFG} delete -f "${full_service_name}" >/dev/null 2>&1
        if [ ${?} -ne 0 ]
        then
             # print only message when needed (not a rollback)
            if [ -z "${cacao_creating_instance}" ]
            then
                cacao_print_error_message \
                    "${CACAO_MSG_ERROR_SMF_UNREGISTRATION}" "${instance_name}"
            fi
            return ${CACAO_CR_ERROR}
        fi
    fi
    return ${CACAO_CR_SUCCESS}
}

#-------------------------------------------------------------------------------
# cacao_delete_startup
#
# DESCRIPTION :
# Delete (with -f option) the whole cacao startup smf service
#
# PARAMETERS :
# None
#
# RETURN CODE:
# CACAO_CR_ERROR in case of error
# CACAO_CR_SUCCESS in case of sucess
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_delete_startup() {
    full_service_name=${CACAO_SMF_SVC_NAME_PREFIX}"${cacao_unique_name}"
    service_name=${CACAO_SMF_SVC_SERVICE_NAME_PREFIX}"${cacao_unique_name}"

    ret_val=${CACAO_CR_SUCCESS}
    # should be svcs -l call
    if [ -n "`${SVCCFG} -s ${service_name} list  2>/dev/null`" ]; then
        ${SVCCFG} delete -f "${full_service_name}" >/dev/null 2>&1
        if [ ${?} -ne 0 ]
        then
            cacao_print_error_message \
                    "${CACAO_MSG_ERROR_SMF_SERVICE_UNREGISTRATION}" "${service_name}"
            return ${CACAO_CR_ERROR}
        fi
    fi
    return ${CACAO_CR_SUCCESS}
}

#------------------------------------------------------------------------------
# cacao_update_smf_service_manifest
#
# DESCRIPTION :
# Update the SMF service manifest file for a given instance. Update consist in
# updating the instance name of the SMF service and having relocated links.
#
# PARAMETERS :
# $1 the instance name.
#
# RETURN CODE:
# CACAO_CR_SUCCESS
# CACAO_CR_ERROR
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_update_smf_service_manifest() {

    instance_name="${1}"
    etc_instance_dir="${cacao_etc_instances_dir}/${instance_name}"

    # copy and update manifest file
    service_manifest_file="${etc_instance_dir}"/private/${CACAO_SMF_SVC_MANIFEST_FILE_NAME}

    #if destination file exists delete it first.
    if [ -f ${service_manifest_file} ]
    then
	${RM} -f ${service_manifest_file} 2>/dev/null
	if [ $? -ne 0 ]
	then
	    cacao_print_error_message "${FAIL_RM}" "${service_manifest_file}"
	    return ${CACAO_CR_ERROR}
	fi
    fi


    template_manifest_file="${cacao_template_startup_dir}"/${CACAO_SMF_SVC_MANIFEST_FILE_NAME}
    smf_service_name="${CACAO_SMF_SVC_SERVICE_NAME_PREFIX}${cacao_unique_name}"
    cacao_copy_and_update_file ${template_manifest_file} ${service_manifest_file} "_SMF_SERVICE_NAME_#${smf_service_name}" "_CACAO_TOOLS_DIR_#${cacao_tools_dir}" "_SMF_INSTANCE_NAME_#${instance_name}" "_CACAO_BASE_DIR_#${cacao_rt_base_dir}"
    if [ $? -ne 0 ]
    then
        return ${CACAO_CR_ERROR}
    fi
    return ${CACAO_CR_SUCCESS}
}

#------------------------------------------------------------------------------
# cacao_register_instance_smf_service
#
# DESCRIPTION :
# Register inside SMF registry a cacao instance service.
#
# PARAMETERS :
# $1 the instance name.
# $2 the SMF generic service name
#
# RETURN CODE:
# CACAO_CR_SUCCESS
# CACAO_CR_ERROR
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_register_instance_smf_service() {

    instance_name="${1}"
    smf_name="${2}"
    etc_dir="${cacao_etc_instances_dir}/${instance_name}"
    service_manifest_file="${etc_dir}"/private/${CACAO_SMF_SVC_MANIFEST_FILE_NAME}

    # Some cleanup just in case ...
    # No need to check the return value as svccfg never returns an error
    tmp_svcs=`${SVCCFG}  list "TEMP/${smf_name}" 2>/dev/null`
    if [ -n "${tmp_svcs}" ]
    then
	${SVCCFG}  delete "TEMP/${smf_name}" 2>/dev/null
	if [ ${?} -ne 0 ]
	then
	    return ${CACAO_CR_ERROR}
	fi
    fi

    # Register the restarter service into SMF
    # IMPORTANT!!!: svccfg import returns error code 0 when  a former call did not succeed and the following string on STDERR
    #	svccfg: Temporary service "TEMP/application/management/common-agent-container-1" must be deleted before this manifest can be imported.
    #	svccfg: Import of /etc/cacao/instances/default/private/svc_cacao.xml failed.  Progress:
    #	svccfg:   Service "application/management/common-agent-container-1": not reached.
    #	svccfg:     Instance "default": not reached.
    err=`${SVCCFG} import "${service_manifest_file}" 2>&1 | ${WC} -l`
    if [ ${?} -ne 0 -o $err -ne 0 ]
    then
        return ${CACAO_CR_ERROR}
    fi
    return ${CACAO_CR_SUCCESS}
}
#------------------------------------------------------------------------------
# cacao_update_instance_smf_service
#
# DESCRIPTION :
# Update service manifest of SMF registry a cacao instance service.
# For now do only importation
#
# PARAMETERS :
# $1 the instance name.
# $2 the SMF generic service name
#
# RETURN CODE:
# CACAO_CR_SUCCESS
# CACAO_CR_ERROR
#
# OUTPUT:
# None.
#------------------------------------------------------------------------------
cacao_update_instance_smf_service() {
    instance_name="${1}"
    smf_name="${2}"
    etc_dir="${cacao_etc_instances_dir}/${instance_name}"
    service_manifest_file="${etc_dir}"/private/${CACAO_SMF_SVC_MANIFEST_FILE_NAME}

    #check first, avoid service corruption
    ${SVCCFG} validate "${service_manifest_file}" 2>/dev/null
    if [ ${?} -ne 0 ]
    then
	return ${CACAO_CR_ERROR}
    fi

    ${SVCCFG} import "${service_manifest_file}" 2>/dev/null
    if [ ${?} -ne 0 ]
    then
	return ${CACAO_CR_ERROR}
    fi

    return ${CACAO_CR_SUCCESS}
}
#------------------------------------------------------------------------------
# cacao_get_highest_install_id
#
# DESCRIPTION :
# Print the hihest install id found in startup resources
#
# PARAMETERS :
# None
#
# RETURN CODE:
# CACAO_CR_ERROR in case of error
# CACAO_CR_SUCCESS in case of sucess
#
# OUTPUT:
# See description
#------------------------------------------------------------------------------
cacao_get_highest_install_id() {

    _max_id=`${SVCCFG} list ${CACAO_SMF_SVC_SERVICE_NAME_PREFIX}${CACAO_STARTUP_PREFIX}[0-9]* 2>/dev/null | \
        ${AWK}  'BEGIN {higher=0} {last=split($0,res,"-") ; if (res[last] > higher) higher=res[last]} END {print higher}'`

    if [ -z "${_max_id}" ]
    then
	#should not happen, higher is initialized to 0
	_max_id=0
    fi

    ${ECHO} "${_max_id}"
}
