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

BRANCH=rstudio/mariposa-orchid
#
# RStudio fork of GWT started with BRANCH off of GWT 2.8.2 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
#
# GWT branches will use the RStudio release names: e.g. rstudio/chocolate-cosmos. Not all
# RStudio releases will have a corresponding GWT branch (we don't change GWT very often).
#
# To take a new release of GWT, merge new tag (e.g. tags/2.9.0) 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.
#
#   git fetch upstream --tags
#   git checkout rstudio/the-branch
#   git merge 2.10.0
#
# To start work on a new release of RStudio, manually create a new branch, e.g. rstudio/new-name
# from the desired starting commit (most likely the head of the prior rstudio/release-name branch),
# and update the BRANCH constant in this script. For example, assuming prior release was
# "rstudio/kousa-dogwood" and the new one is "rstudio/mariposa-orchid":
#
#   git fetch origin
#   git checkout rstudio/kousa-dogwood
#   git checkout -b rstudio/mariposa-orchid
#   git push -u origin rstudio/mariposa-orchid

# 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 main from upstream
git fetch upstream
git checkout main
git merge upstream/main

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.
