diff options
Diffstat (limited to 'bin/rfc')
-rwxr-xr-x | bin/rfc | 117 |
1 files changed, 117 insertions, 0 deletions
@@ -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" |