#!/usr/local/bin/perl -w
use strict;

use ExtUtils::Manifest "/mani/";
use POSIX qw(getcwd);

use vars qw($opt_dev $opt_man $version);

use Getopt::Long;

GetOptions('dev=s','man');

sub System
{
 print STDERR join(' ',@_),"\n" if (@_ < 8);
 die "Cannot " . join(' ',@_) if (system(@_));
}

open(MF,"<Makefile.PL") || die "Cannot open Makefile.PL:$!";
while (<MF>)
 {
  if (/\$VERSION\s*=\s*'([^']+)'/)
   {
    $version = $1;
    last;
   }
 }
close(MF);

die "Cannot find version\n" unless defined $version;

System("perl","Makefile.PL") unless (-r "Makefile");

System("make","manifest") if ($opt_man);

my $dir   = getcwd();
my $files = maniread();
$dir   =~ s#^.*/([^/]+)$#$1#;

my @files = map("$dir$version/$_",sort(keys %$files));
chdir("..");

unlink("$dir$version") if (-l "$dir$version");
symlink($dir,"$dir$version") || die "Cannot link $dir $dir$version:$!";

$dir .= $version;

system("mv","$dir.tar.gz","$dir.tar.gz%") if (-e "$dir.tar.gz");

if (defined $opt_dev)
 {
  System("tar","-cf",$opt_dev,@files);
  System("tar","-tvf",$opt_dev);
 }
else
 {
  warn "Writing $dir.tar\n";
  System("tar","-cf","$dir.tar",@files);
  warn "gzip'ing $dir.tar\n";
  System("gzip",'-9',"$dir.tar");
 }


