#!/bin/bash

. "$MKIT_DIR/include/build.sh"  || die "cannot import build.sh"
. "$MKIT_DIR/include/deploy.sh" || die "cannot import deploy.sh"
. "$MKIT_DIR/include/release.sh" || die "cannot import release.sh"
. "$MKIT_DIR/include/ini.sh"    || die "cannot import ini.sh"

_valid_targets() {
    #
    # List valid routes
    #
    echo build
    echo build_manpages
    echo clean
    echo debstuff
    echo dist
    echo install
    echo release_x
    echo release_y
    echo release_z
    echo rpmstuff
    echo uninstall
    echo vbump_x
    echo vbump_y
    echo vbump_z
}

debug() {
    #
    # Print debug message
    #
    $MKIT_DEBUG || return 0
    echo "MKIT_DEBUG: ${FUNCNAME[1]}()" "$@" >&2
}

debug_var() {
    #
    # Print debug message
    #
    $MKIT_DEBUG || return 0
    local __mkit_debug_var_name__       # variable name to debug
    for __mkit_debug_var_name__ in "$@"; do
        {
            echo -n "MKIT_DEBUG: ${FUNCNAME[1]}():"
            echo -n " $__mkit_debug_var_name__"
            echo -n "='${!__mkit_debug_var_name__}'"
            echo
        } >&2
    done
}

die() {
    #
    # Exit with message and non-zero exit status
    #
    echo "fatal: $*" >&2
    exit 4
}

_compver() {
    #
    # True if version $1 matches our version
    #
    # If our x is 0, check first two fragments, otherwise check just
    # the x.  Fragments must equal.
    #
    local their_ver=$1      # their version
    local our_x             # our X
    local our_y             # our Y
    local their_x           # their X
    local their_y           # their Y
    their_x=${their_ver%%.*}
    their_y=${their_ver##$their_x.}
    their_y=${their_y%%.*}
    our_x=${MKIT_VERSION%%.*}
    our_y=${MKIT_VERSION##$our_x.}
    our_y=${our_y%%.*}
    debug_var MKIT_VERSION our_x our_y their_ver their_x their_y
    test "$their_x" -eq "$our_x" || return 1
    test "$our_x" -eq 0 && {
        test "$their_y" = "$our_y"
        return $?
    }
    return 0
}

_chkiniversion() {
    #
    # Check if ini version is supported
    #
    # Look for "#mkit version=0.0.0" or similar in first or last
    # 3 lines of the file; then check if the version is supported.
    #
    local ver_line      # line declaring version
    local their_ver     # the declared version
    ver_line=$(
        {
            head -3 "$MKIT_INI"
            tac "$MKIT_INI" | tail -3
        } | grep -m 1 -E '^# *mkit +version *= *v?[0-9]+\.[0-9]+\.[0-9]+'
    )
    test -n "$ver_line" \
     || die "version mark ('#mkit version=x.y.z') not found in: $MKIT_INI"
    their_ver="$(tr -d '[:blank:]v' <<<"${ver_line##*=}")"
    _compver "$their_ver" \
     || die "bad mkit.ini version: $their_ver does not match $MKIT_VERSION"
}

mkit_init() {
    #
    # Do basic initialization
    #
    # Check for ini file and some variables
    #
    $MKIT_DRY && MKIT_DEBUG=true
    MKIT_PROJ_PKGNAME=$(ini 1value "project:pkgname")
    test -f "$MKIT_INI" || die "cannot find mkit.ini: $MKIT_INI"
    _chkiniversion
    test -n "$(tr -d '[:space:]' <<<"$MKIT_LOCAL")" \
     || die "MKIT_LOCAL must be non-blank: '$MKIT_LOCAL'"
}

route() {
    #
    # Call correct function based on $1
    #
    if _valid_targets | grep -qwx "^$1"; then
        "$1"
    else
        {
            echo "usage: $(basename "$0") TARGET"
            echo
            echo "valid targets:"
            _valid_targets | sed 's/^/    /'
        } >&2
    fi
}

warn() {
    #
    # Print warning message
    #
    echo "$@" >&2
}