#!/usr/bin/ruby
# -*- coding:utf-8 -*-

require 'json'
require 'inifile'
def ust2json(ustfile)
  ust = ""
  ustjson = Hash.new
  ustjson["setting"] = Hash.new
  ustjson["notes"] = Array.new

#mmrb = '../ust/mimirobo.ust'
#drm = '../ust/doremi.ust'

#File.open(drm) do |file|
  File.open(ustfile) do |file|
    file.each_line do |line|
      if /.*version.*/i !~ line
        ust += line
      end
    end
  end
  
  File.open("cache.ini", "w") do |ufile|
    ufile.puts(ust)
  end
  
  inifile = IniFile.load('./cache.ini')
  
  inifile.each_section do |section|
    
#Lyric, Length, NoteNum
    if section == "#SETTING"
      for x in inifile[section].keys do
        ustjson["setting"].store(x, inifile[section][x])
      end
    elsif section == "#TRACKEND"

    else
      z = Hash.new
      for y in inifile[section].keys do
        z.store(y, inifile[section][y])
      end
      ustjson["notes"].push(z)
    end
  end

  File.delete('./cache.ini')

  File.open("#{File.basename(ustfile, ".ust")}.ustjson", "w") do |jfile|
    JSON.dump(ustjson, jfile)
  end
end

#ust2json('../ust/doremi.ust')
