#!/bin/bash

set -e

function extract_unicums {
    commit=$1
    git reset --hard HEAD
    git clean -dfx
    git checkout $commit
    git reset --hard origin/$commit || true
    find . -type f \! -path './.git/*' \! -path './src/test/ssl/ssl/*' \
     \! -path './src/pl/plperl/ppport.h' \
     \! -path './src/timezone/data/tzdata.zi' \
     \! -path './src/backend/utils/errcodes.txt' \
     \! -path './src/backend/catalog/sql_features.txt' \
     \! -path './src/pl/plperl/ppport.h' \
     \! -path './src/backend/utils/mb/Unicode/*' \
     \! -name '*.po' \! -name '*.stop' \! -name '*.data' \
     -exec grep -o -P '\b[a-zA-Z_][a-zA-Z0-9-_]+' {} \; | sed 's/-\+$//g' | \
     sort >$WD/entities-sorted-$commit.txt
    uniq -u $WD/entities-sorted-$commit.txt >$WD/unicums-$commit.txt
}

if [ -z "$1" ] || [ -z "$2" ] ; then
    echo "Usage: $0 {OLD_COMMIT} {NEW_COMMIT}"
    echo "Find new unicums in the NEW_COMMIT state as compared to the OLD_COMMIT."
    echo "Examples: $0 REL_12_STABLE master"
    echo "$0 REL_11_4 REL_11_5"
    exit 1
fi

WD=${TMPDIR:-/tmp}

extract_unicums $1
extract_unicums $2

diff --changed-group-format='%>' --unchanged-group-format='' \
 $WD/unicums-$1.txt $WD/unicums-$2.txt > $WD/new-unicums.txt || true
for w in `cat $WD/new-unicums.txt`; do
  echo -e -n "$w:\t"
  grep -P "\b$w\b" -r * --exclude='*.po' --exclude='*.stop' --exclude='*.data' --color=auto
done
