#!/bin/bash

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

echo "base: $base"

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 type in ChordID ChordID-simple Segmentation; do
	
	target="output/$dirname/$type"
	mkdir -p "$target"

	(
	    flock -en 200 || exit

	    echo "Have lock on $target"
	
	    for f in "$source"/*.wav ; do 
		[ -f "$f" ] || continue
		filebase=`basename "$f"`
		if [ ! -s "$target/$filebase.txt" ]; then
		    echo "$f" 
		fi
	    done > "$target/source.txt"

	    bash ./do$type.sh "$target/source.txt" "$target"

	)200>"$target/.lock"

    done

done
