# Settings in the [build] context are global and are applied to all contexts unless otherwise overridden by more specific contexts.
[build]
  publish = "_site/"
  command = '''

  cp netlify/Gemfile Gemfile && bundle install
  make bin/ocp.tgz
  export PATH=$PATH:$(pwd)/bin

  # LATEST_RELEASE is set in Netlify UI for environment-variables ONLY for docs.projectcalico.org and projectcalico.docs.tigera.io sites.
  # Any branch deployed there will have the production ready build that gets deployed at root /

  if [ -z "$LATEST_RELEASE" ]; then

    # generate only manifests served at /<version>
    jekyll build --config _config.yml,$(pwd)/netlify/_config_noindex.yml,$(pwd)/netlify/_manifests_only.yml --baseurl /$RELEASE_VERSION --destination _site/$RELEASE_VERSION
    cp -r ../manifests _site/$RELEASE_VERSION

    # generate the website which is served at /archive/<version>/
    # note: this must be done second since destination folders are cleaned on each builds
    jekyll build --config _config.yml,$(pwd)/netlify/_config_noindex.yml --baseurl /archive/$RELEASE_VERSION --destination _site/archive/$RELEASE_VERSION
    cp -r ../manifests _site/archive/$RELEASE_VERSION
    cp bin/ocp.tgz _site/archive/$RELEASE_VERSION/manifests/

    # move 404 page to root of the site so that netlify shows it automatically
    mv $(pwd)/_site/archive/$RELEASE_VERSION/404.html $(pwd)/_site/404.html

  else

    # generate the website which is served at root /
    jekyll build --config _config.yml,$(pwd)/netlify/_config_latest.yml --destination _site
    cp -r ../manifests _site/$RELEASE_VERSION
    cp bin/ocp.tgz _site/$RELEASE_VERSION/manifests/

    # copy the manifests into the the latest manifests path since they are no longer generated by the docs site
    cp -r ../manifests _site/manifests/
    cp bin/ocp.tgz _site/manifests/

    # generate the website which is served at /archive/<version>/
    jekyll build --config _config.yml,$(pwd)/netlify/_config_noindex.yml --baseurl /archive/$RELEASE_VERSION --destination _site/archive/$RELEASE_VERSION
    cp -r ../manifests _site/archive/$RELEASE_VERSION
    cp bin/ocp.tgz _site/archive/$RELEASE_VERSION/manifests/

    mv $(pwd)/_site/sitemap.xml $(pwd)/_site/latest-sitemap.xml
    mv $(pwd)/netlify/sitemap-index.xml $(pwd)/_site/sitemap.xml
    mv $(pwd)/netlify/_redirects $(pwd)/_site/_redirects
  fi
  '''

[build.environment]
  RUBY_VERSION = "2.5.4"
  RELEASE_VERSION = "v3.25"

[context.deploy-preview]
  command = '''
  cp netlify/Gemfile Gemfile && bundle install
  make bin/ocp.tgz
  export PATH=$PATH:$(pwd)/bin
  echo "url: $DEPLOY_PRIME_URL" > _config_url.yml
  jekyll build --config _config.yml,_config_url.yml
  mv netlify/_redirects _site/_redirects
  cp -r ../manifests _site/
  cp bin/ocp.tgz _site/manifests/
  '''

# Deploys master branch as a separate individual site, master build is different from branch deploys
# it doesn't have a manifests only build and served without /archive prefix
[context.master]
  command = '''
  cp netlify/Gemfile Gemfile && bundle install
  make bin/ocp.tgz
  export PATH=$PATH:$(pwd)/bin
  jekyll build --config _config.yml,$(pwd)/netlify/_config_noindex.yml --baseurl /master --destination _site/master
  cp -r ../manifests _site/master/
  cp bin/ocp.tgz _site/master/manifests/
  '''

# proxy redirect for Helm chart repo
[[redirects]]
   from = "/charts/*"
   to = "https://calico-public.s3.amazonaws.com/charts/:splat"
   status = 200
   headers = {X-From = "Netlify"}

