#!/bin/bash

# Destroys diy instance

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

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

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


[ $# -eq 3 ] || print_help

cartridge_type="diy-0.1"
source "/etc/openshift/node.conf"
source ${CARTRIDGE_BASE_PATH}/abstract/info/lib/util

setup_deconfigure "$1" $2 $3

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


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

    rm_cartridge_instance_dir "$cartridge_type"
    
    enable_cgroups
fi


#
# Remove virtualhost definition for apache
#
oo-frontend-disconnect \
    --with-container-uuid "$uuid" \
    --with-container-name "$application" \
    --with-namespace "$namespace" \
    --path "" --path "/health"
