aboutsummaryrefslogtreecommitdiffstats
path: root/ariketa.sh
blob: 25df78f3a5d4c7897b01b71c7c59096800933e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash

_ariketa.dir () {
    local _ariketa_filename _ariketa_dir
    _ariketa_filename="${BASH_SOURCE[$(( ${#BASH_SOURCE[@]} - 1 ))]}"
    [[ "$_ariketa_filename" == */* ]] && _ariketa_dir="${_ariketa_filename%/*}"

    # get an absolute path to ariketa in case we need it
    _ariketa_dir=$(cd "${_ariketa_dir:-$PWD}" && printf "%s" "$PWD")
    printf "%s" "$_ariketa_dir"
}

_ariketa.help () {
    >&2 printf "%s\n" "$(<${ARIKETA_DIR:-.}${ARIKETA_DIR+/}USAGE)"
    return 1
}

_sourced? () {
    [[ ${FUNCNAME[$(( ${#FUNCNAME[@]} - 1 ))]} == "source" ]]
}

ARIKETA_DIR=$(_ariketa.dir)

_sourced? || {
    _ariketa.help
    exit $?
}

until [[ -z "$1" ]]; do
    case "$1" in
        -h|--help)
            _ariketa.help
            return $?;;
        -a|--array)
            # copy by indirection to set of discrete elements in named array
            _ariketa_array_elements="${2:?Missing name parameter for '-a'}[@]"
            _examples=( "${!_ariketa_array_elements}" )
            shift 2;;
        *)
            _ariketa_examples_file="${1}"
            [[ -r "${_ariketa_examples_file}" ]] || {
                _ariketa.help
                return $?
            }
            shift;;
    esac
done

# load examples array from file if array not passed / copied from args
[[ -z "${_ariketa_array_elements}" ]] && {
    source "${_ariketa_examples_file:-${ARIKETA_DIR:?}/config/examples.sh}"
}
unset _ariketa_array_elements

# if examples array has no content, bail with USAGE text
(( ${#_examples[@]} )) || {
    >&2 printf "**  %s\n" \
        "" ""\
        "$(declare -p _examples)"\
        "_examples array needs content -- see 'configuration' below"\
        "" ""
    _ariketa.help
    return $?
}

# preserve multiline commands as such in history
HISTCONTROL=ignoredup
shopt -s lithist

# load presentation prompt
source "${ARIKETA_DIR:?}/lib/prompt.sh"

# load misc functions
source "${ARIKETA_DIR:?}/lib/functions.sh"

# set up bash macros to page through examples
source "${ARIKETA_DIR:?}/lib/bindings.sh"

# unset example index/directionality markers
unset _I _D