#!/usr/bin/env ruby
#
#scvim [the executable]
#
#Copyright 2008 Alex Norman
#
#This file is part of SCVIM.
#
#SCVIM is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#SCVIM is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with SCVIM.  If not, see <http://www.gnu.org/licenses/>.

require 'optparse'

vim = "vim"

#test for MacVim if we're on a darwin/osX box
if RUBY_PLATFORM =~ /darwin/
  if system('which mvim')
    vim = 'mvim'
  elsif File.exists?('/Applications/MacVim.app/Contents/MacOS/Vim')
    vim = '/Applications/MacVim.app/Contents/MacOS/Vim'
  end
end

rcfilelocs = [
  File.join(ENV["HOME"], ".scvimrc"),
  "/usr/local/share/scvim/scvimrc",
  "/usr/share/scvim/scvimrc"
]
addonlocs = [
  File.join(ENV["HOME"], ".vim/"),
  "/usr/local/share/vim/addons/",
  "/usr/share/vim/addons/",
]

#do we use graphical mode
graphical = false
#the location of the rc file to source
rcloc = nil

#the options that we pass to vim
vimops = []

# debian-based systems do not activate vim plugins automatically, we must check
# test to see if we have vim-addons and if vim-addons knows about scvim
if %x[which vim-addons] != "" and %x[vim-addons -q list | grep supercollider] != ""
  if %x[vim-addons -q status supercollider | grep installed] == ""
    puts "scvim has detected that you haven't activated the vim supercollider plugin."
    puts "to enable scvim to work, please execute either "
    puts "    vim-addons install supercollider"
    puts "to enable it for the current user, or "
    puts "    sudo vim-addons -w install supercollider"
    puts "to enable it system-wide."
    puts ""
    puts "Would you like me to run \"vim-addons install supercollider\" for you?"
    puts "Press 'y' and then Enter if so.  Otherwise just press Enter to exit."
    userreply = gets.chomp
    if userreply == 'y'
      %x[vim-addons install supercollider]
    else
      exit
    end
  end
else
  #ensure that the supercollider ftplugin is in the runtime path
  addonlocs.each do |loc|
    if File.exists?(File.join(loc, "ftplugin/supercollider.vim"))
      vimops += ["-c", "set runtimepath+=#{loc}"]
      break
    end
  end
end

opts = OptionParser.new do |opts|
  #the usage banner
  opts.banner = "Usage: #{$0} [OPTION]... [FILE]..."

  #the options
  opts.on("-g", "--graphical", "Use vim -g") do
    graphical = true
  end
  opts.on("-r", "--rcfile rcfile", "Use the file indicated as the scvim configuration file") do |f|
    if File.exists?(f)
      rcloc = f
    else
      STDERR.puts "rcfile #{f} does not exists, aborting"; exit
    end
  end
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts; exit
  end
end

#parse the command line options
begin
  opts.parse!(ARGV)
rescue => e
  STDERR.puts e.to_s
  STDERR.puts opts.to_s
  exit
end

unless rcloc
  #find the rc file
  rcfilelocs.each do |loc|
    if File.exists?(loc)
      rcloc = loc
      break
    end
  end
  #if no rc file found, no user customizations
  #hopefully ftplugin/supercollider.vim is in the vimruntime
  STDERR.puts "scvimrc file not found" unless rcloc
end

if rcloc
  vimops += ["--cmd", "source #{rcloc}"] 
  puts "scvim: sourcing #{rcloc}"
end

vimops += ["-c",
  "set filetype=supercollider | runtime ftplugin/supercollider.vim | SClangStart", 
  *ARGV
]

#add -g if we're going graphical
vimops = ["-g"] + vimops if graphical

#see if the vim install supports servers
if IO.popen("#{vim} --version") {|f| f.readlines.flatten.join(" ").include?("+clientserver")}
	vimops = ["--servername", "scvim"] + vimops
end

#actually execute
exec(vim, *vimops)
