--- /dev/null	2011-05-14 23:17:03.000000000 -0400
+++ tmp/flag-dedup	2011-05-14 23:45:43.000000000 -0400
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+# -*- mode: Perl; tab-width: 4; -*-
+
+# A dirty hack by Benjamin Reed
+# Based on a dirty hack by Daniel Macks
+
+use warnings;
+use strict;
+
+my $verbose = 0;
+if (@ARGV && $ARGV[0] eq '-v') {
+	$verbose = 1;
+	shift;
+}
+
+if (!@ARGV) {
+	warn "Usage: $0 [-v] cmd [flags for cmd]\n";
+	warn "  resort [flags for cmd] and call cmd with them\n";
+	warn "  -v causes display of some diagnostics on STDOUT\n";
+	exit 1;
+}
+
+my @flag_prefixes = qw( -I -L -F -Wl -l -framework );
+
+# keep a list of seen flags
+my %existing_args = ();
+
+# what we will launch after organizing the flags
+my @subcmd = ();
+
+# separate the args according to flag
+my $skip_next = 0;
+while (@ARGV) {
+	if ($skip_next) {
+		$skip_next = 0;
+		next;
+	}
+	my $arg = shift @ARGV;
+	my($match) = grep { $arg =~ /^$_/ } @flag_prefixes;
+	if (defined $match) {
+		if ($match eq "-framework") {
+			$skip_next = 1;
+			$arg = "-Wl,-framework," . shift @ARGV;
+		}
+		# parsed off a known flag
+		if (not exists $existing_args{$arg}) {
+			push(@subcmd, $arg);
+		}
+		$existing_args{$arg}++;
+	} else {
+		# unhandled flag, add it
+		push(@subcmd, $arg);
+	}
+}
+
+print "$0: @subcmd\n" if $verbose;
+exec {$subcmd[0]} @subcmd or die "Could not exec $subcmd[0]: $!\n";