#!/usr/bin/perl -w

use Cwd;
use File::Copy;

## transfer files to a standard location, splitting them into chunks.
## If a directory is specified, it will be recursed into.


## where to copy to
my $standard_to_directory = "~/transfer";
## chunk size. This must be in bytes as we use it both with perl's
## stat command and through the command line split command.
my $chunk_size = "5000000";

$standard_to_directory = glob( $standard_to_directory );

transfer_split ($standard_to_directory, @ARGV);

sub transfer_split{
  my $to_directory = shift;
  my @files = @_;

  my $current_directory = getcwd();

  if ( ! -e $to_directory ){
    mkdir( $to_directory ) || die "Cannot make directory $to_directory: $!\n";
  }

  foreach my $file (@files){
    if ( -d $file ){
      chdir ( $file ) || die "Cannot change into directory $current_directory"
        . "$file: $!\n";

      transfer_split ( "$to_directory/$file",
                       glob ( "*" ) );

      chdir( ".." );
    }
    else{

      my $size = (stat( $file ))[ 7 ];

      if( $size < $chunk_size ){
        copy( $file, $to_directory );
      }
      else{

        my $cwd = getcwd();
        my $file_directory = "$to_directory/file_$file";

        mkdir ( $file_directory ) ||
          die "Cannot make file directory $file_directory: $!\n";

        chdir( $file_directory ) ||
          die "Cannot change into directory $file_directory: $!\n";

        ## split file. Increase default suffix size or we will run out
        system( "split -b $chunk_size -a 5 $cwd/$file" ) == 0 ||
          die "File split failed $cwd/$file: $!\n";

        ## Think we can make a simple checksum by concating all of the
        ## names and their lengths into a big string. Might want to
        ## store a CRC64 as well for the file, although this might not
        ## be good if the file is large.
        my $checksum = "";
        foreach $file ( glob( "*" ) ){
          $checksum .= $file;
          $checksum .= (stat( $file ))[ 7 ];
        }

        open MANIFEST, ">MANIFEST" || die "Cannot open MANIFEST file: $!\n";
        print MANIFEST $checksum;
        close MANIFEST || die "Cannot close MANIFEST file: $!\n";

        chdir( $cwd ) ||
          die "Cannot change into directory $cwd: $!\n";
      }
    }
  }
}


