#! /bin/bash

# Copyright (C) 2011-2024 Erik Auerswald <auerswal@unix-ag.uni-kl.de>

# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

# BUGS:
# - draft-rep-wg-topic-00.txt is not formatted well by rfc-reader
#   - the text version lacks the '\f' characters needed for filling the pages
# - RFC 603 is not formatted well by rfc-reader
#   - first and second line are too long (79 characters)
#   - 7th line is too long (76 characters)
#   - 10th line is too long (75 characters)
#   -> workaround: env C=81 rfc-reader 602
# - RFC 698 is not formatted well by rfc-reader
#   - first line is too long (79 characters)
#   -> workaround: env C=81 rfc-reader 698
# - RFC 713 is not formatted well by rfc-reader
#   - first two lines are too long (81 characters)
#   - pages of the RFC are too long
#   -> workaround: env L=61 C=81 rfc-reader 713
# - RFC 734 is not formatted well by rfc-reader
#   - lines are too long and thus truncated
#   -> workaround: env C=84 rfc-reader 734
# - RFC 827 is not formatted well by rfc-reader
#   - some lines are too long
#   -> workaround: env C=82 rfc-reader 827
# - RFC 888 is not formatted well by rfc-reader
#   - some lines are too long
#   -> workaround: env C=79 rfc-reader 888
# - RFC 911 is not formatted well by rfc-reader
#   - lines are too long and thus truncated
#   -> workaround: env C=83 rfc-reader 911
# - RFC 994 is not formatted well by rfc-reader
#   - some lines are too long
#   -> workaround: env C=84 rfc-reader 994
# - RFC 995 is not formatted well by rfc-reader
#   - lines of the RFC are too long
#   -> workaround: env C=79 rfc-reader 995
# - RFC 1142 is not paginated, but contains lots of stray Form Feed characters,
#   thus rfc-reader cannot format it well
# - RFC 1347 is not formatted well by rfc-reader
#   - lines of the RFC are too long
#   - pages of the RFC are too long
#   -> workaround: env L=64 C=81 XT=xterm XTFS=4 rfc-reader 1347
# - RFC 1454 is not formatted well by rfc-reader
#   - dome lines are too long
#   -> workaround: env C=80 rfc-reader 1454
# - RFC 2329 is not formatted well by rfc-reader
#   - too long lines
#   -> workaround: env C=82 rfc-reader 2329
# - XTerm / GNOME Terminal font size heuristic breaks on high DPI displays
# - XTerm and GNOME Terminal (and/or the window manager) do not honor size
#   requests if the size does not fit inside the $DISPLAY

set -u

B=$(basename "$0")

# e.g. openSUSE and Ubuntu provide RFCs as a package
RFCDIRS='/usr/share/doc/rfc /usr/share/doc/RFC/links'
# use a local cache if available
# a relative XDG_CACHE_HOME must be ignored
RFCCACHE=''
xdg_cache="${XDG_CACHE_HOME:-$HOME/.cache}/$B"
test "${xdg_cache:0:1}" = '/' || xdg_cache="$HOME/.cache/$B"
for DIR in "$HOME/.$B/cache" "$xdg_cache"; do
  test \( -d "$DIR" \) -a \( -r "$DIR" \) &&
    RFCDIRS="$DIR $RFCDIRS" &&
    test -w "$DIR" && RFCCACHE="$DIR"
done

copyright()
{
  echo 'Copyright (C) 2011-2024 Erik Auerswald <auerswal@unix-ag.uni-kl.de>'
}

version()
{
  local V="0.67"
  printf -- "%s\n" "$B version $V"
}

license()
{
  cat <<EOL
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.  This file is offered as-is,
without any warranty.
EOL
}

