job "nomad-consul-setup" {
  datacenters = ["dc1"]
  type = "system"

  group "nomad-config" {
    task "setup-consul" {
      driver = "exec"
      
      config {
        command = "sh"
        args = [
          "-c",
          "if grep -q 'server.*enabled.*true' /etc/nomad.d/nomad.hcl; then sed -i '/^consul {/,/^}/c\\consul {\\n  address = \"ch4.tailnet-68f9.ts.net:8500,ash3c.tailnet-68f9.ts.net:8500,warden.tailnet-68f9.ts.net:8500\"\\n  server_service_name = \"nomad\"\\n  client_service_name = \"nomad-client\"\\n  auto_advertise = true\\n  server_auto_join = true\\n  client_auto_join = false\\n}' /etc/nomad.d/nomad.hcl; else sed -i '/^consul {/,/^}/c\\consul {\\n  address = \"ch4.tailnet-68f9.ts.net:8500,ash3c.tailnet-68f9.ts.net:8500,warden.tailnet-68f9.ts.net:8500\"\\n  server_service_name = \"nomad\"\\n  client_service_name = \"nomad-client\"\\n  auto_advertise = true\\n  server_auto_join = false\\n  client_auto_join = true\\n}' /etc/nomad.d/nomad.hcl; fi && systemctl restart nomad"
        ]
      }

      resources {
        cpu = 100
        memory = 128
      }
    }
  }
}
