module NyanCatFormat::Music

def dump_summary(duration, example_count, failure_count, pending_count)

def dump_summary(duration, example_count, failure_count, pending_count)
  kill_music
  super
end

def kernel

def kernel
  @kernel ||= Kernel
end

def kernel=(kernel)

def kernel=(kernel)
  @kernel = kernel
end

def kill_music

def kill_music
  if File.exists? nyan_mp3
    if osx?
      system("killall -9 afplay &>/dev/null")
    elsif linux?
      kill_music_on_linux
    end
  end
end

def kill_music_on_linux

def kill_music_on_linux
  system("killall -9 mpg321 &>/dev/null") if kernel.system("which mpg321 &>/dev/null && type mpg321 &>/dev/null")
  system("killall -9 mpg123 &>/dev/null") if kernel.system("which mpg123 &>/dev/null && type mpg123 &>/dev/null")
end

def linux?

def linux?
  platform.downcase.include?('linux')
end

def nyan_mp3

def nyan_mp3
  File.expand_path('../../../data/nyan-cat.mp3', __FILE__)
end

def osx?

def osx?
  platform.downcase.include?("darwin")
end

def platform

def platform
  @platform ||= RUBY_PLATFORM
end

def platform=(platform)

def platform=(platform)
  @platform = platform
end

def play_on_linux

def play_on_linux
  kernel.system("[ -e #{nyan_mp3} ] && type mpg321 &>/dev/null && mpg321 #{nyan_mp3} &>/dev/null &") if kernel.system('which mpg321 &>/dev/null && type mpg321 &>/dev/null')
  kernel.system("[ -e #{nyan_mp3} ] && type mpg123 &>/dev/null && mpg123 #{nyan_mp3} &>/dev/null &") if kernel.system('which mpg123 &>/dev/null && type mpg123 &>/dev/null')
end

def start input

def start input
  super
  t = Thread.new do
    loop do
      if osx?
        kernel.system("afplay #{nyan_mp3} &")
      elsif linux?
        play_on_linux
      end
      Thread.current["started_playing"] ||= true
      sleep MUSIC_LENGTH
    end
  end
  until t["started_playing"]
    sleep 0.001
  end
end