usage()
{
  printf -- '%s\n' "Usage: $B [OPTS] [{rfc|bcp|fyi|ien|std}][{-|.| }]NUMBER[.txt]"
  printf -- '%s\n' "       $B [OPTS] {I-D.|draft-}DRAFT_NAME[-NUMBER][.txt]"
  printf -- '%s\n' "       $B [OPTS]"
  echo
  echo 'Download specified RFC or I-D (unless found locally), and show it'
  echo 'in a pager inside a new terminal window of appropriate size.'
  # shellcheck disable=SC2016
  echo 'Display in current terminal if $DISPLAY is empty or unset.'
  echo
  echo 'Options:'
  printf -- '  -h   print this help and exit\n'
  printf -- '  -V   print version information and exit\n'
  printf -- '  -L   print license and exit\n'
  # shellcheck disable=SC2016
  printf -- '  -t   display in current terminal (irrespective of $DISPLAY)\n'
  echo
  echo 'Examples:'
  printf -- '%s\n' "  $B                       # print version, copyright, help"
  printf -- '%s\n' "  $B 1                     # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc1                  # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc 1                 # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc-1                 # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc.1                 # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc1.txt              # downloads and displays RFC 1"
  printf -- '%s\n' "  $B rfc0001.txt           # downloads and displays RFC 1"
  printf -- '%s\n' "  $B bcp78                 # downloads and displays BCP 78"
  printf -- '%s\n' "  $B fyi3                  # downloads and displays FYI 3"
  printf -- '%s\n' "  $B ien137                # downloads and displays IEN 137"
  printf -- '%s\n' "  $B std1                  # downloads and displays STD 1"
  printf -- '%s\n' "  $B draft-rep-wg-topic-00 # downloads & displays REP I-D"
  printf -- '%s\n' "  $B I-D.rep-wg-topic-00   # downloads & displays REP I-D"
  printf -- '%s\n' "  $B draft-rep-wg-topic    # downloads & displays REP I-D"
  printf -- '%s\n' "  $B I-D.rep-wg-topic      # downloads & displays REP I-D"
  echo
  echo 'Looking for RFCs in the following local directories:'
  for DIR in $RFCDIRS; do
    printf -- '  %s\n' "$DIR"
  done
  test -n "$RFCCACHE" &&
    printf -- '\nUsing "%s" as download cache.\n' "$RFCCACHE" &&
    echo '(BCP and STD files in the download cache are refreshed if possible.)'
}

# parse options
while getopts 'hVLt' OPT; do
 case "$OPT" in
   'h') version; copyright; license; echo; usage; exit 0;;
   'V') version; copyright; exit 0;;
   'L') version; copyright; license; exit 0;;
   't') unset DISPLAY;;
   '?') exit 1;;
   *) printf -- '%s: ERROR: getopts() failure\n' "$B"; exit 1;;
 esac
done
shift $((OPTIND - 1))

# exactly one or two arguments must be given
test \( $# -eq 1 \) -o \( $# -eq 2 \) ||
  { version; copyright; license; echo; usage; exit 1; }

# check some prerequisites:
# wget is needed to download files
HAVE_WGET=0
command -v wget >/dev/null 2>&1 && HAVE_WGET=1
# local directories with RFC (and similar) files can obviate donwloads
HAVE_LOCAL_DIRS=0
for DIR in $RFCDIRS; do
  test -d "$DIR" && test -r "$DIR" && test -x "$DIR" && HAVE_LOCAL_DIRS=1 &&
    break
done
# no wget is worth a warning
test "$HAVE_WGET" -eq 0 &&
  printf -- '%s: WARN: no wget found, cannot download files\n' "$B"
# no local RFC directory is rather common and not worth a warning
# neither wget nor local RFCs means rfc-reader cannot work
test "$HAVE_WGET" -eq 0 && "$HAVE_LOCAL_DIRS" -eq 0 && {
  printf -- '%s: ERROR: neither wget nor local file repo available\n' "$B"
  exit 1
}

# parse arguments into type and number of document and build file name
if test $# -eq 2; then
  DOC="$1$2"
else
  DOC="$1"
fi
# remove optional extension .txt
DOC="$(printf -- '%s' "$DOC" | sed s/\.[Tt][Xx][Tt]$//)"
# shellcheck disable=SC2003
expr " $DOC" : '  *[0-9][0-9]* *$' > /dev/null && DOC="rfc$DOC"
# determine document type
# shellcheck disable=SC2003
DT="$(expr " $DOC" : ' \([Dd][Rr][Aa][Ff][Tt]\)' \
      | tr '[:upper:]' '[:lower:]')"
# shellcheck disable=SC2003
test -z "$DT" &&
  DT="$(expr " $DOC" : ' \([Ii]-[Dd]\)\.' \
        | tr '[:lower:]' '[:upper:]')"
# shellcheck disable=SC2003
test -z "$DT" &&
  DT="$(expr " $DOC" : ' \([A-Za-z][A-Za-z]*\)' \
        | tr '[:upper:]' '[:lower:]')"
# determine document number (or name for I-Ds)
# shellcheck disable=SC2003
case "$DT" in
  draft|I-D)
    DOC="$(printf -- '%s' "$DOC" | sed 's/^[Ii]-[Dd]\./draft-/')"
    DN="$(printf -- '%s' "$DOC" | tr '[:upper:]' '[:lower:]' \
          | sed 's/^[[:space:]]\+//;s/\.txt[[:space:]]*$//;s/^i-d\./draft-/')"
    expr " $DN" : ' .*-[0-9][0-9][0-9]*$' > /dev/null || DN='';;
  *) DN="$(expr " $DOC" : ' [-A-Za-z .]*0*\([1-9][0-9]*\)')";;
