#!/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 "sudo npm install --global dryice"
#
# To sync to a more recent upstream commit of Ace, update the "git checkout"
# line below with the SHA-1 of the upstream commit. (Do NOT use a branch, for
# example "upstream/master"; use the SHA-1 as it's the only thing guaranteed
# never to change.) Then run this script.
#
# As upstream Ace evolves, inevitably they introduce changes that conflict
# with the patches we maintain. To resolve these conflicts, go to the ./ace
# subdirectory that this script creates, and:
# - `git remote set-url origin git@github.com:rstudio/ace.git`
#                               # Only need to do set-url once
# - `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
# - `git push -u origin <new-bugfix-branch>`
# - 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

if [ ! -d "./ace" ]; then
    git clone git://github.com/rstudio/ace.git
    cd ace
    git remote add upstream git://github.com/ajaxorg/ace.git
    cd ..
fi

if [ ! -d "./ace/node_modules" ]; then
    cd ace
    npm install uglify-js
    npm install dryice
    cd ..
fi

cd ace
git checkout -- build

git fetch origin
git fetch upstream

# Point this to the upstream commit that we've tested
# NOTE: the following commits have been merged by upstream
# so should be removed the next time we sync:
#
#  origin/patch/eclipse-selectedword
#  origin/patch/blinking-cursor
#  origin/patch/vim-ctrl-behavior
#
git checkout fc01cc624296c1b1be59b55ce03051fa29b5bd96

git submodule update --init --recursive

# Merge all the bugfix branches
git merge \
    origin/bugfix-webkit-paste2 \
    origin/bugfix-updatefontsize2 \
    origin/bugfix-active-line-back \
    origin/patch-advanced-indent4 \
    origin/patch/normalizeinput4 \
    origin/bugfix/no-paste-event-on-indent \
    origin/patch/build-matchingbraceoutdent3
    
git merge \
    origin/patch/mousewheel3 \
    origin/patch/consistent-gutter-width3 \
    origin/patch/suppress-native-scroll \
    origin/patch/build \
    origin/patch/remove-webfont \
    origin/patch/eclipse-selectedword \
    origin/patch/emacs-vim-keybindings

git merge \
    origin/patch/blinking-cursor \
    origin/patch/vim-ctrl-behavior \
    origin/patch/laggy-typing \
    origin/bugfix/outdent-newline


node ./Makefile.dryice.js normal
node ./Makefile.dryice.js -m normal
cp build/src/ace.js ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/ace-uncompressed.js
cp build/src-min/ace.js ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/ace.js

stripUseStrict() {
    sed 's/"use strict";//g' "$1" > "$1.stripped"
    mv "$1.stripped" "$1"
}

stripUseStrict ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/ace.js
stripUseStrict ../../src/org/rstudio/studio/client/workbench/views/source/editors/text/ace/ace-uncompressed.js
