Created
April 14, 2009 15:02
-
-
Save slaskis/95228 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
#=> [email protected] | |
require 'rubygems' | |
require 'fileutils' | |
require 'optparse' | |
default_browser = 'firefox' | |
OPTIONS = {} | |
OPTIONS[:default_plugin] = 'flash' | |
OPTIONS[:default_user] = `whoami`.chop! | |
plugin_path = "/Library/Internet Plug-Ins/" | |
rosetta = false | |
def players | |
"/Users/" + OPTIONS[:user] + "/.plugout/" | |
end | |
#sniff chip target_pluginitecture | |
def sniff_architecture | |
if [0x12345678].pack("L") == "\x12\x34\x56\x78" | |
return :big_endian | |
else | |
return :little_endian | |
end | |
end | |
def get_plugin_list(players) | |
list = " | |
#{full_plugin_name} versions available | |
PPC:" | |
ppcfiles = Dir.glob("#{players}#{full_plugin_name}/ppc/*") | |
ppcfiles.each do |f| | |
list += "\n #{f.split('/').last}" | |
end | |
list += " | |
UB:" | |
xfiles = Dir.glob("#{players}#{full_plugin_name}/ub/*") | |
xfiles.each do |f| | |
list += "\n #{f.split('/').last}" | |
end | |
list | |
end | |
def flash_plugin? | |
if OPTIONS[:plugin] == 'flash' || OPTIONS[:plugin] == 'fl' | |
return true | |
end | |
false | |
end | |
def full_plugin_name | |
if flash_plugin? | |
return "flash" | |
else | |
return OPTIONS[:plugin] | |
end | |
end | |
OptionParser.new do |opts| | |
opts.banner = " | |
plugout - Plugin switcher for OS X. | |
Usage: plugout [OPTIONS] | |
Plugout works with Intel and PPC Macs and allows Intel macs | |
to run your target browser in Rosetta if the plugin is PPC. | |
-Flash | |
Anything lower than version 9.0.28 on Intel Macs will restart the | |
browser in Rosetta. | |
-Browser Restart | |
Browsers must be restarted before changes take effect. plugout will | |
do this for you, just answer 'y' to the prompt. You can supply the | |
-r switch to force the restart (don't prompt you to restart). | |
-Default Browser | |
The default browser is firefox. You can change the default browser | |
plugout uses so you don't have to specify the -b switch everytime. | |
Open up this file and change the 'default_browser' variable. | |
-Default Plugin | |
'flash' is the default plugin used. So if switching flash players | |
you don't have to supply the -p switch. | |
-Installing New Plugins. | |
See ~/.plugout/ADD_PLUGINS | |
USAGE EX: Switching Plugins. | |
plugout -v 9.0.47 => Install flash player 9.0.47. Without a browser start/restart | |
plugout -p flash -v 9.0.47 => (same as above) | |
plugout -p fl -v 9.0.47 => (same as above) | |
plugout -v 9.0.47d => Install the flash debug player for 9.0.47 | |
plugout -v 8.0.35 => Install flash 8.0.35. The browser will run in Rosetta. | |
plugout -v 9.0.47 -b camino => Install flash 9.0.47 and start/restart camino | |
plugout -p sl -v 1.0 -b camino => (same as above) | |
plugout -v 9.0.47 -b camino -r => Install flash 9.0.47 and restart camino without prompting you | |
USAGE EX: Listing Plugins Available. | |
plugout -l => List available plugins to use. (top level directories in ~/plugout_plugins) | |
plugout -l -p flash => List PPC and UB plugins available in ~/plugout_plugins/[myplugin]/[ppc & ub] | |
Commandline Switches Available: | |
" | |
opts.on("-u", "--user=username", "Set which user directory to for plugins in. Attempts to use ´whoami´ default.") do |u| | |
OPTIONS[:user] = u | |
end | |
opts.on("-l", "--list", "List available plugins. Use -l and -p for specific plugin players available.") do |l| | |
OPTIONS[:list] = true | |
end | |
opts.on("-p","--plugin=flash", "Which plugin do you want to switch? Default is flash. Supports (flash,fl).") do |p| | |
OPTIONS[:plugin] = p.downcase | |
end | |
opts.on("-v", "--version=9.0.45", "Plugin version.") do |v| | |
OPTIONS[:version] = v | |
end | |
opts.on("-b", "--browser=firefox", "Launch or Restart firefox. Supports (firefox,safari,camino,opera,shiira)") do |b| | |
OPTIONS[:browser] = b | |
end | |
opts.on("-r","--restart","Force restart your browser (from -b) if it's already running. ") do |r| | |
OPTIONS[:restart] = true | |
end | |
opts.on_tail("-h", "--help", "Show this usage statement.") do |h| | |
OPTIONS[:exit] = true | |
puts opts | |
puts "\n" | |
end | |
end.parse! | |
if OPTIONS[:exit] | |
exit(0) | |
return | |
end | |
#setup default | |
if !OPTIONS[:plugin] then OPTIONS[:plugin] = OPTIONS[:default_plugin] end | |
if !OPTIONS[:user] then OPTIONS[:user] = OPTIONS[:default_user] end | |
#list | |
if OPTIONS[:list] && OPTIONS[:plugin] | |
list = get_plugin_list(players) | |
puts list | |
OPTIONS[:exit] = true | |
exit(0) | |
return | |
elsif OPTIONS[:list] | |
plugins = Dir.glob("#{players}/*") | |
list = "\n Available Plugins:" | |
plugins.each do |f| | |
if f.match(/.plugout$/) || f.match(/ADD_PLUGINS/) | |
next | |
end | |
list += "\n -#{f.split('/').last}" | |
end | |
list += "\n\n To see each version available for a player, run 'plugout -l -p [plugin]'\n\n" | |
puts list | |
OPTIONS[:exit] = true | |
exit(0) | |
return | |
end | |
#set up target_pluginitecture specific info | |
endian = sniff_architecture | |
#find target plugin | |
if File.exist?("#{players}#{full_plugin_name}/ub/#{OPTIONS[:version]}") | |
target_plugin = 'ub' | |
elsif File.exist?("#{players}#{full_plugin_name}/ppc/#{OPTIONS[:version]}") | |
target_plugin = 'ppc' | |
end | |
#turn on rosetta | |
if target_plugin == 'ppc' && endian == :little_endian | |
rosetta = true | |
end | |
#ensure PPC macs don't launch with non existent rosetta | |
if endian == :big_endian | |
target_plugin = 'ppc' | |
rosetta = false | |
end | |
#target the players to install | |
target_players = "#{players}#{full_plugin_name}/#{target_plugin}/#{OPTIONS[:version]}/" | |
#grab target players | |
files = Dir.glob(target_players + "*") | |
if files.empty? | |
puts "No #{full_plugin_name} player exists for #{OPTIONS[:version]}, try 'plugout -l -p #{full_plugin_name}' to list available players" | |
exit(0) | |
return | |
end | |
#delete existing players | |
begin | |
files = Dir.glob("#{plugin_path}/*") | |
files.each do |f| | |
reg = Regexp.new(full_plugin_name, Regexp::IGNORECASE) | |
if f.split('/').last.match(reg) | |
FileUtils.rm_rf("#{f.to_s}") | |
end | |
end | |
pl = Dir.glob("#{players}#{full_plugin_name}/#{target_plugin}/#{OPTIONS[:version]}/*") | |
FileUtils.cp_r(pl, "#{plugin_path}") | |
rescue Exception => e | |
puts e.message | |
end | |
if OPTIONS[:browser] == nil && (target_plugin == 'ppc' || rosetta) | |
OPTIONS[:browser] = default_browser | |
end | |
#open browser | |
if OPTIONS[:browser] && !OPTIONS[:exit] | |
#get browser location | |
app_location = "" | |
apploc_files = Dir.glob("/Users/#{user}/Applications/*") | |
apploc_files.concat(Dir.glob("/Applications/*")) | |
apploc_files.each do |f| | |
reg = Regexp.new("#{OPTIONS[:browser]}", Regexp::IGNORECASE) #match browser | |
reg2 = Regexp.new("#{user}", Regexp::IGNORECASE) #match user | |
if f.match(reg) && f.match(reg2) | |
app_location = "/Users/#{user}/Applications/" | |
OPTIONS[:nobrowser] = false | |
OPTIONS[:exit] = false | |
break | |
elsif f.match(reg) | |
app_location = "/Applications/" | |
OPTIONS[:nobrowser] = false | |
OPTIONS[:exit] = false | |
break | |
else | |
OPTIONS[:nobrowser] = true | |
OPTIONS[:exit] = true | |
end | |
end | |
if OPTIONS[:nobrowser] | |
puts "You do not have #{OPTIONS[:browser]} installed" | |
end | |
output = "2> ~/.plugout/output 1> ~/.plugout/output >> ~/.plugout/output &" | |
b = {} | |
b['camino'] = "#{app_location}Camino.app" | |
b['camino_ppc'] = "#{app_location}Camino.app/Contents/MacOS/Camino #{output}" | |
b['firefox'] = "#{app_location}Firefox.app" | |
b['firefox_ppc'] = "#{app_location}Firefox.app/Contents/MacOS/firefox-bin #{output}" | |
b['opera'] = "#{app_location}Opera.app" | |
b['opera_ppc'] = "#{app_location}Opera.app/Contents/MacOS/Opera #{output}" | |
b['safari'] = "#{app_location}Safari.app" | |
b['safari_ppc'] = "#{app_location}Safari.app/Contents/MacOS/Safari #{output}" | |
b['shiira'] = "#{app_location}Shiira.app" | |
b['shiira_ppc'] = "#{app_location}Shiira.app/Contents/MacOS/Shiira #{output}" | |
b['netscape'] = "#{app_location}Navigator.app" | |
b['netscape_ppc'] = "#{app_location}Navigator.app/Contents/MacOS/navigator-bin #{output}" | |
first = OPTIONS[:browser][0,1] | |
rest = OPTIONS[:browser][1,OPTIONS[:browser].length] | |
browserprocess = `ps x | grep -i [#{first}]#{rest} | awk {'print $1'}` | |
if browserprocess != false && browserprocess.length > 5 | |
begin | |
proccess = browserprocess.match(/([0-9]).*(\n)/)[0].to_s.split("\n")[0] | |
if proccess != nil | |
if !OPTIONS[:restart] | |
puts "#{OPTIONS[:browser]} is currently running, it needs to be restarted for changes to take effect. \nDo you want to restart? (y/n)" | |
restart = gets | |
if restart.chop!.to_s == 'y' | |
system("kill -15 #{proccess}") | |
puts "Restarting..." | |
else | |
puts "#{full_plugin_name} #{OPTIONS[:version]} has been installed, but you need to restart your browser." | |
OPTIONS[:exit] = true | |
exit(0) | |
end | |
else | |
system("kill -15 #{proccess}") | |
end | |
end | |
rescue Exception => e | |
end | |
end | |
if rosetta | |
pre = '/usr/libexec/oah/translate ' | |
post = b[OPTIONS[:browser] + '_ppc'] | |
else | |
pre = 'open ' | |
post = b[OPTIONS[:browser]] | |
end | |
begin | |
together = pre + post | |
if !OPTIONS[:exit] | |
system(together) | |
end | |
rescue Exception => e | |
puts "could not start/restart your browser - #{OPTIONS[:browser]} is not valid." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment