From 3ddfe98fa9625ea54dea1128fbef09da51f0d97a Mon Sep 17 00:00:00 2001
From: Andres Montano <andres.montano@tecnalia.com>
Date: Tue, 7 Jan 2025 16:22:23 +0100
Subject: [PATCH] Add .rosinstall and .repos to substitution options

---
 config/.vscode/launch.json     |   8 ++
 scripts/set_dotdev_remotes.sh  | 116 ------------------
 scripts/set_dotdev_tecnalia.sh | 215 +++++++++++++++++++++++++++++++++
 3 files changed, 223 insertions(+), 116 deletions(-)
 delete mode 100755 scripts/set_dotdev_remotes.sh
 create mode 100755 scripts/set_dotdev_tecnalia.sh

diff --git a/config/.vscode/launch.json b/config/.vscode/launch.json
index 21cb808..3bfe8ad 100644
--- a/config/.vscode/launch.json
+++ b/config/.vscode/launch.json
@@ -15,6 +15,14 @@
                 "launch_rviz:=false"
             ],
             "type": "ros"
+        },
+        {
+            "type": "bashdb",
+            "request": "launch",
+            "name": "Bash simple",
+            "cwd": "${workspaceFolder}",
+            "program": "${file}",
+            "terminalKind": "integrated"
         }
     ]
 }
