#!/usr/bin/perl -w
# This is a quick-and-dirty script to identify RSCDS "book" tunes and move
# them into the appropriate ../BookNN/ directory

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

:files = `ls [0-9][0-9][0-9][0-9]*.abc`;

for $file (:files) {
    $file =~ s/[\r\s]+$//;
    if (($bk,$dn,$rest) = ($file =~ /^(\d\d)(\d\d)(.*)$/)) {
        $path = "../Book$bk/$bk$dn$rest";
        print "Move \"$file\" -> \"$path\"\n" if $V>0;
        if (link($file,$path)) {
            unlink($file);
        } else {
            print STDERR "$P: Can't link \"$file\" to \"$path\" [$!]\n" if $V>0;
        }
    } else {
        print STDERR "$P: Can't parse \"$file\"\n" if $V>0;
    }
}

print "$P: Exit with status $exitstat.\n" if $V>1;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
