<commit_before>#!/bin/bash
set -ue

. functions.sh

cd "$(cd "${0%/*}" && pwd -P)"

IFS=' ' read -ra versions <<<"$(get_versions .)"
IFS=' ' read -ra update_versions <<<"$(get_versions . "$@")"
if [ ${#versions[@]} -eq 0 ]; then
  fatal "No valid versions found!"
fi

# Global variables
# Get architecure and use this as target architecture for docker image
# See details in function.sh
# TODO: Should be able to specify target architecture manually
arch=$(get_arch)

yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)"

function in_versions_to_update() {
  local version=$1

  for version_to_update in "${update_versions[@]}"; do
    if [ "${version_to_update}" = "${version}" ]; then
      echo 0
      return
    fi
  done

  echo 1
}

function update_node_version() {

  local baseuri=${1}
  shift
  local version=${1}
  shift
  local template=${1}
  shift
  local dockerfile=${1}
  shift
  local variant=""
  if [ $# -eq 1 ]; then
    variant=${1}
    shift
  fi

  fullVersion="$(curl -sSL --compressed "${baseuri}" | grep '<a href="v'"${version}." | sed -E 's!.*<a href="v([^"/]+)/?".*!\1!' | cut -d'.' -f2,3 | sort -n | tail -1)"
  (
    cp "${template}" "${dockerfile}"
    local fromprefix=""
    if [ "${arch}" != "amd64" ] && [ "${variant}" != "onbuild" ]; then
      fromprefix="${arch}\\/"
    fi

    nodeVersion="${version}.${fullVersion:-0}"

    sed -Ei -e 's/^FROM (.*)/FROM '"$fromprefix"'\1/' "${dockerfile}"
    sed -Ei -e 's/^(ENV NODE_VERSION ).*/\1'"${nodeVersion}"'/' "${dockerfile}"
    sed -Ei -e 's/^(ENV YARN_VERSION ).*/\1'"${yarnVersion}"'/' "${dockerfile}"

    # Only for onbuild variant
    sed -Ei -e 's/^(FROM .*node:)[^-]*(-.*)/\1'"${nodeVersion}"'\2/' "${dockerfile}"

    # shellcheck disable=SC1004
    new_line=' \\\
'

    # Add GPG keys
    for key_type in "node" "yarn"; do
      while read -r line; do
        pattern='"\$\{'$(echo "${key_type}" | tr '[:lower:]' '[:upper:]')'_KEYS\[@\]\}"'
        sed -Ei -e "s/([ \\t]*)(${pattern})/\\1${line}${new_line}\\1\\2/" "${dockerfile}"
      done <"keys/${key_type}.keys"
      sed -Ei -e "/${pattern}/d" "${dockerfile}"
    done

    if [ "${variant}" = "alpine" ]; then
      alpine_version=$(get_config "./" "alpine_version")
      sed -Ei -e "s/(alpine:)0.0/\\1${alpine_version}/" "${dockerfile}"
    fi

    # Required for POSIX sed
    if [ -f "${dockerfile}-e" ]; then
      rm "${dockerfile}-e"
    fi
  )
}

function add_stage() {
  local baseuri=${1}
  shift
  local version=${1}
  shift
  local variant=${1}
  shift

  echo '
    - stage: Build
      env:<commit_msg>Add option to skip updating yarn and alpine when doing a security update<commit_after>#!/bin/bash
set -ue

function usage() {
  cat <<EOF

  Update the node docker images.

  Usage:
    $0 [-s] [MAJOR_VERSION] [VARIANT]

  Examples:
    - update.sh            # Update all images
    - update.sh -s         # Update all images, skip updating Alpine and Yarn
    - update.sh 8          # Update version 8 and variants (default, slim, alpine etc.)
    - update.sh -s 8       # Update version 8 and variants, skip updating Alpine and Yarn
    - update.sh 8 slim     # Update all variants for version 8
    - update.sh -s 8 slim  # Update all variants for version 8, skip updating Alpine and Yarn

  OPTIONS:
    -s Security update; skip updating the yarn and alpine versions.
    -h Show this message

EOF
}

SKIP=false
while getopts "sh" opt; do
  case "${opt}" in
    s)
      SKIP=true
      ;;
    h)
      usage
      exit
      ;;
    \?)
      usage
      exit
      ;;
  esac
