#!/usr/bin/env ruby
# encoding: ASCII-8BIT

require "../yenc.rb"
require 'getoptlong'
require 'fileutils'

def usage
	puts <<EOT
Usage: #{$0.sub(/.*\//, "")} [options] <file>

-h, --help			show this message
-o <outfile>			output file
EOT
	exit
end

def cmdline
	options = Hash.new
	begin
		opts = GetoptLong.new(
			[ "-h", "--help",		GetoptLong::NO_ARGUMENT ],
			[ "-o",				GetoptLong::REQUIRED_ARGUMENT ]
		)
		opts.quiet=true

		opts.each do |opt, arg|
			options[opt] = arg
		end

	rescue
		print "#{$!}\n"
		usage
	end
	if options["-h"]
		usage
	end
	return options
end

options = cmdline
file = ARGV[0]

filehandle = File.open(file, "r")
tmpfile = Tempfile.new("ynctmp")
tmpfile.sync=true
mode, filename, body = YEnc.ydecode(filehandle, tmpfile)
puts "Mode: #{mode}"
puts "Filename: #{filename}"
if !options["-o"].nil?
	FileUtils.mv(tmpfile, options["-o"])
end
filehandle.close
tmpfile.close

