#!/usr/bin/env ruby
#
# Copyright 2006, Michael Conrad Tadpol Tilstra <tadpol@tadpol.org>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    

# Bits and pieces based on things found at: http://pascal-central.com/gpc2.html 

require 'open3'
require 'pathname'

def get_path_from_bbedit
    cmd=%{tell application "BBEdit"
        set f to the file of document 1
        set p to the POSIX path of f
    end tell}
    ret=''
    Open3.popen3('osascript') {|sin, out, err| sin.puts cmd; sin.close; ret = out.readlines.join}
    Pathname.new(ret.chomp!)
end

# returns what command to use, and the default target.
def build_command
    return ['make', 'all'] if((Pathname.pwd + 'Makefile').exist?)
    return ['rake', 'default'] if((Pathname.pwd + 'Rakefile').exist?)
end

def target_map(path)
    path.basename(path.extname).to_s +
    case path.extname
        when '.c'
            '.o'
        when '.m'
            '.o'
        else
            path.extname
    end
end

fork do # Kludge. BBEdit won't respond to osa requests while running a shell script. ishy.
    begin
        cmd='false'
        case $0
        when /makeall/
            Dir.chdir(get_path_from_bbedit.dirname)
            cmd, target = build_command
        when /makeone/
            p = get_path_from_bbedit
            Dir.chdir(p.dirname)
            cmd, target = build_command
            target = target_map(p)
        else
            raise "Unknown run mode."
        end
        cmd = "#{cmd} #{target}"

        entries = Array.new
        Open3.popen3(cmd) do |sin, out, err|
            lineno = 0
            err.readlines.each do |line|
                raise line if line =~ /No targets specified and no makefile found/
                next unless line.chomp.match %r{^(/Users/[^:]*):(?:(\d+):)? (.*)}
                file = $1
                lineno = $2 unless $2.nil?
                msg = $3
                case msg
                when /^warning/
                    kind = 'warning_kind'
                when /^note/
                    kind = 'note_kind'
                when %{error: (Each undeclared identifier is reported only once}
                    next
                when %{error: for each function it appears in.)}
                    next
                when /^error/
                    kind = 'error_kind'
                else
                    kind = 'note_kind'
                end
                msg.gsub!(/"/,"'")
                entries.push %{{result_kind:#{kind}, result_file:alias (posix file "#{file}"), result_line:#{lineno}, message:"#{msg}"}}
            end
        end
    
        if entries.empty?
            osacmd = %{tell application "BBEdit"
                    try
                        close (first window whose name is "Compile Results for #{target}")
                    on error
                    end try
                    display alert "Compile finished" message "Compiled #{target}" as informational
                end tell}
        else
            osacmd =%{tell application "BBEdit"
                    try
                        close (first window whose name is "Compile Results for #{target}")
                    on error
                    end try
                    make new results browser with properties {name:"Compile Results for #{target}"} with data {#{entries.join(',')}}
                end tell}
        end
        Open3.popen3('osascript') {|sin, out, err| sin.puts osacmd}
    rescue => e
        a,f,n = e.backtrace[0].match(%r{([^:]+):(\d+)}).to_a
        msg = e.to_s.gsub(/"/,"'")
        data=%{{result_kind:error_kind, result_file:alias (posix file "#{f}"), result_line:#{n}, message:"#{msg}"}}
        osacmd=%{tell application "BBEdit"
                make new results browser with properties {name:"Script Failed"} with data {#{data}}
            end tell}
        Open3.popen3('osascript') {|sin, out, err| sin.puts osacmd}
    end
end
