#!/bin/sh

set -e

#---------------------------------------------------------------------
# This quick and dirty script assists in updating the C++ demangler
# machinery in coregrind/m_demangle.
# The script will check out 
# - old and new revisions of the C++ demangler related files from GCC's trunk
# - m_demangle from valgrind's trunk.
# It will assemble
# - a patch file with local changes that were applied to the C++
#   demangler to make it work within valgrind
# - a directory new_m_demangle whose contents should be copied to
#   m_demangle in valgrind trunk
# The patch will *not* be applied automatically.
#---------------------------------------------------------------------

# You need to modify these revision numbers for your update.
old_gcc_revision=b3585c0836e729bed56b9afd4292177673a25ca0 # the revision of the previous update
new_gcc_revision=d3b2ead595467166c849950ecd3710501a5094d9 # the revision for this update

# Unless the organization of demangler related files has changed, no
# changes below this line should be necessary.

# Setup a temp directory
DIR=/tmp/demangle

rm -rf $DIR
mkdir -p $DIR

cd $DIR

echo "Updating the demangler in $DIR"

# 1) Make a shallow clone of the GCC repo containing only the 2 commits we need
mkdir gcc
cd gcc
git init
git remote add origin https://gcc.gnu.org/git/gcc.git
git config core.sparsecheckout true
echo "libiberty/*" > .git/info/sparse-checkout
echo "include/*" >> .git/info/sparse-checkout
git fetch --depth 1 origin $old_gcc_revision $new_gcc_revision
git checkout $old_gcc_revision
cd ..

# 2) Check out files from old GCC revision
echo "Checking out GCC files @ $old_gcc_revision"

# 3) Assemble the ones we need in $DIR/$old_gcc_revision
mkdir $old_gcc_revision
cd    $old_gcc_revision
cp    ../gcc/include/ansidecl.h .
cp    ../gcc/include/demangle.h .
cp    ../gcc/include/dyn-string.h .
cp    ../gcc/include/safe-ctype.h .
cp    ../gcc/libiberty/cp-demangle.c .
cp    ../gcc/libiberty/cp-demangle.h .
cp    ../gcc/libiberty/cplus-dem.c .
cp    ../gcc/libiberty/dyn-string.c .
cp    ../gcc/libiberty/d-demangle.c .
cp    ../gcc/libiberty/rust-demangle.c .
cp    ../gcc/libiberty/safe-ctype.c .
cd ..

# 4) Check out files from new GCC revision
echo "Checking out GCC files @ $new_gcc_revision"
cd gcc
git checkout $new_gcc_revision
cd ..

# 5) Assemble the ones we need in $DIR/$new_gcc_revision
mkdir $new_gcc_revision
cd    $new_gcc_revision
cp    ../gcc/include/ansidecl.h .
cp    ../gcc/include/demangle.h .
cp    ../gcc/include/dyn-string.h .
cp    ../gcc/include/safe-ctype.h .
cp    ../gcc/libiberty/cp-demangle.c .
cp    ../gcc/libiberty/cp-demangle.h .
cp    ../gcc/libiberty/cplus-dem.c .
cp    ../gcc/libiberty/dyn-string.c .
cp    ../gcc/libiberty/d-demangle.c .
cp    ../gcc/libiberty/rust-demangle.c .
cp    ../gcc/libiberty/safe-ctype.c .
cd ..

# 6) Sparse check out valgrind coregrind/m_demangle into old_m_demangle
echo "Checking out coregrind/m_demangle"
mkdir valgrind-sparse-clone
cd valgrind-sparse-clone
git init
git remote add origin -f https://sourceware.org/git/valgrind.git/
git config core.sparsecheckout true
echo "coregrind/m_demangle/*" > .git/info/sparse-checkout
git pull origin master
cd ..
mv valgrind-sparse-clone/coregrind/m_demangle old_m_demangle
rm -rf valgrind-sparse-clone

# 7) Create new_m_demangle
cp -rp old_m_demangle new_m_demangle
cp -rp $new_gcc_revision/*.[ch] new_m_demangle

# 8) Compare files from previous GCC revision against old_m_demangle
#    (This gets us the changes we made to the demangler).
echo "Creating patch"
set +e
diff -r -u $old_gcc_revision old_m_demangle > our-changes
echo "Patch file 'our-changes' created"

# 9) See how the patch would apply
echo "Attempting to apply the patch (but not actualy doing it)."
cd new_m_demangle
patch --dry -p1 < ../our-changes
