#!/bin/bash

# Script to create git-repos directories on the deployment server
# This fixes the EACCES permission denied error for git-integration service

set -e

echo "🔧 Setting up git-repos directories for deployment..."

# Define the base directory
BASE_DIR="/home/ubuntu/codenuk-backend-live"
GIT_REPOS_DIR="$BASE_DIR/git-repos"
DIFFS_DIR="$GIT_REPOS_DIR/diffs"

# Create directories if they don't exist
echo "📁 Creating directories..."
mkdir -p "$GIT_REPOS_DIR"
mkdir -p "$DIFFS_DIR"

# Set proper ownership (UID 1001 matches the git-integration user in container)
echo "👤 Setting ownership to UID 1001 (git-integration user)..."
sudo chown -R 1001:1001 "$GIT_REPOS_DIR"

# Set proper permissions
echo "🔒 Setting permissions..."
chmod -R 755 "$GIT_REPOS_DIR"

# Verify the setup
echo "✅ Verifying setup..."
ls -la "$GIT_REPOS_DIR"

echo "🎉 Git repos directories setup completed successfully!"
echo "📍 Base directory: $GIT_REPOS_DIR"
echo "📍 Diffs directory: $DIFFS_DIR"
echo ""
echo "Now you can run the deployment again:"
echo "docker compose up -d git-integration"
