11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci# Shell script to update icu in the source tree to a specific version
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciBASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
61cb0ef41Sopenharmony_ciDEPS_DIR="$BASE_DIR/deps"
71cb0ef41Sopenharmony_ciTOOLS_DIR="$BASE_DIR/tools"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node"
101cb0ef41Sopenharmony_ci[ -x "$NODE" ] || NODE=$(command -v node)
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciNEW_VERSION="$("$NODE" --input-type=module <<'EOF'
131cb0ef41Sopenharmony_ciconst res = await fetch('https://api.github.com/repos/unicode-org/icu/releases/latest');
141cb0ef41Sopenharmony_ciif (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res });
151cb0ef41Sopenharmony_ciconst { tag_name } = await res.json();
161cb0ef41Sopenharmony_ciconsole.log(tag_name.replace('release-', '').replace('-','.'));
171cb0ef41Sopenharmony_ciEOF
181cb0ef41Sopenharmony_ci)"
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciICU_VERSION_H="$DEPS_DIR/icu-small/source/common/unicode/uvernum.h"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciCURRENT_VERSION="$(grep "#define U_ICU_VERSION " "$ICU_VERSION_H" | cut -d'"' -f2)"
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
271cb0ef41Sopenharmony_ci  echo "Skipped because icu is on the latest version."
281cb0ef41Sopenharmony_ci  exit 0
291cb0ef41Sopenharmony_cifi
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciDASHED_NEW_VERSION=$(echo "$NEW_VERSION" | sed 's/\./-/g')
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciLOW_DASHED_NEW_VERSION=$(echo "$NEW_VERSION" | sed 's/\./_/g')
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciNEW_VERSION_TGZ="icu4c-${LOW_DASHED_NEW_VERSION}-src.tgz"
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciNEW_VERSION_TGZ_URL="https://github.com/unicode-org/icu/releases/download/release-${DASHED_NEW_VERSION}/$NEW_VERSION_TGZ"
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciNEW_VERSION_MD5="https://github.com/unicode-org/icu/releases/download/release-${DASHED_NEW_VERSION}/icu4c-${LOW_DASHED_NEW_VERSION}-sources.md5"
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci./configure --with-intl=full-icu --with-icu-source="$NEW_VERSION_TGZ_URL"
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci"$TOOLS_DIR/icu/shrink-icu-src.py"
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cirm -rf "$DEPS_DIR/icu"
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciCHECKSUM=$(curl -sL "$NEW_VERSION_MD5" | grep "$NEW_VERSION_TGZ" | grep -v "\.asc$" | awk '{print $1}')
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciGENERATED_CHECKSUM=$( curl -sL "$NEW_VERSION_TGZ_URL" | md5sum | cut -d ' ' -f1)
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciecho "Comparing checksums: deposited $CHECKSUM with $GENERATED_CHECKSUM"
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ciif [ "$CHECKSUM" != "$GENERATED_CHECKSUM" ]; then
541cb0ef41Sopenharmony_ci  echo "Skipped because checksums do not match."
551cb0ef41Sopenharmony_ci  exit 0
561cb0ef41Sopenharmony_cifi
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciperl -i -pe "s|\"url\": .*|\"url\": \"$NEW_VERSION_TGZ_URL\",|" "$TOOLS_DIR/icu/current_ver.dep"
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciperl -i -pe "s|\"md5\": .*|\"md5\": \"$CHECKSUM\"|" "$TOOLS_DIR/icu/current_ver.dep"
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_cirm -rf out "$DEPS_DIR/icu" "$DEPS_DIR/icu4c*"
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciecho "All done!"
651cb0ef41Sopenharmony_ciecho ""
661cb0ef41Sopenharmony_ciecho "Please git add icu, commit the new version:"
671cb0ef41Sopenharmony_ciecho ""
681cb0ef41Sopenharmony_ciecho "$ git add -A deps/icu-small"
691cb0ef41Sopenharmony_ciecho "$ git add tools/icu/current_ver.dep"
701cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update icu to $NEW_VERSION\""
711cb0ef41Sopenharmony_ciecho ""
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
741cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
751cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
76