#!/bin/sh -e
# According to https://github.com/scipy/scipy/issues/16331
# some git submodules are needed to build scipy since version 1.9.x
# This script clones the scipy repository initialises the submodules
# and creates a submodules tarball

COMPONENT=submodules
EXT=gz

if [ $(basename $PWD) = "debian" ] ; then
  cd ..
fi

VERSION=$(dpkg-parsechangelog | grep '^Version' | cut -d' ' -f2 | cut -f1 -d-)

TMPDIR=$(mktemp -d /tmp/scipy.XXXXXX)
cd $TMPDIR
git clone https://github.com/scipy/scipy
cd scipy
git submodule update --init

MODULES=$(grep 'path =' .gitmodules | sed 's/^.*path = *//')
set -x
mkdir ../scipy-${VERSION}
cd ../scipy-${VERSION}
for m in $MODULES ; do
  mkdir -p $m
  rsync -a -v ../scipy/$m $(dirname $m)
done
cd ..
pwd

tar caf scipy_${VERSION}.orig-${COMPONENT}.tar.${EXT} scipy-${VERSION}

cat <<EOT
You can find the submodules tarball scipy_${VERSION}.orig-${COMPONENT}.tar.${EXT}
in the temporary dir $TMPDIR.

Please inspect the tarball and check licenses in the following directories:
   $MODULES
EOT
