WITH metamod_rule AS (
  SELECT '[
    {
      "id": "cs2-install-latest-metamod",
      "event": "server.install.completed",
      "enabled": true,
      "runOncePerServer": true,
      "continueOnError": false,
      "actions": [
        {
          "id": "install-cs2-metamod",
          "type": "http_directory_extract",
          "indexUrl": "https://mms.alliedmods.net/mmsdrop/2.0/",
          "assetNamePattern": "^mmsource-2\\.0\\.0-git\\d+-linux\\.tar\\.gz$",
          "destination": "/game/csgo",
          "stripComponents": 0,
          "maxBytes": 268435456
        }
      ]
    }
  ]'::jsonb AS rule
)
UPDATE "games" g
SET
  "automation_rules" = CASE
    WHEN g."automation_rules" IS NULL OR jsonb_typeof(g."automation_rules") <> 'array'
      THEN (SELECT rule FROM metamod_rule)
    ELSE g."automation_rules" || (SELECT rule FROM metamod_rule)
  END,
  "updated_at" = now()
WHERE
  g."slug" = 'cs2'
  AND NOT (
    COALESCE(g."automation_rules", '[]'::jsonb) @> '[{"id":"cs2-install-latest-metamod"}]'::jsonb
  );
