#!/bin/bash

# Stop all the application processes

# Exit on any errors
set -e

function print_help {
    echo "Usage: $0 app-name namespace uuid"
    echo "Stop a running application and disable it"

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

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

[ $# -eq 3 ] || print_help

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

setup_basic_hook "$1" $2 $3


#
# Stop application
#

# Do not exit on errors past this point; force kill no matter what else happens.
set +e

set_app_state stopped
for i in {1..10}
do
    if killall -s KILL -u "$uuid" 2> /dev/null
    then
        sleep .5
    else
        break
    fi
done
