# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
##@require instinc/wobase
##@require instinc/wofunctions
##@require instinc/womonitor

function get_configuration() {
    siteconf_get_instance_data_for_updateWotaskd "$@"
}

function restore_configuration() {
    echo "$1" | wotaskd_update_instances_from_stdin
}

function enable_autoRecover() {
    siteconf_get_instance_data_for_updateWotaskd "$@" | awk '{
if (match($0, /<autoRecover.*<\/autoRecover>/) != 0) {
    $0 = substr($0, 1, RSTART - 1) "<autoRecover type=\"NSString\">YES</autoRecover>" substr($0, RSTART + RLENGTH)
}
print
}' | wotaskd_update_instances_from_stdin
}

function disable_autoRecover() {
    siteconf_get_instance_data_for_updateWotaskd "$@" | awk '{
if (match($0, /<autoRecover.*<\/autoRecover>/) != 0) {
    $0 = substr($0, 1, RSTART - 1) "<autoRecover type=\"NSString\">NO</autoRecover>" substr($0, RSTART + RLENGTH)
}
if (match($0, /<schedulingEnabled.*<\/schedulingEnabled>/) != 0) {
    $0 = substr($0, 1, RSTART - 1) "<schedulingEnabled type=\"NSString\">NO</schedulingEnabled>" substr($0, RSTART + RLENGTH)
}
print
}' | wotaskd_update_instances_from_stdin
}


######################################################################
# Services de haut niveau

function start_instances() {
    # Lancer les instances dont on donne le nom
    eval "$(_getopt_host_and_password)"

    outputfile="$(mktempf)"
    siteconf_get_instance_data_for_commandWotaskd "$@" | wotaskd_command_instances_from_stdin -h "$host" -P "$password" -o "$outputfile" START

    local status=0
    if [ $(grep 'success.*YES' "$outputfile" | wc -l) -ne $# ]; then
        #eerror "Une erreur s'est produite:"
        #cat "$outputfile"
        status=1
    fi
    /bin/rm -f "$outputfile"
    return $status
}

function stop_instances() {
    # ArrĂȘter les instances dont on donne le nom
    eval "$(_getopt_host_and_password)"

    outputfile="$(mktempf)"
    siteconf_get_instance_data_for_commandWotaskd "$@" | wotaskd_command_instances_from_stdin -h "$host" -P "$password" -o "$outputfile" STOP

    local status=0
    if [ $(grep 'success.*YES' "$outputfile" | wc -l ) -ne $(($# + 1)) ]; then
        #eerror "Une erreur s'est produite:"
        #cat "$outputfile"
        status=1
    fi
    /bin/rm -f "$outputfile"
    return $status
}

function quit_instances() {
    # Forcer Ă  quiter les instances dont on donne le nom
    eval "$(_getopt_host_and_password)"

    outputfile="$(mktempf)"
    siteconf_get_instance_data_for_commandWotaskd "$@" | wotaskd_command_instances_from_stdin -h "$host" -P "$password" -o "$outputfile" QUIT

    local status=0
    if [ $(grep 'success.*YES' "$outputfile" | wc -l ) -ne $(($# + 1)) ]; then
        #eerror "Une erreur s'est produite:"
        #cat "$outputfile"
        status=1
    fi
    /bin/rm -f "$outputfile"
    return $status
}