#!/bin/bash

start_time=$(date +%s)

# Set environment variables
export GLITCHTIP_DSN="https://d272afa8b4bd4308b3cf99ee74e1dc97@glitch.nixc.us/2"
export LOG_FILES="test_log_1.log,test_log_2.log"

echo "Using Glitchtip DSN: $GLITCHTIP_DSN"
echo "Monitoring log files: $LOG_FILES"

# Create test log files
echo "Creating test log files..."
echo "Initial log content" > test_log_1.log
echo "Initial log content" > test_log_2.log

# Run the Go program in the background
echo "Starting the Go program..."
./dist/inotify-glitch_darwin_arm64 &

# Capture the PID of the Go program
GO_PID=$!

# Give the program a moment to start
sleep 2

# Append a line with an error to one of the log files
echo "Appending error line to test_log_1.log..."
echo "This is an error line" >> test_log_1.log

# Give the program some time to process the change
sleep 2

# Check if the Go program detected the change (you can enhance this section with more specific checks)
echo "Check the logs to verify the program detected the error line."

# Capture Go program logs for debugging
ps -p $GO_PID > /dev/null
if [ $? -eq 0 ]; then
  echo "The Go program is running."
else
  echo "The Go program has terminated unexpectedly."
fi

# Cleanup: kill the Go program
echo "Terminating the Go program..."
kill $GO_PID

# Optionally: Remove test log files
echo "Removing test log files..."
rm test_log_1.log test_log_2.log

end_time=$(date +%s)
execution_time=$((end_time - start_time))

# Fix for macOS date command
execution_time_formatted=$(printf '%02d:%02d:%02d' $((execution_time/3600)) $((execution_time%3600/60)) $((execution_time%60)))

echo "Execution time: $execution_time_formatted"
