#!/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=musixtex-$debv

TMP=`mktemp -d`
currdir=`pwd`
cd $TMP
wget http://mirrors.ctan.org/macros/musixtex.zip
unzip musixtex.zip
rm musixtex.zip
mv musixtex $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/macros/musixtex.tds.zip
# clean out stuff that is replaced by the .tds.zip
rm -r doc scripts source tex
# clean out Windows and OSX stuff
rm -r Windows
# unzip further files
unzip musixtex.tds.zip
rm musixtex.tds.zip
# get additional musixtex-fonts package
wget http://mirrors.ctan.org/fonts/musixtex-fonts.zip
unzip musixtex-fonts.zip
rm musixtex-fonts.zip
#cd musixtex-fonts
#unzip musixtex-fonts-texmf.zip
#rm musixtex-fonts-texmf.zip
#cd ..
cd ..
tar -cJf musixtex_${debv}.orig.tar.xz $td
if [ ! -r $currdir/musixtex_${debv}.orig.tar.xz ] ; then 
  mv musixtex_${debv}.orig.tar.xz $currdir
  cd $currdir
  rm -r $TMP
else
  echo "musixtex_$(debv).orig.tar.xz already exists in $currdir, not replacing it!"
  echo "new one is in $TMP"
fi

