tao-test/app/vendor/respect/validation/bin/update-iso-codes

99 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# Usage: {script} TLD_FILENAME
# Update list of TLD
set -euo pipefail
declare -r IFS=$'\n'
declare -r REPOSITORY_URL="https://salsa.debian.org/iso-codes-team/iso-codes.git"
declare -r REPOSITORY_DIRECTORY=$(mktemp --directory)
declare -r LIBRARY_DIRECTORY="${1}"
clone_repository()
{
echo "Cloning repository ${REPOSITORY_URL}"
git clone --quiet "${REPOSITORY_URL}" "${REPOSITORY_DIRECTORY}"
}
list_iso_3166()
{
local -r number_of_items=${1}
local -r filename=${2}
for index in $(seq 0 ${number_of_items}); do
local json=$(jq ".[][${index}]" < "${filename}")
local alpha_2=$(jq ".alpha_2" <<< "${json}" | tr '"' "'")
local alpha_3=$(jq ".alpha_3" <<< "${json}" | tr '"' "'")
local numeric=$(jq ".numeric" <<< "${json}" | tr '"' "'")
local name=$(jq -r ".name" <<< "${json}")
echo " [${alpha_2}, ${alpha_3}, ${numeric}], // ${name}"
done
}
update_country_codes()
{
local -r iso_3166_1_filename="${REPOSITORY_DIRECTORY}/data/iso_3166-1.json"
local -r iso_3166_1_count=$(grep "alpha_3" "${iso_3166_1_filename}" | wc --lines)
local -r iso_3166_3_filename="${REPOSITORY_DIRECTORY}/data/iso_3166-3.json"
local -r iso_3166_3_count=$(grep "alpha_3" "${iso_3166_3_filename}" | wc --lines)
local -r temporary_filename=$(mktemp)
local -r country_rule_filename="${LIBRARY_DIRECTORY}/Rules/CountryCode.php"
echo "Updating country codes using ISO 3166-1 and ISO 3166-3"
{
sed -n '/^</,/ protected/p' "${country_rule_filename}"
{
list_iso_3166 $[iso_3166_1_count - 1] ${iso_3166_1_filename}
list_iso_3166 $[iso_3166_3_count - 1] ${iso_3166_3_filename}
} | sort
sed -n '/^ ]/,/^}/p' "${country_rule_filename}"
} > "${temporary_filename}"
mv "${temporary_filename}" "${country_rule_filename}"
}
rule_from_country_code()
{
local country_code=${1,,}
echo "${LIBRARY_DIRECTORY}/Rules/SubdivisionCode/${country_code^}SubdivisionCode.php"
}
update_subdivision_codes()
{
local -r iso_3166_2_filename="${REPOSITORY_DIRECTORY}/data/iso_3166-2.json"
local -r iso_3166_2_count=$(grep --word-regexp "code" "${iso_3166_2_filename}" | wc --lines)
local -r temporary_filename=$(mktemp)
local last_country_code=""
local last_country_rule=""
echo "Updating subdivision codes using ISO 3166-2"
for index in $(seq 0 $[iso_3166_2_count - 1]); do
local json=$(jq ".[][${index}]" < "${iso_3166_2_filename}")
local code=$(jq -r ".code" <<< "${json}")
local name=$(jq -r ".name" <<< "${json}")
local current_country_code=$(cut --delimiter "-" --fields 1 <<< "${code}")
local current_country_rule=$(rule_from_country_code ${current_country_code})
if [[ ${last_country_code} != ${current_country_code} ]] && [[ ! -z "${last_country_code}" ]]; then
sed -n '/^ \]/,/^}/p' "${last_country_rule}" >> "${temporary_filename}"
cat "${temporary_filename}" > "${last_country_rule}"
fi
if [[ ${last_country_code} != ${current_country_code} ]]; then
echo "- Updating ${current_country_code}"
sed -n '/^</,/public \$haystack = \[/p' "${current_country_rule}" > "${temporary_filename}"
fi
echo " '$(cut --delimiter "-" --fields 2 <<< ${code})', // ${name}" >> "${temporary_filename}"
last_country_code=${current_country_code}
last_country_rule=${current_country_rule}
done
}
clone_repository
update_country_codes
update_subdivision_codes