#!/usr/bin/env perl -w
# $Id: openbrowser 57831 2009-09-17 07:57:54Z ryandesign@macports.org $
#
# openbrowser by Ryan Schmidt
# MIT license
#
# openbrowser opens files and URLs into your default web browser (or
# just opens the browser, if you provide no arguments). Unlike "open",
# openbrowser will still open local .html files into your web browser
# even if you have configured LaunchServices to open .html files into
# e.g. a text editor.
#
# usage: openbrowser [ file | url ] [ ... ]

sub GetBrowser {
	use Mac::PropertyList qw(parse_plist_file);
	my $browser = "com.apple.safari";
	my $plist_file = "$ENV{HOME}/Library/Preferences/com.apple.LaunchServices.plist";
	$data = parse_plist_file($plist_file);
	if ($data) {
		for my $handler ($data->{"LSHandlers"}->value()) {
			$handler->{"LSHandlerURLScheme"} || next;
			"http" eq $handler->{"LSHandlerURLScheme"}->value() || next;
			$browser = $handler->{"LSHandlerRoleAll"}->value();
			last;
		}
	}
	return $browser;
}

my $browser = GetBrowser();
@open_args = ("-b", $browser);
push @open_args, @ARGV;

exec "open", @open_args;
