#!/usr/bin/env ruby # # Reorganizes Frets on Fire songs in libraries # using artist's name (from song.ini) # # Usage: ruby reorder.rb /path/to/fof/data/songs # # (c) Y.Guillot, 08/2007 # # License: Wtfpl Dir.chdir ARGV.shift || 'D:\games\FretsonFire\data\songs' songs = Dir['**/song.ini'].map { |f| File.dirname f } puts " [+] found #{songs.length} songs" # map song to library name hash = {} songs.each { |s| if artist = File.read(File.join(s, 'song.ini')).grep(/artist/i).first libname = artist.split('=', 2).last.sub(/^\s*the\s+/i, '').strip.downcase[0,1] end libname = '_' if libname !~ /[a-z]/i (hash[libname] ||= []) << s } # regroup songs in bigger libraries lib, content = '', [] while not hash.empty? k = hash.keys.min lib << k content.concat hash.delete(k) if content.length > 100 or hash.empty? content.each { |s| np = File.join lib, File.basename(s) if not File.exist? np Dir.mkdir lib if not File.exist? lib puts np File.rename s, np end } lib, content = '', [] end end # remove empty directories Dir['*'].each { |d| next if not File.directory? d li = File.join d, 'library.ini' if Dir.entries(d) - %w[. .. library.ini] == [] File.unlink li if File.exist? li Dir.rmdir d else File.open(li, 'w') { |fd| fd.puts '[library]', "name = #{File.basename d}" } end }