#!/usr/bin/ruby
# == Synopsis
# 
# Extract an animation channel from a Flame setup
#
# == Usage
# 
#   bake_flame_channel --channel Timing/Timing -e 123 /usr/discreet/projects/Luxury/timewarp/shot2_tw.timewarp > /mnt/3d/curves/shot2_tw.txt
#   
# == Author
#   Julik <me@julik.nl>

require File.expand_path(File.dirname(__FILE__)) + '/../lib/flame_channel_parser'
require 'optparse'
require 'rubygems'
require "update_hints"

options = {:destination => $stdout}

op = OptionParser.new
op.banner = "Usage: bake_flame_channel --channel Timing/Timing -e 123 /usr/discreet/projects/Luxury/timewarp/shot2_tw.timewarp > /mnt/3d/curves/shot2_tw.txt\n" +
  "The output file can be used as Time+Value ASCII input for Nuke " + 
  "or parsed with any simple script"
op.on(" -c", "--channel CHANNEL_NAME", String,
  "Select the channel to bake (for example in Timewarp setups the useful one is Timing/Timing)."
) {|chan| options[:channel] = chan }
op.on(" -s", "--startframe FRAME", Integer,
  "Bake the curve from this specific frame onwards (defaults to the first keyframe in the setup)"
) {|from| options[:start_frame] = from }
op.on(" -e", "--endframe FRAME", Integer,
  "Bake the curve upto this specific frame (defaults to the last keyframe in the setup)"
) {|upto| options[:end_frame] = upto }
op.on(" -k", "--keyframed-range-only",
  "Bake the curve from the first keyframe to the last only (overrides --startframe and --endframe)"
) { options[:on_curve_limits] = true }

op.on(" -f", "--to-file FILENAME",  String, 
  "Write the curve to a file at this path instead of printing it to STDOUT"
) {|path| options[:destination] = File.open(path, "wb") }

op.parse!
setup_path = ARGV.shift

fail "No input file path provided. Please see bake_flame_channel --help for usage information." unless setup_path
fail "File does not exist." unless File.exist?(setup_path)

FlameChannelParser::Extractor.extract(setup_path, options)
options[:destination].close if options[:destination].respond_to?(:close)

UpdateHints.version_check("flame_channel_parser", FlameChannelParser::VERSION, $stderr)