#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  tunedir - Make a directory for a set of tunes and move them to it.
#  todir - Make a directory for a set of tunes and move them to it.
#
#SYNOPSIS
#  tunedir dir [file]..
#  todir dir [file]..
#REQUIRES
#
#DESCRIPTION
#
#OPTIONS
# None yet, but there may be some soon.
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
$files = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level.

if ($dir = shift) {
	if (mkdir $dir) {
		print STDERR "$P: Make \"$dir\" succeeded.\n" if $V>2;
	} else {
		print STDERR "$P: Make \"$dir\" failed [$!]\n" if $V>0;
	}
	if (-d $dir) {
		print STDERR "$P: \"$dir\" is a directory.\n" if $V>2;
	} elsif (-e $dir) {
		print STDERR "$P: \"$dir\" exists and is not a directory.\n" if $V>0;
		exit 1;
	} else {
		print STDERR "$P: \"$dir\" not created [$!]\n" if $V>0;
		exit 1;
	}
	foreach $fil (@ARGV) {
		print STDERR "$P: Move \"$fil\" ...\n" if $V>2;
		if (($pth,$nam) = ($fil =~ m"^(.*)/(.+)")) {
			print STDERR "$P: pth=\"$pth\" nam=\"$nam\"\n" if $V>2;
		} else {
			$pth = '';
			$nam = $fil;
		}
		if (rename($fil, "$dir/$nam")) {
			print STDERR "$P: \"$fil\" moved to \"$fil\".\n" if $V>2;
			++$files;	# Count the files moved.
		} else {
			print STDERR "$P: Move \"$fil\" failed [$!]\n" if $V>0;
		}
	}
}
print STDERR "$P: $files files moved.\n" if $V>1;

exit $exitstat;
