#!/usr/bin/env bash
set -e

BRANCH=rstudio/v1.3
GWTRELEASE=tags/2.8.2
#
# RStudio fork of GWT started with BRANCH off of GWTRELEASE plus our changes.
# That is, started life with:
#
#   git branch rstudio/v1.3 tags/2.8.2
#   git checkout rstudio/v1.3
#   git push --set-upstream origin rstudio/v1.3
#
# To take a new release of GWT, update GWTRELEASE and run this script to merge subsequent
# GWT changes into our branch and rebuild with build-gwt (after resolving any merge conflicts).
# Once successful, be sure to make PR of changes to rstudio/gwt as well as to rstudio itself.
#
# To start work on a new release of RStudio, manually create a new branch, e.g. rstudio/v1.4,
# from the desired starting commit (most likely the head of the prior rstudio/v1.x branch),
# and update the BRANCH constant in this script. For example:
#
#   git branch rstudio/v1.4 rstudio/v1.3
#   git checkout rstudio/v1.4
#   git push --set-upstream origin rstudio/1.4
#
# For new version of RStudio, recommend giving custom GWT build a new version name in 
# "build-gwt" and in "build.xml" to make it easier to work on multiple RStudio releases in the
# same cloned repo..
# 

# check out our copy of gwt and build tools
if [ ! -d gwt ]; then
    mkdir gwt
fi
cd gwt
if [ ! -d gwt ]; then
    git clone git@github.com:rstudio/gwt.git
    cd gwt
    git remote add upstream https://github.com/gwtproject/gwt
    cd ..
fi
if [ ! -d tools ]; then
    git clone git@github.com:gwtproject/tools.git
fi

cd gwt

if [ "`git status --porcelain`" != "" ]; then
    echo "ERROR: Can't merge, working tree is dirty"
    exit 1
fi

# get latest master from upstream
git fetch upstream
git checkout master
git merge upstream/master

git checkout "${BRANCH}"
git reset --hard "${BRANCH}"
git status
cd ../..

echo Reminder, if you are making changes to GWT sources, use the build-gwt script to rebuild 
echo the GWT jars into the rstudio source tree, and commit those files AND the gwt-source files
echo when done.
