prefix aliases for pijul: pj cl https://nest.pijul.com/jorsn/pj
#!/bin/sh

# Compatible with pijul 1.0.0-alpha.38.
# For new versions, update the cmds list.

# if PJ_ASK is set, we ask before executing pijul
# PJ_ALIASES :: [Alias]
# Alias = (=) Name SimpleCmd
# Cmd = SimpleCmd | Alias
# cmds :: [Cmd]
#cmds="add apply branches checkout clone credit dependencies diff dist fork grep help init key log ls mv patch prune pull push record remove revert rollback sign status tag unrecord ${PJ_ALIASES:-}" # pijul 0.12
cmds="add apply archive change channel clone credit diff fork git help init log ls mv pull push record remote remove reset tag unrecord ${PJ_ALIASES:-}"

cmds="$( echo "$cmds" \
       | tr ' ' \\n \
       | sed 's/^\([a-z]\+\)$/=&/g' \
       | sort --field-separator== --key=2 \
       | sed 's/^=//g' | tr \\n ' ' \
       )"

al="$1"
shift

for c in $cmds; do
        case "$c" in "$al"* )
                cands="$cands $c" # candidates
                ;;
        esac
done

candsDeduped="$( echo "$cands" \
               | sed -E -e 's/\b([a-z]+)=([a-z]+)/\2/g' -e 's/ /\n/g' \
               | sed '/^$/d' | uniq \
               )"
i="$(echo "$candsDeduped" | wc -l)"

if [ "$i" = 1 ]; then
        cands="$candsDeduped"
        i=1
fi

if [ -z "$al" ]; then
        exec pijul
elif [ "$i" -gt 1 ]; then
        echo "pj: did you mean one of these"
        for c in $cands; do
                printf '\t%s\n' "$c"
        done
else
        cmd="${cands##*=}" # exec alias command
        if [ "$PJ_ASK" ]; then
                printf 'press return to pijul %s %s ' "$cands" "$*"
                # shellcheck disable=SC2034
                # (`nothing` unused)
                read -r nothing
        fi
        exec pijul "$cmd" "$@"
fi

# vim: tw=0