#!/usr/pkg/bin/ruby193

require 'maruku'

if File.basename($0) =~ /^marutex/
	# Convert each file 
	ARGV.each do |f|
		puts "Opening #{f}"
	
		# read file content
		input =  File.open(f,'r').read
		
		# create Maruku
		doc = Maruku.new(input)
		# convert to a complete html document
		latex = doc.to_latex_document
	
		# write to file
		dir = File.dirname(f)
		job = File.join(dir, File.basename(f, File.extname(f)))
		filename = job + ".tex"

		File.open(filename,'w') do |f| f.puts latex end

		# run twice for cross references
		system "pdflatex '#{job}'  '-output-directory=#{dir}' "
		system "pdflatex '#{job}' '-output-directory=#{dir}' "
		
#		system "open #{job}.pdf"
	end
end