#!/bin/bash #----------------------------------------------- # Opens an I-D from the I-D directory. If not # present, tries to fetch it first. May fetch # and open multiple matching documents. #----------------------------------------------- # # Version 1.3 # Mrch 26, 2009 # Randall Gellens # #----------------------------------------------- #----------------------------------------------- # Verbose messages #----------------------------------------------- function say () { if [ "${opt_verbose}" = "y" ]; then echo "$1" fi } function vsay () { if [ "${opt_super_verbose}" = "y" ]; then echo "$1" fi } #----------------------------------------------- # Start of main script #----------------------------------------------- # # For those of us who like BBEdit, often a # command-line flag or variant is used in # $EDITOR which doesn't return until the file # is closed. This is great for things like # crontab, but not so good when we want to just # open the file in the editor. So, if the # environment variable EDITOR_NOWAIT is defined, # use it, otherwise use EDITOR. # OPEN=${EDITOR_NOWAIT:-$EDITOR} cd ~/IETF/Internet_Drafts if [ $# -eq 0 ]; then X=`basename $0` echo "usage: $X [flags] "; echo "Flags: -c Check for updated draft in background" echo " -v (passed to get_id if called)" echo " -V (passed to get_id if called)" echo " -m (passed to get_id if called)" echo " -n (passed to get_id if called)" echo " -- Read draft names/substrings from stdin" exit; fi get_id_opt_str="" opt_check="n" opt_super_verbose="n" opt_stdin="n" open_id_opt_str="" while getopts ":cnvVms" opt do case $opt in s) echo "reading draft names/substrings from stdin" opt_stdin="y" ;; c) echo "will check for updates in background" opt_check="y" open_id_opt_str="$open_id_opt_str -c" ;; n) ;; v) get_id_opt_str="${get_id_opt_str} -v" ;; m) get_id_opt_str="${get_id_opt_str} -m" ;; V) get_id_opt_str="${get_id_opt_str} -V" open_id_opt_str="$open_id_opt_str -V" echo "super verbose mode" opt_super_verbose="y" ;; *) echo "Unrecognized option: ${opt}" exit ;; esac done if [ "${get_id_opt_str}X" != "X" ]; then get_id_opt_str="-${get_id_opt_str}" fi shift $(($OPTIND - 1)) if [ "${opt_stdin}" = "y" ]; then drafts="" while IFS= read -r line do drafts="$drafts $line" done vsay "will open $drafts" open_id "$open_id_opt_str $get_id_opt_str $drafts" # call ourselves with draft name exit 0 fi for idx do # make sure to use lowercase idx=`echo "${idx}" | tr "[:upper:]" "[:lower:]"` vsay "Looking for ${idx}..." id="${idx}" filen="" if [ -f ${id} ]; then filen=${id}; vsay "...found $filen" elif [ -f ${id}.txt ]; then filen=${id}.txt; vsay "...found $filen" else # # Use ls to get list of matching files, sorted by time. # Break after first element in list, which we assume is # latest file. # RSLT=`ls -t *${id}* 2>/dev/null | grep "[0-9]*\.txt"` for fx in $RSLT do filen=${fx} break done vsay "Matching files: $RSLT" vsay "Selected: $filen" fi # # If we found a file to open, open it. # If run with -c, call get_id, and if we have a later # file, open it. # if [ "${filen}X" != "X" ]; then $OPEN ${filen}; if [ "$opt_check" = "y" ]; then get_id ${get_id_opt_str} ${id} RSLT=`ls -t *${id}* 2>/dev/null | grep "[0-9]*\.txt"` for fx in $RSLT do nfilen=${fx} break done vsay "Matching files: $RSLT" vsay "Selected: $filen" if [ "$nfilen" != "$filen" ]; then $OPEN ${nfilen} fi fi else echo "id $id not found locally; fetching..." get_id ${get_id_opt_str} $id; for fx in `ls *${id}* 2>/dev/null` do $OPEN ${fx} done fi; done