#!/bin/bash
# Script to set Git credentials in production .env file

set -e

cd "$(dirname "$0")/.."

echo "Setting Git credentials in production .env file..."
echo ""
echo "Choose authentication method:"
echo "1) Personal Access Token (recommended)"
echo "2) Username/Password"
read -p "Enter choice (1 or 2): " choice

case $choice in
  1)
    read -p "Enter Gitea Personal Access Token: " token
    ansible production -i inventory/production.yml -m lineinfile \
      -a "path=~/deployment/stacks/application/.env regexp='^GIT_TOKEN=' line='GIT_TOKEN=$token' state=present" 2>&1
    echo "✅ GIT_TOKEN set successfully"
    ;;
  2)
    read -p "Enter Gitea Username: " username
    read -s -p "Enter Gitea Password: " password
    echo ""
    ansible production -i inventory/production.yml -m lineinfile \
      -a "path=~/deployment/stacks/application/.env regexp='^GIT_USERNAME=' line='GIT_USERNAME=$username' state=present" 2>&1
    ansible production -i inventory/production.yml -m lineinfile \
      -a "path=~/deployment/stacks/application/.env regexp='^GIT_PASSWORD=' line='GIT_PASSWORD=$password' state=present" 2>&1
    echo "✅ GIT_USERNAME and GIT_PASSWORD set successfully"
    ;;
  *)
    echo "❌ Invalid choice"
    exit 1
    ;;
esac

echo ""
echo "Next steps:"
echo "1. Restart the nginx container: docker compose restart nginx"
echo "2. Check logs: docker compose logs nginx"
