#!/usr/bin/perl

# xmms2rhythmbox -- by greenfly
#
# This script will take a standard xmms playlist as input and output a rhythmbox-compliant playlist
# usage: xmms2rhythmbox <xmms playlist input file> <rhythmbox playlist output file>

use strict;

my $infile = shift;
my $outfile = shift;

if($infile eq "" or $outfile eq "")
{
   print_usage();
   exit;
}

my %files;

parse_xmms_playlist();
print "found " . $files{0}{"title"} . " files in $infile, writing to $outfile\n";
print_rhythmbox_playlist();

sub print_usage
{
   print "usage: xmms2rhythmbox <xmms playlist input file> <rhythmbox playlist output file>\n";
}

sub parse_xmms_playlist
{
   open XMMS, "$infile" or die "Can't open $infile: $!";

   my $count = 0;
   while(<XMMS>)
   {
      chomp;
      if(/^\#EXTINF:.+,.+ - .* - (.*)/)
      {
	 $count++;
	 $files{$count}{"title"} = $1;
      }
      elsif(/^\/\w+/)
      {
	 $files{$count}{"file"} = $_;
      }
   }
   close XMMS;
# store how many files you found
   $files{0}{"title"} = $count;

}



sub print_rhythmbox_playlist
{

   open RHYTHMBOX, ">$outfile" or die "Can't open $outfile:$!";
   print RHYTHMBOX "[playlist]\n";
   print RHYTHMBOX "numberofentries=" . $files{0}{"title"} . "\n";

   for(my $i = 1; $i <= $files{0}{"title"}; $i++)
   {
      print RHYTHMBOX "file$i=file://" . $files{$i}{"file"} . "\n";
      print RHYTHMBOX "title$i=" . $files{$i}{"title"} . "\n";
   }
   close RHYTHMBOX;
}


