#!/usr/bin/perl
#
# Prabakar 11/7/08
#
# This script file has been customized for Windows 32
#
# This perl script merges all mp3 files present in a given directory
#  into a single mp3 file (named as dirname.mp3 outside the directory)
#
# e.g.   S:\perl\bin\perl.exe MergeMP3files.pl cd3

use strict;

if ($#ARGV != 0) { # in Perl, argcnt is zero for one argument
    die "Usage: perl $0 mp3FilesDir \n";
}


my $dirName = $ARGV[0];
my $finalMP3 ="$dirName.mp3";

print " Directory name: $dirName\n";

# DIR/B/O:D lists files as well as folders one per line without summary
#  and list files in ascending timestamp values
my @files = `dir/b/o:d $dirName`;

my $fileCount = 0;
chdir($dirName) or die "Unable to enter dir $dirName:$!\n";
foreach my $file (@files) {
  $file =~ s/\s*$//;
  if ($file =~ /mp3$/oi) {
    if (!$fileCount) {
      system(" copy /b \"$file\" ..\\$finalMP3 > NUL ");
    }
    else {
      system(" copy /b ..\\$finalMP3 + \"$file\" ..\\$finalMP3 > NUL ");
    }
    $fileCount++;
  }
}
chdir("..");
print "\n From the directory $dirName, $fileCount mp3 files \n";
print "      have been merged to $finalMP3 \n\n";

exit(0);

