#!/bin/bash

# Main repository path
MAIN_REPO="C:/_devData/_frame/_master/frontend"
MAIN_BRANCH_NAME="master"
# List of local repositories
LOCAL_REPOS=(
  "C:/_devData/_frame/bufearu/frontend"
  "C:/_devData/_frame/foodpack/frontend"
  "C:/_devData/_frame/matisz/frontend"
  "C:/_devData/_frame/ndrhorse/frontend"
  "C:/_devData/_frame/overtones/frontend"
  "C:/_devData/_frame/portableweb/frontend"
  # Add more paths as needed
)
LOCAL_BRANCH="staging"

# Main script
echo "Updating local repositories and pushing changes from the original repository: $MAIN_REPO"

push_to_all_local_repos() {
  echo "Push to all local repos"
  for repo in "${LOCAL_REPOS[@]}"; do
    push_to_single_local_repo "$repo"
  done
}

push_to_single_local_repo() {
  local local_repo=$1
  echo "Pushing changes from the original: $MAIN_REPO repository to: $local_repo"
  # Enter the local repository directory
  cd "$local_repo" || exit

  # Check if git initialised on repo
  if [ ! -d .git ]; then
    echo "Git repository not initialized. Initializing..."
    git init
  else
    echo "Git repository already initialized."
  fi

  # Check if local_changes branch exists in current repo
  if git rev-parse --verify $LOCAL_BRANCH > /dev/null 2>&1; then
    echo "Branch exists"
    # Get changes from $MAIN_REPO
    git pull $MAIN_REPO $MAIN_BRANCH_NAME
    # Merge it with local repo's master
    select_merge_action
  else
      echo "Branch does not exist"
      git checkout -B $LOCAL_BRANCH
      git pull $MAIN_REPO $MAIN_BRANCH_NAME
      select_merge_action
  fi 
}

merge_with_master() {
   # Check if master branch exists in the current repo
  if git rev-parse --verify master > /dev/null 2>&1; then
    echo "Master branch exists"
    # Switch to the master branch
    git checkout master
    # Merge changes from the local branch to master
    git merge $LOCAL_BRANCH
  else
    echo "Master branch does not exist"
    # Create and switch to the master branch
    git checkout -b master
    # Merge changes from the local branch to master
    git merge $LOCAL_BRANCH
  fi
}

select_local_repo() {
   PS3="Select a local repository to reset and push changes: "
  select repo_option in "${LOCAL_REPOS[@]}"; do
    if [ -n "$repo_option" ]; then
      push_to_single_local_repo "$repo_option"
      break
    else
      echo "Invalid option. Please choose a number from the list."
    fi
  done
}

select_action() {
  PS3="Select an action:"
  select option in "Push changes to a SINGLE local repository" "Push changes to ALL local repositories" "Exit"; do
    case $REPLY in
      1)
        select_local_repo
        break
        ;;
      2)
        push_to_all_local_repos
        break
        ;;
      
      3)
        echo "Stop update process."
        exit
        ;;
      *)
        echo "Invalid option. Please choose 1 or 2."
        ;;
    esac
  done
}
select_merge_action() {
  PS3="Select a merge action:"
  select option in "Merge with master" "Exit"; do
    case $REPLY in
      1)
        merge_with_master
        break
        ;;
      2)
        echo "Stop process without merge with master."
        exit
        ;;
      *)
        echo "Invalid option. Please choose 1 or 2."
        ;;
    esac
  done
}
select_action



echo "Update and push complete!"