# kaeru (https://github.com/gloomydumber/kaeru) keeps track of this tab, so
# that it can come back after the terminal closes. Your ~/.bashrc loads this
# with: eval "$(kaeru init bash)"

# Everything runs in a function, so that its variables stay local and a
# problem never stops the rest of ~/.bashrc.
__kaeru_init() {
    case $- in
    *i*) ;;
    *) return ;;
    esac
    # Loaded once, also when ~/.bashrc runs again in the same shell.
    [ -n "${__kaeru_cwd_file+set}" ] && return
    __kaeru_cwd_file=
    __kaeru_ask=
    __kaeru_last_cwd=
    __kaeru_pid=$$
    local parent= output
    # In Git Bash and other MSYS shells, $$ is not the process id that Windows
    # knows, which /proc/$$/winpid holds. mintty starts bash through an MSYS
    # fork, so Windows doesn't know bash's parent, and kaeru gets it from
    # here. A bash that a Windows program started has no MSYS parent.
    if [ -r "/proc/$$/winpid" ]; then
        read -r __kaeru_pid <"/proc/$$/winpid"
        if [ -r "/proc/$PPID/winpid" ]; then
            read -r parent <"/proc/$PPID/winpid"
        fi
    fi
    # kaeru resume prints the code that resumes the session, and this runs it
    # here, as if you typed it, so that no kaeru process waits while the
    # session runs. A kaeru that waits keeps its file in use, which an update
    # can't replace on Windows. Other commands run kaeru as they are.
    kaeru() {
        if [ "$#" -ne 1 ] || [ "$1" != resume ]; then
            command kaeru "$@"
            return
        fi
        local code
        code=$(command kaeru resume --shell bash) || return
        if [ -n "$code" ]; then
            eval "$code"
        fi
    }
    output=$(command kaeru shell start --pid "$__kaeru_pid" ${parent:+--parent "$parent"})
    # Exit code 3: tabs are waiting, and this tab is the one that asks. 5: the
    # same, in a terminal that gives Windows programs no console, as mintty,
    # where kaeru draws through winpty, which Git for Windows ships.
    case $? in
    3) __kaeru_ask=kaeru ;;
    5) __kaeru_ask=winpty ;;
    esac
    # A tab that kaeru reopened gets the code that resumes its session from
    # KAERU_RUN, which kaeru shell start has seen, and runs it at the first
    # prompt, once ~/.bashrc has set everything up. Programs started in the
    # tab don't inherit it.
    __kaeru_run=${KAERU_RUN-}
    unset KAERU_RUN
    # The path is a Windows path on Windows, which MSYS shells also take with
    # forward slashes.
    __kaeru_cwd_file=${output//\\//}
    [ -n "$__kaeru_cwd_file" ] || return
    # Programs in this tab, such as an agent and its hook, learn the tab's
    # shell from this, for when Windows can't tell, as through winpty.
    export KAERU_SHELL_PID=$__kaeru_pid
    # Git Bash's pwd -W prints the Windows path, which kaeru needs there.
    if builtin pwd -W >/dev/null 2>&1; then
        __kaeru_pwd='-W'
    else
        __kaeru_pwd='-L'
    fi
    PROMPT_COMMAND="__kaeru_prompt${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
}

# Writes the folder to the tab's .cwd file when it changes, and touches the
# file at every prompt, without starting a process. The file's time says when
# the prompt last showed, so that kaeru knows an agent that ran in this tab
# ended.
__kaeru_prompt() {
    # Prompt themes show whether the last command failed, from $?, which
    # this function's own commands would reset.
    local status=$?
    if [ -n "$__kaeru_ask" ]; then
        # The picker waits for the user, so it shows at the first prompt and
        # not while ~/.bashrc runs. It learns which shell asks from
        # KAERU_SHELL_PID, because through winpty, Windows doesn't know.
        if [ "$__kaeru_ask" = winpty ] && command -v winpty >/dev/null 2>&1; then
            __kaeru_ask=
            KAERU_NEW_TAB=1 KAERU_SHELL_PID=$__kaeru_pid winpty "$(command -v kaeru)"
        else
            __kaeru_ask=
            KAERU_NEW_TAB=1 KAERU_SHELL_PID=$__kaeru_pid command kaeru
        fi
        # Exit code 4: every tab came back in this window, so this new tab,
        # which only showed the picker, is one too many.
        [ $? -eq 4 ] && exit 0
    fi
    if [ -n "$__kaeru_run" ]; then
        local code=$__kaeru_run
        __kaeru_run=
        eval "$code"
    fi
    # pwd writes to the file itself, because $(pwd) would start a subshell,
    # and >| writes even when the noclobber option is on. Once the file is
    # gone, as when kaeru forgot the tab, it stays gone until the folder
    # changes.
    if [ "$PWD" != "$__kaeru_last_cwd" ] || [ -e "$__kaeru_cwd_file" ]; then
        builtin pwd $__kaeru_pwd 2>/dev/null >|"$__kaeru_cwd_file" && __kaeru_last_cwd=$PWD
    fi
    return $status
}

__kaeru_init
unset -f __kaeru_init