esac
# determine latest I-D version number if I-D name w/o number was provided
IDLST='all_id.txt'
IDLSTBS='https://www.ietf.org/id'
test -z "$DN" && { test "$DT" = 'draft' || test "$DT" = 'I-D'; } &&
  printf -- '%s: determining latest version of %s ... ' "$B" "$DOC" &&
  if test \( -n "$RFCCACHE" \) -a \( -d "$RFCCACHE" \) -a \( -w "$RFCCACHE" \)
  then
    printf -- 'refreshing I-D list %s/%s ... ' "$RFCCACHE" "$IDLST"
    if wget -q -o/dev/null -N -P "$RFCCACHE" -- "${IDLSTBS}/${IDLST}"; then
      echo 'done'
    else
      echo 'failed, using cached I-D list if available'
    fi
    test -r "${RFCCACHE}/${IDLST}" ||
      { printf -- '%s: ERROR: no I-D list available\n' "$B"; exit 1; }
    DN="$(grep -- "^${DOC}-[0-9][0-9]" "${RFCCACHE}/${IDLST}"|awk '{print $1}')"
  else
    DN="$(wget -q -o/dev/null -O- -- "${IDLSTBS}/${IDLST}" | grep -- "^${DOC}" \
          | awk '{print $1}')"
  fi
# determine download base URL for document
test -z "$DT" && {
  printf -- '%s: ERROR: could not determine document type\n' "$B"
  usage
  exit 1
}
test -z "$DN" && {
  printf --  '%s: ERROR: could not determine document number or name\n' "$B"
  usage
  exit 1
}
R="${DT}${DN}.txt"
case "$DT" in
  rfc) DB='https://www.rfc-editor.org/rfc/';;
  bcp|fyi|ien|std) DB="https://www.rfc-editor.org/rfc/${DT}/";;
  draft|I-D) DB='https://www.ietf.org/archive/id/'; R="${DN}.txt";;
  *) printf -- '%s: ERROR: unknown document type "%s"\n' "$B" "$DT"
     usage
     exit 1
     ;;
esac

# create temporary files for original and formatted RFC(-like) text
F=$(mktemp -t rfc-reader.$$.XXXXXXXXXX)
test -e "$F" || {
  printf -- '%s: ERROR: could not create temp. file for unformatted RFC\n' "$B"
  exit 1
}
trap 'rm -rf "$F"' 0
FMT=$(mktemp -t rfc-reader.$$.XXXXXXXXXX)
test -e "$F" || {
  printf -- '%s: ERROR: could not create temp. file for formatted RFC\n' "$B"
  exit 1
}
trap 'rm -rf "$F" "$FMT"' 0

# obtain RFC(-like) text
printf -- '%s: looking for %s ... ' "$B" "$R"
# shellcheck disable=SC2086
LR=$(find $RFCDIRS -name "${R}*" 2>/dev/null | head -n1)
LRZ=''
# search local directories (a local copy may be compressed)
test -n "$LR" && printf -- '%s' "found $LR ... " &&
  LRZ=$(file -L "$LR" | sed -n 's/^.* \([^ ]*\) compressed .*$/\1/p')
# BCP contents change with time, so refresh a cached copy if possible
test "$DT" = 'bcp' && test -n "$LR" && test -z "$LRZ" && test -n "$RFCCACHE" &&
  test "$(dirname "$LR")" = "$RFCCACHE" && {
  printf -- 'refreshing BCP ... '
  wget -q -o/dev/null -N -P"$RFCCACHE" -- "${DB}${R}" ||
    printf -- 'ERROR, using cached BCP version ... '
}
# STD contents change with time, so refresh a cached copy if possible
test "$DT" = 'std' && test -n "$LR" && test -z "$LRZ" && test -n "$RFCCACHE" &&
  test "$(dirname "$LR")" = "$RFCCACHE" && {
  printf -- 'refreshing STD ... '
  wget -q -o/dev/null -N -P"$RFCCACHE" -- "${DB}${R}" ||
    printf -- 'ERROR, using cached STD version ... '
}
# decompress a compressed local copy
test -n "$LR" &&
  case "$LRZ" in
    '') cp "$LR" "$F";;
    'gzip') printf -- '%s' 'decompressing ... '; zcat "$LR" > "$F";;
    'bzip2') printf -- '%s' 'decompressing ... '; bzcat "$LR" > "$F";;
    'XZ') printf -- '%s' 'decompressing ... '; xzcat "$LR" > "$F";;
    *) printf -- 'WARN: ignoring "%s" with unknown compression "%s" ... ' \
                 "$LR" "$LRZ";;
  esac
