{{- $latest       := index site.Data.releases 1 -}}
{{- $releases     := site.Data.releases -}}
{{- $docRedirects := site.Data.redirects }}

# Redirect to latest doc version by default
{{- if eq (len site.Data.releases) 1 }}
/docs     /docs/edge      301!
{{- else }}
/docs     /docs/latest    301!
{{- end }}

# v0.14.0 re-org redirects
# TODO: Remove at some point in the future when its more ok to break old links en-mass
/docs/latest/get-started                            /docs/latest/                           301!
/docs/latest/how-does-opa-work/                     /docs/latest/#how-does-opa-work         301!
/docs/latest/how-do-i-write-policies/               /docs/latest/policy-language/           301!
/docs/latest/how-do-i-test-policies/                /docs/latest/policy-testing/            301!
/docs/latest/language-reference/                    /docs/latest/policy-reference/          301!
/docs/latest/language-cheatsheet/                   /docs/latest/edge/policy-cheatsheet/    301!
/docs/latest/bundles/                               /docs/latest/management-bundles/        301!
/docs/latest/status/                                /docs/latest/management-status/         301!
/docs/latest/decision-logs/                         /docs/latest/management-decision-logs/  301!
/docs/latest/discovery/                             /docs/latest/management-discovery/      301!
/docs/latest/management/                            /docs/latest/management-introduction/   301!
/docs/latest/kubernetes-admission-control/          /docs/latest/kubernetes-tutorial/       301!

# v0.21.0 re-org
/docs/latest/policy-cheatsheet/                     /docs/latest/policy-reference/          301!

# v0.35.0 extended contrib sections
/docs/latest/development/                           /docs/latest/contrib-code/          301!

{{- range $docRedirects }}
/docs/{{ . }}      /docs/latest/{{ . }} 301!
/docs/{{ . }}.html /docs/latest/{{ . }} 301! # Legacy git book redirects
{{- end }}

# Download URLs
/downloads/edge/*       https://opa-releases.s3.amazonaws.com/edge/:splat 200
/downloads/latest/*     https://github.com/open-policy-agent/opa/releases/download/{{ $latest }}/:splat 200
/downloads/*            https://github.com/open-policy-agent/opa/releases/download/:splat 200

# previous patch versions
{{- range $releases }}
  {{- if (not (or (eq . "latest") (eq . "edge"))) }}
    {{- $major := index (split . ".") 0 }}
    {{- $minor := index (split . ".") 1 }}
    {{- $patch := int (index (split . ".") 2) }}
    {{- range after 1 (seq $patch -1 0) }}
/docs/{{ $major }}.{{ $minor }}.{{ . }}/* /docs/{{ $major }}.{{ $minor }}.{{ $patch }}/:splat
    {{- end }}
  {{- end }}
{{- end }}
