#!/usr/bin/perl
# author: J.Ursetto
use strict;

sub usage {
    die <<'EOF'
mvln [-t] /full/path/to/dest file1 ...

Move files $2 .. $n to directory $1.  Directories are created
automatically.  Links are made from the original location
to the new destination.  Moves may span filesystems.
-t option touches each file after the move.

Typical use: mvln /mnt/mp3/cd */*/*.mp3
EOF
}

umask 0002;
my $touch = ($ARGV[0] eq '-t') ? 1 : 0;
shift if $touch;

my $DEST=$ARGV[0];
usage() unless $DEST;

shift @ARGV;

for (@ARGV) {
	my $DIR="$DEST/" . `dirname "$_"`;
	my $BASE=`basename "$_"`;
	chomp $DIR; chomp $BASE;
	system(qq{mkdir -p "$DIR" && mv "$_" "$DIR"}) == 0
		or warn("failed to move $_\n"), next;
	system(qq{ln -s "$DIR/$BASE" "$_"}) == 0
		or warn("failed to link $_\n"), next;
	if ($touch) {
		system(qq{touch "$_"}) == 0
			or warn("failed to touch $_\n"), next;
	}
	print "$_ -> $DIR\n";
}

