#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

# 20090711 (ed) I use this script so that when I copy stuff off my camera's SD card it can be stored in the dated directories. This is highly useful for my file organisation
#
# usage:
#   copy_shit.pl * ~/Photos/nikon/
		

sub _mkdir {
	my $path = shift;
	my @p = split( /\//, $path );
	my $c = "";

	foreach( @p ) {
		$c .= $_ . "/";
		mkdir( $c );
	}
}

sub do_copy {
	my $dest = shift;
	my @files = @_;

	foreach( @files ) {
		my @t = localtime( (stat( $_ ))[9] ) or die;
		my $path = sprintf( "./%s/%.4d/%.2d/%.2d/", $dest, $t[5]+1900, $t[4]+1, $t[3] );
		_mkdir( $path );
		die if( ! -d $path );
		copy( $_, $path . $_ ) or die;
	}

}


sub main {
	die if( !defined( $ARGV[0] ) || !defined( $ARGV[1] ) );
	my $dest = pop( @ARGV );
	
	mkdir( $dest );
	die if( ! -d $dest );

	do_copy( $dest, @ARGV );
}


main();

