#! /usr/bin/ruby -Ks
$KCODE = "SJIS"
################################################################################
# 󕶌ɂJIS X0208`̃eLXgAۂŕ\Ȋ𒊏oXNvg
# 
#
# ̓eLXg̑Ó@ShiftJIS sR[hCRLF (CR,LF݂̂łv)
#
# Useage
#    UnifDripper.rb ̓eLXgt@C
#    
#
#
#
require "rbconfig"
$os = "Mac" if (/darwin/ =~ Config::CONFIG['build_os'])
$os = "Win" if (/mswin32/ =~ Config::CONFIG['build_os'])
require "jcode"
require 'iconv' if ($os == "Mac")
require 'Win32API' if ($os == "Win")
################################################################################
myRequirePath = ""
thisAppPath = ""
#ConfigFileName = '43drop.conf'
DicFileName = 'jitai.csv'
if (/\.app/ =~ ARGV[0])
	thisAppPath = ARGV.shift
	thisAppPath += '/'
	$standalone = true
#	$config_path = thisAppPath.sub(/\/[^\/]*?\.app/, '') + "#{ConfigFileName}"
#	$dictionaly_path = thisAppPath.sub(/\/[^\/]*?\.app/, '') + "#{DicFileName}"
else
	$standalone = false
	if ($Exerb)
#		$config_path = ExerbRuntime.filepath.sub(ExerbRuntime.filename, '') + "./#{ConfigFileName}"
#		$dictionaly_path = ExerbRuntime.filepath.sub(ExerbRuntime.filename, '') + "./#{DicFileName}"
	else
#		$config_path = "./#{ConfigFileName}"
#		$dictionaly_path = "./#{DicFileName}"
	end # if exerb
end # if #standalone
myRequirePath = thisAppPath + "Contents/Resources/" if thisAppPath != ""
require "#{myRequirePath}./sconv"
require "#{myRequirePath}./unification.rb"
# 萔`

# O[oϐ`
UniScan=Regexp.new("[" + UniGroupA + "]")
UniA = UniTable.invert

$encoding = "utf"	# or sjis
################################################################################
# NX`
class Queue < Array
	def initialize(size = 5)
		@size = size
	end #def new

	def push(data)
		super
		self.shift if (self.size > @size)
	end # def
end # class Queue
################################################################################
# Tu[``
def noFileError
  message_box = Win32API.new('user32', 'MessageBoxA', 'LPPI', 'I')
  message_box.call(0, 'ϊt@ChbOhbvĂ', 'UnifDripper', 0)
end

def endProcess
  message_box = Win32API.new('user32', 'MessageBoxA', 'LPPI', 'I')
  message_box.call(0, 'ϊI܂', 'UnifDripper', 0)
end

def appendsuffix(base, ext)
	path = Iconv.conv('SJIS', 'UTF-8-MAC', infile)
end # def appendsuffix
################################################################################
# C[``
if ((ARGV.size == 0) && ($os == "Win"))
	noFileError
	exit
end

ARGV.each do |arg|
	infile = arg.dup
	repfile = infile.dup
	repfile = Iconv.conv('SJIS', 'UTF-8-MAC', repfile) if ($os == 'Mac')
	repfile.sub!(/(\.[Tt][Ee]?[Xx][Tt])$/, '_unif\1')
	repfile = Iconv.conv('UTF-8-Mac', 'SJIS', repfile) if ($os == 'Mac')
	open(repfile, "w") do |rep|
	open(infile) do |inf|
		if ($os == "Mac")
			rep.printf("\xef\xbb\xbf#%s\x0d\x0a", File.expand_path(infile))	# unif file is UTF-8 (BOM)
		else
			rep.printf("\xef\xbb\xbf#%s\x0d\x0a", File.expand_path(infile).sj2utf8)	# unif file is UTF-8 (BOM)
		end #if

		lineno = 0
		while inf.gets
			sub!(/^\xef\xbb\xbf/, '')	# remove Byte Order Mark
			chop
			lineno += 1
			if (~UniScan)
				char = 0
				offset = 0
				sample = Queue.new(5)
				$_.each_char do |ch|
					char += 1
					offset += ch.size
					if  (ch =~ UniScan)
						post = ($_[offset, $_.size] || "").match(/.{0,5}/).to_s
						buf = sprintf("%s,%s%s,%04d,%04d\t%s%s%s\t%s[%s%s]%s\x0d\x0a", ch, ch, UniA[ch], lineno, char, sample.join, ch, post, sample.join, ch, UniA[ch], post)
						rep.write buf.sj2utf8
					end # if UniScan
					sample.push(ch)
				end # each $_
			end # if UniScan
		end # while in
	end # open infile
	end # open report
end # each ARGV