#!/usr/bin/env ruby
require 'date'
# $KCODE = "e"

module Rdfilter

  # rd ファイル中に 「((#{ }))」 を埋め込む
  def ruby_to_rd(indata)
    outdata =""
    indata.each{ |i|
      if i =~ /\(\(#\{.*\}\)\)/
	i.split(/\(\(#\{/).each { |ii|
	  if ii =~ /\}\)\)/
	    iii = ii.split(/\}\)\)/)[0]
	    iiii = `ruby  -e 'print #{iii}'`
	    outdata << iiii
	    outdata << ii.sub(/#{iii}\}\)\)/,"")
	  else 
	  outdata << ii
	  end
	}
      else 
	outdata << i 
      end
    }
    return outdata
  end
  
  
  # rd ファイルから, tex 記述部を抽出, 削除
  def tex_to_rd(indata)
    
    outdata =""
    texsrc = ""
    $texfile = ""
    count = 0
    
    indata.each{ |i|
      if count == 1
	if  i =~ /^=endtex/
	  count = 0
	  tex2png($texfile,texsrc)
	  outdata << "\n=end\n"
	    outdata << "latex2png outputfile \n"
	    outdata << "\(\(\:\<img src=\"tex/#{$texfile}.png\"\>\:\)\)\n"
	    outdata << "=begin\n\n"
	    texsrc = ""
	else
	  texsrc << i 
	end
      else 
	if  i =~ /^=begintex/
	  $texfile = i.split(" ")[1]
	  count = 1 
	else 
	  outdata << i 
	end
      end
    }
    return outdata
  end
  
  
  # rd から抽出した tex 部を latex2png で png に変換
  def tex2png(texfile,texsrc)

    unless File.exist?(Dir.pwd + "/tex/") then
      Dir.mkdir(Dir.pwd + "/tex/",0775) 
    end
    cdir = Dir.pwd
    Dir.chdir(Dir.pwd + "/tex")
    
    
    #  File.delete(texfile + ".tex") if File.exist?(texfile + ".tex")
    
    if File.exist?(texfile + ".png")
#      print "  #{texfile}.png already exist. \n" 
    else
#      print "  #{texfile}.png not found. now creating...\n" 
      tex = "\\documentclass[a4j,12pt]{article}\n"
      tex << "\\pagestyle{empty}\n"
      tex << "\\begin{document}\n"
      tex << texsrc
      tex << "\\end{document}\n" 
      tfile = open(texfile  + ".tex","w") 
      tfile.print tex
      tfile.close

      if File.exist?("/usr/bin/latex2png")
	`latex2png #{texfile}.tex > /dev/null 2> /dev/null`
      elsif File.exist?("/home/daktu32/bin/latex2png")
	`/home/daktu32/bin/latex2png #{texfile}.tex > /dev/null 2> /dev/null`
      else 
	print "latex2png not found. pngfile not creating."
      end
      #  File.delete(texfile + ".tex") if File.exist?(texfile + ".tex")  
    end
    
    Dir.chdir(cdir)
    
  end

  def  rdfilter_make(infile, outfile)
    
    # ファイルからデータ抽出
    rfile = open(infile,"r")
    indata = []
    rfile.each{ |line|
      indata.push(line)
    }
    rfile.close
    
#    print "rd_filter-d.rd start\n"
    
    # tex, ruby フィルター
    outdata1 = ruby_to_rd(indata)
    outdata  = tex_to_rd(outdata1)
    
    # 出力
    File.delete(outfile) if File.exist?(outfile)
    wfile = open(outfile,"w") 
    wfile.print outdata
    wfile.close
    
#    print "rd_filter-d.rd end\n"
    
  end
  
end
  
  
######################################################
if $0 == __FILE__

  include Rdfilter
  # 入力, 出力ファイル解析
  opt = ARGV
  infile = Dir.pwd + "/" + ARGV[0]
  if ARGV[1] == nil
    outfile = Dir.pwd + "/" + "rdout.rd" 
  else
    outfile = Dir.pwd + "/" + ARGV[1] 
  end

  rdfilter_make(infile, outfile)

end




