#!/bin/sh
# get source for mrbayes to obtain documentation source from SVN

set -e
# set -x
NAME=`dpkg-parsechangelog | awk '/^Source/ { print $2 }'`
COMPRESSION=xz

if ! echo $@ | grep -q upstream-version ; then
    VERSION=`dpkg-parsechangelog | awk '/^Version:/ { print $2 }' | sed 's/\([0-9\.]\+\)-[0-9]\+$/\1/'`
    # regard dversionmangle
    VERSION=`echo ${VERSION} | sed 's/\([0-9]\)~\([br]\)/\1-\2/'`
else
    VERSION=`echo $@ | sed "s?^.*--upstream-version \([-0-9.rcbeta]\+\) .*${NAME}.*?\1?"`
    if echo "$VERSION" | grep -q "upstream-version" ; then
        echo "Unable to parse version number"
        exit
    fi
fi

mkdir -p ../tarballs
cd ../tarballs
TARDIR=${NAME}-${VERSION}
mv ../${NAME}-${VERSION}.tar.gz .
tar -xzf ${NAME}-${VERSION}.tar.gz

# remove PDF without source
rm -f ${TARDIR}/documentation/commref*.pdf

find . -name "._*" -delete

# fetch sources for remaining doc from SVN into temporary dir
svn export https://svn.code.sf.net/p/mrbayes/code/documentation tmp
# remove unneeded stuff from documentation SVN
rm -f tmp/Manual*.pdf
rm -f tmp/*.rtf
rm -f tmp/*.rb
rm -f tmp/Release_instructions.txt
# move remaining doc to source dir
mv tmp/* ${TARDIR}/documentation
rmdir tmp

GZIP="--best --no-name" XZ_OPT="-6v" tar --owner=root --group=root --mode=a+rX -a -cf ${NAME}_${VERSION}+dfsg.orig.tar.${COMPRESSION} ${TARDIR}
rm -rf ${TARDIR}
