#!/bin/bash -e

### This is how I built and tested, it's a bit crude.

export WORKING_DIR=$HOME/sandbox
mv $WORKING_DIR $WORKING_DIR-saved
mkdir $WORKING_DIR

cd $WORKING_DIR
# two directories to save stuff after ding the work in WORKING_DIR
# hopefully it will make the results comparisons done later on easier
# on the eyes
mkdir before after

# get the sources
hg clone https://hg.mozilla.org/projects/nspr
hg clone https://hg.mozilla.org/projects/nss
git clone git@github.com:dogtagpki/jss.git

# change this to the one you want to test
export pull_request=5fe56ad33308c8a0c15131947c2544c03860da57

# Save it here to later appy it in after phase
wget https://github.com/dogtagpki/jss/commit/$pull_request.patch

####### Setup Environment Variables
export JAVA_HOME=/etc/alternatives/java_sdk_1.8.0_openjdk
export USE_64=1
export CHECK_DEPRECATION=1

cd jss
####### Build Before #######
## Build the simple way just to take care of all
script -c 'make clean all' > ../typescript.build.before.full
# Build again so that only jss shows up
script -c 'make clean all' > ../typescript.build.before
grep warning: ../typescript.build.before > ../warnings.before
script -c 'make test_jss' ../typescript.tests.before
cd ..

# save the directories
cd before
mv ../nspr .
mv ../nss  .
mv ../jss  .
mv ../dist .
mv ../tests_results .
cd ..

# check out again for the after phase
hg clone https://hg.mozilla.org/projects/nspr
hg clone https://hg.mozilla.org/projects/nss
git clone git@github.com:dogtagpki/jss.git
cd jss
# first apply the patch from the pull request
patch -p1 < ../$pull_request.patch
# now build
script -c 'make clean all' > ../typescript.build.after.full
script -c 'make clean all' > ../typescript.build.after
grep warning: ../typescript.build.after > ../warnings.after
script -c 'make test_jss' > ../typescript.tests.after
## It's hard to visually compare warnings.before and warnings.after as
## they don't have the same sequence, you can sort them and that helps a bit
sort ../warnings.before > ../sorted.warnings.before
sort ../warnings.after > ../sorted.warnings.after
cd ..
# save the directories
cd after
mv ../nspr .
mv ../nss  .
mv ../jss  .
mv ../dist .
mv ../tests_results .
cd ..

diff --unified sorted.warnings.before sorted.warnings.after > before-after-warnings.diff
# do also meld as it might be esier on the eyes than reading the diff
# meld sorted.warnings.before sorted.warnings.after 
### I bet you have a better method than this one



