#!/bin/sh -e
#
# sn - start a new script in the current directory

if [ "$#" -eq 0 ]; then
  printf '%s\n' "Usage: $(basename "$0") [command|file]"
  exit 1
fi

if [ -f "$1" ]; then
  ${EDITOR:-vi} "$1"
else
  extension="${1##*.}"
  if [ -z "${extension}" ] || [ "${extension}" = "sh" ]; then
    printf '#!/bin/sh\n\nset -e\n\n' >"$1" && chmod -- 700 "$1" && ${EDITOR:-vi} "$1"
  else
    touch -- "$1" && chmod -- 700 "$1" && ${EDITOR:-vi} "$1"
  fi
fi
