1#! /bin/sh
2
3# Copyright 2018 Google Inc.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7set -e
8
9BASE_DIR='platform_tools/android/apps/skqp/src/main/assets'
10PATH_LIST='gmkb'
11BUCKET=skia-skqp-assets
12
13EXTANT="$(mktemp "${TMPDIR:-/tmp}/extant.XXXXXXXXXX")"
14gsutil ls gs://$BUCKET/ | sed "s|^gs://$BUCKET/||"  > "$EXTANT"
15
16upload() {
17    MD5=$(md5sum < "$1" | head -c 32)
18    if ! grep -q "$MD5" "$EXTANT"; then
19        URL="gs://${BUCKET}/$MD5"
20        gsutil cp "$1" "$URL" > /dev/null 2>&1 &
21    fi
22    echo $MD5
23}
24
25size() { gsutil du -s gs://$BUCKET | awk '{print $1}'; }
26
27cd "$(dirname "$0")/../../$BASE_DIR"
28
29rm -f files.checksum
30
31FILES="$(mktemp "${TMPDIR:-/tmp}/files.XXXXXXXXXX")"
32
33: > "$FILES"
34
35COUNT=$(find $PATH_LIST -type f | wc -l)
36INDEX=1
37SHARD_COUNT=32
38
39SIZE=$(size)
40find $PATH_LIST -type f | sort | while IFS= read -r FILENAME; do
41    printf '\r %d / %d   ' "$INDEX" "$COUNT"
42    if ! [ -f "$FILENAME" ]; then
43        echo error [${FILENAME}] >&2;
44        exit 1;
45    fi
46    case "$FILENAME" in *\;*) echo bad filename: $FILENAME >&2; exit 1;; esac
47    MD5=$(upload "$FILENAME")
48    printf '%s;%s\n' "$MD5" "$FILENAME" >> "$FILES"
49
50    if [ $(($INDEX % $SHARD_COUNT)) = 0 ]; then
51        wait
52    fi
53    INDEX=$(( $INDEX + 1))
54done
55printf '\rdone          \n'
56upload "$FILES" > files.checksum
57wait
58
59D=$(( $(size) - $SIZE ))
60printf 'Added %d bytes to %s, %d%%\n' $D $BUCKET $(( $D * 100 / $SIZE ))
61
62rm "$EXTANT"
63