#!/bin/sh -e
#
# ss - open one of the existing scripts to edit

sdir=$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd) && cd "${sdir}" || exit 1

if [ -z "$1" ]; then
  file=$(find "${sdir}"/* -type f | sed "s|${sdir}/||" | ,picker --preview "cat ${sdir}/{}" --print-query | tail -n 1)
  [ -z "${file}" ] && exit 1
else
  file="$1"
fi

if [ -f "${sdir}/${file}" ]; then
  ${EDITOR:-vi} "${sdir}/${file}"
else
  printf '#!/bin/sh\n\nset -e\n\n' >"${sdir}/${file}" && chmod +x "${sdir}/${file}" && ${EDITOR:-vi} "${sdir}/${file}"
fi

