#!/bin/bash

set -e

if [ -z "$1" ]; then
    echo "Usage: $0 some.patch"
    echo "Check the patch for new unicums."
    echo "Example: $0 new-feature.patch"
    exit 1
fi

patch="$1"

function extract_unicums {
    state_id=$1
    find . -type f \! -path './.git/*' \! -path './.git-blame-ignore-revs' \
     \! -name '*.po' \! -name '*.stop' \! -name '*.data' \! -name '*.dict' \! -name '*.affix' \
     \! -path './src/backend/catalog/sql_features.txt' \
     \! -path './src/backend/utils/errcodes.txt' \
     \! -path './src/backend/utils/fmgroids.h' \
     \! -path './src/backend/utils/mb/Unicode/*' \
     \! -path './src/interfaces/ecpg/preproc/preproc.c' \
     \! -path './src/pl/plperl/ppport.h' \
     \! -path './src/port/win32.ico' \
     \! -path './src/test/ssl/ssl/*' \
     \! -path './src/timezone/data/tzdata.zi' \
     \! -path './src/tools/pgindent/typedefs.list' \
     -exec grep -o -P '\b[a-zA-Z_][a-zA-Z0-9-_]+' {} \; | sed 's/-\+$//g;/^.$/d' | \
     sort >"$WD/entities-sorted-$state_id.txt"
    uniq -u "$WD/entities-sorted-$state_id.txt" >"$WD/unicums-$state_id.txt"
}

WD="${TMPDIR:-/tmp}"

extract_unicums unpatched

patch -p1 -i "$patch" >/dev/null

extract_unicums patched

echo "New unicums:"
diff --changed-group-format='%>' --unchanged-group-format='' "$WD/unicums-unpatched.txt" "$WD/unicums-patched.txt" > "$WD/new-unicums.txt" || true
while IFS= read -r w; do
  grep -q "\bid=\"$w\"" -r doc/ && continue
  echo -e -n "$w:\t"
  grep -P "\b$w\b" -r ./* .cirrus.yml \
    --exclude='*.po' --exclude='*.stop' --exclude='*.data' \
    --exclude='*.dict' --exclude='*.affix' --exclude='typedefs.list' \
    --exclude='gb-18030-2000.xml' \
    --color=auto || true
done < "$WD/new-unicums.txt"
echo "===="
