=begin
= Kenny Translator in Ruby

by anton.bangratz@gmail.com
http://tony.twincode.net/dl/kenny.rb

Idea by SyneRyder (Kohan Ikin)
http://www.namesuppressed.com/kenny/


= MIT LICENSE

Copyright (c) 2008 Anton Bangratz

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


=end 

class SubLetter
  attr_accessor :value
  def initialize(value='m')
    @value = value
  end
  def succ
    case @value
    when 'm'
      self.class.new('p')
    when 'p'
      self.class.new('f')
    when 'f'
      nil
    end
  end
  def to_s
    @value
  end
end

class KennySpeech
  def initialize
    chars = 'a'..'z'
    @kennymap = {}
    subs = [SubLetter.new, SubLetter.new, SubLetter.new]
    chars.each {|c|
      tmp = subs.join
      @kennymap[c] = tmp
      subs[2] = subs[2].succ
      if subs[2].nil?
        subs[2] = SubLetter.new
        subs[1] = subs[1].succ
        if subs[1].nil?
          subs[1] = SubLetter.new
          subs[0] = subs[0].succ
          if subs[0].nil?
            raise 'Moo'
          end
        end
      end
    }
    @kennymap.each {|k,v|
      @kennymap[k.upcase] = v[0,1].upcase + v[1,2]
    }
    @kennyunmap = @kennymap.invert
  end
  def [](c)
    if c =~ /\w/
      @kennymap[c]
    else
      c
    end
  end
  def unmap(c)
    if c =~ /[mpf]{3}/i
      @kennyunmap[c]
    else
      c
    end
  end
  def kenny(string)
    string.split(//).map{|c| self[c]}.join
  end
  def unkenny(string)
    string.scan(/[mpf]{3}|\W/i).map{|c| unmap(c)}.join
  end
end

if __FILE__ == $0
  if ARGV.empty?
    puts <<-EOF

    Usage: #{$0} (-k|-u) TEXT

    Parameters: 

      -k force kennying
      -u force unkennying


    2008 by anton.bangratz@gmail.com

    EOF
    exit 0
  end
  k = KennySpeech.new
  if ARGV[0] == '-k'
    puts k.kenny(ARGV[1..-1].join(' '))
  elsif ARGV[0] == '-u'
    puts k.unkenny(ARGV[1..-1].join(' '))
  else
    text = ARGV[0..-1].join(' ')
    if text.scan(/(mmm|mpp|ppf|mff|fmf)/i).flatten.size > 0
      puts k.unkenny(text)
    else
      puts k.kenny(text)
    end
  end
end

