#!/bin/sh
# "cdadd" copyright 1994 thomas insel
#         copyright 1996 sven oliver moll
#         copyright 2004 Max Vozeler
usage()
{
  echo >&2 Usage: `basename $0` [-[0-7]]
  echo >&2 This program is part of cdtool. See \"man cdtool\" for more info.
  exit 1
}

PATH=/bin:/usr/bin:/usr/local/bin
TMPFILE=~/.cdadd-$$

case $# in
  [01]) ;;
  *) usage;;
esac

case x$1 in
  x-[0-7]) DRIVE=-$1;;
  x) ;;
  *) usage;;
esac

cdir $DRIVE -t > $TMPFILE

if test -z $EDITOR; then
  for favourite in emacs vim vi; do
    if command -v $favourite >/dev/null 2>&1; then
      EDITOR=$favourite
      break
    fi
  done
fi

if test -z $EDITOR; then
  echo Unable to find an editor. Please set the environment variable
  echo EDITOR to the location of your preferred editor. Eg:
  echo
  echo   \$ export EDITOR=/path/to/editor
  echo   \$ cdadd
  echo
  exit 1
fi

$EDITOR $TMPFILE

echo ==== about to add =================
cat $TMPFILE
echo -n ==== ok? [Yn]
read n;
case $n in
  [nN])
    echo aborting.
    exit 1
    ;;
esac

cat $TMPFILE >>~/.cdtooldb
rm $TMPFILE

