#!/usr/bin/env bash

# shellcheck shell=bash

set -eu

if [ $# -gt 0 ]; then
  if pushd "$1" >/dev/null; then
    # ${@[0]} = $0 = name
    # ${@[1]} = $1 = directory
    # ${@:2}  = remaining args
    ${EDITOR:-vi} "${@:2}"
    # this must work because pushd succeeded
    # popdd >/dev/null || return
    cd >/dev/null || return
  fi
else
  printf '%s\n' "Usage: $(basename "$0") dir [args] execute ${EDITOR} in {dir}"
fi
