terraform {
  required_providers {
    null = {
      source = "hashicorp/null"
      version = "~> 3.0"
    }
  }
}

resource "null_resource" "update_config" {
  triggers = {
    always_run = timestamp()
  }

  provisioner "local-exec" {
    command = <<-EOT
      echo "Starting update_config script..." >&2
      echo "Current directory: $(pwd)" >&2
      echo "Looking for .rapidrc file..." >&2
      
      # Look for .rapidrc in the parent directory (project root)
      CONFIG_PATH="../../.rapidrc"
      if [ ! -f "$CONFIG_PATH" ]; then
        echo "ERROR: Config file not found at: $CONFIG_PATH" >&2
        echo "Directory contents:" >&2
        ls -la ../../ >&2
        exit 1
      fi
      
      echo "Found .rapidrc file. Current content:" >&2
      cat "$CONFIG_PATH" >&2
      
      echo "Attempting to update remote_machine with IP: ${var.floating_ip}" >&2
      # Use sed to update the remote_machine field in .rapidrc format
      sed -i '' "s/^  remote_machine:.*/  remote_machine: ${var.floating_ip}/" "$CONFIG_PATH"
      
      echo "Update complete. New content:" >&2
      cat "$CONFIG_PATH" >&2
      
      echo "Updated remote_machine in .rapidrc with IP: ${var.floating_ip}" >&2
    EOT
    interpreter = ["/bin/bash", "-c"]
  }
}