11cb0ef41Sopenharmony_ci#!/bin/sh
21cb0ef41Sopenharmony_ciset -e
31cb0ef41Sopenharmony_ci# Shell script to update nghttp2 in the source tree to specific version
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciBASE_DIR=$(cd "$(dirname "$0")/../.." && pwd)
61cb0ef41Sopenharmony_ciDEPS_DIR="$BASE_DIR/deps"
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci[ -z "$NODE" ] && NODE="$BASE_DIR/out/Release/node"
91cb0ef41Sopenharmony_ci[ -x "$NODE" ] || NODE=$(command -v node)
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci# shellcheck disable=SC1091
121cb0ef41Sopenharmony_ci. "$BASE_DIR/tools/dep_updaters/utils.sh"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciNEW_VERSION="$("$NODE" --input-type=module <<'EOF'
151cb0ef41Sopenharmony_ciconst res = await fetch('https://api.github.com/repos/nghttp2/nghttp2/releases/latest');
161cb0ef41Sopenharmony_ciif (!res.ok) throw new Error(`FetchError: ${res.status} ${res.statusText}`, { cause: res });
171cb0ef41Sopenharmony_ciconst { tag_name } = await res.json();
181cb0ef41Sopenharmony_ciconsole.log(tag_name.replace('v', ''));
191cb0ef41Sopenharmony_ciEOF
201cb0ef41Sopenharmony_ci)"
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciCURRENT_VERSION=$(grep "#define NGHTTP2_VERSION" ./deps/nghttp2/lib/includes/nghttp2/nghttp2ver.h | sed -n "s/^.*VERSION \"\(.*\)\"/\1/p")
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciecho "Comparing $NEW_VERSION with $CURRENT_VERSION"
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciif [ "$NEW_VERSION" = "$CURRENT_VERSION" ]; then
271cb0ef41Sopenharmony_ci  echo "Skipped because nghttp2 is on the latest version."
281cb0ef41Sopenharmony_ci  exit 0
291cb0ef41Sopenharmony_cifi
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciecho "Making temporary workspace"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciWORKSPACE=$(mktemp -d 2> /dev/null || mktemp -d -t 'tmp')
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cicleanup () {
361cb0ef41Sopenharmony_ci  EXIT_CODE=$?
371cb0ef41Sopenharmony_ci  [ -d "$WORKSPACE" ] && rm -rf "$WORKSPACE"
381cb0ef41Sopenharmony_ci  exit $EXIT_CODE
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_citrap cleanup INT TERM EXIT
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciNGHTTP2_REF="v$NEW_VERSION"
441cb0ef41Sopenharmony_ciNGHTTP2_TARBALL="nghttp2-$NEW_VERSION.tar.gz"
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cicd "$WORKSPACE"
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciecho "Fetching nghttp2 source archive"
491cb0ef41Sopenharmony_cicurl -sL -o "$NGHTTP2_TARBALL" "https://github.com/nghttp2/nghttp2/releases/download/$NGHTTP2_REF/$NGHTTP2_TARBALL"
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciDEPOSITED_CHECKSUM=$(curl -sL "https://github.com/nghttp2/nghttp2/releases/download/$NGHTTP2_REF/checksums.txt" | grep "$NGHTTP2_TARBALL")
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cilog_and_verify_sha256sum "nghttp2" "$NGHTTP2_TARBALL" "$DEPOSITED_CHECKSUM"
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cigzip -dc "$NGHTTP2_TARBALL" | tar xf -
561cb0ef41Sopenharmony_cirm "$NGHTTP2_TARBALL"
571cb0ef41Sopenharmony_cimv "nghttp2-$NEW_VERSION" nghttp2
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciecho "Removing everything, except lib/ and COPYING"
601cb0ef41Sopenharmony_cicd nghttp2
611cb0ef41Sopenharmony_cifor dir in *; do
621cb0ef41Sopenharmony_ci  if [ "$dir" = "lib" ] || [ "$dir" = "COPYING" ]; then
631cb0ef41Sopenharmony_ci    continue
641cb0ef41Sopenharmony_ci  fi
651cb0ef41Sopenharmony_ci  rm -rf "$dir"
661cb0ef41Sopenharmony_cidone
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci# Refs: https://github.com/nodejs/node/issues/45572
691cb0ef41Sopenharmony_ciecho "Copying config.h file"
701cb0ef41Sopenharmony_cicp "$DEPS_DIR/nghttp2/lib/includes/config.h" "$WORKSPACE/nghttp2/lib/includes"
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ciecho "Copying existing gyp files"
731cb0ef41Sopenharmony_cicp "$DEPS_DIR/nghttp2/nghttp2.gyp" "$WORKSPACE/nghttp2"
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciecho "Replacing existing nghttp2"
761cb0ef41Sopenharmony_cirm -rf "$DEPS_DIR/nghttp2"
771cb0ef41Sopenharmony_cimv "$WORKSPACE/nghttp2" "$DEPS_DIR/"
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciecho "All done!"
801cb0ef41Sopenharmony_ciecho ""
811cb0ef41Sopenharmony_ciecho "Please git add nghttp2, commit the new version:"
821cb0ef41Sopenharmony_ciecho ""
831cb0ef41Sopenharmony_ciecho "$ git add -A deps/nghttp2"
841cb0ef41Sopenharmony_ciecho "$ git commit -m \"deps: update nghttp2 to $NEW_VERSION\""
851cb0ef41Sopenharmony_ciecho ""
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci# The last line of the script should always print the new version,
881cb0ef41Sopenharmony_ci# as we need to add it to $GITHUB_ENV variable.
891cb0ef41Sopenharmony_ciecho "NEW_VERSION=$NEW_VERSION"
90