done

. functions.sh

cd "$(cd "${0%/*}" && pwd -P)"

IFS=' ' read -ra versions <<<"$(get_versions .)"
IFS=' ' read -ra update_versions <<<"$(get_versions . "$@")"
if [ ${#versions[@]} -eq 0 ]; then
  fatal "No valid versions found!"
fi

# Global variables
# Get architecure and use this as target architecture for docker image
# See details in function.sh
# TODO: Should be able to specify target architecture manually
arch=$(get_arch)

if [ "${SKIP}" != true ]; then
  alpine_version=$(get_config "./" "alpine_version")
  yarnVersion="$(curl -sSL --compressed https://yarnpkg.com/latest-version)"
fi

function in_versions_to_update() {
  local version=$1

  for version_to_update in "${update_versions[@]}"; do
    if [ "${version_to_update}" = "${version}" ]; then
      echo 0
      return
    fi
  done

  echo 1
}

function update_node_version() {

  local baseuri=${1}
  shift
  local version=${1}
  shift
  local template=${1}
  shift
  local dockerfile=${1}
  shift
  local variant=""
  if [ $# -eq 1 ]; then
    variant=${1}
    shift
  fi

  fullVersion="$(curl -sSL --compressed "${baseuri}" | grep '<a href="v'"${version}." | sed -E 's!.*<a href="v([^"/]+)/?".*!\1!' | cut -d'.' -f2,3 | sort -n | tail -1)"
  (
    cp "${template}" "${dockerfile}-tmp"
    local fromprefix=""
    if [ "${arch}" != "amd64" ] && [ "${variant}" != "onbuild" ]; then
      fromprefix="${arch}\\/"
    fi

    nodeVersion="${version}.${fullVersion:-0}"

    sed -Ei -e 's/^FROM (.*)/FROM '"$fromprefix"'\1/' "${dockerfile}-tmp"
    sed -Ei -e 's/^(ENV NODE_VERSION ).*/\1'"${nodeVersion}"'/' "${dockerfile}-tmp"

    if [ "${SKIP}" = true ]; then
      # Get the currently used Yarn version
      yarnVersion="$(grep "ENV YARN_VERSION" "${dockerfile}" | cut -d' ' -f3)"
    fi
    sed -Ei -e 's/^(ENV YARN_VERSION ).*/\1'"${yarnVersion}"'/' "${dockerfile}-tmp"

    # Only for onbuild variant
    sed -Ei -e 's/^(FROM .*node:)[^-]*(-.*)/\1'"${nodeVersion}"'\2/' "${dockerfile}-tmp"

    # shellcheck disable=SC1004
    new_line=' \\\
'

    # Add GPG keys
    for key_type in "node" "yarn"; do
      while read -r line; do
        pattern='"\$\{'$(echo "${key_type}" | tr '[:lower:]' '[:upper:]')'_KEYS\[@\]\}"'
        sed -Ei -e "s/([ \\t]*)(${pattern})/\\1${line}${new_line}\\1\\2/" "${dockerfile}"
      done <"keys/${key_type}.keys"
      sed -Ei -e "/${pattern}/d" "${dockerfile}-tmp"
    done

    if [ "${variant}" = "alpine" ]; then
      if [ "${SKIP}" = true ]; then
        # Get the currently used Alpine version
        alpine_version=$(grep "FROM" "${dockerfile}" | cut -d':' -f2)
      fi
      sed -Ei -e "s/(alpine:)0.0/\\1${alpine_version}/" "${dockerfile}-tmp"
    fi

    # Required for POSIX sed
    if [ -f "${dockerfile}-tmp-e" ]; then
      rm "${dockerfile}-tmp-e"
    fi
    mv -f "${dockerfile}-tmp" "${dockerfile}"
  )
}

function add_stage() {
  local baseuri=${1}
  shift
  local version=${1}
  shift
  local variant=${1}
  shift

  echo '
    - stage: Build
      env:
{'fraction_non_alphanumeric': 0.20167555217060168, 'fraction_numerical': 0.012642802741812642, 'mean_word_length': 2.7349260523321957, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1582918', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2274, 'n_words': 648}