# # Copyright © 2013 # Nathan Currier # # Use, modification, and distribution are all subject to the # Boost Software License, Version 1.0. (See the accompanying # file LICENSE.md or at rideliner.tk/LICENSE.html). # # <description> #

module Bakery

module Ingredient
  module Cpp
    class File < Bakery::Ingredient::File
      def toBlockCommentLine line
        " *#{super line}"
      end

      def initialize filename, *args, &block
        super filename, *args do
          unless args.include? :no_copyright
            puts '/*!'
            insertCopyrightNotice
            puts '**/'
            puts ''
          end

          dispatch &block
        end
      end
    end

    class SourceFile < File
      def initialize filename, *args, &block
        super filename, *args do
          if args.include? :main
            if args.include? :vita
              puts '#include <vita.hpp>'
              puts ''
              puts 'int main(Ortus& ortus, Orcus& orcus)'
            else
              puts 'int main(int argc, char** argv, char** env)'
            end

            puts '{'
            puts '    return 0;'
            puts '}'
            puts ''
          end

          dispatch &block
        end
      end
    end

    class HeaderFile < File
      def initialize filename, *args, &block
        super filename, *args do
          guard = Bakery.getLatestMarkerOfType Bakery::Ingredient::Cpp::HeaderMarker

          guard_name = "#{guard ? guard.getProject : ''}_#{filename.gsub(/\./, '_')}".split(/[\/\\]/).join('_').upcase

          puts "#ifndef #{guard_name}"
          puts "#define #{guard_name}"
          puts ''
          puts "#endif // #{guard_name}"
          puts ''

          dispatch &block
        end
      end
    end

    class TestFile < File
      def initialize filename, *args, &block
        super filename, *args do
          puts '#include <gtest/gtest.h>'
          puts ''

          if args.include? :main
            if args.include? :vita
              puts '#include <vita.hpp>'
              puts ''
              puts 'int main(Ortus& ortus, Orcus& orcus)'
              puts '{'
              puts '    testing::InitGoogleTest(ortus);'
            else
              puts 'int main(int argc, char** argv)'
              puts '{'
              puts '    testing::InitGoogleTest(&argc, argv);'
            end

            puts ''
            puts '    return RUN_ALL_TESTS();'
            puts '}'
            puts ''
          end

          dispatch &block
        end
      end
    end

    class HeaderMarker < Bakery::Ingredient::Marker
      def initialize *args, &block
        super *args, &block
      end

      def getProject
        Dir.pwd.gsub /^#{Regexp.quote @wd}[\/\\]?/, ''
      end
    end
  end
end

end