#!/usr/bin/env bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# rfc [-p] [-f] [-n] - let's you look at rfcs (Request For Comments)
# std [-p] [-f] [-n] - let's you look at stds (Internet STandarDs)
# fyi [-p] [-f] [-n] - let's you look at fyis (For Your Information)
# bcp [-p] [-f] [-n] - let's you look at bcps (Best Common Practices)
# ien [-p] [-f] [-n] - let's you look at iens (Internet Experiment Notes)
#
# The intention is to save it as one of these names, and then link the
# other four to it, for example, if you saved it in ~/bin/rfc, then
# you would do:
# ln -s ~/bin/rfc ~/bin/std
# ln -s ~/bin/rfc ~/bin/fyi
# ln -s ~/bin/rfc ~/bin/bcp
# ln -s ~/bin/rfc ~/bin/ien
#
# author - Patrick J. Horgan with thanks to many with the same idea.
#          patrick at dbp-consulting dot com
#
# Setup, just set the six variables ftpuser, docdir, textviewer, psviewer,
# pdfviewer, ftp, and printer to what you want.  
#
# Features - Caches everything you access for later access.  Forces index
#            to be fetched each time, since it might change, but if not
#            available uses a cached version of the index (eventually
#            after a LONG timeout).
#            With the -p argument it will print the rfc instead.
#            indexes always try to go to ftp, if you know you don't have
#            network access used the -f argument to skip the ftp timeout
#            and go directly to using a cached index if available.
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
usage()
{
    echo "Usage:   " `basename $0` "[-f] [-p] [-n] NUMBER|index"
    echo "               -f              don't try to get index by ftp"
    echo "               -p              try to print instead of showing on screen"
    echo "               -n              don't use cached one, try to get by ftp"
    echo "Example: " `basename $0` "index"
    echo "         " `basename $0` "1023"
    echo 
    exit
}
ftpuser=''                      # Your password for anonymous ftp
                                # (usually blank or a email address)
docdir=~/docs	# Where you want to save cached copies
textviewer="less"          	# What you want to view text with
#textviewer="emacs -nw"         # What you want to view text with
#textviewer="vim"               # What you want to view text with
psviewer=evince                 # What you want to view postscript with
pdfviewer=evince		# What you want to view pdfs with
ftp=ftp                         # ftp program to use
printer=lpr                     # The printing command you use.

type=$(basename $0)
case $type in
	std|bcp|fyi|ien)
		ftpdir=in-notes/$type
		;;
	*)
		ftpdir=in-notes
esac
cachedir=${docdir%/}/$type

if [ ! -d "$cachedir"  ] ; then
    mkdir -p "$cachedir"
fi

if [ ! -d "$cachedir" ] ; then
    echo "No cache directory, $cachedir"
    exit
fi

forceindex='no'
getnew='no'
getit='none'

for arg in $* ; do
    case $arg in
        -p)     textviewer=$printer ; psviewer=$printer ;;
	-f)     forceindex='yes';;
	-n)     getnew='yes';;
        index)  getit=$type-index;;
        *)      getit=$type$arg
    esac
done

if [ $getit = 'none' ] ; then
    usage
fi

if [ $getnew = 'no' -a \( \
       -f $cachedir/$getit.txt \
    -o -f $cachedir/$getit.ps \
    -o -f $cachedir/$getit.pdf \
    -o \(\
	   $getit = $type-index \
        -a $forceindex = 'yes' \
        -a \(\
	      -f $cachedir/$type-index.txt.old \
           -o -f $cachedir/$type-index.pdf.old \
	   -o -f $cachedir/$type-index.ps.old \
	   \) \
       \) \
       \) ] ; then
    echo Using cached copy.
else
    # $ftp -n >/dev/null 2>&1 ftp.rfc-editor.org << EOF
    echo "Attempting to acquire $getit via anonymous ftp"
    $ftp 2>&1 -np ftp.rfc-editor.org >/dev/null << EOF
user anonymous "-$ftpuser"
cd $ftpdir
get $getit.txt $cachedir/$getit.txt
get $getit.ps  $cachedir/$getit.ps
get $getit.pdf  $cachedir/$getit.pdf
bye
EOF
fi

if [ -f $cachedir/$getit.pdf ] ; then
    $pdfviewer $cachedir/$getit.pdf
elif [ -f $cachedir/$getit.ps ] ; then
    $psviewer $cachedir/$getit.ps
elif [ -f $cachedir/$getit.txt ] ; then
    $textviewer $cachedir/$getit.txt
elif [ "$getit" = $type-index -a -f $cachedir/$type-index.txt.old ] ; then
    echo "Failed getting the new index, using the old one..."
    $textviewer $cachedir/$type-index.txt.old
else
    echo "I can't find $cachedir/$getit.txt or $cachedir/$getit.ps or $cachedir/$getit.pdf."
fi
# save the index to index old.  Right now I only see text indices, but you
# never know, some of the other files come in postscript or portable document
# format, so it might happen!
if [ -f $cachedir/$type-index.txt ] ; then
    mv $cachedir/$type-index.txt $cachedir/$type-index.txt.old
fi
if [ -f $cachedir/$type-index.ps ] ; then
    mv $cachedir/$type-index.ps $cachedir/$type-index.ps.old
fi
if [ -f $cachedir/$type-index.pdf ] ; then
    mv $cachedir/$type-index.pdf $cachedir/$type-index.pdf.old
fi
