#!/bin/sh

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

if [ $# -eq 0 ]; then

  if [ -d "${releng_directory}/.git" ]; then
    git -C "${releng_directory}" remote update
  else
    git -C "${releng_directory}" fetch origin master:master
  fi || \
    true

  country_codes=$(
    git -C "${releng_directory}" archive master -- 'flag-names' | \
      tar -Ox
  )

  tmp_file=$(
    mktemp "tmp.check-mirrors.XXXXXXXXXX" --tmpdir
  )
  trap 'rm "${tmp_file}"' EXIT

  git -C "${repo_paths__archlinux32}" archive HEAD -- 'core/pacman-mirrorlist/mirrorlist' | \
    sed -n '
      1,/^$/d
      /^## [^A-Z]/d
      s/^##/Country/
      s/^#Server = /Server /
      T
      p
    ' | \
    while read -r what rest; do
      case "${what}" in
        'Country')
          country="${rest}"
          country_code=$(
            printf '%s\n' "${country_codes}" | \
              grep '^\S\+ '"$(str_to_regex "${country}")"'$' | \
              cut -d' ' -f1
          )
          continue
        ;;
        'Server')
          printf '%s %s %s\n' \
            "$(printf '%s' "${country}" | base64 -w0)" \
            "$(printf '%s' "${country_code}" | base64 -w0)" \
            "$(printf '%s' "${rest}" | base64 -w0)"
        ;;
      esac
    done | \
    parallel -j10 "$0" "{}" > \
    "${tmp_file}"

  # shellcheck disable=SC2016
  {
    printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `mirror_statuses` (' \
      "${tmp_file}"
    printf '`%s`,' \
      'protocol' \
      'url' \
      'country' \
      'country_code' \
      'last_sync' \
      'start' \
      'stop' \
      'isos' \
      'ipv4' \
      'ipv6' \
      'active' | \
      sed 's/,$//'
    printf ');\n'
  } | \
    mysql_run_query

elif [ $# -eq 1 ]; then
  # check a single mirror
  url="$1"
  country=$(
    printf '%s' "${url%% *}" | \
      base64 -d
  )
  url="${url#* }"
  country_code=$(
    printf '%s' "${url%% *}" | \
      base64 -d
  )
  url=$(
    # shellcheck disable=SC2016
    printf '%s' "${url#* }" | \
      base64 -d | \
      sed 's,/\$arch/\$repo$,/,'
  )
  start=$(
    date '+%s.%N'
  )
  success=1
  has_ipv4=1
  if last_sync=$(
    curl -4 -s "${url}lastsync"
  ); then
    if printf '%s' "${last_sync}" | \
      tr '\n' 'X' | \
      grep -q '[^0-9]'; then
      has_ipv4=0
    fi
  else
    has_ipv4=0
  fi
  has_ipv6=1
  if cnt=$(
    curl -6 -s "${url}lastsync"
  ); then
    if printf '%s' "${cnt}" | \
      tr '\n' 'X' | \
      grep -q '[^0-9]'; then
      has_ipv6=0
    else
      last_sync="${cnt}"
    fi
  else
    has_ipv6=0
  fi
  if [ ${has_ipv6} -eq 1 ]; then
    ip_flag='-6'
  elif [ ${has_ipv4} -eq 1 ]; then
    ip_flag='-4'
  else
    success=0
  fi
  if [ ${success} -eq 1 ]; then
    has_isos=1
    cnt=$(
      curl -s "${ip_flag}" "${url}archisos/"
    ) || has_isos=0
    for suffix in 'i686' 'dual'; do
      if ! printf '%s\n' "${cnt}" | \
        grep -qF "$(date '+archlinux-%Y.%m.01-'"${suffix}"'.iso')"; then
        has_isos=0
        break
      fi
    done
  else
    has_isos=0
    last_sync='0'
  fi
  stop=$(
    date '+%s.%N'
  )
  # columns to output:
  #    "protocol" => "rsync",
  #    "url" => "rsync://mirror.aarnet.edu.au/archlinux/",
  #    "country" => "Australia",
  #    "country_code" => "AU",
  #    "last_sync" => "1529051726",
  #    "start" => "1529051726.369490757",
  #    "stop" => "1529051726.369490757",
  #    "isos" => true,
  #    "ipv4" => true,
  #    "ipv6" => true,
  #    "active" => true,
  printf '%s\t' \
    "${url%%:*}" \
    "${url}" \
    "${country}" \
    "${country_code}" \
    "${last_sync}" \
    "${start}" \
    "${stop}" \
    "${has_isos}" \
    "${has_ipv4}" \
    "${has_ipv6}" \
    "${success}" | \
    sed 's/\t$/\n/'
else
  >&2 echo 'usage:'
  >&2 echo '  check-mirrors'
  >&2 echo '    check all mirrors and update database'
  # shellcheck disable=SC2016
  >&2 echo '  check-mirrors $country $country_code $url'
  >&2 echo '    check the given mirror and output values to stdout'
fi