#!/usr/bin/env ruby

require 'erb'

# Helper class to encapsulate context and include logic
class TemplateContext
    def initialize(mode)
        @mode = mode
    end

    def test_mode
        @mode == 't'
    end

    def figure_mode
        @mode == 'f'
    end

    # Include another ERB file and render it in the same context
    def include_partial(filename, extra_demo_code = nil)
        content = File.read(filename)
        ERB.new(content, trim_mode: '-').result(binding)
    end

    # Expose binding for ERB evaluation
    def get_binding
        binding
    end
end

# --- Main Program ---

# Parse arguments
mode = ARGV[0]
input_file = ARGV[1]

unless ['d', 'f', 't'].include?(mode)  && input_file
    puts "Usage: ruby render.rb (d|f|t) [input_file]"
    puts "Modes:"
    puts "  d: generated script demonstrates interactive use"
    puts "  f: generated script will write figure file"
    puts "  t: generated script will perform persistence test"
    exit 1
end

# Prepare context
context = TemplateContext.new(mode)

# Read and render the main template
template_content = File.read(input_file)
template = ERB.new(template_content, trim_mode: '-')
puts template.result(context.get_binding)
