#!/bin/bash

set -e

# common shell functions

die()
{
  echo "$0: $@"
  exit 1
}

#----- checks
echo "*** dist: checks"

test -f make-dist || die "run make-dist from the directory it lives in"

VERSION=$(grep version index.smindex | sed 's/.*"\([0-9a-z.-]*\)"$/\1/g')

#----- create dirs
echo "*** dist: create dirs"
mkdir -p instruments/standard || die "need dir instruments/standard"
mkdir -p tmp || die "need dir tmp"

#----- clean
echo "*** dist: clean old stuff"

rm -f instruments/standard/*.smset
rm -f instruments/standard/*.smindex
rm -f spectmorph-instruments-$VERSION.tar
rm -f spectmorph-instruments-$VERSION.tar.xz

#----- build
echo "*** dist: building instruments ($VERSION)"
INSTS=$(cat index.smindex | while read cmd arg1 arg2
do
  if [ "_$cmd" == "_smset" ]; then
    echo $arg1 | sed 's/"//g;s/[.]smset//g'
  fi
done)

for i in $INSTS
do
  if [ -d "$i" ]; then
    echo ./build.sh $i
  else
    die "instrument data for $i missing"
  fi
done | parallel || die "errors during building instruments"

cp index.smindex instruments/standard

#----- tar
echo "*** dist: create tarball: spectmorph-instruments-$VERSION.tar"
tar cvf spectmorph-instruments-$VERSION.tar instruments

#----- xz
echo "*** dist: compress tarball => spectmorph-instruments-$VERSION.tar.xz"
xz -9 spectmorph-instruments-$VERSION.tar

#----- download size
ls -hl spectmorph-instruments-$VERSION.tar.xz