#!/bin/sh
set -e

PACKAGE=tifffile

VERSION=${VERSION:-$2}
TARFILE=${TARFILE:-$3}

if [ -z ${VERSION}]; then
VERSION=`dpkg-parsechangelog | sed -n -e 's/^Version: \(.*\)-[^-]*$/\1/p'`
fi

REVISION=`echo $VERSION | sed -e 's/^.*svn//g'`


if [ -z ${TARFILE}]; then
TARFILE=${PACKAGE}_${VERSION}.orig.tar.gz
fi

FOLDER=${PACKAGE}-${VERSION}

mkdir ${FOLDER}
cd ${FOLDER}
# http://bugs.debian.org/692752
#wget -c http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html
curl -s -o tifffile.py.html "http://www.lfd.uci.edu/~gohlke/code/tifffile.py.html"
#wget -c http://www.lfd.uci.edu/~gohlke/code/tifffile.c.html
curl -s -o tifffile.c.html "http://www.lfd.uci.edu/~gohlke/code/tifffile.c.html"
# do not keep .py extension as we install it into usr/bin
links -dump tifffile.py.html > tifffile.py
sed -i -e 's/^ //g' tifffile.py
links -dump tifffile.c.html > tifffile.c
sed -i -e 's/^ //g' tifffile.c
rm *.html

# compute version:
PYVERSION=`grep "^:Version:" tifffile.py | cut -d' ' -f2 | sed -e 's/\.//g'`
if [ "$PYVERSION" != "$VERSION" ]; then
  echo "$PYVERSION and $VERSION are not compatible"
  exit 1
fi
cd ..

GZIP="--best --no-name" tar czf ${TARFILE} ${FOLDER}
rm -rf ${FOLDER}
