#!/usr/bin/perl

# Copyright 2004 - Damien Raude-Morvan - http://www.drazzib.com/
# Under GPL (http://www.gnu.org/licenses/gpl.html)

$maindir = @ARGV[0];
chdir($maindir) or die ("Répertoire \"$maindir\" inaccessible...");
opendir MAIN, ".";

my @lstalbums = readdir(MAIN);
foreach $album (@lstalbums) {
        next if $album eq "." or $album eq "..";

        if (-d $album) {
                chdir $album;
                print "Traitement de $album...";
                @mp3 = listdir(".");

                open(FH, ">$album.m3u");
                print " -> ($#mp3 fichier(s))\n";
                foreach $f (@mp3) {
                        print FH $f."\n";
                }
                close FH;

                chdir "../";
        }
}



sub listdir {
        my $dir = $_[0];
        opendir R, $dir;
        my @retour = ();
        my @liste = readdir(R);
        foreach $f (@liste) {
                next if $f eq "." or $f eq "..";
                if (-d $f) {
                        push(@retour, listdir($f));
                } else {
                        if ($f =~ /^(.*)\.mp3/) {
                                push(@retour, $dir."/".$f);
                        }
                        if ($f =~ /^(.*)\.ogg/) {
                                push(@retour, $dir."/".$f);
                        }

                }
        }
        return @retour;
}