site/generate-htaccess.sh
#!/bin/bash

USAGE="Usage: $0 [<release> ...]
"

[[ $# -gt 0 ]] || { echo "${USAGE}Expected at least one argument." >&2 ; exit 1 ; }

set -o pipefail
set -o errexit
set -o nounset

cat <<EOF
# THIS FILE IS GENERATED
#
# See: <https://github.com/jenkins-infra/update-center2/blob/master/site/generate-htaccess.sh>
RewriteEngine on

# https://github.com/jenkinsci/jenkins/blob/56b6623915c50b4a2ef994a143a8fe0829587f3c/core/src/main/java/hudson/model/UpdateCenter.java#L1182-L1207
RewriteCond %{QUERY_STRING} ^.*uctest$
RewriteRule ^(|.+/)(update\-center.*\.(json|html)+) /uctest.json [NC,L]
EOF

echo "# Version-specific rulesets generated by generate.sh"
n=$#
versions=( "$@" )
newestStable=
oldestStable=
oldestWeekly=

for (( i = n-1 ; i >= 0 ; i-- )) ; do
  version="${versions[i]}"
  IFS=. read -ra versionPieces <<< "$version"

  major=${versionPieces[0]}
  minor=${versionPieces[1]}
  patch=
  if [[ ${#versionPieces[@]} -gt 2 ]] ; then
    patch=${versionPieces[2]}
  fi

  if [[ "$version" =~ ^2[.][0-9]+[.][0-9]$ ]] ; then
    # This is an LTS version
    if [[ -z "$newestStable" ]] ; then
      newestStable="$version"
    fi

    cat <<EOF

# If major > ${major} or major = ${major} and minor > ${minor} or major = ${major} and minor = ${minor} and patch >= ${patch}, use this LTS update site
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)(|[-].*)$ [NC]
RewriteCond %1 >${major}
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R]
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)(|[-].*)$ [NC]
RewriteCond %1 =${major}
RewriteCond %2 >${minor}
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R]
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)\.(\d+)(|[-].*)$ [NC]
RewriteCond %1 =${major}
RewriteCond %2 =${minor}
RewriteCond %3 >=${patch}
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-stable-${major}\.${minor}\.${patch}%{REQUEST_URI}? [NC,L,R]
EOF
    oldestStable="$version"
  else
    # This is a weekly version
    # Split our version up into an array for rewriting
    # 1.651 becomes (1 651)
    oldestWeekly="$version"
    cat <<EOF

# If major > ${major} or major = ${major} and minor >= ${minor}, use this weekly update site
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)(|[-].*)$ [NC]
RewriteCond %1 >${major}
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-${major}\.${minor}%{REQUEST_URI}? [NC,L,R]
RewriteCond %{QUERY_STRING} ^.*version=(\d)\.(\d+)(|[-].*)$ [NC]
RewriteCond %1 =${major}
RewriteCond %2 >=${minor}
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-${major}\.${minor}%{REQUEST_URI}? [NC,L,R]
EOF

  fi
done

cat <<EOF


# First LTS update site (stable-$oldestStable) gets all older LTS releases

RewriteCond %{QUERY_STRING} ^.*version=\d\.(\d+)\.\d+(|[-].*)$ [NC]
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-stable-${oldestStable}%{REQUEST_URI}? [NC,L,R]

RewriteCond %{QUERY_STRING} ^.*version=\d\.(\d+)+(|[-].*)$ [NC]
RewriteRule ^(update\-center.*\.(json|html)+) /dynamic-${oldestWeekly}%{REQUEST_URI}? [NC,L,R]

EOF


echo "# Add a RewriteRule for /stable which will always rewrite to the last LTS site we have"
cat <<EOF
RewriteRule ^stable/(.+) "/dynamic-stable-${newestStable}/\$1" [NC,L,R]

EOF



# Further static rules
cat <<EOF


# These are static rules

# If that all failed, but we have an update center, let's go to current
RewriteRule ^update\-center.*\.(json|html)+ /current%{REQUEST_URI}? [NC,L,R=301]

RewriteRule ^latestCore\.txt /current%{REQUEST_URI}? [NC,L,R=301]

# Ensure /release-history.json goes to the right place
RewriteRule ^release\-history\.json$ /current%{REQUEST_URI}? [NC,L,R=301]

# Ensure /plugin-documentation-urls.json goes to the right place
RewriteRule ^plugin\-documentation\-urls\.json$ /current%{REQUEST_URI}? [NC,L,R=301]

# Ensure /plugin-versions.json goes to the right place
RewriteRule ^plugin\-versions\.json$ /current%{REQUEST_URI}? [NC,L,R=301]


DirectoryIndex index.html

# download/* directories contain virtual URL spaces for redirecting download traffic to mirrors.

# 'latest' need special handling here since they're not getting mirrored properly to get.jenkins.io
RewriteRule ^download/war/latest/jenkins[.]war$ https://updates.jenkins.io/latest/jenkins.war [NC,L,R=302]
RewriteRule ^download/plugins/(.*)/latest/(.+)[.]hpi$ https://updates.jenkins.io/latest/\$2.hpi [NC,L,R=302]

RewriteRule ^download/war/([0-9]+[.][0-9]+[.][0-9]+/jenkins)[.]war$ https://get.jenkins.io/war-stable/\$1.war [NC,L,R=302]
RewriteRule ^download/war/(.+)[.]war$ https://get.jenkins.io/war/\$1.war [NC,L,R=302]
RewriteRule ^download/plugins/(.+)[.]hpi$ https://get.jenkins.io/plugins/\$1.hpi [NC,L,R=302]
EOF
