From 7ad41a8423d501ae178af29024690df53c5a9d3e Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 7 Apr 2024 10:49:45 +0200 Subject: [PATCH] Add config.sh --- dotfiles/.local/bin/config.sh | 61 +++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 dotfiles/.local/bin/config.sh diff --git a/dotfiles/.local/bin/config.sh b/dotfiles/.local/bin/config.sh new file mode 100755 index 0000000..db2b67f --- /dev/null +++ b/dotfiles/.local/bin/config.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +find_config_file() { + local program="$1" + local config_file="" + + if [ -z "$XDG_CONFIG_HOME" ]; then + XDG_CONFIG_HOME="$HOME/.config" + fi + + local config_files=($(find -L "$XDG_CONFIG_HOME" \( -name "$program*" -o -name ".$program*" \) -type f)) + #local config_files=($(find -L "$XDG_CONFIG_HOME" -name "$program*" -type f)) + if [ ${#config_files[@]} -gt 0 ]; then + local min_subfolder_count=10 + for file in "${config_files[@]}"; do + subfolder_count=$(echo "$file" | awk -F/ '{print NF-2}') + #if [ -z "$config_file" ] || [ "$subfolder_count" -lt "$min_subfolder_count" ]; then + if [ -z "$config_file" ] || { [ "$subfolder_count" -lt "$min_subfolder_count" ] || { [ "$subfolder_count" -eq "$min_subfolder_count" ] && [ "$(basename "$file")" == "$program.${file##*.}" ]; }; }; then + min_subfolder_count="$subfolder_count" + config_file="$file" + fi + done + + #exact_match_files=() + #for file in "${config_files[@]}"; do + # if [ "$(basename "$file")" == "$program.${file##*.}" ]; then + # exact_match_files+=("$file") + # fi + #done + + #if [ ${#exact_match_files[@]} -gt 0 ]; then + # config_file="${exact_match_files[0]}" + #fi + fi + + if [ -z "$config_file" ]; then + local home_config_files=($(find -L "$HOME" -name ".$program*" -type f -maxdepth 1)) + if [ ${#home_config_files[@]} -gt 0 ]; then + config_file="${home_config_files[0]}" + fi + fi + + echo "$config_file" +} + +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + +program="$1" + +config_file=$(find_config_file "$program") + +if [ -z "$config_file" ]; then + echo "Config file for $program not found." + exit 1 +fi + +$EDITOR "$config_file" +