#!/bin/bash

function goalkeeper () {
	goal=$1
	file=$2
	if [ "$lastgoal" == "" ]; then
		lastgoal=$goal
	fi
	if [ "$goal" != "$lastgoal" ]; then
		echo "  '"$lastgoal"':"
		echo "    files = '"$files"'"
		lastgoal=$goal
		files=""
	fi
	if [ "$files" == "" ]; then
		files=$file
	else
		files="$files, $file"
	fi
}


lastgoal=""
files=""
EOF=$(mktemp)
echo "zzzzzzz	zzzzzzzzzz"> $EOF
echo "# Generated by $0 - insert this into your projects .prefs file"
echo
echo "goals:"
cat $1 $EOF | sort | while read line
do
	goal=$(echo $line | sed "s/\s.*$//")
	file=$(echo $line | sed "s/^[^ ]\+\s\.\///")
	goalkeeper $goal $file
done
