#!/bin/bash

SCRIPT_DIR=$(cd $(dirname ${0}); pwd)

. ${SCRIPT_DIR}/utils.sh

BASEDIR=${SCRIPT_DIR}/devTeamDemo

usage () {
    declaim "${0} [-s]"
}

# We expect the group, project and branch as parameters
export SLEEPS="N"
while getopts "sh" opt; do
  case ${opt} in
    s) # We answer "Yes" to all questions
       SLEEPS="Y"
       ;;
    h) # We want to know how this works.
       usage
       exit 0
       ;;
    *) usage
       exit 1
       ;;
  esac
done
# shift to the first non-option parameter.
shift $(( ${OPTIND} - 1 )); unset OPTIND

declaim "Preparing..."
cd 
rm -rf ${BASEDIR}
mkdir -pv ${BASEDIR}
cd ${BASEDIR}

declaim "Initialising 'remote' git repository"
# Create my sample git project
cat <<EOF | base64 -d - | tar zxf -
[encoded data, 458689 characters]
BfVJqggAyAUA
EOF

declaim "Cloning for the Master"
# Clone
git_command git clone ${BASEDIR}/javaBootcampNoEclipse.git ${BASEDIR}/javaBootcampNoEclipse.mastersClone
cd ${BASEDIR}/javaBootcampNoEclipse.mastersClone

# Set Master's config
git_command git config --local user.name "\"Master O'Theuniverse\""
git_command git config --local user.email "master.otheuniverse@example.com"

# Master creates a release branch
declaim "Master creates release branch"
git_command git branch REL2018.11 master
git_command git push origin REL2018.11

sleepies 15

# Developer 1 clone
declaim "Developer 1 clones"
git_command git clone ${BASEDIR}/javaBootcampNoEclipse.git ${BASEDIR}/javaBootcampNoEclipse.dev1
cd ${BASEDIR}/javaBootcampNoEclipse.dev1

# Set Developer 1's config
git_command git config --local user.name "\"Developer O'Ne\""
git_command git config --local user.email "developer.one@example.com"

# Developer 1 creates branch
declaim "Developer 1 creates development branch from release branch"
git_command git checkout REL2018.11
git_command git branch REL2018.11_dev1
git_command git checkout REL2018.11_dev1
git_command git push --set-upstream origin REL2018.11_dev1

sleepies 15

# Developer 2 clone
declaim "Developer 2 clones"
git_command git clone ${BASEDIR}/javaBootcampNoEclipse.git ${BASEDIR}/javaBootcampNoEclipse.dev2
cd ${BASEDIR}/javaBootcampNoEclipse.dev2

# Set Developer 2's config
git_command git config --local user.name "\"Developer Two\""
git_command git config --local user.email "developer.two@example.com"

# Developer 2 creates branch
declaim "Developer 2 creates development branch from release branch"
git_command git checkout REL2018.11
git_command git branch REL2018.11_dev2
git_command git checkout REL2018.11_dev2
git_command git push --set-upstream origin REL2018.11_dev2

sleepies 15

