#!/usr/bin/env bash

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

#Calculate the peakiness of the main microphone
#signal.

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

rewrite=0
if test "$1" = '-rewrite'
then
  rewrite=1
fi

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.pkns.dat -o $n.dat -nt $n.pkns.sd
    elif test $rewrite = 1 -o $n.dat -nt $n.pkns.dat
    then
      # rm -f $o $n.pkns.dat $n.pkns.sd
      rm -f $o $n.pkns.dat
      if $OV/peakiness.py -hpf 60.0 -dt 0.010 -f $n.dat -c 0 -o $o
      then
        imop -write BITPIX=-32 r $o 5 Fmin = $n.pkns.dat
        # imop -write BITPIX=-32 r $o 5 Fmin = $n.pkns.sd
      else
        g_die "peakiness.py fails!" || exit 1
      fi
    fi
done

