+$| = 1; # autoflush stdout after each print, to avoid output after die
+
+my $defaultrepo;
+
+my $verbose = 2;
+my $ignore_failure = 0;
+my $want_remote_repo = 0;
+my $checked_out_flag = 0;
+
+my %tags;
+
+my @packages;
+
+# Figure out where to get the other repositories from.
+sub getrepo {
+ my $basedir = ".";
+ my $repo = $defaultrepo || `cat $basedir/_darcs/prefs/defaultrepo`;
+ chomp $repo;
+
+ my $repo_base;
+ my $checked_out_tree;
+
+ if ($repo =~ /^...*:/) {
+ # HTTP or SSH
+ # Above regex says "at least two chars before the :", to avoid
+ # catching Win32 drives ("C:\").
+ $repo_base = $repo;
+
+ # --checked-out is needed if you want to use a checked-out repo
+ # over SSH or HTTP
+ if ($checked_out_flag) {
+ $checked_out_tree = 1;
+ } else {
+ $checked_out_tree = 0;
+ }
+
+ # Don't drop the last part of the path if specified with -r, as
+ # it expects repos of the form:
+ #
+ # http://darcs.haskell.org
+ #
+ # rather than
+ #
+ # http://darcs.haskell.org/ghc
+ #
+ if (!$defaultrepo) {
+ $repo_base =~ s#/[^/]+/?$##;
+ }
+ }
+ elsif ($repo =~ /^\/|\.\.\/|.:(\/|\\)/) {
+ # Local filesystem, either absolute or relative path
+ # (assumes a checked-out tree):
+ $repo_base = $repo;
+ $checked_out_tree = 1;
+ }
+ else {
+ die "Couldn't work out repo";
+ }
+
+ return $repo_base, $checked_out_tree;
+}
+
+sub message {
+ if ($verbose >= 2) {
+ print "@_\n";
+ }
+}
+
+sub warning {
+ if ($verbose >= 1) {
+ print "warning: @_\n";
+ }
+}
+
+sub darcs {
+ message "== running darcs @_";
+ system ("darcs", @_) == 0
+ or $ignore_failure
+ or die "darcs failed: $?";
+}
+
+sub parsePackages {
+ my @repos;
+ my $lineNum;
+
+ my ($repo_base, $checked_out_tree) = getrepo();
+
+ open IN, "< packages" or die "Can't open packages file";
+ @repos = <IN>;
+ close IN;
+
+ @packages = ();
+ $lineNum = 0;
+ foreach (@repos) {
+ chomp;
+ $lineNum++;
+ if (/^([^# ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)$/) {
+ my %line;
+ $line{"localpath"} = $1;
+ $line{"tag"} = $2;
+ $line{"remotepath"} = $3;
+ $line{"vcs"} = $4;
+ $line{"upstream"} = $5;
+ push @packages, \%line;
+ }
+ elsif (! /^(#.*)?$/) {
+ die "Bad content on line $lineNum of packages file: $_";
+ }
+ }
+}
+
+sub darcsall {
+ my $localpath;
+ my $remotepath;
+ my $path;
+ my $tag;
+ my @repos;
+ my $command = $_[0];
+ my $line;
+
+ my ($repo_base, $checked_out_tree) = getrepo();
+
+ for $line (@packages) {
+ $localpath = $$line{"localpath"};
+ $tag = $$line{"tag"};
+ $remotepath = $$line{"remotepath"};
+
+ if ($checked_out_tree) {
+ $path = "$repo_base/$localpath";
+ }
+ else {
+ $path = "$repo_base/$remotepath";
+ }
+
+ if (-d "$localpath/_darcs") {
+ if ($want_remote_repo) {
+ if ($command =~ /^opt/) {
+ # Allows ./darcs-all optimize --relink
+ darcs (@_, "--repodir", $localpath, "--sibling=$path");
+ } else {
+ darcs (@_, "--repodir", $localpath, $path);
+ }
+ } else {
+ darcs (@_, "--repodir", $localpath);
+ }
+ }
+ elsif ($tag eq "-") {
+ message "== Required repo $localpath is missing! Skipping";
+ }
+ else {
+ message "== $localpath repo not present; skipping";
+ }
+ }