#!/usr/bin/env perl
# Copyright (c) 2010 David Sugar, Tycho Softworks
#
# This file is free software; as a special exception the author gives
# unlimited permission to copy and/or distribute it, with or without
# modifications, as long as this notice is preserved.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

strict;

die("*** not a git project") unless(-d ".git");

my $project;
my $tag = shift @ARGV;
if(-e "CMakeLists.txt") {
    open FILE, "CMakeLists.txt" or die("*** $basename: cannot access cmake");
    while(<FILE>) {
	$line = $_;
	chomp($line);
	if($line =~ m/^[ \t]*PROJECT[ \t]*[\(]/i) {
	    $project = $line;
	    $project =~ s/^[ \t]*PROJECT[ \t]*[\(]//;
	    $project =~ s/[\)].*$//;
	    close FILE;
	}
    }
    close FILE;
}
die("*** could not get project") if("$project" eq "");

my $version = "$tag";

if("$tag" eq "") {
    $tag = "HEAD";
    $version = "current";
}

$version =~ s/^v//;

system "git archive --format tar.gz --output=$project-$version.tar.gz --prefix=$project-$version/ $tag";
system "git archive --format zip --output=$project-$version.zip --prefix=$project-$version/ $tag";

