#!/bin/bash

base=/import/c4dm-music-a/C4DM\ Music\ Collection

mine=`date '+%Y-%m-%d_%H.%M.%S'`_$$
logfile="logs/$mine.log"
statusfile="logs/$mine.status"
matlogfile="logs/$mine.matlog"
mkdir -p logs

echo "base: $base"
echo "log file to $logfile"
echo "overall status reports to $statusfile"

report_failure() {
    echo "FAIL   $@" >> "$statusfile"
}
report_success() {
    echo "WIN    $@" >> "$statusfile"
}
report_existing() {
    echo "HAZ    $@" >> "$statusfile"
}
report_busy() {
    echo "BUSY   $@" >> "$statusfile"
}

find "$base" -type d | while read source; do

    ls "$source" | fgrep -q .wav || continue

    echo "source: $source"
    dirname=${source#$base}
    dirname=${dirname#/}
    echo "dirname: $dirname"
    [ -n "$dirname" ] || continue

    for wavfilename in "$source/"*.wav; do          # loop over songs

	echo "$wavfilename"

        songbasename=`basename "${wavfilename%.wav}"`
        echo .................. new song "$dirname/$songbasename" .............
        songdir="output/$dirname/$songbasename"                      # this is where all song-related features will go into
        mkdir -p "$songdir"

	( if ! flock -en 200; then
	    echo "songdir $songdir is locked already, skipping it"
	    report_busy "$wavfilename"
	    exit
	  fi

	echo "have lock on songdir $songdir"

	if [ -s "$songdir/$songbasename""_vamp_matthiasm_nnls_chroma_bothchroma.csv" ]; then
	    echo "non-empty output file(s) exist -- not re-generating"
 	    report_existing "$wavfilename"
	    exit
	fi

        echo oooooooooooooooooooooooooooooooooooooooooooooooo
        echo low level feature extraction
        echo oooooooooooooooooooooooooooooooooooooooooooooooo                
        VAMP_PATH=. ./sonic-annotator-unix \
                -d vamp:matthiasm:nnls_chroma:simplechord \
                -d vamp:matthiasm:nnls_chroma:bothchroma \
                -d vamp:matthiasm:nnls_chroma:localtuning \
                -d vamp:matthiasm:nnls_chroma:logfreqspec \
                -d vamp:matthiasm:nnls_chroma:semitonespectrum \
                -d vamp:matthiasm:nnls_chroma:tunedlogfreqspec \
                -d vamp:matthiasm:nnls_chroma:tuning \
                -w csv \
                --csv-basedir "$songdir/" \
                "$wavfilename"
        matlab_infile=`echo "$wavfilename" | sed "s/'/''/g"`
        matlab_chromafile=`echo "$songdir/$songbasename" | sed "s/'/''/g"`_vamp_matthiasm_nnls_chroma_bothchroma.csv
        matlab_outbase=`echo "$songdir/$songbasename" | sed "s/'/''/g"`
        matlab_segout="$matlab_outbase".seg
        matlab_chordout="$matlab_outbase".chord
        # matlab_keyout="$matlab_outbase".key # is still done in matlab
        echo oooooooooooooooooooooooooooooooooooooooooooooooo
        echo segmentation
        echo oooooooooooooooooooooooooooooooooooooooooooooooo
        echo "run_segmentation('$matlab_infile','$matlab_segout','$matlab_chromafile')" | matlab -nodisplay -nojvm 2>&1 >/dev/null | tee "$matlogfile"
	if grep -qi "error" "$matlogfile" ; then
	    report_failure "$wavfilename"
	    exit
	else
	    rm "$matlogfile"
	fi
        echo oooooooooooooooooooooooooooooooooooooooooooooooo
        echo DBN chord estimation
        echo oooooooooooooooooooooooooooooooooooooooooooooooo
        echo "run_chordandkey('$matlab_infile','$matlab_chordout','$matlab_chromafile')" | matlab -nodisplay -nojvm 2>&1 >/dev/null | tee "$matlogfile" 
	cat "$matlogfile"
	if grep -qi "error" "$matlogfile" ; then
	    report_failure "$wavfilename"
	    exit
	else
	    rm "$matlogfile"
	fi
	report_success "$wavfilename"

	)200>"$songdir/.lock"
    done

done > "$logfile" 2>&1


