From 15f342bfe37514a3f4cb53f14937272d290c66f6 Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Sun, 25 Jun 2023 01:00:26 +0200
Subject: [PATCH] Now sbs configuration automatically registers the
 autocompletion scripts

---
 scripts/sbs_autocomplete   | 42 +++++++++++++++++++++++---------------
 scripts/sbs_implementation | 33 ++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/scripts/sbs_autocomplete b/scripts/sbs_autocomplete
index 3ebad7a97..4610ff722 100755
--- a/scripts/sbs_autocomplete
+++ b/scripts/sbs_autocomplete
@@ -20,25 +20,33 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-source "$(cd -- "$(dirname "$0")" > /dev/null 2>&1 && pwd -P)/sbs_implementation"
+current_path="$(cd -- "$(dirname "$0")" >/dev/null 2>&1 && pwd -P)"
+
+# Target directory name
+target_dir="skyward-boardcore"
+
+# Cut the path to the target directory
+while [[ "$current_path" != "/" && "$(basename "$current_path")" != "$target_dir" ]]; do
+    current_path="$(dirname "$current_path")"
+done
+
+source "$current_path/scripts/sbs_implementation"
 
 function _sbs_autocomplete() {
-    local curr_arg="${COMP_WORDS[COMP_CWORD]}"
-    local prev_arg="${COMP_WORDS[COMP_CWORD-1]}"
+    declare curr_arg="${COMP_WORDS[COMP_CWORD]}"
+    declare prev_arg="${COMP_WORDS[COMP_CWORD - 1]}"
 
     case "$prev_arg" in
-        "-b"|"--build")
-            ;&
-        "-f"|"--flash")
-        ;&
-        "-t"|"--test")
-            # Autocomplete options for build, flash and test
-            init_dirs
-            COMPREPLY=( $(compgen -W "$(list --only-list)" -- "$curr_arg") )
-            ;;
-        *)
-            # Autocomplete options for general options
-            COMPREPLY=( $(compgen -W "\
+    "-b" | "--build") ;&
+    "-f" | "--flash") ;&
+    "-t" | "--test")
+        # Autocomplete options for build, flash and test
+        init_dirs
+        COMPREPLY=($(compgen -W "$(list --only-list)" -- "$curr_arg"))
+        ;;
+    *)
+        # Autocomplete options for general options
+        COMPREPLY=($(compgen -W "\
                 -h --help      \
                 -j --jobs      \
                 -l --list      \
@@ -52,8 +60,8 @@ function _sbs_autocomplete() {
                 -v --verbose   \
                 -n --lint      \
                 -e --edit      \
-                --clang-tidy" -- "$curr_arg") )
-            ;;
+                --clang-tidy" -- "$curr_arg"))
+        ;;
     esac
 }
 
diff --git a/scripts/sbs_implementation b/scripts/sbs_implementation
index f0e6a3b87..5792c46b2 100755
--- a/scripts/sbs_implementation
+++ b/scripts/sbs_implementation
@@ -126,6 +126,39 @@ configure() {
 
     { [ "$config_debug" = true ] && touch "$build_dir/$DEBUG_FILENAME"; } || rm -f "$build_dir/$DEBUG_FILENAME"
     { [ "$config_verbose" = true ] && touch "$build_dir/$VERBOSE_FILENAME"; } || rm -f "$build_dir/$VERBOSE_FILENAME"
+
+    install_autocompletion
+}
+
+install_autocompletion() {
+    declare autocomplete_script="$sbs_base/scripts/sbs_autocomplete"
+    declare -A shell_files=(
+        ["bash"]="$HOME/.bashrc"
+        ["zsh"]="$HOME/.zshrc"
+        ["fish"]="$HOME/.config/fish/config.fish"
+    )
+
+    declare -a installed_shells
+
+    for shell in "${!shell_files[@]}"; do
+        local shell_file="${shell_files[$shell]}"
+
+        if [[ -f "$shell_file" ]]; then
+            if ! grep -q "source $autocomplete_script" "$shell_file"; then
+                echo "source $autocomplete_script" >>"$shell_file"
+            fi
+            installed_shells+=("$shell")
+        fi
+    done
+
+    if [[ ${#installed_shells[@]} -gt 0 ]]; then
+        echo "Autocomplete script has been added for the following shells:"
+        for shell in "${installed_shells[@]}"; do
+            echo "- $shell"
+        done
+    else
+        echo "No supported shells found. Please add the following line to your shell's initialization file manually: 'source $autocomplete_script'"
+    fi
 }
 
 check_configured() {
-- 
GitLab