# proxy redirects for website and manifests for v3.25
#[[redirects]]
#  from = "/archive/v3.25/*"
#  to = "https://calico-v3-25.netlify.app/archive/v3.25/:splat"
#  status = 200

# proxy redirects for website and manifests for v3.24
[[redirects]]
  from = "/archive/v3.24/*"
  to = "https://calico-v3-24.netlify.app/archive/v3.24/:splat"
  status = 200

# proxy redirects for website and manifests for v3.23
[[redirects]]
  from = "/archive/v3.23/*"
  to = "https://calico-v3-23.netlify.app/archive/v3.23/:splat"
  force = true
  status = 200

# proxy redirects for website and manifests for v3.22
[[redirects]]
  from = "/archive/v3.22/*"
  to = "https://calico-v3-22.netlify.app/archive/v3.22/:splat"
  status = 200

# proxy redirects for website and manifests for v3.21
[[redirects]]
  from = "/archive/v3.21/*"
  to = "https://calico-v3-21.netlify.app/archive/v3.21/:splat"
  status = 200

# proxy redirects for website and manifests for v3.20
[[redirects]]
  from = "/archive/v3.20/*"
  to = "https://calico-v3-20.netlify.app/archive/v3.20/:splat"
  status = 200

# proxy redirects for website and manifests for v3.19
[[redirects]]
  from = "/archive/v3.19/*"
  to = "https://calico-v3-19.netlify.app/archive/v3.19/:splat"
  status = 200

[[redirects]]
  from = "/v3.19/*"
  to = "https://calico-v3-19.netlify.app/v3.19/:splat"
  status = 200

# proxy redirects for website and manifests for v3.18
[[redirects]]
  from = "/archive/v3.18/*"
  to = "https://calico-v3-18.netlify.app/archive/v3.18/:splat"
  status = 200

[[redirects]]
  from = "/v3.18/*"
  to = "https://calico-v3-18.netlify.app/v3.18/:splat"
  status = 200

# proxy redirects for website and manifests for v3.17
[[redirects]]
  from = "/archive/v3.17/*"
  to = "https://calico-v3-17.netlify.app/archive/v3.17/:splat"
  status = 200

[[redirects]]
  from = "/v3.17/*"
  to = "https://calico-v3-17.netlify.app/v3.17/:splat"
  status = 200

# proxy redirects for website and manifests for v3.16
[[redirects]]
  from = "/archive/v3.16/*"
  to = "https://calico-v3-16.netlify.app/archive/v3.16/:splat"
  status = 200

[[redirects]]
  from = "/v3.16/*"
  to = "https://calico-v3-16.netlify.app/v3.16/:splat"
  status = 200

# proxy redirects for website and manifests for v3.15
[[redirects]]
  from = "/archive/v3.15/*"
  to = "https://calico-v3-15.netlify.app/archive/v3.15/:splat"
  status = 200

[[redirects]]
  from = "/v3.15/*"
  to = "https://calico-v3-15.netlify.app/v3.15/:splat"
  status = 200

# proxy redirects for website and manifests for v3.14
[[redirects]]
  from = "/archive/v3.14/*"
  to = "https://calico-v3-14.netlify.app/archive/v3.14/:splat"
  status = 200

[[redirects]]
  from = "/v3.14/*"
  to = "https://calico-v3-14.netlify.app/v3.14/:splat"
  status = 200

# proxy redirects for website and manifests for v3.13
[[redirects]]
  from = "/archive/v3.13/*"
  to = "https://calico-v3-13.netlify.app/archive/v3.13/:splat"
  status = 200

[[redirects]]
  from = "/v3.13/*"
  to = "https://calico-v3-13.netlify.app/v3.13/:splat"
  status = 200

# proxy redirects for website and manifests for v3.12
[[redirects]]
  from = "/archive/v3.12/*"
  to = "https://calico-v3-12.netlify.app/archive/v3.12/:splat"
  status = 200

[[redirects]]
  from = "/v3.12/*"
  to = "https://calico-v3-12.netlify.app/v3.12/:splat"
  status = 200

# proxy redirects for website for master, it doesn't need manifests redirect
[[redirects]]
  from = "/master/*"
  to = "https://calico-master.netlify.app/master/:splat"
  status = 200

