#!/bin/bash

# Destroys jboss instance

function print_help {
    echo "Usage: $0 app-name namespace uuid"

    echo "$0 $@" | logger -p local0.notice -t openshift_origin_jboss_deconfigure
    exit 1
}

while getopts 'd' OPTION
do
    case $OPTION in
        d) set -x
        ;;
        ?) print_help
        ;;
    esac
done


[ $# -eq 3 ] || print_help

# Load the jboss_version and jboss_home settings
. `dirname $0`/jboss.version

source "/etc/openshift/node.conf"
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util

cartridge_type="${jboss_version}"
setup_deconfigure "$1" $2 $3

JBOSS_INSTANCE_DIR=$(get_cartridge_instance_dir "$cartridge_type")

$CART_INFO_DIR/hooks/conceal-port "$application" "$namespace" "$uuid"

M2_DIR="$APP_HOME/.m2" # maven dir
JAVA_PREFS_DIR="$APP_HOME/.java" # Java preferences dir

if [ ! -d "$APP_HOME/app-root" ]
then
    echo "${application}.  Application directory doesn't exist:  $APP_HOME/app-root"
else

    disable_cgroups

    destroy_git_repo $application $uuid

    stop_app

    confirm_pid_gone "${JBOSS_INSTANCE_DIR}/run/jboss.pid"

    rm_cartridge_instance_dir "$cartridge_type"
    
    enable_cgroups
fi

# remove the Maven repository.
runcon -l s0-s0:c0.c1023 rm -rf "$M2_DIR"

# remove the Java preferences dir
runcon -l s0-s0:c0.c1023 rm -rf "$JAVA_PREFS_DIR"


#
# Remove virtualhost definition for apache
#
rm_httpd_proxy $uuid $namespace $application
