#!/usr/bin/perl

# workaround for zaurus mplayer crash when advancing to next file under following playing conditions:
#   a. -playlist
#   b. file*
#   c. file1.mpg file2.mpg

# usage: mplaylist.pl playlist.m3u [mplayer options]

if ($#ARGV < 0) {
  die "usage: mplaylist.pl playlist.m3u [mplayer options]\n";
} elsif (! -T $ARGV[0]) {
  die "\"$ARGV[0]\" is not a plain text file\n";
} elsif ($ARGV[0] !~ /\.m3u$/) {
  die "\"$ARGV[0]\" does not end in \".m3u\"\n";
}
open IN, "<$ARGV[0]" || die "Can't open \"$ARGV[0]\".\n";
my @file = <IN>;
close IN || die "Can't close \"$ARGV[0]\".\n";
my $options;
for (1 .. $#ARGV) {
  $options .= ' ' . $ARGV[$_];
}
foreach (@file) { chomp; }
foreach (@file) {
  my $file = $_;
  $file =~ s/\\/\//g;
  my $cmd = 'mplayer' . $options . " /mnt/cf/" . $file . " >/dev/null 2>&1";
  #print "\$cmd = \"$cmd\"\n";
  system $cmd;
  system 'clear';
}
exit 0;
