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

source $APP_HOME/.env/OPENSHIFT_APP_UUID
container_uuid=$3
/usr/bin/oo-delete-endpoints --with-app-uuid $OPENSHIFT_APP_UUID --with-container-uuid $container_uuid --cart-name $cartridge_type

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
#
oo-frontend-disconnect \
    --with-container-uuid "$uuid" \
    --with-container-name "$application" \
    --with-namespace "$namespace" \
    --path "" --path "/swydws"
