#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  tunedir - Make a directory for a set of tunes and link them to it.
#  todir - Make a directory for a set of tunes and link them to it.
#  tdir - Make a directory for a set of tunes and link them to it.
#  gdir - Gather [files] into a directory
#
#SYNOPSIS
#  tunedir dir [file]..
#  todir dir [file]..
#REQUIRES
#
#DESCRIPTION
# The directory is created if it doesn't exist, then the files are all
# linked into it.  Warnings are produced (on stderr) for files that
# can't be linked into the directory.
#
#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: Link \"$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 (link($fil, "$dir/$nam")) {
			print STDERR "$P: \"$fil\" linked to \"$fil\".\n" if $V>2;
			++$files;	# Count the files linked.
		} else {
			print STDERR "$P: Link \"$fil\" failed [$!]\n" if $V>0;
		}
	}
}
print STDERR "$P: $files files linked.\n" if $V>1;

exit $exitstat;
