#!/usr/bin/env sh
# -*- coding: utf-8-unix -*-
# This is a shebang interpreter for launching Emacs Lisp scripts with Doom's CLI
# framework preloaded, plus any environment variables it needs. Use it like so:
#
#   #!/usr/bin/env doomscript
#   (print! "Hello world!")
#
# For this to work (and to avoid an absolute path in your shebang line), this
# file must be in your $PATH:
#
#   export PATH="$HOME/.config/emacs/bin:$PATH"
#
# This isn't used for bin/doom because of this $PATH/absolute path requirement
# (and using $BASH_SOURCE to locate it would reduce its POSIX compliance), but
# this should be less of an issue for folks writing their own doomscripts.

if [ "$#" -eq 0 ]; then
    >&2 echo "Error: missing required file argument"
    exit 1
fi

case "$EMACS" in
    *term*) EMACS=emacs ;;   # in {ansi-,v}term
    *\ *) ;;
    *) EMACS="${EMACS:-emacs}"
       # Only sanity-check $EMACS if it's a path or executable
       if ! type "$EMACS" >/dev/null 2>&1; then
           echo "Error: failed to run Emacs with command '$EMACS'"
           echo
           echo "Are you sure Emacs is installed and in your \$PATH?"
           exit 1
       fi >&2
       ;;
esac

# Careful not to use -Q! It implies --no-site-lisp, which omits the site-lisp
# directory from `load-path', which would prevent Doom from manually loading the
# site files later. These are important on some systems or deployment methods
# (like Snap or NixOS).
emacs="$EMACS -q --no-site-file --batch"

# Doom respects $EMACSDIR to tell it where Doom lives. If it fails, then this is
# either isn't bash, or it's a posix shell being directly sourced with sh, which
# is unsupported.
export EMACSDIR="${EMACSDIR:-$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE:-$0}")/.." && pwd)}"
if [ ! -f "$EMACSDIR/early-init.el" ]; then
    echo "Error: cannot load $EMACSDIR/early-init.el."
    echo
    echo "Either the file doesn't exist (indicating a broken or missing Doom install)"
    echo "or that doomscript is being source directly (which is unsupported)."
    echo
    echo "Set \$EMACSDIR to the path of an existing Doom installation."
    exit 1
fi >&2
# Some state that Doom's CLI framework needs to know about the terminal. Read
# the comments at the top of bin/doom for explanations.
export __DOOMSH="${__DOOMSH:-sh}"
export __DOOMPID="${__DOOMPID:-$$}"
export __DOOMSTEP="${__DOOMSTEP:-0}"
export __DOOMGEOM="${__DOOMGEOM:-$(tput cols 2>/dev/null)x$(tput lines 2>/dev/null)}"
export __DOOMGPIPE="${__DOOMGPIPE:-$__DOOMPIPE}"
export __DOOMPIPE=
[ -t 0 ] || __DOOMPIPE="${__DOOMPIPE}0"
[ -t 1 ] || __DOOMPIPE="${__DOOMPIPE}1"

# Now we're ready to execute the given script. $EMACSDIR/early-init.el is Doom's
# universal bootstrapper (and will only load the bare minimum), so it must be
# loaded first.
script="$1"
shift
$emacs --load "$EMACSDIR/early-init" \
       --load "$script" \
       -- "$@"
exit=$?

# To simulate execve syscalls (which replaces the running process), Doom
# generates a temporary exit-script if a Doomscript returns a 254 exit code.
if [ "${exit:-0}" -eq 254 ]; then
    # $TMPDIR (or $TEMP and $TMP on Windows) aren't guaranteed to have values,
    # and mktemp isn't available on all systems, but you know what is? Emacs! So
    # I rely on it to provide TMPDIR.
    export TMPDIR="${TMPDIR:-${TMP:-${TEMP:-$($emacs -Q --eval '(princ (temporary-file-directory))' 2>/dev/null)}}}"

    # The user may have a noexec flag set on /tmp, so the exit-script should be
    # passed to /bin/sh rather than executed directly.
    sh "${TMPDIR}/doom.${__DOOMPID}.${__DOOMSTEP}.sh" "$0" "$@"
    exit="$?"
fi
exit $exit

# doomscript ends here... Unless?