\ No newline at end of file
diff --git a/scripts/set_dotdev_remotes.sh b/scripts/set_dotdev_remotes.sh
deleted file mode 100755
index 9c3c9e2..0000000
--- a/scripts/set_dotdev_remotes.sh
+++ /dev/null
@@ -1,116 +0,0 @@
-#!/usr/bin/env bash
-#
-# Updates Git remote URLs by replacing 'tecnalia.com' with 'tecnalia.dev'
-# recursively in all Git repositories under the current directory
-#
-
-set -e pipefail  # Keep error handling but remove -u flag
-
-# Color definitions for better readability
-readonly GREEN='\033[0;32m'
-readonly YELLOW='\033[1;33m'
-readonly BLUE='\033[0;34m'
-readonly NC='\033[0m'  # No Color
-
-usage() {
-    echo "Usage: $(basename "$0") [-d|--dry-run] [-m|--max-depth N]"
-    echo "Options:"
-    echo "  -d, --dry-run     Show changes without applying them"
-    echo "  -m, --max-depth N Maximum depth for recursive search (default: unlimited)"
-    echo "                    Use 1 for current directory only"
-    echo "  -h, --help        Show this help message"
-    exit 1
-}
-
-# Improved argument processing
-dry_run=0
-max_depth=""
-
-while [[ $# -gt 0 ]]; do
-    case $1 in
-        -d|--dry-run)
-            dry_run=1
-            shift
-            ;;
-        -m|--max-depth)
-            if [[ -n "${2:-}" && $2 =~ ^[0-9]+$ ]]; then
-                max_depth="-maxdepth $2"
-                shift 2
-            else
-                echo "Error: --max-depth requires a number"
-                usage
-            fi
-            ;;
-        -h|--help)
-            usage
-            ;;
-        *)
-            echo "Error: Unknown option $1"
-            usage
-            ;;
-    esac
-done
-
-# Function to process a repository
-process_repo() {
-    local repo_path=$1
-    local remote=$2
-    local remote_url
-
-    if ! remote_url=$(git -C "${repo_path}" remote get-url "${remote}" 2>/dev/null); then
-        echo "  Warning: Could not get URL for remote ${remote}" >&2
-        return 1
-    fi
-
-    if [[ ${remote_url} == *"tecnalia.com"* ]]; then
-        local new_url=${remote_url//tecnalia.com/tecnalia.dev}
-        echo -e "${YELLOW}Found deprecated remote URL${NC}"
-        echo "  Repository: ${repo_path}"
-        echo "  Remote: ${remote}"
-        echo "  Current URL: ${remote_url}"
-        echo "  New URL: ${new_url}"
-
-        if [[ ${dry_run} -eq 0 ]]; then
-            if git -C "${repo_path}" remote set-url "${remote}" "${new_url}"; then
-                echo -e "  ${GREEN}Replacement completed!${NC}"
-            else
-                echo "  Error updating remote URL" >&2
-                return 1
-            fi
-        fi
-    fi
-}
-
-main() {
-    local count=0
-    local errors=0
-    local repos_found=0
-
-    echo -e "${BLUE}Starting recursive search for Git repositories...${NC}"
-
-    # Use process substitution for better handling of spaces in names
-    while IFS= read -r -d '' git_folder || [ -n "$git_folder" ]; do
-        ((repos_found++))
-        repo_path=$(dirname "$(readlink -f "${git_folder}")")
-        echo -e "${BLUE}Processing repository: ${repo_path}${NC}"
-
-        while IFS= read -r remote || [ -n "$remote" ]; do
-            if process_repo "${repo_path}" "${remote}"; then
-                ((count++))
-            else
-                ((errors++))
-            fi
-        done < <(git -C "${repo_path}" remote 2>/dev/null || echo "")
-
-    done < <(find . ${max_depth:-} -name '.git' -type d -print0 2>/dev/null || echo "")
-
-    # Final summary
-    echo
-    echo "Execution summary:"
-    echo "  Git repositories found: $repos_found"
-    echo "  Repositories processed: $count"
-    [[ $errors -gt 0 ]] && echo "  Errors found: $errors"
-    [[ $dry_run -eq 1 ]] && echo "  (Dry-run mode: no changes were made)"
-}
-
-main
\ No newline at end of file
diff --git a/scripts/set_dotdev_tecnalia.sh b/scripts/set_dotdev_tecnalia.sh
new file mode 100755
index 0000000..80b50ec
--- /dev/null
+++ b/scripts/set_dotdev_tecnalia.sh
@@ -0,0 +1,215 @@
+#!/usr/bin/env bash
+#
+# Updates URLs by replacing 'tecnalia.com' with 'tecnalia.dev' in:
+# - Git repository remotes
+# - .rosinstall files
+# - .repos files
+#
+
+# set -e
+# set -euo pipefail
+
+# Color definitions for better readability
+readonly GREEN='\033[0;32m'
+readonly YELLOW='\033[1;33m'
+readonly BLUE='\033[0;34m'
+readonly NC='\033[0m'  # No Color
+
+usage() {
+    echo "Usage: $(basename "$0") [-d|--dry-run] [-m|--max-depth N] [-t|--type TYPE]"
+    echo "Options:"
+    echo "  -d, --dry-run     Show changes without applying them"
+    echo "  -m, --max-depth N Maximum depth for recursive search (default: unlimited)"
+    echo "                    Use 1 for current directory only"
+    echo "  -t, --type TYPE   Specify which types of files to process:"
+    echo "                    git     - only process Git remotes"
+    echo "                    repos   - only process .rosinstall and .repos files"
+    echo "                    all     - process all files (default)"
+    echo "  -h, --help        Show this help message"
+    exit 1
+}
+
+# Improved argument processing
+dry_run=0
+max_depth=""
+file_type="all"
+
+while [[ $# -gt 0 ]]; do
+    case $1 in
+        -d|--dry-run)
+            dry_run=1
+            shift
+            ;;
+        -m|--max-depth)
+            if [[ -n "${2:-}" && $2 =~ ^[0-9]+$ ]]; then
+                max_depth="-maxdepth $2"
+                shift 2
+            else
+                echo "Error: --max-depth requires a number"
+                usage
+            fi
+            ;;
+        -t|--type)
+            if [[ "${2:-}" =~ ^(git|repos|all)$ ]]; then
+                file_type=$2
+                shift 2
+            else
+                echo "Error: --type must be 'git', 'repos', or 'all'"
+                usage
+            fi
+            ;;
+        -h|--help)
+            usage
+            ;;
+        *)
+            echo "Error: Unknown option $1"
+            usage
+            ;;
+    esac
+done
+
+# Function to process a repository
+process_repo() {
+    local repo_path=$1
+    local remote=$2
+    local remote_url
+
+    if ! remote_url=$(git -C "${repo_path}" remote get-url "${remote}" 2>/dev/null); then
+        echo "  Warning: Could not get URL for remote ${remote}" >&2
+        return 1
+    fi
+
+    if [[ ${remote_url} == *"tecnalia.com"* ]]; then
+        local new_url=${remote_url//tecnalia.com/tecnalia.dev}
+        echo -e "${YELLOW}Found deprecated remote URL${NC}"
+        echo "  Repository: ${repo_path}"
+        echo "  Remote: ${remote}"
+        echo "  Current URL: ${remote_url}"
+        echo "  New URL: ${new_url}"
+
+        if [[ ${dry_run} -eq 0 ]]; then
+            if git -C "${repo_path}" remote set-url "${remote}" "${new_url}"; then
+                echo -e "  ${GREEN}Replacement completed!${NC}"
+                return 0
+            else
+                echo "  Error updating remote URL" >&2
+                return 1
+            fi
+        fi
+        return 0
+    fi
+    return 2  # No changes needed
+}
+
+# Function to process .rosinstall and .repos files
+process_config_file() {
+    local file=$1
+    local file_type=$2
+    local changes_made=0
+    local temp_file
+
+    echo -e "${BLUE}Processing ${file_type} file: ${file}${NC}"
+
+    # Create temporary file in the same directory to preserve permissions
+    temp_file="${file}.tmp"
+
+    if [[ -f "$file" ]]; then
+        if grep -q "tecnalia.com" "$file"; then
+            echo -e "${YELLOW}Found deprecated URLs in: ${file}${NC}"
+
+            if [[ ${dry_run} -eq 0 ]]; then
+                # Create backup
+                cp "$file" "${file}.bak"
+
+                # Replace URLs
+                sed 's/tecnalia.com/tecnalia.dev/g' "$file" > "$temp_file"
+
+                # Show diff
+                echo "Changes made:"
+                diff "$file" "$temp_file" || true
+
+                # Replace original file
+                mv "$temp_file" "$file"
+                echo -e "  ${GREEN}Replacement completed!${NC}"
+                echo "  Backup saved as: ${file}.bak"
+                changes_made=1
+            else
+                echo "Changes that would be made:"
+                sed 's/tecnalia.com/tecnalia.dev/g' "$file" | diff "$file" - || true
+                changes_made=1
+            fi
+        fi
+    fi
+
+    # Clean up temporary file if it exists
+    [[ -f "$temp_file" ]] && rm "$temp_file"
+
+    return $changes_made
+}
+
+main() {
+    local git_count=0
+    local rosinstall_count=0
+    local repos_count=0
+    local errors=0
+    local repos_found=0
+    local config_files_found=0
+
+    echo -e "${BLUE}Starting recursive search for specified file types...${NC}"
+
+    # Process Git repositories
+    if [[ $file_type == "git" || $file_type == "all" ]]; then
+        echo -e "${BLUE}Looking for Git repositories...${NC}"
+        while IFS= read -r -d '' git_folder || [ -n "$git_folder" ]; do
+            ((repos_found++))
+            repo_path=$(dirname "$(readlink -f "${git_folder}")")
+            echo -e "${BLUE}Processing repository: ${repo_path}${NC}"
+
+            while IFS= read -r remote || [ -n "$remote" ]; do
+                result=$(process_repo "${repo_path}" "${remote}")
+                case $? in
+                    0) ((git_count++));;
+                    1) ((errors++));;
+                esac
+            done < <(git -C "${repo_path}" remote 2>/dev/null || echo "")
+
+        done < <(find . ${max_depth:-} -name '.git' -type d -print0 2>/dev/null || echo "")
+    fi
+
+    # Process config files
+    if [[ $file_type == "repos" || $file_type == "all" ]]; then
+        echo -e "${BLUE}Looking for configuration files...${NC}"
+        # Process .rosinstall files
+        while IFS= read -r -d '' rosinstall_file || [ -n "$rosinstall_file" ]; do
+            ((config_files_found++))
+            if process_config_file "$rosinstall_file" ".rosinstall"; then
+                ((rosinstall_count++))
+            fi
+        done < <(find . ${max_depth:-} -name '*.rosinstall' -type f -print0 2>/dev/null || echo "")
+
+        # Process .repos files
+        while IFS= read -r -d '' repos_file || [ -n "$repos_file" ]; do
+            ((config_files_found++))
+            if process_config_file "$repos_file" ".repos"; then
+                ((repos_count++))
+            fi
+        done < <(find . ${max_depth:-} -name '*.repos' -type f -print0 2>/dev/null || echo "")
+    fi
+
+    # Final summary
+    echo
+    echo "Execution summary:"
+    if [[ $file_type == "git" || $file_type == "all" ]]; then
+        echo "  Git repositories found: $repos_found"
+        echo "  Git remotes updated: $git_count"
+    fi
+    if [[ $file_type == "config" || $file_type == "all" ]]; then
+        echo "  Configuration files found: $config_files_found"
+        echo "  .rosinstall files updated: $rosinstall_count"
+        echo "  .repos files updated: $repos_count"
+    fi
+    [[ $errors -gt 0 ]] && echo "  Errors found: $errors"
+    [[ $dry_run -eq 1 ]] && echo "  (Dry-run mode: no changes were made)"
+}
+
+main
\ No newline at end of file
-- 
GitLab