<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>beforeRunningCommand</key>
	<string>nop</string>
	<key>command</key>
	<string>#!/usr/bin/env ruby -wKU

require ENV['TM_SUPPORT_PATH'] + '/lib/osx/plist'
require ENV['TM_SUPPORT_PATH'] + '/lib/ui'

abort 'No domain defined – set TM_OAK_DEBUG_DOMAIN' unless Domain = ENV['TM_OAK_DEBUG_DOMAIN']

def edit_defaults(path)
  defaults = OSX::PropertyList.load(File.open(path))
  yield defaults
  OSX::PropertyList.dump(File.open(path, 'w+'), defaults)
end

def toggle_flag(path, flag)
  edit_defaults(path) do |defaults|
    defaults['OakDebug Enabled'] ||= []
    if defaults['OakDebug Enabled'].include?(flag)
      defaults['OakDebug Enabled'] -= [flag]
      puts "Disabled #{flag}"
    else
      defaults['OakDebug Enabled'] &lt;&lt; flag
      puts "Enabled #{flag}"
    end
    defaults['OakDebug Enabled'].uniq!
  end
end

flags = STDIN.read.scan(/OAK_DEBUG_VAR\((.+?)\)/).flatten
flag = if flags.size &gt; 1
  abort unless choice = TextMate::UI.menu(flags)
  flags[choice]
else
  flags[0]
end

path = File.join(File.expand_path('~/Library/Preferences'), Domain + '.plist')
abort "Domain #{Domain} not found in defaults" unless File.exist?(path)
toggle_flag(path, flag)
</string>
	<key>fallbackInput</key>
	<string>line</string>
	<key>input</key>
	<string>document</string>
	<key>name</key>
	<string>Toggle Flag</string>
	<key>output</key>
	<string>showAsTooltip</string>
	<key>uuid</key>
	<string>DD812257-31E9-42A0-80C4-230580660010</string>
</dict>
</plist>
