#!/usr/bin/ruby -w

# This script is (c) Copyright 2007, P. Lutus
# and is released under the GPL

# relaunch in window

term=`echo $TERM`.chomp

exec("konsole -e #{$0} #{ARGV.join(' ')}") if term == "dumb"

# create m3u format playlists for each music directory

base="/netbackup/music" # change this path to suit your needs

albums = Dir["#{base}/*"]

albums.sort.each do |path|
   data = "#EXTM3U\n"
   # get the directory name, last element in path
   name = path.sub(/.*\/(.*)/,"\\1")
   name.gsub!(/_/," ")
   name.gsub!(/(\A|\s)\w/) { |c| c.upcase }
   puts name + " ..."
   tracks = Dir["#{path}/*.mp3"]
   tracks.sort.each do |track|
      track_name = track.sub(/.*\/(.*)/,"\\1")
      info =`mp3info -x #{track} 2>&1`
      info.sub!(/.*Length:\s*([\d|:]+).*/m,"\\1")
      m,s = info.split(":")
      secs = m.to_i * 60 + s.to_i
      data += "#EXTINF:#{secs},\n" + track_name + "\n"
      File.open("#{path}/#{name}.m3u","w") { |f| f.write data }
   end
end

print "Press Enter to close:"
STDIN.readline
