#!/bin/bash
# -*- coding: utf-8; mode: sh; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=sh:et:sw=4:ts=4:sts=4

# Note:
# This script is sourced by the mpbb wrapper script.
# Do not execute this directly!

help-usage() {
    cat <<EOF
usage: $prog help [<command>]

Show usage information for \`$prog' or a specific \`$prog' subcommand.

Run \`$prog help' for global options and a list of other subcommands.
EOF
}

help() {
    local cols helpfunc prog
    cols=$(tput cols) || cols=80
    # The main program should define "mpbb-usage", which is called if
    # "mpbb help" is invoked without any arguments.
    helpfunc=${1-mpbb}-usage
    prog=$(basename "$0")
    readonly cols helpfunc prog

    # This loop exits with 0 if usages contains helpfunc or is empty.
    for usage in "${usages[@]}"; do
        [[ $usage == "$helpfunc" ]] && break
    done
    if (( $? != 0 || ${#usages[@]} == 0 )); then
        err "No help available for subcommand \`${helpfunc%-usage}'"
        return 2
    fi

    "$helpfunc" | fmt -w $((cols - 8)) >&2
}