#!/bin/sh -ef

po_domain="alterator-mkve"

alterator_api_version=1
. alterator-sh-functions

# functions {{{

current_name() # {{{
{
    # If in_name is empty or has no sense (in case we deleted machine),
    # then we are running this function for first time (or there were no
    # machines), so we must obtain it directly from mkve
    mkve info "$in_name" >/dev/null 2>&1 ||
        in_name=
    [ -z "$in_name" ] &&
        in_name="$(mkve list | sort | head -1)"

    echo "$in_name"
} # }}}
_mkve() # {{{
{
    local err

    err=$(mkve "$1" "$(current_name)" "$@" 2>&1) ||
    write_error "mkve failed: $err"
} # }}}
_mkve_info() # {{{
{
    mkve info "$(current_name)" "$@" ||
    write_error "mkve failed here"
} # }}}

# _apply {{{
_apply()
{
    case "$in__objects" in
    /)
        case "$in_machine_action" in
            start|stop|destroy) _mkve "$in_machine_action" ;;
            restart)
                _mkve 'stop'
                _mkve 'start'
                ;;
            *) ;;
        esac
        ;;
    autostart)
        [ "$in_autostart" = "#t" ] &&
            _mkve autostart ||
            _mkve autostart --disable
        ;;
    pack)
        _mkve pack --output "$in_output" ${in_lzma:+--no-lzma}
        ;;
    esac
}
# }}}

# function gimme_state {{{
gimme_state()
{
    local ret

    case "$1" in
        1) ret="`_ "the domain is running"`" ;;
        2) ret="`_ "the domain is blocked on resource"`" ;;
        3) ret="`_ "the domain is paused by user"`" ;;
        4) ret="`_ "the domain is being shut down"`" ;;
        5) ret="`_ "the domain is shut off"`" ;;
        6) ret="`_ "the domain is crashed"`" ;;
        *) ret="error" ;;
    esac
    printf %s "$ret"
}
# }}}

# }}}

# action list {{{
action_list()
{
    case "${in__objects}" in
    list_machines)
        for n in $(mkve list | sort); do write_enum_item "$n"; done
        ;;
    esac
}
# }}}
# action read {{{
action_read()
{
    local n="$(current_name)"

    write_string_param 'name' "$n"
    write_string_param 'show_pluck' "$n"

    if [ -n "$n" -a -z "$in_onlyname" ]; then

        mkve info "$(current_name)" | while read line; do
            case "${line%%:*}" in
                hypervisor)
                    write_string_param 'hypervisor' "${line##*: }"
                    write_string_param 'hidden_hypervisor' "${line##*: }"
                    ;;
                id)
                    write_string_param 'id' "${line##*: }"
                    ;;
                memory)
                    write_string_param 'memory' "${line##*: }"
                    ;;
                vcpu)
                    write_string_param 'vcpu' "${line##*: }"
                    ;;
                cpu-time)
                    write_string_param 'cpu_time' "${line##*: }"
                    ;;
                "start on boot")
                    write_bool_param 'autostart' "${line##*: }"
                    ;;
                "vnc display")
                    local display="${line##*: }"
                    [ "$display" != 'none' ] || display="`_ "none"`"
                    write_string_param 'vnc_display' "$display"
                    ;;
                state)
                    local state="${line##state: }"
                    local numerical_state="${state%%: *}"
                    write_string_param 'state' "$(gimme_state "$numerical_state")"
                    write_string_param 'hidden_state' "$numerical_state"
                    ;;
                *) ;;
            esac
        done

    fi
}
# }}}
# action write {{{
action_write()
{
    [ -n "$in_apply" ] && _apply
}
# }}}

on_message()
{
    set | egrep '^in_'; echo '====================================='
    case "$in_action" in
        list)        action_list        ;;
        write)       action_write       ;;
        read)        action_read        ;;
        type)        write_nop          ;;
        *)           write_bool 'false' ;;
    esac
}

message_loop

# vim:fdm=marker et sw=4 ts=4