# if no usable local copy was found, then download document
test -s "$F" ||
  { printf -- 'downloading "%s" from "%s" ... ' "$R" "${DB}${R}" &&
    wget -q -o/dev/null -O"$F" -- "${DB}${R}";
    # optionally cache downloaded document
    test \( -s "$F" \) -a \( -n "$RFCCACHE" \) -a \( -d "$RFCCACHE" \) -a \
         \( -w "$RFCCACHE" \) &&
      { printf -- 'copying %s to cache %s ... ' "$R" "$RFCCACHE";
        cp -- "$F" "${RFCCACHE}/$R"; }; }
test -s "$F" || { printf -- "ERROR\n"; exit 1; }
printf -- 'done\n'

# format RFC(-like) text for viewing in a terminal window
C=${C:=76} # 72 chars max. per line (RFC 1 has line length <= 77) + 4 blanks
L=${L:=59} # 58 lines max. per page + separator line in, e.g., STD 1

# use $PAGER or less
P=${PAGER:=less}
test -x "$(command -v "$P")" || P='less'  # de-facto GNU/Linux pager
test -x "$(command -v "$P")" || P='most'  # nice alternative pager
test -x "$(command -v "$P")" || P='pager' # pager from Debian's alternatives
test -x "$(command -v "$P")" || P='more'  # POSIX and BSD compatibility
test -x "$(command -v "$P")" || P='page'  # alternate name for 'more -p'
test -x "$(command -v "$P")" || P='pg'    # System V compatibility
test -x "$(command -v "$P")" || {
  printf -- '%s: ERROR: no pager found\n' "$B"
  exit 1
}
PO=${PO:=''}
# rfc-reader has default options for less and most only
test "$P" = "less" && test -z "$PO" && PO="-M -S"
test "$P" = "most" && test -z "$PO" && PO="+s"
# most uses two status lines instead of one
PSL='1'
PA='0'
test "$P" = 'most' && PSL='2' PA='1'

# check if RFC / I-D is paginated (i.e., contains Form Feeds)
PAGINATED=1
awk '/\014/{exit 1}' "$F" && {
  # do not warn about current RFCs w/o pagination
  test "$DT" = 'rfc' && test "$DN" -ge 8650 || \
    printf -- '%s: WARN: no Form Feed characters in RFC/I-D "%s"\n' "$B" "$R"
  PAGINATED=0
  # adjust pager options to unpaginated text
  case "$P" in
    more|less) PO="$PO -s -c";;
    most) PO="$PO -s";;
  esac
}

