#!/usr/bin/env bash

show_error()
{
    qarma --error \
        --title="${APP_WITH_VER}" \
        --text="$1"
}


show_info()
{
    qarma --info \
        --title="${APP_WITH_VER}" \
        --text="$1"
}


ask_question()
{
    qarma --question \
        --title="${APP_WITH_VER}" \
        --text="$1"
}


read_config_value()
{
    local file="$1"
    local key="$2"

    awk -F= -v key="$key" '
        $1 == key {
            sub(/^[^=]*=/, "")
            print
            exit
        }
    ' "$file"
}
write_config_value()
{
    local file="$1"
    local key="$2"
    local value="$3"
    local tmpfile

    tmpfile=$(mktemp)

    awk -F= \
        -v key="$key" \
        -v value="$value" '
        BEGIN {
            found=0
        }

        $1 == key {
            print key "=" value
            found=1
            next
        }

        {
            print
        }

        END {
            if (!found)
                print key "=" value
        }
    ' "$file" > "$tmpfile"

    mv "$tmpfile" "$file"
}
