Added multi-playlist support to upod
authorChip Black <bytex64@bytex64.net>
Tue, 3 Jun 2008 01:59:23 +0000 (20:59 -0500)
committerChip Black <bytex64@bytex64.net>
Tue, 3 Jun 2008 01:59:23 +0000 (20:59 -0500)
upod.pl

diff --git a/upod.pl b/upod.pl
index 2afbc33..e2e7409 100755 (executable)
--- a/upod.pl
+++ b/upod.pl
@@ -1,27 +1,76 @@
 #!/usr/bin/perl
 use File::Basename;
+use File::Copy;
+use Getopt::Long;
 use strict;
 
 my $uthome = $ENV{HOME} . '/.utunes';
-my $outdir = "/home/yomiko/chip/dspod/playlist";
+my $outdir = $ENV{HOME} . "/dspod";
+my $name = "playlist";
+my $multi;
+my $proc;
+#$proc = sub ...
 
+GetOptions(
+       "outdir=s"      => \$outdir,
+       "multi=s"       => \$multi,
+       "name=s"        => \$name,
+);
+
+mkdir $outdir unless -d $outdir;
+
+if ($multi) {
+       if ($multi =~ /[Kk]$/) {
+               $multi *= 10**3;
+       } elsif ($multi =~ /[Mm]$/) {
+               $multi *= 10**6;
+       } elsif ($multi =~ /[Gg]$/) {
+               $multi *= 10**9;
+       } elsif ($multi =~ /[Tt]$/) {
+               $multi *= 10**12;
+       }
+}
+
+my @files;
 open FILES, $ARGV[0] || "$ENV{HOME}/.utunes/playlist";
-my @files = <FILES>;
+chomp(@files = <FILES>);
 close FILES;
-chomp foreach @files;
-
-system("rm -rf $outdir");
-mkdir $outdir;
 
-open PLAYLIST, ">$outdir/list.m3u";
-print PLAYLIST "# Generated by musicload\n";
+my $n = 0;
+LIST: {
+       my $c = 0;
+       my ($dir, $playlist);
+       if ($multi) {
+               $dir = "$outdir/${name}_$n";
+               $playlist = "$outdir/${name}_$n/${name}_$n.m3u";
+       } else {
+               $dir = "$outdir/$name";
+               $playlist = "$outdir/$name/$name.m3u";
+       }
+       mkdir $dir unless -d $dir;
+       open PLAYLIST, ">$playlist";
+       print PLAYLIST "# Generated by upod\n";
 
-my $i = 0;
-foreach my $file (@files) {
-       my $outfile = sprintf("%04d_", $i++) . basename $file;
-       print PLAYLIST "$outfile\n";
-       symlink $file, "$outdir/$outfile"
-               or die "Symlink from $file to $outdir/$outfile failed";
+       my $i = 0;
+       while (@files) {
+               if ($multi && $c + -s $files[0] > $multi) {
+                       $n++;
+                       close PLAYLIST; # I don't strictly have to close the
+                                       # file (it is automatically closed when
+                                       # PLAYLIST is reopened above), but if I
+                                       # don't some hapless C programmer will
+                                       # come across this and freak out.
+                       goto LIST;
+               }
+               my $file = shift @files;
+               $c += -s $file;
+               my $outfile = sprintf("%04d_", $i++) . basename $file;
+               print PLAYLIST "$outfile\n";
+               die "$dir/$outfile already exists!" if -e "$dir/$outfile";
+               symlink $file, "$dir/$outfile"
+                       or copy $file, "$dir/$outfile"
+                       or die "Could not symlink or copy $file to $dir/$outfile";
+       }
 }
 
 close PLAYLIST;