#!/bin/bash

# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Script for updating Node binaries.
# 1) Update NODE_VERSION variable below to the desired version.
# 2) Run this script.
# 3) Upload the binaries to the Google Storage bucket (commands to upload
#    binaries are printed at step 2, look for "gsutil.py").
# 4) Land a CL with the changes generated by this script.

set -eu
cd "$(dirname "$0")"

BASE_URL="https://nodejs.org/dist"
NODE_VERSION="v8.9.1"

update_unix() {
  local SUFFIX="$1"
  local FOLDER="$2"
  local FILENAME="node-${NODE_VERSION}-${SUFFIX}.tar.gz"
  local URL="${BASE_URL}/${NODE_VERSION}/${FILENAME}"

  rm -f "${FOLDER}/${FILENAME}"
  wget -P "${FOLDER}/" "${URL}"

  # Check SHASUMS256 of downloaded binary.
  local sha256_expected
  sha256_expected="$(grep "$FILENAME" SHASUMS256.txt | cut -d ' ' -f1)"
  local sha256_actual
  sha256_actual="$(sha256sum "${FOLDER}/${FILENAME}" | cut -d ' ' -f1)"

  if [ "${sha256_expected}" != "${sha256_actual}" ]; then
    echo "SHA256 mismatch. Exiting..."
    exit 1
  fi

  # Unpack temporarily, delete NPM symlink and re-pack.
  tar xfz "${FOLDER}/${FILENAME}" -C "${FOLDER}/"
  rm "${FOLDER}/${FILENAME}"
  rm "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/bin/npm"
  rm "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/bin/npx"

  # Drop the version info from the name, since it is redundant and would make
  # rolling new versions more involved.
  rm -rf "${FOLDER}/node-${SUFFIX}/"
  mv "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/" "${FOLDER}/node-${SUFFIX}/"
  tar cfz "${FOLDER}/node-${SUFFIX}.tar.gz" -C "${FOLDER}" "node-${SUFFIX}/"
  local sha1
  sha1="$(sha1sum ${FOLDER}/node-${SUFFIX}.tar.gz | cut -d ' ' -f1)"
  echo "${sha1}" > "${FOLDER}/node-${SUFFIX}.tar.gz.sha1"
  echo "Please execute manually the following:"
  echo "> gsutil.py cp ${FOLDER}/node-${SUFFIX}.tar.gz gs://chromium-nodejs/${NODE_VERSION:1}/${sha1}"
  echo "DONE updating for ${SUFFIX}."
}

update_win() {
  local FILENAME="node.exe"
  local FOLDER="win"
  local WINDOWS_URL="${BASE_URL}/${NODE_VERSION}/win-x64/${FILENAME}"
  rm -f "${FOLDER}/${FILENAME}"
  wget -P "${FOLDER}/" "${WINDOWS_URL}"

  # Check SHASUMS256 of downloaded binary.
  local sha256_expected
  sha256_expected="$(grep "win-x64/$FILENAME" SHASUMS256.txt | cut -d ' ' -f1)"
  local sha256_actual
  sha256_actual="$(sha256sum "${FOLDER}/${FILENAME}" | cut -d ' ' -f1)"

  if [ "${sha256_expected}" != "${sha256_actual}" ]; then
    echo "SHA256 mismatch. Exiting..."
    exit 1
  fi

  local sha1
  sha1="$(sha1sum ${FOLDER}/node.exe | cut -d ' ' -f1)"
  echo "${sha1}" > "${FOLDER}/node.exe.sha1"
  echo "Please execute manually the following:"
  echo "> gsutil.py cp ${FOLDER}/node.exe gs://chromium-nodejs/${NODE_VERSION:1}/${sha1}"
  echo "DONE updating Windows."
}

# First download checksum file.
rm -f "SHASUMS256.txt"
wget "https://nodejs.org/dist/latest-v8.x/SHASUMS256.txt"

update_unix "darwin-x64" "mac"
update_unix "linux-x64" "linux"
update_win

# Update DEPS to point to the new Google Storage bucket subfolder.
sed -i "s@\(chromium-nodejs/\)\([0-9\.]\)\+@\1${NODE_VERSION:1}@" ../../DEPS
