# shellcheck shell=dash

xrc:mod:lib     git         \
    meta/info   meta/size   meta/is     meta/all

___x_cmd_git_meta(){
    [ -n "$1" ] || set -- info
    local op="$1"; shift
    case "$op" in
        info|size|\
        id|branch|tag|name|owner|path|\
        root|root_|config|sizetop|\
        url|\
        lastfetch|sincelastfetch|\
        lastmodified|sincelastmodified|\
        isclean|commit|blob)
            "___x_cmd_git_meta_$op" "$@" ;;
        -h|--help)
            ___x_cmd_git___help meta; return ;;
    esac
}

___x_cmd_git_meta_lastmodified(){
    local ___X_CMD_GIT_META_ROOT_
    ___x_cmd_git_meta_root_ "$PWD" || {
        git:error "Cannot locate .git/index"
        return 1
    }

    stat -f "%m" "${___X_CMD_GIT_META_ROOT_}/.git/index"
}

___x_cmd_git_meta_lastfetch(){
    local ___X_CMD_GIT_META_ROOT_
    ___x_cmd_git_meta_root_ "$PWD" || {
        git:error "Cannot locate .git/index"
        return 1
    }

    stat -f "%a" "${___X_CMD_GIT_META_ROOT_}/.git/index"
}

___x_cmd_git_meta_sincelastmodified(){
    printf "%s\n" $(( $(date +%s) - $(___x_cmd_git_meta_lastmodified) ))
}

___x_cmd_git_meta_sincelastfetch(){
    printf "%s\n" $(( $(date +%s) - $(___x_cmd_git_meta_lastfetch) ))
}

___x_cmd_git_meta_info(){
    ___x_cmd_git_meta_size "$@"
}

___x_cmd_git_meta_isclean(){
    local s; s=$(command git status -s | command wc -l)
    [ "$s" -eq 0 ]
}

___x_cmd_git_meta_commit(){
    command git rev-list --header --all \
        | x cawk --no-interactive -m date,csv -f "$___X_CMD_ROOT_MOD/git/lib/meta/awk/commit.awk" |{
            if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
                x csv --app --width "20%,10%,10%,10%,10%,10%,10%,10%,10%,10%" --clear
            else
                ___x_cmd_cmds_cat
            fi
        }
}

___x_cmd_git_meta_blob(){
    command git verify-pack -v .git/objects/pack/*.idx  \
        | ___x_cmd_cmds_awk -f "$___X_CMD_ROOT_MOD/git/lib/meta/awk/blob.awk" | {
            if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
                x csv --app --clear
            else
                ___x_cmd_cmds_cat
            fi
        }
}