#!/usr/bin/env bash

m=$HOME/tmp/$$.dat
test -d $HOME/tmp || mkdir -p $HOME/tmp
trap "rm -f $m" EXIT

#Calculate the loudness of the main microphone
#signal.

err="$0: OXIVOICE needs to be set in the environment.  Try /home/mint/gpk/voicing."
OV=${OXIVOICE:?$err}
CLUSTERSIZE=0

rewrite=''
DT=0.010
s='True'
while true
do
	arg=$1; shift;
	case "$arg" in
	'') break;;
	-rewrite) rewrite=$m;;
	-qmake) CLUSTERSIZE=$1; shift;;
	-s) s="$1"; shift;;
	-dt) DT=$1; shift;;
	*)	g_die "Unrecognized arg: $arg" || exit 2;;
	--) break;;
	esac
done

if test "$CLUSTERSIZE" -gt 0 && type qmake >/dev/null 2>&1
then
  np=''
  make="qmake -V -cwd -- -j $CLUSTERSIZE"
else
  np=-j\ $(grep '^processor.:' /proc/cpuinfo | wc -l)
  make=make
fi

>$m
echo DT=$DT >>$m
echo OV=$OV >>$m
echo REWRITE=$rewrite >>$m
echo '.PHONY: all' >>$m
echo '.SUFFIXES:' >>$m
echo >>$m
select_fiat_entries.py -select "$s" -show "'all : %s/%s/sss.dat\n' % (d, f)" DB.fiat >>$m

cat <<'ENDEND' >>$m

%/sss.dat : %/raw.dat $(OV)/ssslope.py $(REWRITE)
	mkdir -p $$(dirname $*); python $(OV)/ssslope.py -dt $(DT) -f $*/raw.dat -c 0 -o $@

ENDEND

$make $np -f $m || g_die "$0 fails!" 
