#!/usr/bin/env ruby

BASE = "/pegasus/pub/music/artists"

DEST = "/media/josh/JoshMusic"

ARTISTS = <<EOF
A Perfect Circle
All That Remains
#Avenged Sevenfold
Collective Soul
Dave Matthews Band
Days Of The New
DevilDriver
deadmau5
#Falling in Reverse
Eluveitie
Fear Factory
Fuel
#Godsmack
Hans Zimmer
Incubus
LFO
Lamb of God
Linkin Park
London Symphony Orchestra
#Machine Head
Matchbox Twenty
Mudvayne
Our Lady Peace
POD
Ra
Rush
#Staind
#Static-X
#System of a Down
Tantric
Taproot
The Goo Goo Dolls
The Offspring
#Tool
Trans-Siberian Orchestra
Trivium
Volbeat
Weezer
EOF

EXCLUDE = <<EOF.split("\n").map(&:chomp)
L02 Machine Fucking Head Live
EOF

require "fileutils"

artists = ARTISTS.lines.map do |artist|
  if artist !~ /#/
    artist.chomp
  end
end.compact

if ARGV[0] == "-s"
  cmd = %w[du -shc] + artists.map {|a| "#{BASE}/#{a}"}
  system(*cmd)
  exit 0
end

unless Dir.exist?(DEST)
  $stderr.puts "#{DEST} does not exist"
end

artists.each do |artist|
  Dir.glob("#{BASE}/#{artist}/*").sort.each do |album_dir|
    next if album_dir.split("/").any? {|part| EXCLUDE.include?(part)}
    album = File.basename(album_dir)
    mp3_files = Dir.glob("#{album_dir}/*.mp3").sort
    dest = "#{DEST}/#{artist}/#{album}"
    puts "Creating #{dest}" unless Dir.exist?(dest)
    FileUtils.mkdir_p(dest)
    mp3_files.each do |mp3_file|
      unless File.exist?("#{dest}/#{File.basename(mp3_file)}")
        puts "Copying #{mp3_file} -> #{dest}"
        FileUtils.cp(mp3_file, dest)
        system("sync", DEST)
      end
    end
  end
end