# find a usable terminal emulator program or disable X Window System use
DISPLAY=${DISPLAY:=}
test -n "$DISPLAY" && {
  # determine window sizes
  Y=$(xrandr | sed -n '/\*/s/^[^x]*x *\([0-9]*\)[^0-9].*$/\1/p' | head -n1)
  test -z "$Y" && Y=0
  if test "$Y" -ge 1100; then
    XTFS=${XTFS:=5} #XTerm font size: 5 is 'large'
    GTZ='1.0'
  else
    XTFS=${XTFS:=4} #XTerm font size: 4 is 'medium'
    GTZ='0.9'
  fi

  # find a terminal program to use
  XT=${XT:=''}
  test -z "$XT" && test -n "${WINDOWID:-}" &&
    XT=$(xprop -id "$WINDOWID" WM_CLASS | sed -n 's/^[^"]*"\([^"]*\)".*$/\1/p')
  test -x "$(command -v "$XT")" || XT=""
  test -z "$XT" && { env | grep -F gnome-terminal >/dev/null 2>&1; } &&
    XT='gnome-terminal'
  test -x "$(command -v "$XT")" || XT=""
  test -z "$XT" && XT='xterm'
  found=''
  while test -z "$found"; do
    case "$XT" in
      xterm|uxterm)
        # XTerm
        # (tested on Debian/Lenny, Debian/Sid, Ubuntu 12.04 LTS, openSUSE 12.3)
        XO='+sb -xrm'
        XF="xterm*vt100.initialFont: ${XTFS}"
        G="-g ${C}x$((L+PSL))"
        T='-T '
        E="-e $P $PO"
        found='xterm'
        ;;
      gnome-terminal)
        # GNOME Terminal (tested on Debian/Lenny and Ubuntu 12.04 LTS)
        XO="--hide-menubar --disable-factory --zoom ${GTZ}"
        XF=''
        G="--geometry ${C}x$((L+PSL))"
        T='-t '
        E="-x $P $PO"
        found='gnome-terminal'
        ;;
      konsole)
        # Konsole (tested on openSUSE 12.3 with KDE 4.11.3)
        # XO="--hide-menubar --hide-tabbar -p ScrollBarPosition=2 --nofork"
        # XF=''
        # G="-p TerminalColumns=${C} -p TerminalRows=${R}"
        # T="-p tabtitle="
        # E="-e $P $PO"
        printf -- \
          '%s: WARN: found "%s", but cannot set geometry, trying "xterm"\n' \
          "$B" "$XT"
        # found='konsole'
        XT='xterm'
        ;;
      *)
        printf -- \
          '%s: WARN: found unsupported terminal "%s", trying "xterm"\n' \
          "$B" "$XT"
        XT='xterm'
        ;;
    esac
  done
  test -x "$(command -v "$XT")" || {
    printf -- \
      '%s: WARN: no usable terminal program found, using current terminal\n' \
      "$B"
    DISPLAY=''
  }
}

# use current terminal if X Window System is not usable
test -z "$DISPLAY" && test "$PAGINATED" -eq '1' && {
  # check terminal size
  TL="${TL:=${LINES:=}}"
  test -z "$TL" && TL="$(stty size 2>/dev/null | cut -d' ' -f1)"
  test -z "$TL" && TL="$(tput lines 2>/dev/null)"
  test -z "$TL" && TL="$(resize -u 2>/dev/null \
                         | sed -n '/LINES/s/^.*=\([0-9]\+\).*$/\1/p')"
  test -z "$TL" &&
    { printf -- '%s: ERROR: cannot determine terminal height\n' "$B"; exit 1; }
  test "$TL" -lt "$((L+PSL))" && {
    printf -- '%s: ERROR: terminal too short (%d lines, but %d needed)\n' \
               "$B" "$TL" "$((L+PSL))"
    exit 1
  }
  TC="${TC:=${COLUMNS:=}}"
  test -z "$TC" && TC="$(stty size 2>/dev/null | cut -d' ' -f2)"
  test -z "$TC" && TC="$(tput cols 2>/dev/null)"
  test -z "$TC" && TC="$(resize -u 2>/dev/null \
                         | sed -n '/COLUMNS/s/^.*=\([0-9]\+\).*$/\1/p')"
  test -z "$TC" &&
    { printf -- '%s: ERROR: cannot determine terminal width\n' "$B"; exit 1; }
  test "$TC" -lt "$C" && {
    printf -- '%s: ERROR: terminal too narrow (%d columns, but %d needed)\n' \
              "$B" "$TC" "$C"
    exit 1
  }
  # format RFC(-like) text for current terminal's height
  L="$((TL-PSL))"
}

# reformat RFC(-like) text for chosen terminal which has L lines for viewing
# text content (including SL status line(s) used by the pager)
# shellcheck disable=SC2026
if test "$PAGINATED" -eq 1; then
  awk -vn_lines="$L" -vpg_adds="$PA" \
    '!/\f/{line++; print};
     /\f/{for (i=line; i<n_lines; i++) print ""; line=0}
     END {if (line || pg_adds){for (i=line; i<n_lines-pg_adds; i++) print ""}}'\
    "$F" | sed '1s/^'$'\uFEFF''//;/^$/!s/^/  /' > "$FMT"
else
  sed '1s/^'$'\uFEFF''//;/^$/!s/^/  /' "$F" \
  | awk '!text && /^[[:space:]]*$/{next}; !text{text=1}; {print}' > "$FMT"
fi

# open suitable terminal window if X-Windows is available
test -n "$DISPLAY" && {
  # open terminal window with a pager displaying the RFC(-like) text
  # shellcheck disable=SC2086
  "$XT" $XO "$XF" $G $T"$B - $(basename "$R")" $E "$FMT"

  # gnome-terminal cannot run in the foreground
  wait
  exit
}
# otherwise open pager in current terminal
# shellcheck disable=SC2086
"$P" $PO "$FMT"

# vim:expandtab:sw=2:
