#!/bin/sh
ctl=""

if [ -x "/opt/yandex/instancectl/bin/instancectl" ]; then
    ctl="/opt/yandex/instancectl/bin/instancectl"
fi

if [ -z "${ctl}" ] && [ -e "instancectl" ]; then
    ctl="./instancectl"
fi

if [ -z "${ctl}" ] && [ -e "loop-httpsearch" ]; then
    ctl="./loop-httpsearch"
fi

if [ -z "${ctl}" ]; then
    echo "Error: can not locate instancectl" >&2
    exit 30 # Exit code UNKNOWN, check ISS docs
fi

conf=""
if [ -e "instancectl.conf" ]; then
    conf="instancectl.conf"
fi

if [ -z "${conf}" ] && [ -e "loop.conf" ]; then
    conf="loop.conf"
fi

if [ -z "${conf}" ]; then
    case "${tags}" in
        *use_hq_spec*)
            exec "${ctl}" uninstall
            ;;
        *)
            echo "Error: can not locate config file" >&2
            exit 30 # Exit code UNKNOWN, check ISS docs
            ;;
    esac
else
    exec "${ctl}" uninstall -c "${conf}"
fi
