#!/bin/bash

die () {
    local ret=$1;
    local warning="$2"; shift 2
    warn "${warning:-Unknown error}" "$@"
    quit ${ret:-1}
}

warn () {
    local message="$1"; shift
    printf "$message" "$@" 1>&2
}

quit () {
    exit ${1:-0}
}

msg  () {
    local message="$1"
    shift
    printf "$message" $@
}

vmsg () {
    [ $VERBOSE -ge 1 ] && msg "$@"
}

in_array() {
    [ $# -lt 2 ] && return 1
    needle=$1
    shift
    for item
    do
        [ "$item" = "$needle" ] && return 0
    done
    return 1
}

load_config() {
    local conffile="$1"

    if [[ -r $CONFIG_DIR/$conffile ]]; then
        source "$CONFIG_DIR/$conffile"
    fi
    if [[ -r ${XDG_CONFIG_HOME}/pkgtools/$conffile ]]; then
        source "${XDG_CONFIG_HOME}/pkgtools/$conffile"
    elif [[ -r ${HOME}/.pkgtools/$conffile ]]; then
        source "${HOME}/.pkgtools/$conffile"
        warn 'Your %s is located in ~/.pkgtools. Pkgtools now uses $XDG_CONFIG_HOME.\n' "$conffile"
        warn 'Make sure $XDG_CONFIG_DIR is set, then mv ~/.pkgtools into it\n'
        warn '    mv ~/.pkgtools "$XDG_CONFIG_HOME/pkgtools"\n\n'
    fi
}

# vim: set ts=4 sw=4 et:
