{{- if (ne .host.distro.family "windows") -}}
#!/usr/bin/env bash
# @file Bash It!
# @brief Ensures Bash is configured to use Bash It!
# @description
#     This script ensures Bash is configured to use Bash It! It ensures dependencies are installed, installs completions,
#     and enables Bash It! plugins. The completions and plugins are hardcoded in this script. Source file might need `#!/usr/bin/env -S bash -i`.

{{ includeTemplate "universal/profile" }}
{{ includeTemplate "universal/logg" }}

### Ensure Powerline is installed
if ! command -v powerline > /dev/null; then
    logg info 'Installing powerline via install-program'
    install-program powerline > /dev/null
fi

### Include Bash It
export BASH_IT="${XDG_DATA_HOME:-$HOME/.local/share}/bash_it"
export BASH_IT_THEME="powerline"
if command -v powerline-daemon > /dev/null && [ -f "$BASH_IT/bash_it.sh" ] && ! command -v bash-it; then
    logg info "Sourcing the bash_it.sh script"
    . "$BASH_IT/bash_it.sh"
fi

### Ensure ble.sh installed
if [ -d "${XDG_DATA_HOME:-$HOME/.local/share}/blesh/src" ]; then
    logg info 'Make installing blesh'
    make -C "${XDG_DATA_HOME:-$HOME/.local/share}/blesh/src" install > /dev/null && logg success "Installed ble.sh" || logg error "Error running make -C "${XDG_DATA_HOME:-$HOME/.local/share}/blesh/src" install"
fi

### Bash-it completions / plugins
if command -v powerline > /dev/null && [ -f "$HOME/.bashrc" ]; then
    if [ -d "$BASH_IT" ]; then
        ### Ensure bash-it is installed
        if ! command -v bash-it > /dev/null; then
            logg info 'Installing bash-it since it is not available yet as a command'
            bash "$BASH_IT/install.sh" --silent --no-modify-config
        else
            logg info 'bash-it already available'
        fi

        ### Ensure completions are enabled
        if [ ! -d "$BASH_IT/enabled" ]; then
            logg info "Creating the $BASH_IT/enabled directory" && mkdir -p "$BASH_IT/enabled"
        fi
        logg info "Changing directory to $BASH_IT/enabled" && cd "$BASH_IT/enabled"
        logg info 'Enabling bash-it completions'
        # TODO: Move these plugins to the .chezmoidata.yaml
        for COMPLETION in defaults dirs docker docker-compose export git makefile ng ssh system vagrant; do
            if ls "$BASH_IT/enabled" | grep "$COMPLETION" > /dev/null; then
                rm -f "$BASH_IT/enabled/"*"$COMPLETION"* > /dev/null && logg info "Removed old $COMPLETION bash-it completion symlink" || logg error "Failed to remove $COMPLETION bash-it completion symlink"
            fi
            yes | bash-it enable completion "$COMPLETION" > /dev/null && logg info "Enabled the bash-it $COMPLETION completion plugin" || logg error "Failed to install the $COMPLETION bash-it completion plugin"
        done

        ### Ensure plugins are enabled
        logg info 'Enabling bash-it plugins'
        # TODO: Move these plugins to the .chezmoidata.yaml
        for PLUGIN in base blesh browser cht-sh dirs gitstatus powerline sudo xterm; do
            if ls "$BASH_IT/enabled" | grep "$PLUGIN" > /dev/null; then
                rm -f "$BASH_IT/enabled/"*"$PLUGIN"* > /dev/null && logg info "Removed old $PLUGIN bash-it plugin symlink" || logg error "Failed to remove $PLUGIN bash-it plugin symlink"
            fi
            yes | bash-it enable plugin "$PLUGIN" > /dev/null && logg info "Enabled the bash-it $PLUGIN plugin" || logg error "Failed to install the $PLUGIN bash-it plugin"
        done
    else
        logg warn 'The path specified by BASH_IT does not exist yet'
    fi
else
    if ! command -v powerline > /dev/null; then
        logg warn 'powerline is not available'
    else
        logg warn '~/.bashrc is missing'
    fi
fi

{{ end -}}
