#!/bin/bash

listfile="$1"
if [ $# -gt 2 ]; then
	outputdir="$3"
else
	outputdir="$2"
fi

while read infile; do
	if [ "$infile" -a -f "$infile" ]; then
		before="$(date +%s)"
		echo "_____________________________________________________"
		echo " "
		echo Processing file "$infile"
		echo " "
		outfile="$outputdir"/"`basename "$infile"`".txt
		logfile=_logfiles/"`basename "$infile"`".log
		chromafile=_chromadata/"`basename "$infile"`".csv
		echo "[sonic annotator] ..."
		VAMP_PATH=. ./sonic-annotator -d vamp:matthiasm:nnls_chroma:bothchroma -w csv --csv-stdout "$infile" 2> "$logfile" | cut -d ',' -f 2-  > "$chromafile" 
		matlab -nodisplay -nojvm -r run_mirex\(\'"$infile"\',\'"$outfile"\'\,\'"$chromafile"\'\) >> $logfile
		after="$(date +%s)"
		elapsed_seconds="$(expr $after - $before)"
		echo "  -->" time elapsed: $elapsed_seconds seconds
		echo " "
	else if [ ! "$infile" ]; then echo ...; fi
	fi		
done < "$listfile"
