#!/bin/bash

Version=1.01
Myname="${0##*/}"

doc=$(cat \
<<'DOC'
= v - edit file(s) with vim, (or gvim, emacs)

= Synopsis
v [options]		# -h --help -H --Help -V --version
v [file... ]		# edit one or more files
v -script [file...]	# edit script found in PATH plus 0 or more files
v --texfile [file... ]	# edit TeX file found with kpsewhich plus 0 or more files
v nnnn [file... ]	# edit nnnn/main.tex found in |$ARCHIVE| plus 0 or more files

= Description
This script is called v, and it runs vim. However, if installed as links to
v named *g* or *e*, then gvim or emacs are called instead. In the latter
case, if emacs is not found, xemacs is called, and if an emacs server is
running, it is used for editing.

If given one or more /file/s they let you edit those files, just like the
vim, gvim or emacs editors. However, the first of the given /file/
arguments is treated differently from the rest:

nnnn	(a 4-digit number):
	if a non-binary regular file with that name exists in the current
	directory, they let you edit this file; if no such file exists,
	they look for an /archive file/ which is defined as a file
	|main.tex| in the directory |$ARCHIVE/nnnn|, if such a file exists.
	This supposes that you create your letters, faxes, reports and the
	like in a numbered TeX-archive.
	
--file	(filename prefixed with two hyphens):
	they look for a TeX file using kpsewhich and let you edit it.
	
-script	(filename prefixed with a single hyphen):
	they look for a script |script| in |$PATH| and let you edit it.

The idea behind this script is laziness: I very often need to edit one of
my many (mostly bash-)scripts or one of my archive files. Instead of
trusting my memory to know where, in |$PATH|, the script is stored, I
rather like this script to find it. And instead if typing, say,
|vi $ARCHIVE/1234/main.tex| when I want to edit an archive file, I rather
like to let this script type most of these characters.

= Author
[Wybo Dekker](wybodekker@me.com)

= Copyright
Released under the [GNU General Public License](www.gnu.org/copyleft/gpl.html)
DOC
)

# --- colors, help from embedded doc, die ---
[ -t 1 ] && { Red=$'\e[1;31m' Mag=$'\e[1;35m' Nor=$'\e[0m'; } || Red='' Mag='' Nor=''
nv="-$Myname-${Version/./\\.} (press h for help, q to quit)"
helpsrt() { sed -n '/^= Synopsis/,/^= Desc/{//d;p}' <<<"$doc"; exit; }
helpall() { less -P"$nv" <<<"$doc"; exit; }
    die() { printf "%s$Nor\n" "${@/#/$Myname: $Red}" >&2; exit 1; }

declare -A mapping=([v]=vim [g]=gvim [e]=emacs)

# find the full pathname of an executable in the PATH
# returns nil if the executable is not found

file="$1"         # first file to be edited (maybe a script if prefixed with - )
shift

case $file in
(-h|--help)
	helpsrt
	;;
(-H|--Help)
	helpall
	;;
(-V|--version)
	echo $Version
        exit
	;;
(-I)	instscript "$0" ||
	   die 'the -I option is for developers only'
	exit
	;;
(--*)	# if the first argument starts with =--
	f=${file:2}
	file=$(kpsewhich "$f" 2>/dev/null) || 
	     die "No tex file $f found"
	;;
(-*)	# if the first argument starts with =-
	f=${file:1}
	file=$(command -v "$f" 2>/dev/null) || 
	     die "No executable file $f found"
	;;
([[:digit:]][[:digit:]][[:digit:]][[:digit:]]) # 4-digit number
	if [[ ! -e $file ]]; then
	   [[ -d $ARCHIVE/$file ]] ||
              die "$file not found here and is not a directory in $ARCHIVE"
	   [[ -e $ARCHIVE/$file/main.tex ]] ||
              die "$ARCHIVE/$file does not contain a file main.tex"
	   file=$ARCHIVE/$file/main.tex
	fi
esac

editor=${mapping[$Myname]}
$editor "$file" "$@"
