#!/usr/bin/env ruby

require 'fileutils'

renames = {}
renames['Audio'                         ] = 'Onyx.Audio'
renames['AudioSearch'                   ] = 'Onyx.Audio.Search'
renames['Build'                         ] = 'Onyx.Build'
renames['C3'                            ] = 'Onyx.C3'
renames['CommandLine'                   ] = 'Onyx.CommandLine'
renames['Config'                        ] = 'Onyx.Project'
renames['Cytoid'                        ] = 'Onyx.Cytoid'
renames['DeriveHelpers'                 ] = 'Onyx.DeriveHelpers'
renames['Difficulty'                    ] = 'Onyx.Difficulty'
renames['DryVox'                        ] = 'Onyx.Vocal.DryVox'
renames['FretsOnFire'                   ] = 'Onyx.FretsOnFire'
renames['Genre'                         ] = 'Onyx.Genre'
renames['GuitarHeroIOS'                 ] = 'Onyx.GuitarHero.IOS'
renames['GuitarPro'                     ] = 'Onyx.GuitarPro'
renames['Guitars'                       ] = 'Onyx.Guitar'
renames['Image'                         ] = 'Onyx.Image.DTX'
renames['Magma'                         ] = 'Onyx.Harmonix.Magma'
renames['MelodysEscape'                 ] = 'Onyx.MelodysEscape'
renames['MoggDecrypt'                   ] = 'Onyx.Harmonix.MOGG'
renames['OneFoot'                       ] = 'Onyx.Drums.OneFoot'
renames['OpenProject'                   ] = 'Onyx.Import'
renames['OSFiles'                       ] = 'Onyx.Util.Files'
renames['Overdrive'                     ] = 'Onyx.Overdrive'
renames['Preferences'                   ] = 'Onyx.Preferences'
renames['PrettyDTA'                     ] = 'Onyx.Harmonix.DTA.C3'
renames['ProKeysRanges'                 ] = 'Onyx.Keys.Ranges'
renames['QuickConvert'                  ] = 'Onyx.QuickConvert'
renames['Reductions'                    ] = 'Onyx.Reductions'
renames['RenderAudio'                   ] = 'Onyx.Audio.Render'
renames['Resources'                     ] = 'Onyx.Resources'
renames['RockBand2'                     ] = 'Onyx.Build.RockBand2'
renames['RockBand3'                     ] = 'Onyx.Build.RB3CH'
renames['SndfileExtra'                  ] = 'Onyx.Audio.SndfileExtra'
renames['SpinRhythm'                    ] = 'Onyx.SpinRhythm'
renames['U8'                            ] = 'Onyx.Wii.U8'
renames['WAD'                           ] = 'Onyx.Wii.WAD'
renames['WebPlayer'                     ] = 'Onyx.WebPlayer'
renames['YAMLTree'                      ] = 'Onyx.YAMLTree'
renames['Amplitude.File'                ] = 'Onyx.Amplitude.File'
renames['Amplitude.Track'               ] = 'Onyx.Amplitude.Track'
renames['Amplitude.PS2.Ark'             ] = 'Onyx.Harmonix.Ark.Amplitude'
renames['Amplitude.PS2.TxtBin'          ] = 'Onyx.Amplitude.PS2.TxtBin'
renames['Beatmania.BMS'                 ] = 'Onyx.Beatmania.BMS'
renames['Build.CloneHero'               ] = 'Onyx.Build.CloneHero'
renames['Build.Common'                  ] = 'Onyx.Build.Common'
renames['Build.GuitarHero1'             ] = 'Onyx.Build.GuitarHero1'
renames['Build.GuitarHero2'             ] = 'Onyx.Build.GuitarHero2'
renames['Build.GuitarHero3'             ] = 'Onyx.Build.GuitarHero3'
renames['Build.GuitarHero5'             ] = 'Onyx.Build.GuitarHero5'
renames['Build.PowerGig'                ] = 'Onyx.Build.PowerGig'
renames['Build.RockBand'                ] = 'Onyx.Build.RockBand'
renames['Build.Rocksmith'               ] = 'Onyx.Build.Rocksmith'
renames['Control.Monad.Codec.Onyx'      ] = 'Onyx.Codec.Common'
renames['Control.Monad.Codec.Onyx.JSON' ] = 'Onyx.Codec.JSON'
renames['Control.Monad.Codec.Onyx.XML'  ] = 'Onyx.Codec.XML'
renames['Control.Monad.Trans.StackTrace'] = 'Onyx.StackTrace'
renames['Data.DTA'                      ] = 'Onyx.Harmonix.DTA'
renames['Data.Binary.Codec.Class'       ] = 'Onyx.Codec.Binary'
renames['Data.DTA.Base'                 ] = 'Onyx.Harmonix.DTA.Base'
renames['Data.DTA.Crypt'                ] = 'Onyx.Harmonix.DTA.Crypt'
renames['Data.DTA.PrettyPrint'          ] = 'Onyx.Harmonix.DTA.Print'
renames['Data.DTA.Serialize'            ] = 'Onyx.Harmonix.DTA.Serialize'
renames['Data.DTA.Serialize.Amplitude'  ] = 'Onyx.Harmonix.DTA.Serialize.Amplitude'
renames['Data.DTA.Serialize.GH1'        ] = 'Onyx.Harmonix.DTA.Serialize.GH1'
renames['Data.DTA.Serialize.GH2'        ] = 'Onyx.Harmonix.DTA.Serialize.GH2'
renames['Data.DTA.Serialize.Magma'      ] = 'Onyx.Harmonix.DTA.Serialize.Magma'
renames['Data.DTA.Serialize.RB3'        ] = 'Onyx.Harmonix.DTA.Serialize.RockBand'
renames['DDR.SM'                        ] = 'Onyx.DDR.SM'
renames['DTXMania.DTX'                  ] = 'Onyx.DTXMania.DTX'
renames['DTXMania.Set'                  ] = 'Onyx.DTXMania.Set'
renames['DTXMania.ShiftJIS'             ] = 'Onyx.Util.ShiftJIS'
renames['DTXMania.XA'                   ] = 'Onyx.DTXMania.XA'
renames['FeedBack.Base'                 ] = 'Onyx.FeedBack.Base'
renames['FeedBack.Load'                 ] = 'Onyx.FeedBack.Load'
renames['GuitarHeroI.File'              ] = 'Onyx.Harmonix.GH1.File'
renames['GuitarHeroII.Ark'              ] = 'Onyx.Harmonix.Ark.GH2'
renames['GuitarHeroII.Audio'            ] = 'Onyx.Audio.VGS'
renames['GuitarHeroII.BandBass'         ] = 'Onyx.Harmonix.GH2.BandBass'
renames['GuitarHeroII.BandDrums'        ] = 'Onyx.Harmonix.GH2.BandDrums'
renames['GuitarHeroII.BandKeys'         ] = 'Onyx.Harmonix.GH2.BandKeys'
renames['GuitarHeroII.BandSinger'       ] = 'Onyx.Harmonix.GH2.BandSinger'
renames['GuitarHeroII.Convert'          ] = 'Onyx.Build.GuitarHero2.Logic'
renames['GuitarHeroII.Events'           ] = 'Onyx.Harmonix.GH2.Events'
renames['GuitarHeroII.File'             ] = 'Onyx.Harmonix.GH2.File'
renames['GuitarHeroII.PartDrum'         ] = 'Onyx.Harmonix.GH2.PartDrum'
renames['GuitarHeroII.PartGuitar'       ] = 'Onyx.Harmonix.GH2.PartGuitar'
renames['GuitarHeroII.Triggers'         ] = 'Onyx.Harmonix.GH2.Triggers'
renames['GuitarPro.GPX'                 ] = 'Onyx.GuitarPro.GPX'
renames['Harmonix.Ark'                  ] = 'Onyx.Harmonix.Ark'
renames['Import.Amplitude2016'          ] = 'Onyx.Import.Amplitude2016'
renames['Import.Base'                   ] = 'Onyx.Import.Base'
renames['Import.BMS'                    ] = 'Onyx.Import.BMS'
renames['Import.DTXMania'               ] = 'Onyx.Import.DTXMania'
renames['Import.Freetar'                ] = 'Onyx.Import.Freetar'
renames['Import.FretsOnFire'            ] = 'Onyx.Import.FretsOnFire'
renames['Import.GuitarHero1'            ] = 'Onyx.Import.GuitarHero1'
renames['Import.GuitarHero2'            ] = 'Onyx.Import.GuitarHero2'
renames['Import.GuitarPro'              ] = 'Onyx.Import.GuitarPro'
renames['Import.Magma'                  ] = 'Onyx.Import.Magma'
renames['Import.Neversoft'              ] = 'Onyx.Import.Neversoft'
renames['Import.PowerGig'               ] = 'Onyx.Import.PowerGig'
renames['Import.RockBand'               ] = 'Onyx.Import.RockBand'
renames['Import.RockRevolution'         ] = 'Onyx.Import.RockRevolution'
renames['Import.Rocksmith'              ] = 'Onyx.Import.Rocksmith'
renames['Neversoft.Audio'               ] = 'Onyx.Neversoft.Crypt'
renames['Neversoft.Checksum'            ] = 'Onyx.Neversoft.CRC'
renames['Neversoft.Export'              ] = 'Onyx.Build.Neversoft'
renames['Neversoft.GH3'                 ] = 'Onyx.Neversoft.GH3'
renames['Neversoft.Metadata'            ] = 'Onyx.Neversoft.Metadata'
renames['Neversoft.Note'                ] = 'Onyx.Neversoft.Note'
renames['Neversoft.Pak'                 ] = 'Onyx.Neversoft.Pak'
renames['Neversoft.Perf'                ] = 'Onyx.Neversoft.Perf'
renames['Neversoft.PS2'                 ] = 'Onyx.Neversoft.PS2'
renames['Neversoft.QB'                  ] = 'Onyx.Neversoft.QB'
renames['PhaseShift.Dance'              ] = 'Onyx.PhaseShift.Dance'
renames['PhaseShift.Message'            ] = 'Onyx.PhaseShift.Message'
renames['PlayStation.ISO'               ] = 'Onyx.ISO'
renames['PlayStation.PKG'               ] = 'Onyx.PlayStation.PKG'
renames['PlayStation.PSS'               ] = 'Onyx.PlayStation.PSS'
renames['PlayStation.PKG.Backport'      ] = 'Onyx.Util.ByteStringBackport'
renames['PowerGig.Crypt'                ] = 'Onyx.PowerGig.Crypt'
renames['PowerGig.GEV'                  ] = 'Onyx.PowerGig.GEV'
renames['PowerGig.MIDI'                 ] = 'Onyx.PowerGig.MIDI'
renames['PowerGig.Songs'                ] = 'Onyx.PowerGig.Songs'
renames['Reaper.Base'                   ] = 'Onyx.Reaper.Base'
renames['Reaper.Build'                  ] = 'Onyx.Reaper.Build'
renames['Reaper.Extract'                ] = 'Onyx.Reaper.Extract'
renames['RockBand.Codec'                ] = 'Onyx.MIDI.Read'
renames['RockBand.Common'               ] = 'Onyx.MIDI.Common'
renames['RockBand.IOS'                  ] = 'Onyx.Harmonix.RockBand.IOS'
renames['RockBand.Milo'                 ] = 'Onyx.Harmonix.RockBand.Milo'
renames['RockBand.Save'                 ] = 'Onyx.Harmonix.RockBand.Save'
renames['RockBand.Score'                ] = 'Onyx.Harmonix.RockBand.Score'
renames['RockBand.Sections'             ] = 'Onyx.Sections'
renames['RockBand.SongCache'            ] = 'Onyx.Harmonix.RockBand.SongCache'
renames['RockBand.Codec.Beat'           ] = 'Onyx.MIDI.Track.Beat'
renames['RockBand.Codec.Drums'          ] = 'Onyx.MIDI.Track.Drums'
renames['RockBand.Codec.Events'         ] = 'Onyx.MIDI.Track.Events'
renames['RockBand.Codec.File'           ] = 'Onyx.MIDI.Track.File'
renames['RockBand.Codec.Five'           ] = 'Onyx.MIDI.Track.FiveFret'
renames['RockBand.Codec.FullDrums'      ] = 'Onyx.MIDI.Track.Drums.Full'
renames['RockBand.Codec.Lipsync'        ] = 'Onyx.MIDI.Track.Lipsync'
renames['RockBand.Codec.ProGuitar'      ] = 'Onyx.MIDI.Track.ProGuitar'
renames['RockBand.Codec.ProKeys'        ] = 'Onyx.MIDI.Track.ProKeys'
renames['RockBand.Codec.Six'            ] = 'Onyx.MIDI.Track.SixFret'
renames['RockBand.Codec.Venue'          ] = 'Onyx.MIDI.Track.Venue'
renames['RockBand.Codec.VenueGen'       ] = 'Onyx.MIDI.Track.VenueGen'
renames['RockBand.Codec.Vocal'          ] = 'Onyx.MIDI.Track.Vocal'
renames['RockBand.Legacy.Vocal'         ] = 'Onyx.MIDI.Track.Vocal.Legacy'
renames['RockBand.Milo.Compression'     ] = 'Onyx.Harmonix.RockBand.Milo.Compression'
renames['RockBand.Milo.Dir'             ] = 'Onyx.Harmonix.RockBand.Milo.Dir'
renames['RockBand.Milo.Lipsync'         ] = 'Onyx.Harmonix.RockBand.Milo.Lipsync'
renames['RockBand.Milo.SongPref'        ] = 'Onyx.Harmonix.RockBand.Milo.SongPref'
renames['RockBand.Milo.Venue'           ] = 'Onyx.Harmonix.RockBand.Milo.Venue'
renames['RockBand.ProGuitar.Play'       ] = 'Onyx.Controller.ProGuitar'
renames['RockBand.RB4.Image'            ] = 'Onyx.Image.DTX.RB4'
renames['RockBand.RB4.RBMid'            ] = 'Onyx.Harmonix.RockBand.RB4.RBMid'
renames['RockBand.RB4.SongDTA'          ] = 'Onyx.Harmonix.RockBand.RB4.SongDTA'
renames['Rocksmith.ArrangementXML'      ] = 'Onyx.Rocksmith.ArrangementXML'
renames['Rocksmith.BNK'                 ] = 'Onyx.Rocksmith.BNK'
renames['Rocksmith.Crypt'               ] = 'Onyx.Rocksmith.Crypt'
renames['Rocksmith.CST'                 ] = 'Onyx.Rocksmith.CST'
renames['Rocksmith.DLCBuilder'          ] = 'Onyx.Rocksmith.DLCBuilder'
renames['Rocksmith.MIDI'                ] = 'Onyx.MIDI.Track.Rocksmith'
renames['Rocksmith.PSARC'               ] = 'Onyx.Rocksmith.PSARC'
renames['Rocksmith.Sng2014'             ] = 'Onyx.Rocksmith.Sng2014'
renames['Sound.FSB'                     ] = 'Onyx.Audio.FSB'
renames['Sound.MIDI.File.FastParse'     ] = 'Onyx.MIDI.Parse'
renames['Sound.MIDI.Script.Base'        ] = 'Onyx.MIDI.Script.Base'
renames['Sound.MIDI.Script.Read'        ] = 'Onyx.MIDI.Script.Read'
renames['STFS.Package'                  ] = 'Onyx.Xbox.STFS'
renames['Text.Decode'                   ] = 'Onyx.Util.Text.Decode'
renames['Text.Transform'                ] = 'Onyx.Util.Text.Transform'
renames['Xbox.ISO'                      ] = 'Onyx.Xbox.ISO'
renames['Data.DTA.Lex'                  ] = 'Onyx.Harmonix.DTA.Scan'
renames['Data.DTA.Parse'                ] = 'Onyx.Harmonix.DTA.Parse'
renames['FeedBack.Parse'                ] = 'Onyx.FeedBack.Parse'
renames['FeedBack.Scan'                 ] = 'Onyx.FeedBack.Scan'
renames['Reaper.Parse'                  ] = 'Onyx.Reaper.Parse'
renames['Reaper.Scan'                   ] = 'Onyx.Reaper.Scan'
renames['Sound.MIDI.Script.Parse'       ] = 'Onyx.MIDI.Script.Parse'
renames['Sound.MIDI.Script.Scan'        ] = 'Onyx.MIDI.Script.Scan'
renames['Data.SimpleHandle'             ] = 'Onyx.Util.Handle'
renames['NPData'                        ] = 'Onyx.PlayStation.NPData'
renames['FFMPEG'                        ] = 'Onyx.FFMPEG'
renames['ArkTool'                       ] = 'Onyx.Harmonix.Ark.ArkTool'
renames['Sound.GamecubeDSP'             ] = 'Onyx.Audio.GamecubeDSP'
renames['GUI.FLTK'                      ] = 'Onyx.GUI'
renames['GUI.FLTK.Util'                 ] = 'Onyx.GUI.Util'
renames['RhythmGame.Graphics.Config'    ] = 'Onyx.Game.Graphics.Config'
renames['RhythmGame.Graphics.Video'     ] = 'Onyx.Game.Graphics.Video'
renames['RhythmGame.Audio'              ] = 'Onyx.Game.Audio'
renames['RhythmGame.Graphics'           ] = 'Onyx.Game.Graphics'
renames['RhythmGame.PNF'                ] = 'Onyx.Game.Time'
renames['RhythmGame.Track'              ] = 'Onyx.Game.Track'
renames['Sound.WW2Ogg'                  ] = 'Onyx.Audio.WWise'
renames['Kakasi'                        ] = 'Onyx.Kakasi'

ARGV.each do |hs|
  text = File.read(hs)
  module_name = nil
  new_module_name = nil
  # rewrite imports and module name according to the mapping
  renames.each_pair do |k, v|
    text.gsub!(/\b(import\s+(qualified\s+)?)#{Regexp::escape(k)}(\s)/) do |m|
      $1 + v + $3
    end
    if /\b#{Regexp::escape(k.split('.').join('/'))}\./.match?(hs)
      module_name = k
      new_module_name = v
      text.gsub!(/\b(module\s+)#{Regexp::escape(k)}/) do |m|
        $1 + v
      end
    end
  end
  File.open(hs, 'w') do |h|
    h.write(text)
  end
  # move the file to its new location/name
  if module_name
    new_path = hs.gsub(/#{Regexp::escape(module_name.split('.').join('/'))}(\.\w+)/) do |m|
      new_module_name.split('.').join('/') + $1
    end
    FileUtils.mkdir_p(File.dirname(new_path))
    FileUtils.mv(hs, new_path)
  end
  # does not handle editing .cabal files
end
