#!/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
git checkout 4d024f49758f851423ac69d61ba631590dd4f54d

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/invisible-colors2 \
    origin/patch-normalizeinput3 \
    origin/bugfix/search-regexp \
    origin/bugfix/selected-word-colors2 \
    origin/bugfix/no-paste-event-on-indent \
    origin/patch/build-matchingbraceoutdent2
    
git merge \
    origin/patch/mousewheel3 \
    origin/patch/consistent-gutter-width \
    origin/patch/suppress-native-scroll \
    origin/patch/build \
    origin/patch/remove-webfont \


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/
