#!/bin/sh
# You probably want to stick the following into your .bash_profile:
# alias ev=~/crawl/crawl-ref/source/util/edit_vault
#
# You need to define $EDITOR as well; on Debian there's some weak-ass
# autodetection but that's unlikely to match _your_ preferences. And it
# may start something as vile as vi.


if [ -z "$1" ]
  then
    echo >&2 "Usage: edit-vault vault_name"
    exit 1
fi

V=`grep -rHn --include='*.des' "NAME: *$1$"`
if [ -z "$V" ]
  then
    echo >&2 "Vault \"$1\" not found."
    exit 1
fi

if which sensible-editor >/dev/null
  then
    ED=sensible-editor
  else
    ED="${EDITOR:-editor}"
fi

FILE="${V%%:*}"
LINE="${V#*:}"; LINE="${LINE%%:*}"

"$ED" +"$LINE" "$FILE"
