#
# Copyright (c) 2010 by RightScale Inc., all rights reserved
#
# Write given user data to files in /var/spool/ec2 in text, shell and ruby 
# script formats

module RightScale

  module MetadataWriters

    # Ruby script writer
    class RubyMetadataWriter < MetadataWriter

      attr_accessor :generation_command

      # Initializer.
      #
      # === Parameters
      # options[:file_extension](String):: dotted extension for ruby files or nil
      def initialize(options)
        # defaults
        options = options.dup
        options[:file_extension] ||= '.rb'

        # super
        super(options)

        @formatter = FlatMetadataFormatter.new(options)

        # local options.
        @generation_command = options[:generation_command]
      end

      protected

      RUBY_HEADER = ['# encoding: utf-8',
                     '# Warning: this file has been auto-generated',
                     '# any modifications can be overwritten']

      # Write given metadata to a ruby file.
      #
      # === Parameters
      # file_name_prefix(String):: name prefix for generated file
      # metadata(Hash):: Structured Hash-like metadata to write
      #
      # === Return
      # always true
      def write_file(metadata)
        return unless @formatter.can_format?(metadata)
        flat_metadata = @formatter.format(metadata)

        # write the cached file variant if the code-generation command line was passed.
        env_file_name = @generation_command ? "#{@file_name_prefix}-cache" : @file_name_prefix
        env_file_path = create_full_path(env_file_name)
        File.open(env_file_path, "w", DEFAULT_FILE_MODE) do |f|
          f.puts RUBY_HEADER
          flat_metadata.each do |k, v|
            # escape backslashes and single quotes.
            v = self.class.escape_single_quotes(v)
            f.puts "ENV['#{k}']='#{v}'"
          end
        end
 
        # write the generation command, if given.
        if @generation_command
          File.open(create_full_path(@file_name_prefix), "w", DEFAULT_FILE_MODE) do |f|
            f.puts RUBY_HEADER
            f.puts "raise 'ERROR: unable to fetch metadata' unless system(\"#{@generation_command}\")"
            f.puts "require '#{env_file_path}'"
          end
        end
        true
      end

    end  # RubyMetadataWriter

  end  # MetadataWriters

end  # RightScale
