#!/usr/bin/env bash

SPKG_ROOT=`pwd`
PATCHES="$SPKG_ROOT/patches"

set -e
shopt -s extglob

TOPCOM_VERSION=0.17.7

if [ -z "$TOPCOM_KEEP" ] ; then
    # Remove old sources and download new
    rm -rf src
    tar xzf <( curl http://www.rambau.wm.uni-bayreuth.de/Software/TOPCOM-$TOPCOM_VERSION.tar.gz )
    mv TOPCOM-$TOPCOM_VERSION src
fi

# Make everything writable
chmod -R u+w src
cd src

# Delete some unneeded stuff:
# * remove external/
rm -rf external

# * Delete everything under examples/, but keep directory
rm -rf examples/*


# libtool-ized automake sources
cp -p "$PATCHES"/configure.ac                .
cp -p "$PATCHES"/Makefile.am                 Makefile.am
cp -p "$PATCHES"/src-Makefile.am             src/Makefile.am
cp -p "$PATCHES"/src-reg-Makefile.am         src-reg/Makefile.am
cp -p "$PATCHES"/lib-src-Makefile.am         lib-src/Makefile.am
cp -p "$PATCHES"/lib-src-reg-Makefile.am     lib-src-reg/Makefile.am
cp -p "$PATCHES"/wrap-gmp-gmpxx-Makefile.am  wrap-gmp-gmpxx/Makefile.am

# Makefile.am with all examples deleted to save disk space
cp -p "$PATCHES"/examples-Makefile.am     examples/Makefile.am

# regenerate auto-generated files
libtoolize   # only needed because of our broken autotools package
autoreconf -fiv
#rm -rf autom4te.cache
./configure
make dist-bzip2
mv topcom-$TOPCOM_VERSION.tar.bz2 "$SPKG_ROOT"

