aboutsummaryrefslogtreecommitdiff
path: root/bin/rfc
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2022-05-12 12:01:54 -0300
committerEuAndreh <eu@euandre.org>2022-05-12 12:01:54 -0300
commit90eaebabcaaea74237f34cf05709625345f276cc (patch)
tree349e7609d20ecfb6567652a7e28595cec9647eb0 /bin/rfc
parent.usr/etc/i3/config: WIP setup extra bindings (diff)
downloaddotfiles-90eaebabcaaea74237f34cf05709625345f276cc.tar.gz
dotfiles-90eaebabcaaea74237f34cf05709625345f276cc.tar.xz
Move Git repository into ~/.usr/.git/
Diffstat (limited to 'bin/rfc')
-rwxr-xr-xbin/rfc117
1 files changed, 117 insertions, 0 deletions
diff --git a/bin/rfc b/bin/rfc
new file mode 100755
index 0000000..a483595
--- /dev/null
+++ b/bin/rfc
@@ -0,0 +1,117 @@
+#!/bin/sh
+set -eu
+
+TARBALL_URL='https://www.rfc-editor.org/in-notes/tar/RFC-all.tar.gz'
+
+usage() {
+ cat <<-'EOF'
+ Usage:
+ rfc [-w] RFC_NUMBER
+ rfc -h
+ EOF
+}
+
+help() {
+ cat <<-'EOF'
+
+ Options:
+ -w Show the path to the RFC file instead of displaying
+ its contents.
+ -h, --help show this message
+
+ Lookup the given RFC
+ in $XDG_DATA_HOME/doc/rfc/ (defaults to ~/.local/share),
+ and feed it into the $PAGER, akin to doing:
+
+ $ $PAGER $XDG_DATA_HOME/doc/rfc/rfc$RFC_NUMBER.txt
+
+ If the $XDG_DATA_HOME/doc/rfc/ directory doesn't exist, it tries to
+ create it by downloading the latest RFC tarball [0] and placing all .txt
+ files there.
+ EOF
+
+ printf '\n[0]: %s\n' "$TARBALL_URL"
+}
+
+for flag in "$@"; do
+ case "$flag" in
+ --)
+ break
+ ;;
+ --help)
+ usage
+ help
+ exit
+ ;;
+ *)
+ ;;
+ esac
+done
+
+while getopts 'wh' flag; do
+ case "$flag" in
+ w)
+ WHERE=true
+ ;;
+ h)
+ usage
+ help
+ exit
+ ;;
+ *)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+RFC_NUMBER="${1:-}"
+if [ -z "$RFC_NUMBER" ]; then
+ echo 'Missing argument RFC_NUMBER' >&2
+ usage >&2
+ exit 2
+fi
+
+D="${XDG_DATA_HOME:-$HOME/.local/share}/doc/rfc"
+if [ ! -e "$D" ]; then
+ printf 'RFC directory does not exist:\n\t%s/\n\n' "$D"
+ printf 'Do you want to download the files to create it? [Y/n] '
+ read -r yesno
+ if [ "$yesno" != 'n' ] && [ "$yesno" != 'N' ]; then
+ CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/rfc-cli"
+ mkdir -p "$CACHE_DIR"
+ wget -cO "$CACHE_DIR"/RFC-all.tar.gz "$TARBALL_URL"
+ rm -rf "$CACHE_DIR/tmp"
+ mkdir -p "$CACHE_DIR/tmp"
+ tar \
+ -C "$CACHE_DIR/tmp" \
+ -xvf "$CACHE_DIR"/RFC-all.tar.gz \
+ --wildcards \
+ 'rfc*.txt'
+ mkdir -p "$(dirname "$D")"
+ mv "$CACHE_DIR/tmp" "$D"
+ fi
+fi
+
+F="$D/rfc${RFC_NUMBER}.txt"
+if [ ! -e "$F" ]; then
+ printf 'Given RFC_NUMBER "%s" does not exist at:\n%s\n' \
+ "$RFC_NUMBER" "$F" >&2
+ exit 2
+fi
+
+if [ "${WHERE:-}" = true ]; then
+ printf '%s\n' "$F"
+ exit
+fi
+
+view() {
+ if [ -t 1 ]; then
+ ${PAGER:-cat}
+ else
+ cat
+ fi
+}
+
+view < "$F"