#!/bin/sh

# This script is used to automate combining an upstream version of Ace
# with the patches that we maintain.
#
# If you need to run this script, some prerequisites are required.
# - Install Node.js and npm
# - Run tools/sync-ace-commit (it will fail)
# - Go to tools/ace/support/dryice and run "npm link ."
#
# To sync to a more recent commit of Ace:
# - Clone git@github.com:rstudio/ace.git
# - From that repo, configure an upstream remote:
#     git remote add upstream git@github.com:ajaxorg/ace.git
# == The above steps just need to be performed once, not every time ==
# - `git fetch upstream`
# - `git checkout master`       # make sure you're on the master branch
# - `git reset --hard upstream/master` # go to latest commit
# - `git push origin master`    # put the latest into GitHub
# - `git rev-parse HEAD`        # this is the latest commit SHA-1
# - Paste the SHA-1 from the previous command into the line below that
#   begins "git reset --hard"
# - Run sync-ace-commit
#
# As upstream Ace evolves, inevitably they introduce changes that conflict
# with the patches we maintain. To resolve these conflicts, go to the rep
# you created in the previous set of instructions, and:
# - `git checkout <SHA-1>`      # Go to the upstream commit
# - `git merge <bugfix-branch>` # Try merging
# - If previous step SUCCEEDS, start at the top but try the next branch.
# - If the merge FAILS:
# - `git merge --abort`
# - `git checkout -b <new-bugfix-branch>` # I use the old name plus a number
# - `git merge <bugfix-branch>`
# - Resolve the merge conflict as you usually would, and commit
# - Update this sync-ace-commits script to refer to the new bugfix branch
# - Repeat all of these steps if you still get merge conflicts

set -e

rm -rf ace
git clone git@github.com:rstudio/ace.git
cd ace
git checkout master

# Point this to the upstream commit that we've tested
git reset --hard 26a1af8778178fb2488915a24b3c0d951cf4d8a9

git submodule update --init --recursive

# Merge all the bugfix branches
git merge \
    origin/bugfix-webkit-paste2 \
    origin/bugfix-updatefontsize2 \
    origin/bugfix-ctrl-backspace \
    origin/bugfix-active-line-back \
    origin/patch-advanced-indent3 \
    origin/patch/invisible-colors2 \
    origin/patch-build-matchingbraceoutdent \
    origin/patch-normalizeinput2 \
    origin/bugfix/search-regexp \
    origin/bugfix/selected-word-colors2 \
    origin/patch/scroll-granularity \
    origin/patch/escape-characters2 \
    origin/bugfix/no-paste-event-on-indent \


make clean
make
cp build/src/ace.js ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/
cp build/src/ace-uncompressed.js ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/
