#!/bin/bash

echo "🚀 Starting sync to Git repository..."

# Check if git is initialized
if [ ! -d ".git" ]; then
  git init
  git branch -M main
  echo "✅ Git repository initialized."
fi

# Add all files respecting .gitignore
git add .

# Commit with current timestamp
COMMIT_MSG="Sync update: $(date +'%Y-%m-%d %H:%M:%S')"
git commit -m "$COMMIT_MSG"

echo "✅ Committed with message: $COMMIT_MSG"

# Ensure the user has added a remote origin before pushing
REMOTE=$(git remote -v)
if [ -z "$REMOTE" ]; then
  echo "⚠️  No remote origin found."
  echo "Please add your GitHub/Git repository URL first by running:"
  echo "git remote add origin YOUR_GIT_URL"
  echo "Then run this script again."
  exit 1
fi

# Push to the current branch
BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "📤 Pushing to branch: $BRANCH..."
git push origin "$BRANCH"

echo "✅ Sync complete! You can now run 'git pull' on your server."