# site redirects for archive sites, one redirect is sufficient for the entire release legacy site.
[[redirects]]
  from = "/archive/*"
  to = "https://calico-legacy.netlify.app/archive/:splat"
  status = 200

# supporting /<version>/manifests/<manifest.yaml> format for archive sites
# all the redirect rules are nessesarry since the proxy needs to know where to redirect for each path
[[redirects]]
  from = "/v3.11/*"
  to = "https://calico-legacy.netlify.app/v3.11/:splat"
  status = 200

[[redirects]]
  from = "/v3.10/*"
  to = "https://calico-legacy.netlify.app/v3.10/:splat"
  status = 200

[[redirects]]
  from = "/v3.9/*"
  to = "https://calico-legacy.netlify.app/v3.9/:splat"
  status = 200

[[redirects]]
  from = "/v3.8/*"
  to = "https://calico-legacy.netlify.app/v3.8/:splat"
  status = 200

[[redirects]]
  from = "/v3.7/*"
  to = "https://calico-legacy.netlify.app/v3.7/:splat"
  status = 200

[[redirects]]
  from = "/v3.6/*"
  to = "https://calico-legacy.netlify.app/v3.6/:splat"
  status = 200

[[redirects]]
  from = "/v3.5/*"
  to = "https://calico-legacy.netlify.app/v3.5/:splat"
  status = 200

[[redirects]]
  from = "/v3.4/*"
  to = "https://calico-legacy.netlify.app/v3.4/:splat"
  status = 200

[[redirects]]
  from = "/v3.3/*"
  to = "https://calico-legacy.netlify.app/v3.3/:splat"
  status = 200

[[redirects]]
  from = "/v3.2/*"
  to = "https://calico-legacy.netlify.app/v3.2/:splat"
  status = 200

[[redirects]]
  from = "/v3.1/*"
  to = "https://calico-legacy.netlify.app/v3.1/:splat"
  status = 200

[[redirects]]
  from = "/v3.0/*"
  to = "https://calico-legacy.netlify.app/v3.0/:splat"
  status = 200

[[redirects]]
  from = "/v2.6/*"
  to = "https://calico-legacy.netlify.app/v2.6/:splat"
  status = 200

[[redirects]]
  from = "/v2.5/*"
  to = "https://calico-legacy.netlify.app/v2.5/:splat"
  status = 200

[[redirects]]
  from = "/v2.4/*"
  to = "https://calico-legacy.netlify.app/v2.4/:splat"
  status = 200

[[redirects]]
  from = "/v2.3/*"
  to = "https://calico-legacy.netlify.app/v2.3/:splat"
  status = 200

[[redirects]]
  from = "/v2.2/*"
  to = "https://calico-legacy.netlify.app/v2.2/:splat"
  status = 200

[[redirects]]
  from = "/v2.1/*"
  to = "https://calico-legacy.netlify.app/v2.1/:splat"
  status = 200

[[redirects]]
  from = "/v2.0/*"
  to = "https://calico-legacy.netlify.app/v2.0/:splat"
  status = 200

[[redirects]]
  from = "/v1.6/*"
  to = "https://calico-legacy.netlify.app/v1.6/:splat"
  status = 200

[[redirects]]
  from = "/v1.5/*"
  to = "https://calico-legacy.netlify.app/v1.5/:splat"
  status = 200

# shifting generic redirect of /<version> to / from _redirects to toml
[[redirects]]
  from = "/latest/*"
  to = "/:splat"
  status = 301

[[redirects]]
  from = "/*"
  to = "https://docs.tigera.io/calico/:splat"
  force = true
  status = 301

[[headers]]
  for = "/*.yaml"
  [headers.values]
    content-type = "text/yaml"

[[headers]]
  for = "/*.yml"
  [headers.values]
    content-type = "text/yaml"

[[headers]]
  for = "/*.sh"
  [headers.values]
    content-type = "text/x-shellscript"

[[headers]]
  for = "/*.bash"
  [headers.values]
    content-type = "text/x-shellscript"
