#!/bin/sh

usage() {
    echo "split-to-projects patch.diff basename
    
Split patch into several patches according to projects.
  patch.diff  Patch that will be separated
  basename    Base for the resulting names" >&2;

    exit 1;
}

PATCH="$1"
BASE="$2"

[ "$PATCH" != "" ] || usage
[ "$BASE" != "" ] || usage

for PROJECT in `lsdiff "$PATCH" | sed 's/\/.*//' | sort | uniq`
do
    filterdiff -i "$PROJECT/*" "$PATCH" > "$BASE-$PROJECT.diff"
done
