1#!/bin/bash -eu
2
3# This script generates a commit that updates our copy of UnicodeJS
4
5if [ -n "${2:-}" ]
6then
7	# Too many parameters
8	echo >&2 "Usage: $0 [<version>]"
9	exit 1
10fi
11
12REPO_DIR=$(cd "$(dirname $0)/.."; pwd) # Root dir of the git repo working tree
13TARGET_DIR="lib/unicodejs" # Destination relative to the root of the repo
14NPM_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'update-unicodejs') # e.g. /tmp/update-unicodejs.rI0I5Vir
15
16# Prepare working tree
17cd "$REPO_DIR"
18git reset -- $TARGET_DIR
19git checkout -- $TARGET_DIR
20git fetch origin
21git checkout -B upstream-unicodejs origin/master
22
23# Fetch upstream version
24cd $NPM_DIR
25if [ -n "${1:-}" ]
26then
27	npm install "unicodejs@$1"
28else
29	npm install unicodejs
30fi
31
32UNICODEJS_VERSION=$(node -e 'console.log(require("./node_modules/unicodejs/package.json").version);')
33if [ "$UNICODEJS_VERSION" == "" ]
34then
35	echo 'Could not find UnicodeJS version'
36	exit 1
37fi
38
39# Copy file(s)
40rsync --force ./node_modules/unicodejs/dist/unicodejs.js "$REPO_DIR/$TARGET_DIR"
41
42# Clean up temporary area
43rm -rf "$NPM_DIR"
44
45# Generate commit
46cd $REPO_DIR
47
48COMMITMSG=$(cat <<END
49Update UnicodeJS to v$UNICODEJS_VERSION
50
51Release notes:
52 https://gerrit.wikimedia.org/g/unicodejs/+/$UNICODEJS_VERSION/History.md
53END
54)
55
56# Stage deletion, modification and creation of files. Then commit.
57git add --update $TARGET_DIR
58git add $TARGET_DIR
59git commit -m "$COMMITMSG"
60