#!/usr/bin/env bash

set -o errexit      # Exit on most errors (see the manual)
set -o nounset      # Disallow expansion of unset variables


# Define the Jenkins jobs directory
JENKINS_HOME="/var/lib/jenkins" # Change this to your actual Jenkins home directory
JOBS_DIR="$JENKINS_HOME/jobs"

# Check if the Jenkins jobs directory exists
if [ ! -d "$JOBS_DIR" ]; then
    echo "Jenkins jobs directory does not exist: $JOBS_DIR"
    exit 1
fi

# Search and replace 'python3.6 -m venv venv' with 'python -m venv venv' in job configurations
# in directories containing the word 'deploy'
echo "Updating Jenkins job configurations in 'deploy' directories..."
find "$JOBS_DIR" -maxdepth 1 -type d -name "*fargate-deploy*" | while read -r dir ; do
    grep -Rl "python3.6 -m venv" "$dir" --include=config.xml | while read -r file ; do
        echo "copy ${file} to ${file}.20231212"
        cp "${file}" "${file}.20231212"
        sed -i 's/python3\.6 -m venv/python -m venv/g' "$file"
        echo "Updated: $file"
    done
done

echo "Update complete."

