#!/usr/bin/perl
# simple driver to convert flac files to wav
# just drives flac, mainly to allow
# wildcards and to remember options so I don't 
# have to. Assumes flac is  on path.
#
# Created on May 22, 2004
# Copyright (c) 2004, Pat Farrell, All rights reserved.
# This code will be released with a suitable Open Source
# license, probably BSD-like.
# 
# make it handle arguments the way Windows users expect.
use File::DosGlob 'glob';
use Getopt::Long;
use File::Basename;

my $help;
my $outspec;
my $quality = 2;
my $otherLameOptions;

    usage() if ( $#ARGV < 0 ); 
    GetOptions('help|?' => \$help, 'output=s' => \$outspec, 'quality=i' => \$quality, 
	'lame=s' => \$otherLameOptions);
    usage() if $help;

while ($anArg = shift(@ARGV)) {
    $anArg =~ s/(\s)/\\\1/g;	# quote any spaces before we glob
    @argfile = glob ($anArg);
    foreach $aFile (@argfile) {
	($basename, $dir, $ext) = fileparse($aFile, '\..*?');
	if ($ext =~ /.flac$/i ) {
	    $needQuotes = ($aFile =~ m/\s/) ? "\"" : "";
	    $outfn = (($outspec) ? $outspec :  $dir) . $basename . ".wav"; 
	    $cmd = "flac -ds " . $needQuotes . $aFile . $needQuotes   
		. $needQuotes . $outfn . $needQuotes ;
   	    print "$cmd\n";
	    $retval = system($cmd);
	}
	else {
	    print "$aFile will be ignored\n";
	}
    }
} 
sub usage {
    print "convert flac files to wav (PCM)\n";
    print "Usage:\n   flac2wav.pl [-output=path]  {files}\n";
    print "note, the output path has to end with the directory separator character.\n";
    print "   \\ on Windows, \/ on linux, etc.\n";
    exit;
}

