#!/bin/bash -ef

po_domain="alterator-mkve"

alterator_api_version=1
. alterator-sh-functions

# action write {{{
stopped()
{
    local state

    if ! state="$(mkve info "$in_machine" --state | cut -d: -f1)"; then
        write_error "`_ "mkve failed"`"
        return 1
    fi

    [ "$state" = "5" ]
}

set_password()
{
    if ! stopped "$in_machine"; then
        write_error "`_ "you must stop machine at first"`"
        return
    fi

    local mnt
    if ! mnt="$(mktemp -d 2>&1)"; then
        write_error "`_ "mktemp failed:"` $mnt"
        return
    fi

    local err img="/var/lib/mkve/machines/$in_machine/root.img"
    if ! err=$(mount -o loop "$img" "$mnt" 2>&1); then
        write_error "`_ "can't mount"` $img to $mnt: $err"
        return
    fi

    if ! err=$(printf "root:%s\n" "$in_password" | chroot "$mnt" chpasswd 2>&1); then
        write_error "`_ "chpasswd failed: "` $err"
        umount "$mnt"
        rmdir "$mnt"
        return
    fi

    if ! err=$(umount "$mnt" 2>&1); then
        write_error "`_ "can't umount"` $mnt: $err"
        rmdir "$mnt"
        return
    fi

    if ! err=$(rmdir "$mnt" 2>&1); then
        write_error "`_ "can't remove dir"` $mnt: $err"
        return
    fi

    write_error "`_ "password successfully set"`"
}

action_write()
{
    case ${in__objects} in
        password) set_password ;;
        reset) write_error "`_ "Reset to defaults values"`" ;;
    esac
}
# }}}

on_message()
{
    set | egrep '^in_'; echo

    case "$in_action" in
        constraints) write_nop          ;;
        list)        write_nop          ;;
        write)       action_write       ;;
        read)        write_nop          ;;
        type)        write_nop          ;;
        *)           write_bool 'false' ;;
    esac
}

message_loop

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