#!/bin/bash

# List of all site branches
all_site_branches=("portableweb" "bufearu" "foodpack" "overtones" "matisz" "ndrhorse")

# Make changes to the master branch
git checkout master

# Prompt the user for branch selection
echo "Which branches do you want to revert the last merge for?"
echo "1. Revert last merge for all branches"
echo "2. Select specific branches"

read -p "Enter your choice (1/2): " choice

if [ "$choice" == "1" ]; then
  # If the user chooses to revert last merge for all branches
  site_branches=("${all_site_branches[@]}")
elif [ "$choice" == "2" ]; then
  # If the user chooses to select specific branches
  echo "Enter the branch names you want to revert the last merge for (space-separated):"
  read -a selected_branches

  site_branches=()
  for branch in "${selected_branches[@]}"; do
    if [[ " ${all_site_branches[@]} " =~ " $branch " ]]; then
      site_branches+=("$branch")
    else
      echo "Branch '$branch' not found in the list of site branches."
    fi
  done
else
  echo "Invalid choice. Please enter '1' to revert last merge for all branches or '2' to select specific branches."
  exit 1
fi

# Make changes to the master branch
git checkout master
# Make your changes here
# git add .
# git commit -m "Your commit message"

for branch in "${site_branches[@]}"; do
  echo "Reverting last merge for branch $branch..."
  git checkout "$branch"
  git log --oneline | grep "Merge changes from master" | head -n 1 | cut -d ' ' -f 1 | xargs git revert -m 1
done

# Return to the master branch
git checkout master
