#!/usr/bin/env bash

set -e

# Ask for commit message, use default if empty.
default_msg="Update"

commit_msg="$1"

if [ -z "$commit_msg" ]; then
	read -rp "Enter commit message (Enter to use '$default_msg'): " commit_msg
fi

if [ -z "$commit_msg" ]; then
	commit_msg="$default_msg"
fi

while read -r git_folder; do
	repo_folder=$(dirname $git_folder)
	full_path=$(realpath $repo_folder)
	repo_name=$(basename "$full_path")
	# Un-comment the following for a visual representation of what the above shit do
	# echo "┌────────────────┬────────────────────────────────┐"
	# echo "│ Variable Name  │ Variable Result                │"
	# echo "├────────────────┼────────────────────────────────┘"
	# echo "│ git_folder     │ $git_folder"
	# echo "│ repo_folder    │ $repo_folder"
	# echo "│ full_path      │ $full_path"
	# echo "│ repo_name      │ $repo_name"
	# echo "└────────────────┘"
	(
		output=$(
			{
				echo "──────────────── ℹ️  Pushing $repo_name ℹ️  ────────────────"
				git -C "$full_path" add .
				git -C "$full_path" commit -m "$commit_msg"
				git -C "$full_path" push
				echo "──────────────── ℹ️  Pushed $repo_name ℹ️  ─────────────────"
			} 2>&1
		)
		echo "$output"
	) &
done < <(find . -name ".git" -type d)

wait
