#!/bin/bash

# Define the directory where PostgreSQL logs are stored
LOG_DIR="/c/Program Files/PostgreSQL/16/data/log"

# Define the output file for extracted SQL commands
OUTPUT_FILE="migration.sql"

# Define the AWK script file path
AWK_SCRIPT_FILE="filter_sql.awk" # Make sure this file is in the same directory as your bash script

# Clear the output file if it exists, or create a new one
> "$OUTPUT_FILE"

echo "Starting SQL command extraction from logs in: $LOG_DIR"
echo "Extracted commands will be saved to: $OUTPUT_FILE"
echo "----------------------------------------------------"

# --- NEW PART: GET LAST GIT COMMIT DATE ---
GIT_LAST_COMMIT_DATE_MS="" # This will store the Git commit date in YYYY-MM-DD HH:MM:SS.000 format

# Check if we are inside a Git repository
if git rev-parse --is-inside-work-tree &>/dev/null; then
    # Get the last commit date in ISO 8601 strict format (e.g., 2025-07-31T16:21:44+02:00)
    GIT_LAST_COMMIT_ISO=$(git log -1 --format=%cd --date=iso-strict)

    if [ -n "$GIT_LAST_COMMIT_ISO" ]; then
        # Convert to YYYY-MM-DD HH:MM:SS.000 for comparison with log timestamps
        # cut -d'.' -f1 gets "YYYY-MM-DDTHH:MM:SS"
        # tr 'T' ' ' changes 'T' to space
        # "\.000" appends milliseconds for consistent string comparison
        GIT_LAST_COMMIT_DATE_MS=$(echo "$GIT_LAST_COMMIT_ISO" | cut -d'.' -f1 | tr 'T' ' ')"\.000"
        echo "Git last commit date detected (for log cutoff): $GIT_LAST_COMMIT_DATE_MS"
    else
        echo "Warning: Could not get last Git commit date. Processing all log entries."
    fi
else
    echo "Not inside a Git repository. Processing all log entries."
fi

# Regex to detect the start of a new log entry (used by awk)
NEW_LOG_ENTRY_REGEX="^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3}"

# --- Check if the AWK script file exists ---
if [ ! -f "$AWK_SCRIPT_FILE" ]; then
    echo "ERROR: AWK script file '$AWK_SCRIPT_FILE' not found!"
    echo "Please create '$AWK_SCRIPT_FILE' with the AWK code from Step 1."
    exit 1
fi

# Find all .log files in the specified directory and process them
find "$LOG_DIR" -type f -name "*.log" | while read -r logfile; do
    echo "Processing log file: $logfile"
    LC_ALL=C awk -v new_log_regex="$NEW_LOG_ENTRY_REGEX" \
                 -v git_cutoff_date_str="$GIT_LAST_COMMIT_DATE_MS" \
                 -f "$AWK_SCRIPT_FILE" "$logfile" >> "$OUTPUT_FILE"
done

echo "SQL extraction complete"
echo "----------------------------------------------------"
echo "Final SQL output is in: '$OUTPUT_FILE'."