#!/usr/bin/env ruby
require 'rubygems'
require 'camping'
## Setup
Camping.goes :OrgBugs
ORG_FILE = File.join(File.expand_path(File.dirname(__FILE__)), "bugs.org")
DAYS_OF_THE_WEEK = %w[Sun Mon Tue Wed Thu Fri Sat] 

## Views
module OrgBugs::Views
  def index
    html do
      head do
        title "Org-Mode Bug Reports and Feature Requests"
        style "label {font-weight: bold;}"
      end
      body do
        h2 "Org-Mode Bug Reports and Feature Requests"
        form(:action => "submit", :method => "post", :enctype => "multipart/form-data") do
          p do
            label "Title (required) "
            input({:name => "title", :id => "title", :type => 'text_field', :size => 52})
            br
            label "Name (required)"
            input({:name => "name", :id => "name", :type => 'text_field', :size => 52})
            br
            label "Email (required)"
            input({:name => "email", :id => "email", :type => 'text_field', :size => 52})
            br
            label "Type (required) "
            tag! :select, :name => "report_type", :id => "report_type" do
              tag! :option, "bug"
              tag! :option, "feature request"
              tag! :option, "other"
            end
            br
            p do
              "Please see the <a href=\"http://orgmode.org/org.html#Feedback\">Feedback</a> "+
                "section of the org-mode manual for information on </br> how to submit a useful bug report. "+
                "Don't forget to mention..."
            end
            ol do
              li {"What exactly did you do? "}
              li {"What did you expect to happen? "}
              li {"What happened instead? "}
            end
            label "Description (required)"
            br
            textarea({:name => 'desc', :id => 'desc', :cols => 80, :rows => 16})
            br
            input(:type => "submit", :value => "Submit")
          end
        end
      end
    end
  end
  def thanks
    html do
      head do
        title "Thanks for the Report!"
        style "label {font-weight: bold;}"
      end
      body do
        h1 "Report Submitted"
        p {"Thanks <i>#{@input.name}</i> your report <i>#{@input.title}</i> "+
          "has been submitted, and will be reviewed."}
      end
    end
  end
end

## Controllers
module OrgBugs::Controllers
  class Index < R '/'
    def get; render :index end
  end
  class Submit < R '/submit'
    def post
      report_to_org(@input)
      render :thanks
    end
  end
end

def write_prop(prop, value) "  :#{prop}: #{value}\n" end

def report_to_org(input)
  File.open(ORG_FILE, "a") do |f|
    f << "** SUBMITTED #{input.title}\n"
    f << "  :PROPERTIES:\n"
    f << write_prop(:submitter, input.name)
    f << write_prop(:submitter_email, input.email)
    f << write_prop(:date, DateTime.now.strftime("<%Y-%m-%d -%w- %H:%M>").
                    sub(/-\d-/, DAYS_OF_THE_WEEK[Integer($1)]))
    f << write_prop(:type, input.report_type)
    f << write_prop(:assigned_to, "no-one")
    f << "  :END:\n"
    f << "\n"
    f << input.desc.split(/[\n\r]/).map{|line| ": #{line.chomp}\n" if line.size > 0}.compact.join("")
    f << "\n"
  end
end
