#!/usr/bin/env bash

m=/tmp/$$.dat
# trap "rm -f $m" EXIT

#Calculate the loudness of the main microphone
#signal.

function g_die() { x="ERR: $*"; echo $x; echo $x 1>&2; exit 1;}

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

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

np=$(grep '^processor.:' /proc/cpuinfo | wc -l)

>$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/rms.dat\n' % (d, f)" DB.fiat >>$m

cat <<'ENDEND' >>$m

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

ENDEND

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