#!/usr/bin/env bash
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Builds the current version of catapult, uploads it to GCS and updates the
# pinned SHA256 in install-build-deps.

set -e

PROJECT_ROOT="$(cd -P ${BASH_SOURCE[0]%/*}/..; pwd)"

if [ "$1" == "" ]; then
  echo "Usage: $0 /path/to/catapult/repo"
  exit 1
fi

CATAPULT="$1"
if [ ! -d "$CATAPULT/.git" ]; then
  echo "$CATAPULT must point to a valid catapult repo"
  exit 1
fi

REVISION=$(git -C "$CATAPULT" rev-parse --short HEAD)
OUTDIR="$(mktemp -d)"
echo "Building vulcanized Trace Viewer @ $REVISION into $OUTDIR"
git -C "$CATAPULT" log -1 | cat
echo
set -x
"$CATAPULT/tracing/bin/generate_about_tracing_contents" --outdir "$OUTDIR"
ARCHIVE="$OUTDIR/catapult_trace_viewer.tar.gz"

(
  cd "$OUTDIR"
  cat >extra_origin_trials <<EOF
  <!-- WebComponents V0 origin trial token for https://*.ui.perfetto.dev
  Expires 1 Feb 2021. https://crbug.com/1021137. -->
  <meta http-equiv="origin-trial" content="AjGFDFU57Af4e5OJJQd7kmYR0nEiObDCHkev6BBWzhGohACl1ri+pMhaVe9V8dDBaXDkWy4g7WYj3c5GiPwatgIAAABreyJvcmlnaW4iOiJodHRwczovL3VpLnBlcmZldHRvLmRldjo0NDMiLCJmZWF0dXJlIjoiV2ViQ29tcG9uZW50c1YwIiwiZXhwaXJ5IjoxNjEyMjIzOTk5LCJpc1N1YmRvbWFpbiI6dHJ1ZX0=">

  <!-- WebComponents V0 origin trial token for http://localhost:10000
  Expires 28 Jan 2021. https://crbug.com/1021137. -->
  <meta http-equiv="origin-trial" content="AicMEv5glMGL1lq6ZRsxFJj8xlhn3XDYZrHK0/2KreAD/r62vTFjUBOueeMTxWuU1IlRXqCugRFDD7rY45YEgwkAAABTeyJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjEwMDAwIiwiZmVhdHVyZSI6IldlYkNvbXBvbmVudHNWMCIsImV4cGlyeSI6MTYxMTg0MDczNH0=">

  <!-- WebComponents V0 origin trial token for https://staging-dot-perfetto-ui.appspot.com
  Expires 1 Feb 2021. https://crbug.com/1021137. -->
  <meta http-equiv="origin-trial" content="Au1cwnWfBB/GCD22HnNZE93/KamhGDsz8BZbEewICJB2PRtW+E1bobrtZbTZs8q5748uRiKXPvgaut5JOZ8jSw4AAABseyJvcmlnaW4iOiJodHRwczovL3N0YWdpbmctZG90LXBlcmZldHRvLXVpLmFwcHNwb3QuY29tOjQ0MyIsImZlYXR1cmUiOiJXZWJDb21wb25lbnRzVjAiLCJleHBpcnkiOjE2MTIyMjM5OTl9">

  <!-- WebComponents V0 origin trial token for https://storage.googleapis.com/
  Expires 1 Feb 2021. https://crbug.com/1021137. -->
  <meta http-equiv="origin-trial" content="AtobKUpdVFIb6cx2Ev0EbAFX4SzLuXPnsnADRA8JV5w4B64q65gz42shquyLLNd2QP9rY22oNGxbatpTO0kd2AIAAABfeyJvcmlnaW4iOiJodHRwczovL3N0b3JhZ2UuZ29vZ2xlYXBpcy5jb206NDQzIiwiZmVhdHVyZSI6IldlYkNvbXBvbmVudHNWMCIsImV4cGlyeSI6MTYxMjIyMzk5OX0=">
EOF

  mv about_tracing.html catapult_trace_viewer.html
  mv about_tracing.js catapult_trace_viewer.js
  sed -i -e \
      's|src="tracing.js"|src="catapult_trace_viewer.js"|g' \
      catapult_trace_viewer.html
   sed -i -e '/<head/r extra_origin_trials' catapult_trace_viewer.html
  tar -zcf "$ARCHIVE" catapult_trace_viewer.{js,html}
)

SHA256CMD='import hashlib; import sys; sha1=hashlib.sha256(); sha1.update(sys.stdin.read()); print(sha1.hexdigest())'
SHA256=$(python -c "$SHA256CMD" < "$ARCHIVE")
GCS_TARGET="gs://perfetto/catapult_trace_viewer-$SHA256.tar.gz"
gsutil cp -n -a public-read "$ARCHIVE" "$GCS_TARGET"
rm -rf "$OUTDIR"

# Update the reference to the new prebuilt in tools/install-build-deps.
sed -i -e \
    "s/^CATAPULT_SHA256 =.*/CATAPULT_SHA256 = '"$SHA256"'/g" \
     "$PROJECT_ROOT/tools/install-build-deps"

"$PROJECT_ROOT/tools/install-build-deps" --ui
