#! /bin/sh

# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2004-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]"
#


# This script is being used as a sysidconfig(1M)/sys-unconfig(1M)
# application.
#
# This script must be registered using sysidconfig(1M)

DIRNAME=/usr/bin/dirname
AWK=/usr/bin/nawk
GETTEXT=/usr/bin/gettext


BASE=`${DIRNAME} $0`
CACAOADM=${BASE}/../../bin/cacaoadm

TEXTDOMAIN="cacao"
TEXTDOMAINDIR="${BASE}/../../locale"

#perform sanity on package installation
check_installation() {
    if [ ! -x "${CACAOADM}" ]
    then
	eval echo "`${GETTEXT} 'Cannot find executable ${CACAOADM} on the host, unconfiguration aborted.'`"
	return 1
    fi

    return 0
}


get_non_embedded_instance_list() {
    LANG=C ${CACAOADM} list-instances 2>/dev/null | ${AWK} ' $2 !~ /embedded/ {printf "%s ",$1}'
}


unconfigure_cacao_instances() {
    final_rc=0

    # we try to go as far as we can
    # if error is raised for oen instance, go on with the next one

    for instance in `get_non_embedded_instance_list`
    do
	${CACAOADM} stop --instance ${instance}
	if [ $? -ne 0 ]
        then
	    echo "`${GETTEXT} 'Cannot stop ${instance}, security keys won't be removed.'`"
	    final_rc=1
	    continue
        fi

	${CACAOADM} delete-keys --instance ${instance}
	if [ $? -ne 0 ]
	then
	    echo "`${GETTEXT} 'Cannot remove security keys of ${instance} instance.'`"
	    final_rc=1
	fi
    done
    
    return $final_rc

}


case $1 in 
'-c')
        # nothing to do here.
	;;

'-u')
        check_installation
	if [ $? -eq 0 ]
        then
            unconfigure_cacao_instances
	fi
        ;;
*)
        echo "Usage: $0 { -c | -u }"
        exit 1
        ;;
esac    

exit $?
