#!/usr/bin/perl
# this script will try to read from a list of mp3 files and create unique playlists for them



system ("find /www/html/ | grep -i mp3 > /www/html/radio/master_paths.local"); 
 

open(FILEPATHS, "/www/html/radio/master_paths.local");
open(FILENAMES, ">/www/html/radio/filenames.txt");
open(PATHS, ">/www/html/radio/paths.txt");

@filen = ();
@filep = ();
@m3u = ();

while ($line = <FILEPATHS>) {

 if (! ($line =~ /random_mp3\.cgi/) ) {

    $line =~ s/.*\///gi;
    $line =~ s/\.mp3//gi;
    $line =~ s/'//gi;
#    $line =~ s/&/\\&/gi;
    chomp $line;
    push(@filen,$line);
    print FILENAMES ($line);

 }

}

close FILENAMES;
close FILEPATHS;

open(FILEPATHS, "/www/html/radio/master_paths.local");

while ($line = <FILEPATHS>) { 

    $line =~ s/(.*\/).*/$1/gi;
    chomp $line;   
    push(@filep,$line);
    print PATHS ($line);

} 

close PATHS;
close FILEPATHS;


open(FILEPATHS, "/www/html/radio/master_paths.local");
#open (M3U, ">/www/html/radio/master.m3u");

while ($line = <FILEPATHS>) {

    $line =~ s/\/www\/html/http\:\/\/phobos\.serve\.com/gi;
    chomp $line;   
    push(@m3u,$line);
#    print M3U ($line);

}


close(FILEPATHS);
close(M3U);  

while (@filen) {

    $a = pop(@filen);
    $b = pop(@filep);
    $c = pop(@m3u);
    $d = "$b$a.m3u";
    open(OUT, ">$d");
    print OUT ($c);
    close OUT;
}    


# this next script will read from a directory of mp3 files and create playlists for all of the files in each directory
# this is accomplished by parsing a list of directories and filenames grepped from ls  

system ("ls -R1 /www/html | grep -v m3u > /www/html/radio/directories.local");

open(DIRECTORY, "/www/html/radio/directories.local");


while ($line = <DIRECTORY>) {

     if ($line =~ /\:/) {

          $path = $line;
          $path =~ s/(.*)\/(.*):/$2/g;
          chomp $path;

          $directorypath = $line;
          $directorypath =~ s/\://;
          chomp $directorypath;

          $directory = $directorypath;
          $directory =~ s/\/www\/html//;
          unlink <$directorypath/$path.m3u>;
     }

     if ($line =~ /.*mp3/) {

          open(M3U, ">>$directorypath/$path.m3u");
          chomp $line;
          $m3u = "http://phobos.serve.com$directory/$line\n";
          print M3U ($m3u);
          close M3U;
     }

}

close DIRECTORY; 
