#!/usr/bin/perl
#
# script to sync rob's perl modules to the binary_c perl directory
#
# this version makes the tar.gz files
#
# you shouldn't run it unless you are Rob!
use strict;

my $root=$ENV{HOME}.'/progs/perl/modules';
my $pwd = $ENV{PWD};

if(-f "$root/deps")
{
    # add dependencies file
    open(my $in, '<', "$root/deps")||die("cannot read $root/deps");
    open(my $out, '>', 'deps')||die("cannot write deps");

    while(my $l = <$in>)
    {
        chomp $l;
        if($l=~/^(.*)$/)
        {
            my $file = $root.'/'.$1;
            if(-f $file)
            {
                my $packname;
                my $version;
                
                open(my $module,'<',$file)||die("cannot open $file");
                while(my $x = <$module>)
                {
                    if($x=~/package ([^;]+);/)
                    {
                        $packname = $1;
                    }
                    elsif($x=~/\$VERSION\s*=\s*([^;]+);/)
                    {
                        $version = eval $1;
                    }
                }
                close $module;

                # convert :: to /
                $packname=~s!\:\:!-!g;
                
                # hence the package .tar.gz
                print "Package name $packname, version $version\n";
                print {$out} "$packname-$version.tar.gz\n";
            }
        }
    }
    close $in;
    close $out;
}

foreach my $mod (
    'misc/rob_misc',
    'misc/RobMerge',
    'misc/robqueue',
    'misc/Data/Serializer/',
    'astro/cosmology',
    'astro/binary_grid',
    'astro/IMF',
    'astro/distribution_functions',
    'astro/spacing_functions',
    'astro/binary_stars',
    'maths/Maths_Double',
    'maths/Binning',
    'maths/RobInterpolation',
    'maths/Histogram',
    )
{

    my $path=$mod;
    $path=~s/.*lib\///;
    my $dir = "$root/$mod";
    print "Module $mod (from $dir)\n";

    my $targz = (`cd $dir; perl Makefile.PL; make COMPRESS="gzip --best --force" tardist`=~/Created (\S+)/)[0];
    if(defined $targz && $targz ne '')
    {
	print "Copy $targz to $pwd, add at $pwd/$targz\n";
     	`cp $dir/$targz $pwd; cd $pwd ;  git add $targz`; 
    }
}
