#!/bin/bash

version=$1

if [ -z $version ] ; then
  echo "need version as first argument, exiting."
  exit 1
fi

TMP=get-orig-source-tmp
debv=${version}.ctan`date +%Y%m%d`
td=m-tx-$debv

TMP=`mktemp -d`
currdir=`pwd`
cd $TMP
wget http://mirrors.ctan.org/support/m-tx.zip
unzip m-tx.zip
rm m-tx.zip
mv m-tx $td
cd $td
# we are not sure whether everything is contained in the
# main CTAN dir, and the installation document tells 
# that one should get the following file, so do it
wget http://mirrors.ctan.org/install/support/m-tx.tds.zip
# clean out stuff that is replaced by the .tds.zip
rm -r doc scripts tex
# clean out Windows and OSX stuff
rm -r OSX Windows
# unzip further files
unzip m-tx.tds.zip
rm m-tx.tds.zip
tar -xvf mtx-${version}.tar.gz
rm mtx-${version}.tar.gz
cd ..
tar -cJf m-tx_${debv}.orig.tar.xz $td
if [ ! -r $currdir/m-tx_${debv}.orig.tar.xz ] ; then 
  mv m-tx_${debv}.orig.tar.xz $currdir
  cd $currdir
  rm -r $TMP
else
  echo "m-tx_$(debv).orig.tar.xz already exists in $currdir, not replacing it!"
  echo "new one is in $TMP"
fi

