get_password()
{
    _username="$1"
    _response="$(erl_rpc 'eturnal_ctl' 'get_password' "[\"$_username\"]" || :)"

    case $_response in
        '{ok,'*)
            echo "$_response" | sed 's/{ok, *"\(.*\)"}/\1/'
            exit 0
            ;;
        '{error,'*)
            echo "$_response" | sed 's/{error, *"\(.*\)"}/\1/' >&2
            exit 1
            ;;
        *)
            echo "Unknown error: '$_response'" >&2
            exit 1
            ;;
    esac
}

if [ $# -ge 1 ] && [ "x$1" = 'xhelp' ]
then
    echo "Usage: $REL_NAME password Username"
    echo 'Print the password for the given Username. If multiple secrets are'
    echo 'configured, the password is derived from the first one listed.'
    exit 0
elif [ $# -eq 1 ]
then
    ping_or_exit
    get_password "$1"
else
    echo "Usage: $REL_NAME password Username" >&2
    exit 2
fi
