#!/usr/bin/perl
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#NAME
#   tonew - link files into new/ directory
#
#SYNOPSIS
#   tonew file...
#
#DESCRIPTION
# Link the files into the new/ directory.  We create the new/ directory if it
# doesn't exist. Then we go through the file list, linking all the files into
# new/.  This is typically run as:
#   find */ -mtime -30 -name '*.abc' | xargs tonew
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || 1;

unless (-d "new") {
	if (mkdir("new",0775)) {
		print STDERR "$P: Made \"new\" directory.\n" if $V>1;
	} else {
		die "$P: Can't make directory \"new\" [$!]\n";
	}
}
file:
for $pth (@ARGV) {
	unless (-f $pth) {
		print STDERR "$P: \"$pth\" doesn't exist.\n" if $V>0;
		next file;
	}
	($fil = $pth) =~ s".*/"";	# File name without directory
	$new = "new/$fil";			# New file pathname
	if ($new eq $pth) {			# Don't try to link file to itself
		print STDERR "$P: \"$pth\" ignored.\n" if $V>2;
		next file;
	}
	unlink($new) if -f $new;
	if (link($pth,$new)) {
		print STDERR "$P: \"$pth\" -> \"$new\"\n" if $V>1;
	} else {
		print STDERR "$P: \"$pth\" -> \"$new\" FAILED [$!]\n" if $V>0;
	}
}
