#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use HTTP::Tiny;

my %files = (
  # metaschema for json schemas contained within openapi documents
  'oas/dialect/base.schema.json'  => 'https://spec.openapis.org/oas/3.1/dialect/base',

  # vocabulary definition
  'oas/meta/base.schema.json'  => 'https://spec.openapis.org/oas/3.1/meta/base',

  # openapi document schema + custom json schema dialect
  #'oas/schema-base.json' => 'https://spec.openapis.org/oas/3.1/schema-base',
  'oas/schema-base.json' => 'https://spec.openapis.org/oas/3.1/schema-base/2021-09-28',

  # the main openapi document schema
  #'oas/schema.json' => 'https://spec.openapis.org/oas/3.1/schema',
  'oas/schema.json' => 'https://spec.openapis.org/oas/3.1/schema/2021-09-28',
);

foreach my $target (keys %files) {
  my $source = $files{$target};
  $target = path('share', $target);
  $target->parent->mkpath;

  my $response = HTTP::Tiny->new->get($source);
  die "Failed to fetch $source: $response->{status} $response->{reason}" if not $response->{success};

  $target->spew($response->{content});
}
