#!/bin/bash

set -e

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
if [ -d $td ] ; then
  echo "$td already exists, exiting."
  exit 1
fi
mkdir $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 -rf 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 ..
if [ ! -r $currdir/musixtex_${debv}.orig.tar.xz ] ; then 
  tar -cJf $currdir/musixtex_${debv}.orig.tar.xz $td
  cd $currdir
  rm -r $td
else
  echo "musixtex_$debv.orig.tar.xz already exists in $currdir, not replacing it!"
  echo "orig sources are in $td"
fi

