#!/usr/bin/env perl

$dist = "distributions.txt";

if (@ARGV != 2) { die "USAGE: mkdist (group) (version)\n"; }

open (FILE, $dist) || die "ERROR: can't open distribution file\n";
@list = <FILE>;
close (FILE);

$group = $ARGV[0];
$version = $ARGV[1];

$ingroup = 0;
@name = ();
@tag = ();

foreach $line (@list) {

    ($key) = split (" ", $line);

    # found DISTRIBUTION
    if ($key eq "DISTRIBUTION") {
	($tmp, $word1, $word2) = split (" ", $line);
	if (($word1 eq $group) && ($word2 eq $version)) { $ingroup = 1; }
    }
    if (!$ingroup) { next; }

    # found END
    if ($key eq "END") { last; }

    # found USE
    if ($key eq "USE") {
	($tmp, $word1, $word2) = split (" ", $line);
	if ($word2 eq "") {
	    $tag  = "";
	    $name = $word1;
	} else {
	    $tag  = $word1;
	    $name = $word2;
	}	    
	if ($name eq "") { die "missing tag\n"; }
	push @name, $name;
	push @tag, $tag;
    }
}

if (@name == 0) { die "ERROR: $group $version not found\n"; }

for ($i = 0; $i < @name; $i++) {
    if ($tag[$i] eq "") {
	&vsystem ("cvs co $name[$i]");
    } else {
	&vsystem ("cvs co -r $tag[$i] $name[$i]");
    }	
}

if ($group eq "elixir-config") {
    &vsystem ("mv config $group-$version");
} else {
    &vsystem ("mv ohana $group-$version");
}
&vsystem ("rm -r `find $group-$version -name CVS`");
&vsystem ("tar cvzf $group-$version.tgz  $group-$version");
&vsystem ("rm -r $group-$version");

sub vsystem {
    print STDERR "@_\n";
    $status = system ("@_");
    $status;
}

