#!/bin/sh

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

#Calculate how periodic the main microphone signal is.

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

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

for n in $(select_fiat_entries.py -show "'%s/%s' % (d, f)" DB.fiat)
do
    echo 'Working on' $n
    if test ! -s "$n.dat"
    then
      g_die "No data: $n" || exit 1
    # elif test $rewrite = 1 -o "$n.dat" -nt "$n.irr.dat" -o "$n.dat" -nt "$n.irr.sd"
    elif test $rewrite = 1 -o "$n.dat" -nt "$n.irr.dat"
    then
      rm -f $o "$n.irr.dat"
      python $OV/irregularity.py -write 'BITPIX=-32' -dt $DT -f "$n.dat" -c 0 -o $o ||
		g_die "irregularity.py fails!" || exit 1
      # imop -write 'BITPIX=-32' r $o -0.5 Fmax = "$n.irr.sd"
      imop -write 'BITPIX=-32' r $o -0.5 Fmax = "$n.irr.dat"
    fi
done

