#!/usr/bin/perl -w

# Copyright (c) 2010 Jim Peters (http://uazu.net)

$indir = $ARGV[0];
$outdir = $ARGV[1];

$hashlen = 6;

die "Bad args" unless -d $indir && -d $outdir;

die unless open IN, "cd $indir && md5sum *.ly | sort |";

%used = ();

while (<IN>) {
    my ($hash, $fnam) = split;
    next unless $fnam =~ /\.ly/;
    my $short = uc(substr($hash, 0, $hashlen));
    die "Duplicate short-hash $short: $hash versus $used{$short}"
        if (defined $used{$short} && $used{$short} ne $hash);
    $used{$short} = $hash;

    print "$fnam $short\n";
    system "cp $indir/$fnam $outdir/$short.ly";
}