# Dev 1 does some development
declaim "Developer 1 develops in their clone and on their development branch"
cd ${BASEDIR}/javaBootcampNoEclipse.dev1
cat <<EOF | patch -p1
diff --git a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
index 516d50c..94aea57 100644
--- a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
+++ b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
@@ -4,6 +4,7 @@ import java.util.*;
 
 public class MyHelloWorldBean {
 
+  String resetMessage = new String("Dia duit, a Dhomhan!");
   String strMyMessage;
 
   public MyHelloWorldBean() {
@@ -19,7 +20,7 @@ public class MyHelloWorldBean {
   }
 
   public void reset() {
-    setStrMyMessage("Dia duit, a Dhomhan!");
+    setStrMyMessage(resetMessage);
   }
 
 }
EOF
git_command git add source
git_command git commit -m "\"Moving this out to be a variable\""
git_command git push

sleepies 15

# Dev 2 does some development
declaim "Developer 2 develops in their clone and on their development branch"
cd ${BASEDIR}/javaBootcampNoEclipse.dev2
cat <<EOF | patch -p1
diff --git a/source/jsps/pages/myhelloworld_bean.jsp b/source/jsps/pages/myhelloworld_bean.jsp
index b9f9725..0aad3a3 100644
--- a/source/jsps/pages/myhelloworld_bean.jsp
+++ b/source/jsps/pages/myhelloworld_bean.jsp
@@ -12,10 +12,10 @@
       example
     </h1>
     <p>
-      Bean example: <b><%= MyHelloWorld.getStrMyMessage() %></b>.
+      Bean example: <u><%= MyHelloWorld.getStrMyMessage() %></u>.
     </p>
     <p>
-      The text in <b>bold</b> is the output of a call
+      The text in <u>underline</u> is the output of a call
       to <code>MyHelloWorld.getStrMyMessage()</code>.
     </p>
   </body>
EOF
git_command git add source
git_command git commit -m "\"Formatting changes.\""
git_command git push

sleepies 15

# Master creates a release branch
declaim "Master creates another release branch"
cd ${BASEDIR}/javaBootcampNoEclipse.mastersClone
git_command git checkout REL2018.11
git_command git pull --prune origin
git_command git checkout master
git_command git pull --prune origin
git_command git branch REL2019.02 master
git_command git push origin REL2019.02

sleepies 15

# Developer 1 develops for the later release
declaim "Developer 1 develops for the second release branch"
cd ${BASEDIR}/javaBootcampNoEclipse.dev1

git_command git fetch --prune
git_command git checkout REL2019.02
git_command git branch REL2019.02_dev1
git_command git checkout REL2019.02_dev1
git_command git push --set-upstream origin REL2019.02_dev1

sleepies 15

cat -v <<EOF | patch -p1
diff --git a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldServlet.java b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldServlet.java
index c55118d..6b88814 100644
--- a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldServlet.java
+++ b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldServlet.java
@@ -11,6 +11,7 @@ public class MyHelloWorldServlet extends HttpServlet {
     throws IOException, ServletException
     {

+        String contentType = new String ("text/html");
         double sunD = 0, sunRA = 0, moonD = 0, moonRA = 0;

         String oopsMsg = new String ("No exception");
@@ -22,7 +23,7 @@ public class MyHelloWorldServlet extends HttpServlet {
         moonD = Math.toDegrees(myAstroInfo.getMoonD());
         moonRA = Math.toDegrees(myAstroInfo.getMoonRA());

-        response.setContentType("text/html");
+        response.setContentType(contentType);
         PrintWriter out = response.getWriter();
         out.println("<html>");
         out.println("<head>");
EOF
git_command git add source
git_command git commit -m "\"Further variablisation.\""
git_command git push

sleepies 15

# Master prepares to merge development work to first release branch
declaim "Master prepares for merge requests"
cd ${BASEDIR}/javaBootcampNoEclipse.mastersClone
git_command git checkout master
git_command git fetch --prune
for branch in $(git branch -l | grep -v master)
do
  git_command git branch -D ${branch}
done
for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
do
  git_command git branch ${branch} origin/${branch}
done
git_command git checkout master

# Master does the merge
declaim "Master merges Developer 1 and Developer 2's work for first release"
git_command git checkout REL2018.11
git_command git merge --no-ff -m "\"Merge REL2018.11_dev1 following approved code-review\"" REL2018.11_dev1
git_command git merge --no-ff -m "\"Merge REL2018.11_dev2 following approved code-review\"" REL2018.11_dev2
git_command git push origin

sleepies 15

# Developer 1 merges the new merge into their own branch
declaim "Developer 1 updates their working area following the merge"
cd ${BASEDIR}/javaBootcampNoEclipse.dev1
git_command git fetch --prune
git_command git checkout REL2018.11_dev1
git_command git pull origin REL2018.11
git_command git push origin

sleepies 15

# Developer 2 merges the new merge into their own branch
declaim "Developer 2 updates their working area following the merge"
cd ${BASEDIR}/javaBootcampNoEclipse.dev2
git_command git fetch --prune
git_command git checkout REL2018.11_dev2
git_command git pull origin REL2018.11
git_command git push origin

sleepies 15

# Developer 1 develops for the first release
declaim "Developer 1 makes an update for the first release"
cd ${BASEDIR}/javaBootcampNoEclipse.dev1

git_command git fetch --prune
git_command git checkout REL2018.11_dev1

cat -v <<EOF | patch -p1
diff --git a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
index 94aea57..dd5fb5d 100644
--- a/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
+++ b/source/java/org/gibiris/javaBootcampNoEclipse/MyHelloWorldBean.java
@@ -4,6 +4,7 @@ import java.util.*;

 public class MyHelloWorldBean {

+  // Needed for future the next release, too.
   String resetMessage = new String("Dia duit, a Dhomhan!");
   String strMyMessage;

EOF
git_command git add source
git_command git commit -m "\"Noting that this is needed for the REL2019.02 release.\""
git_command git push origin

sleepies 15

# Master prepares to merge development work to first release branch
declaim "Master merges Developer 1's new update to the first release"
cd ${BASEDIR}/javaBootcampNoEclipse.mastersClone
git_command git checkout master
git_command git fetch --prune
for branch in $(git branch -l | grep -v master)
do
  git_command git branch -D ${branch}
done
for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
do
  git_command git branch ${branch} origin/${branch}
done
git_command git checkout master

# Master does the merge
git_command git checkout REL2018.11
git_command git merge --no-ff -m "\"Merge of latter work on REL2018.11_dev1 following approved code-review\"" REL2018.11_dev1
git_command git push origin

sleepies 15

# Master does the follow-on merge
declaim "Master propagates the work for the first release on to the branch for the second release"
git_command git checkout REL2019.02
git_command git merge --no-ff -m "\"Merge of work on REL2018.11 to here as it's needed\"" REL2018.11
git_command git push origin

sleepies 15

# REL2018.11 goes live. Merge to master
declaim "Master merges the work on the first release to master"
git_command git checkout master
git_command git fetch --prune
for branch in $(git branch -l | grep -v master)
do
  git_command git branch -D ${branch}
done
for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
do
  git_command git branch ${branch} origin/${branch}
done
git_command git checkout master

git_command git tag REL2018.11.0 REL2018.11
git_command git merge --no-ff -m "\"REL2018.11 now live\"" REL2018.11
git_command git push origin

sleepies 15

# Master tags and removes REL2018.11
git_command git push origin :REL2018.11
git_command git push --tags origin

sleepies 15

# Developers remove their development branches and bring their repos up-to-date
declaim "Developers update their working areas"
for dev in dev1 dev2
do
  cd ${BASEDIR}/javaBootcampNoEclipse.${dev}
  git_command git checkout master
  git_command git fetch --prune
  for branch in $(git branch -l | grep -v master)
  do
    git_command git branch -D ${branch}
  done
  git_command git push origin :REL2018.11_${dev}

  sleepies 15
  
  for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
  do
    git_command git branch ${branch} origin/${branch}
  done
  git_command git checkout master
  git_command git merge origin/master
done

sleepies 15

# Developer 1 merges updates to the second release branch into their own development branch
declaim "Developer 1 updates their 2nd release branch working area following the cross-merge"
cd ${BASEDIR}/javaBootcampNoEclipse.dev1
git_command git fetch --prune
git_command git checkout REL2019.02_dev1
git_command git merge -m "\"Updating my dev branch with the 2018.11 release\"" origin/REL2019.02
git_command git push origin

sleepies 15

# Master prepares to merge development work to second release branch
declaim "Master prepares for merge requests"
cd ${BASEDIR}/javaBootcampNoEclipse.mastersClone
git_command git checkout master
git_command git fetch --prune
for branch in $(git branch -l | grep -v master)
do
  git_command git branch -D ${branch}
done
for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
do
  git_command git branch ${branch} origin/${branch}
done
git_command git checkout master

# Master does the merge
declaim "Master merges Developer 1's work for second release"
git_command git checkout REL2019.02
git_command git merge --no-ff -m "\"Merge REL2019.02_dev1 following approved code-review\"" REL2019.02_dev1
git_command git push origin

sleepies 15

# REL2019.02 goes live. Merge to master
declaim "Master merges the work on the second release to master"
git_command git checkout master
git_command git fetch --prune
for branch in $(git branch -l | grep -v master)
do
  git_command git branch -D ${branch}
done
for branch in $(git branch -r | grep -v HEAD | sed 's/^\ \ origin\///')
do
  git_command git branch ${branch} origin/${branch}
done
git_command git checkout master

git_command git tag REL2019.02.0 REL2019.02
git_command git merge --no-ff -m "\"REL2019.02 now live\"" REL2019.02
git_command git push origin

sleepies 15

# Master tags and removes REL2019.02
git_command git push origin :REL2019.02
git_command git push --tags origin
